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 | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 9 | #include <linux/blkdev.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 10 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 11 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 12 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 13 | #include <linux/semaphore.h> |
Andy Shevchenko | 8da4b8c | 2016-05-20 17:01:00 -0700 | [diff] [blame] | 14 | #include <linux/uuid.h> |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 15 | #include <linux/list_sort.h> |
David Sterba | 784352f | 2019-08-21 18:54:28 +0200 | [diff] [blame] | 16 | #include "misc.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 17 | #include "ctree.h" |
| 18 | #include "extent_map.h" |
| 19 | #include "disk-io.h" |
| 20 | #include "transaction.h" |
| 21 | #include "print-tree.h" |
| 22 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 23 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 24 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 25 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 26 | #include "rcu-string.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 27 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 28 | #include "sysfs.h" |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 29 | #include "tree-checker.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 30 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 31 | #include "block-group.h" |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 32 | #include "discard.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 | }, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 61 | [BTRFS_RAID_RAID1C3] = { |
| 62 | .sub_stripes = 1, |
| 63 | .dev_stripes = 1, |
David Sterba | cf93e15 | 2019-11-27 16:10:54 +0100 | [diff] [blame] | 64 | .devs_max = 3, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 65 | .devs_min = 3, |
| 66 | .tolerated_failures = 2, |
| 67 | .devs_increment = 3, |
| 68 | .ncopies = 3, |
David Sterba | db26a02 | 2019-11-25 15:34:48 +0100 | [diff] [blame] | 69 | .nparity = 0, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 70 | .raid_name = "raid1c3", |
| 71 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3, |
| 72 | .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET, |
| 73 | }, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 74 | [BTRFS_RAID_RAID1C4] = { |
| 75 | .sub_stripes = 1, |
| 76 | .dev_stripes = 1, |
David Sterba | cf93e15 | 2019-11-27 16:10:54 +0100 | [diff] [blame] | 77 | .devs_max = 4, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 78 | .devs_min = 4, |
| 79 | .tolerated_failures = 3, |
| 80 | .devs_increment = 4, |
| 81 | .ncopies = 4, |
David Sterba | db26a02 | 2019-11-25 15:34:48 +0100 | [diff] [blame] | 82 | .nparity = 0, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 83 | .raid_name = "raid1c4", |
| 84 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4, |
| 85 | .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET, |
| 86 | }, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 87 | [BTRFS_RAID_DUP] = { |
| 88 | .sub_stripes = 1, |
| 89 | .dev_stripes = 2, |
| 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 = 2, |
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 = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 97 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
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_RAID0] = { |
| 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 = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 106 | .devs_increment = 1, |
| 107 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 108 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 109 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 110 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 111 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 112 | }, |
| 113 | [BTRFS_RAID_SINGLE] = { |
| 114 | .sub_stripes = 1, |
| 115 | .dev_stripes = 1, |
| 116 | .devs_max = 1, |
| 117 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 118 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 119 | .devs_increment = 1, |
| 120 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 121 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 122 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 123 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 124 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 125 | }, |
| 126 | [BTRFS_RAID_RAID5] = { |
| 127 | .sub_stripes = 1, |
| 128 | .dev_stripes = 1, |
| 129 | .devs_max = 0, |
| 130 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 131 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 132 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 133 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 134 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 135 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 136 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 137 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 138 | }, |
| 139 | [BTRFS_RAID_RAID6] = { |
| 140 | .sub_stripes = 1, |
| 141 | .dev_stripes = 1, |
| 142 | .devs_max = 0, |
| 143 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 144 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 145 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 146 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 147 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 148 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 149 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 150 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 151 | }, |
| 152 | }; |
| 153 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 154 | const char *btrfs_bg_type_to_raid_name(u64 flags) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 155 | { |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 156 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 157 | |
| 158 | if (index >= BTRFS_NR_RAID_TYPES) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 159 | return NULL; |
| 160 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 161 | return btrfs_raid_array[index].raid_name; |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Anand Jain | f89e09c | 2018-11-20 16:12:55 +0800 | [diff] [blame] | 164 | /* |
| 165 | * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| 166 | * bytes including terminating null byte. |
| 167 | */ |
| 168 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| 169 | { |
| 170 | int i; |
| 171 | int ret; |
| 172 | char *bp = buf; |
| 173 | u64 flags = bg_flags; |
| 174 | u32 size_bp = size_buf; |
| 175 | |
| 176 | if (!flags) { |
| 177 | strcpy(bp, "NONE"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | #define DESCRIBE_FLAG(flag, desc) \ |
| 182 | do { \ |
| 183 | if (flags & (flag)) { \ |
| 184 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| 185 | if (ret < 0 || ret >= size_bp) \ |
| 186 | goto out_overflow; \ |
| 187 | size_bp -= ret; \ |
| 188 | bp += ret; \ |
| 189 | flags &= ~(flag); \ |
| 190 | } \ |
| 191 | } while (0) |
| 192 | |
| 193 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| 194 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| 195 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| 196 | |
| 197 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| 198 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 199 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| 200 | btrfs_raid_array[i].raid_name); |
| 201 | #undef DESCRIBE_FLAG |
| 202 | |
| 203 | if (flags) { |
| 204 | ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| 205 | size_bp -= ret; |
| 206 | } |
| 207 | |
| 208 | if (size_bp < size_buf) |
| 209 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| 210 | |
| 211 | /* |
| 212 | * The text is trimmed, it's up to the caller to provide sufficiently |
| 213 | * large buffer |
| 214 | */ |
| 215 | out_overflow:; |
| 216 | } |
| 217 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 218 | static int init_first_rw_device(struct btrfs_trans_handle *trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 219 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 220 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 221 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 222 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 223 | enum btrfs_map_op op, |
| 224 | u64 logical, u64 *length, |
| 225 | struct btrfs_bio **bbio_ret, |
| 226 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 227 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 228 | /* |
| 229 | * Device locking |
| 230 | * ============== |
| 231 | * |
| 232 | * There are several mutexes that protect manipulation of devices and low-level |
| 233 | * structures like chunks but not block groups, extents or files |
| 234 | * |
| 235 | * uuid_mutex (global lock) |
| 236 | * ------------------------ |
| 237 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 238 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 239 | * device) or requested by the device= mount option |
| 240 | * |
| 241 | * the mutex can be very coarse and can cover long-running operations |
| 242 | * |
| 243 | * protects: updates to fs_devices counters like missing devices, rw devices, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 244 | * seeding, structure cloning, opening/closing devices at mount/umount time |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 245 | * |
| 246 | * global::fs_devs - add, remove, updates to the global list |
| 247 | * |
| 248 | * does not protect: manipulation of the fs_devices::devices list! |
| 249 | * |
| 250 | * btrfs_device::name - renames (write side), read is RCU |
| 251 | * |
| 252 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 253 | * ------------------------------------------------ |
| 254 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 255 | * |
| 256 | * simple list traversal with read-only actions can be done with RCU protection |
| 257 | * |
| 258 | * may be used to exclude some operations from running concurrently without any |
| 259 | * modifications to the list (see write_all_supers) |
| 260 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 261 | * balance_mutex |
| 262 | * ------------- |
| 263 | * protects balance structures (status, state) and context accessed from |
| 264 | * several places (internally, ioctl) |
| 265 | * |
| 266 | * chunk_mutex |
| 267 | * ----------- |
| 268 | * protects chunks, adding or removing during allocation, trim or when a new |
Nikolay Borisov | 0b6f5d4 | 2019-05-09 18:11:11 +0300 | [diff] [blame] | 269 | * device is added/removed. Additionally it also protects post_commit_list of |
| 270 | * individual devices, since they can be added to the transaction's |
| 271 | * post_commit_list only with chunk_mutex held. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 272 | * |
| 273 | * cleaner_mutex |
| 274 | * ------------- |
| 275 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 276 | * cleaning together with relocation or delayed iputs |
| 277 | * |
| 278 | * |
| 279 | * Lock nesting |
| 280 | * ============ |
| 281 | * |
| 282 | * uuid_mutex |
| 283 | * volume_mutex |
| 284 | * device_list_mutex |
| 285 | * chunk_mutex |
| 286 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 287 | * |
| 288 | * |
| 289 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 290 | * ====================================== |
| 291 | * |
| 292 | * Maintains the exclusivity of the following operations that apply to the |
| 293 | * whole filesystem and cannot run in parallel. |
| 294 | * |
| 295 | * - Balance (*) |
| 296 | * - Device add |
| 297 | * - Device remove |
| 298 | * - Device replace (*) |
| 299 | * - Resize |
| 300 | * |
| 301 | * The device operations (as above) can be in one of the following states: |
| 302 | * |
| 303 | * - Running state |
| 304 | * - Paused state |
| 305 | * - Completed state |
| 306 | * |
| 307 | * Only device operations marked with (*) can go into the Paused state for the |
| 308 | * following reasons: |
| 309 | * |
| 310 | * - ioctl (only Balance can be Paused through ioctl) |
| 311 | * - filesystem remounted as read-only |
| 312 | * - filesystem unmounted and mounted as read-only |
| 313 | * - system power-cycle and filesystem mounted as read-only |
| 314 | * - filesystem or device errors leading to forced read-only |
| 315 | * |
| 316 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 317 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 318 | * A device operation in Paused or Running state can be canceled or resumed |
| 319 | * either by ioctl (Balance only) or when remounted as read-write. |
| 320 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 321 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 322 | */ |
| 323 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 324 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 325 | static LIST_HEAD(fs_uuids); |
David Sterba | 4143cb8 | 2019-10-01 19:57:37 +0200 | [diff] [blame] | 326 | struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 327 | { |
| 328 | return &fs_uuids; |
| 329 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 330 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 331 | /* |
| 332 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 333 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 334 | * @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] | 335 | * |
| 336 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 337 | * The returned struct is not linked onto any lists and can be destroyed with |
| 338 | * kfree() right away. |
| 339 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 340 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 341 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 342 | { |
| 343 | struct btrfs_fs_devices *fs_devs; |
| 344 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 345 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 346 | if (!fs_devs) |
| 347 | return ERR_PTR(-ENOMEM); |
| 348 | |
| 349 | mutex_init(&fs_devs->device_list_mutex); |
| 350 | |
| 351 | INIT_LIST_HEAD(&fs_devs->devices); |
| 352 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 353 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 354 | if (fsid) |
| 355 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 356 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 357 | if (metadata_fsid) |
| 358 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 359 | else if (fsid) |
| 360 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 361 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 362 | return fs_devs; |
| 363 | } |
| 364 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 365 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 366 | { |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 367 | WARN_ON(!list_empty(&device->post_commit_list)); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 368 | rcu_string_free(device->name); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 369 | extent_io_tree_release(&device->alloc_state); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 370 | bio_put(device->flush_bio); |
| 371 | kfree(device); |
| 372 | } |
| 373 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 374 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 375 | { |
| 376 | struct btrfs_device *device; |
| 377 | WARN_ON(fs_devices->opened); |
| 378 | while (!list_empty(&fs_devices->devices)) { |
| 379 | device = list_entry(fs_devices->devices.next, |
| 380 | struct btrfs_device, dev_list); |
| 381 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 382 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 383 | } |
| 384 | kfree(fs_devices); |
| 385 | } |
| 386 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 387 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 388 | { |
| 389 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 390 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 391 | while (!list_empty(&fs_uuids)) { |
| 392 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 393 | struct btrfs_fs_devices, fs_list); |
| 394 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 395 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 396 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 397 | } |
| 398 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 399 | /* |
| 400 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 401 | * 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] | 402 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 403 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 404 | static struct btrfs_device *__alloc_device(void) |
| 405 | { |
| 406 | struct btrfs_device *dev; |
| 407 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 408 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 409 | if (!dev) |
| 410 | return ERR_PTR(-ENOMEM); |
| 411 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 412 | /* |
| 413 | * Preallocate a bio that's always going to be used for flushing device |
| 414 | * barriers and matches the device lifespan |
| 415 | */ |
| 416 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 417 | if (!dev->flush_bio) { |
| 418 | kfree(dev); |
| 419 | return ERR_PTR(-ENOMEM); |
| 420 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 421 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 422 | INIT_LIST_HEAD(&dev->dev_list); |
| 423 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 424 | INIT_LIST_HEAD(&dev->post_commit_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 425 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 426 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 427 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 428 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 429 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 430 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 431 | extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 432 | |
| 433 | return dev; |
| 434 | } |
| 435 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 436 | static noinline struct btrfs_fs_devices *find_fsid( |
| 437 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 438 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 439 | struct btrfs_fs_devices *fs_devices; |
| 440 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 441 | ASSERT(fsid); |
| 442 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 443 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 444 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 445 | if (metadata_fsid) { |
| 446 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 447 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 448 | BTRFS_FSID_SIZE) == 0) |
| 449 | return fs_devices; |
| 450 | } else { |
| 451 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 452 | return fs_devices; |
| 453 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 454 | } |
| 455 | return NULL; |
| 456 | } |
| 457 | |
Su Yue | c6730a0 | 2020-01-10 14:11:33 +0200 | [diff] [blame] | 458 | static struct btrfs_fs_devices *find_fsid_with_metadata_uuid( |
| 459 | struct btrfs_super_block *disk_super) |
| 460 | { |
| 461 | |
| 462 | struct btrfs_fs_devices *fs_devices; |
| 463 | |
| 464 | /* |
| 465 | * Handle scanned device having completed its fsid change but |
| 466 | * belonging to a fs_devices that was created by first scanning |
| 467 | * a device which didn't have its fsid/metadata_uuid changed |
| 468 | * at all and the CHANGING_FSID_V2 flag set. |
| 469 | */ |
| 470 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 471 | if (fs_devices->fsid_change && |
| 472 | memcmp(disk_super->metadata_uuid, fs_devices->fsid, |
| 473 | BTRFS_FSID_SIZE) == 0 && |
| 474 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 475 | BTRFS_FSID_SIZE) == 0) { |
| 476 | return fs_devices; |
| 477 | } |
| 478 | } |
| 479 | /* |
| 480 | * Handle scanned device having completed its fsid change but |
| 481 | * belonging to a fs_devices that was created by a device that |
| 482 | * has an outdated pair of fsid/metadata_uuid and |
| 483 | * CHANGING_FSID_V2 flag set. |
| 484 | */ |
| 485 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 486 | if (fs_devices->fsid_change && |
| 487 | memcmp(fs_devices->metadata_uuid, |
| 488 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 489 | memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid, |
| 490 | BTRFS_FSID_SIZE) == 0) { |
| 491 | return fs_devices; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return find_fsid(disk_super->fsid, disk_super->metadata_uuid); |
| 496 | } |
| 497 | |
| 498 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 499 | static int |
| 500 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 501 | int flush, struct block_device **bdev, |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 502 | struct btrfs_super_block **disk_super) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 503 | { |
| 504 | int ret; |
| 505 | |
| 506 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 507 | |
| 508 | if (IS_ERR(*bdev)) { |
| 509 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 510 | goto error; |
| 511 | } |
| 512 | |
| 513 | if (flush) |
| 514 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 515 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 516 | if (ret) { |
| 517 | blkdev_put(*bdev, flags); |
| 518 | goto error; |
| 519 | } |
| 520 | invalidate_bdev(*bdev); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 521 | *disk_super = btrfs_read_dev_super(*bdev); |
| 522 | if (IS_ERR(*disk_super)) { |
| 523 | ret = PTR_ERR(*disk_super); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 524 | blkdev_put(*bdev, flags); |
| 525 | goto error; |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | |
| 530 | error: |
| 531 | *bdev = NULL; |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 532 | return ret; |
| 533 | } |
| 534 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 535 | static bool device_path_matched(const char *path, struct btrfs_device *device) |
| 536 | { |
| 537 | int found; |
| 538 | |
| 539 | rcu_read_lock(); |
| 540 | found = strcmp(rcu_str_deref(device->name), path); |
| 541 | rcu_read_unlock(); |
| 542 | |
| 543 | return found == 0; |
| 544 | } |
| 545 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 546 | /* |
| 547 | * Search and remove all stale (devices which are not mounted) devices. |
| 548 | * When both inputs are NULL, it will search and release all stale devices. |
| 549 | * path: Optional. When provided will it release all unmounted devices |
| 550 | * matching this path only. |
| 551 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 552 | * devices. |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 553 | * Return: 0 for success or if @path is NULL. |
| 554 | * -EBUSY if @path is a mounted device. |
| 555 | * -ENOENT if @path does not match any device in the list. |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 556 | */ |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 557 | static int btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 558 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 559 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 560 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 561 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 562 | int ret = 0; |
| 563 | |
| 564 | if (path) |
| 565 | ret = -ENOENT; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 566 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 567 | 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] | 568 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 569 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 570 | list_for_each_entry_safe(device, tmp_device, |
| 571 | &fs_devices->devices, dev_list) { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 572 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 573 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 574 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 575 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 576 | if (path && !device_path_matched(path, device)) |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 577 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 578 | if (fs_devices->opened) { |
| 579 | /* for an already deleted device return 0 */ |
| 580 | if (path && ret != 0) |
| 581 | ret = -EBUSY; |
| 582 | break; |
| 583 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 584 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 585 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 586 | fs_devices->num_devices--; |
| 587 | list_del(&device->dev_list); |
| 588 | btrfs_free_device(device); |
| 589 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 590 | ret = 0; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 591 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 592 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 593 | } |
| 594 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 595 | |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 596 | if (fs_devices->num_devices == 0) { |
| 597 | btrfs_sysfs_remove_fsid(fs_devices); |
| 598 | list_del(&fs_devices->fs_list); |
| 599 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 600 | } |
| 601 | } |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 602 | |
| 603 | return ret; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 604 | } |
| 605 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 606 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 607 | struct btrfs_device *device, fmode_t flags, |
| 608 | void *holder) |
| 609 | { |
| 610 | struct request_queue *q; |
| 611 | struct block_device *bdev; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 612 | struct btrfs_super_block *disk_super; |
| 613 | u64 devid; |
| 614 | int ret; |
| 615 | |
| 616 | if (device->bdev) |
| 617 | return -EINVAL; |
| 618 | if (!device->name) |
| 619 | return -EINVAL; |
| 620 | |
| 621 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 622 | &bdev, &disk_super); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 623 | if (ret) |
| 624 | return ret; |
| 625 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 626 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 627 | if (devid != device->devid) |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 628 | goto error_free_page; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 629 | |
| 630 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 631 | goto error_free_page; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 632 | |
| 633 | device->generation = btrfs_super_generation(disk_super); |
| 634 | |
| 635 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 636 | if (btrfs_super_incompat_flags(disk_super) & |
| 637 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 638 | pr_err( |
| 639 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 640 | goto error_free_page; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 643 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 644 | fs_devices->seeding = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 645 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 646 | if (bdev_read_only(bdev)) |
| 647 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 648 | else |
| 649 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 653 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 654 | fs_devices->rotating = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 655 | |
| 656 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 657 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 658 | device->mode = flags; |
| 659 | |
| 660 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 661 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 662 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 663 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 664 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 665 | } |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 666 | btrfs_release_disk_super(disk_super); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 670 | error_free_page: |
| 671 | btrfs_release_disk_super(disk_super); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 672 | blkdev_put(bdev, flags); |
| 673 | |
| 674 | return -EINVAL; |
| 675 | } |
| 676 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 677 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 678 | * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 679 | * being created with a disk that has already completed its fsid change. Such |
| 680 | * disk can belong to an fs which has its FSID changed or to one which doesn't. |
| 681 | * Handle both cases here. |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 682 | */ |
| 683 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 684 | struct btrfs_super_block *disk_super) |
| 685 | { |
| 686 | struct btrfs_fs_devices *fs_devices; |
| 687 | |
| 688 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 689 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 690 | BTRFS_FSID_SIZE) != 0 && |
| 691 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 692 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 693 | return fs_devices; |
| 694 | } |
| 695 | } |
| 696 | |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 697 | return find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 698 | } |
| 699 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 700 | |
| 701 | static struct btrfs_fs_devices *find_fsid_changed( |
| 702 | struct btrfs_super_block *disk_super) |
| 703 | { |
| 704 | struct btrfs_fs_devices *fs_devices; |
| 705 | |
| 706 | /* |
| 707 | * Handles the case where scanned device is part of an fs that had |
| 708 | * multiple successful changes of FSID but curently device didn't |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 709 | * observe it. Meaning our fsid will be different than theirs. We need |
| 710 | * to handle two subcases : |
| 711 | * 1 - The fs still continues to have different METADATA/FSID uuids. |
| 712 | * 2 - The fs is switched back to its original FSID (METADATA/FSID |
| 713 | * are equal). |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 714 | */ |
| 715 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 716 | /* Changed UUIDs */ |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 717 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 718 | BTRFS_FSID_SIZE) != 0 && |
| 719 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 720 | BTRFS_FSID_SIZE) == 0 && |
| 721 | memcmp(fs_devices->fsid, disk_super->fsid, |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 722 | BTRFS_FSID_SIZE) != 0) |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 723 | return fs_devices; |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 724 | |
| 725 | /* Unchanged UUIDs */ |
| 726 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 727 | BTRFS_FSID_SIZE) == 0 && |
| 728 | memcmp(fs_devices->fsid, disk_super->metadata_uuid, |
| 729 | BTRFS_FSID_SIZE) == 0) |
| 730 | return fs_devices; |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | return NULL; |
| 734 | } |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 735 | |
| 736 | static struct btrfs_fs_devices *find_fsid_reverted_metadata( |
| 737 | struct btrfs_super_block *disk_super) |
| 738 | { |
| 739 | struct btrfs_fs_devices *fs_devices; |
| 740 | |
| 741 | /* |
| 742 | * Handle the case where the scanned device is part of an fs whose last |
| 743 | * metadata UUID change reverted it to the original FSID. At the same |
| 744 | * time * fs_devices was first created by another constitutent device |
| 745 | * which didn't fully observe the operation. This results in an |
| 746 | * btrfs_fs_devices created with metadata/fsid different AND |
| 747 | * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the |
| 748 | * fs_devices equal to the FSID of the disk. |
| 749 | */ |
| 750 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 751 | if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 752 | BTRFS_FSID_SIZE) != 0 && |
| 753 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 754 | BTRFS_FSID_SIZE) == 0 && |
| 755 | fs_devices->fsid_change) |
| 756 | return fs_devices; |
| 757 | } |
| 758 | |
| 759 | return NULL; |
| 760 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 761 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 762 | * Add new device to list of registered devices |
| 763 | * |
| 764 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 765 | * device pointer which was just added or updated when successful |
| 766 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 767 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 768 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 769 | struct btrfs_super_block *disk_super, |
| 770 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 771 | { |
| 772 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 773 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 774 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 775 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 776 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 777 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 778 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 779 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 780 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 781 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 782 | if (fsid_change_in_progress) { |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 783 | if (!has_metadata_uuid) |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 784 | fs_devices = find_fsid_inprogress(disk_super); |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 785 | else |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 786 | fs_devices = find_fsid_changed(disk_super); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 787 | } else if (has_metadata_uuid) { |
Su Yue | c6730a0 | 2020-01-10 14:11:33 +0200 | [diff] [blame] | 788 | fs_devices = find_fsid_with_metadata_uuid(disk_super); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 789 | } else { |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 790 | fs_devices = find_fsid_reverted_metadata(disk_super); |
| 791 | if (!fs_devices) |
| 792 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 793 | } |
| 794 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 795 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 796 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 797 | if (has_metadata_uuid) |
| 798 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 799 | disk_super->metadata_uuid); |
| 800 | else |
| 801 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 802 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 803 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 804 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 805 | |
Al Viro | 92900e5 | 2019-01-27 04:58:00 +0000 | [diff] [blame] | 806 | fs_devices->fsid_change = fsid_change_in_progress; |
| 807 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 808 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 809 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 810 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 811 | device = NULL; |
| 812 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 813 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 814 | device = btrfs_find_device(fs_devices, devid, |
| 815 | disk_super->dev_item.uuid, NULL, false); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 816 | |
| 817 | /* |
| 818 | * If this disk has been pulled into an fs devices created by |
| 819 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 820 | * metadata_uuid/fsid values of the fs_devices. |
| 821 | */ |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 822 | if (fs_devices->fsid_change && |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 823 | found_transid > fs_devices->latest_generation) { |
| 824 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 825 | BTRFS_FSID_SIZE); |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 826 | |
| 827 | if (has_metadata_uuid) |
| 828 | memcpy(fs_devices->metadata_uuid, |
| 829 | disk_super->metadata_uuid, |
| 830 | BTRFS_FSID_SIZE); |
| 831 | else |
| 832 | memcpy(fs_devices->metadata_uuid, |
| 833 | disk_super->fsid, BTRFS_FSID_SIZE); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 834 | |
| 835 | fs_devices->fsid_change = false; |
| 836 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 837 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 838 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 839 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 840 | if (fs_devices->opened) { |
| 841 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 842 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 843 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 844 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 845 | device = btrfs_alloc_device(NULL, &devid, |
| 846 | disk_super->dev_item.uuid); |
| 847 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 848 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 849 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 850 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 851 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 852 | |
| 853 | name = rcu_string_strdup(path, GFP_NOFS); |
| 854 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 855 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 856 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 857 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 858 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 859 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 860 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 861 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 862 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 863 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 864 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 865 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 866 | |
| 867 | if (disk_super->label[0]) |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 868 | pr_info( |
| 869 | "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n", |
| 870 | disk_super->label, devid, found_transid, path, |
| 871 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 872 | else |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 873 | pr_info( |
| 874 | "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n", |
| 875 | disk_super->fsid, devid, found_transid, path, |
| 876 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 877 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 878 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 879 | /* |
| 880 | * When FS is already mounted. |
| 881 | * 1. If you are here and if the device->name is NULL that |
| 882 | * means this device was missing at time of FS mount. |
| 883 | * 2. If you are here and if the device->name is different |
| 884 | * from 'path' that means either |
| 885 | * a. The same device disappeared and reappeared with |
| 886 | * different name. or |
| 887 | * b. The missing-disk-which-was-replaced, has |
| 888 | * reappeared now. |
| 889 | * |
| 890 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 891 | * and unintentional. |
| 892 | * |
| 893 | * Further in case of 1 and 2a above, the disk at 'path' |
| 894 | * would have missed some transaction when it was away and |
| 895 | * in case of 2a the stale bdev has to be updated as well. |
| 896 | * 2b must not be allowed at all time. |
| 897 | */ |
| 898 | |
| 899 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 900 | * For now, we do allow update to btrfs_fs_device through the |
| 901 | * btrfs dev scan cli after FS has been mounted. We're still |
| 902 | * tracking a problem where systems fail mount by subvolume id |
| 903 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 904 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 905 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 906 | /* |
| 907 | * That is if the FS is _not_ mounted and if you |
| 908 | * are here, that means there is more than one |
| 909 | * disk with same uuid and devid.We keep the one |
| 910 | * with larger generation number or the last-in if |
| 911 | * generation are equal. |
| 912 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 913 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 914 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 915 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 916 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 917 | /* |
| 918 | * We are going to replace the device path for a given devid, |
| 919 | * make sure it's the same device if the device is mounted |
| 920 | */ |
| 921 | if (device->bdev) { |
| 922 | struct block_device *path_bdev; |
| 923 | |
| 924 | path_bdev = lookup_bdev(path); |
| 925 | if (IS_ERR(path_bdev)) { |
| 926 | mutex_unlock(&fs_devices->device_list_mutex); |
| 927 | return ERR_CAST(path_bdev); |
| 928 | } |
| 929 | |
| 930 | if (device->bdev != path_bdev) { |
| 931 | bdput(path_bdev); |
| 932 | mutex_unlock(&fs_devices->device_list_mutex); |
| 933 | btrfs_warn_in_rcu(device->fs_info, |
| 934 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 935 | disk_super->fsid, devid, |
| 936 | rcu_str_deref(device->name), path); |
| 937 | return ERR_PTR(-EEXIST); |
| 938 | } |
| 939 | bdput(path_bdev); |
| 940 | btrfs_info_in_rcu(device->fs_info, |
| 941 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 942 | disk_super->fsid, devid, |
| 943 | rcu_str_deref(device->name), path); |
| 944 | } |
| 945 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 946 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 947 | if (!name) { |
| 948 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 949 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 950 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 951 | rcu_string_free(device->name); |
| 952 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 953 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 954 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 955 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 956 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 957 | } |
| 958 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 959 | /* |
| 960 | * Unmount does not free the btrfs_device struct but would zero |
| 961 | * generation along with most of the other members. So just update |
| 962 | * it back. We need it to pick the disk with largest generation |
| 963 | * (as above). |
| 964 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 965 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 966 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 967 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 968 | fs_devices->latest_generation); |
| 969 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 970 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 971 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 972 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 973 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 974 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 975 | } |
| 976 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 977 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 978 | { |
| 979 | struct btrfs_fs_devices *fs_devices; |
| 980 | struct btrfs_device *device; |
| 981 | struct btrfs_device *orig_dev; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 982 | int ret = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 983 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 984 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 985 | if (IS_ERR(fs_devices)) |
| 986 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 987 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 988 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 989 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 990 | |
| 991 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 992 | struct rcu_string *name; |
| 993 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 994 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 995 | orig_dev->uuid); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 996 | if (IS_ERR(device)) { |
| 997 | ret = PTR_ERR(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 998 | goto error; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 999 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1000 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1001 | /* |
| 1002 | * This is ok to do without rcu read locked because we hold the |
| 1003 | * uuid mutex so nothing we touch in here is going to disappear. |
| 1004 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1005 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 1006 | name = rcu_string_strdup(orig_dev->name->str, |
| 1007 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1008 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1009 | btrfs_free_device(device); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1010 | ret = -ENOMEM; |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1011 | goto error; |
| 1012 | } |
| 1013 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 1014 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1015 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1016 | list_add(&device->dev_list, &fs_devices->devices); |
| 1017 | device->fs_devices = fs_devices; |
| 1018 | fs_devices->num_devices++; |
| 1019 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1020 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1021 | return fs_devices; |
| 1022 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1023 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1024 | free_fs_devices(fs_devices); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1025 | return ERR_PTR(ret); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1028 | /* |
| 1029 | * After we have read the system tree and know devids belonging to |
| 1030 | * this filesystem, remove the device which does not belong there. |
| 1031 | */ |
| 1032 | 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] | 1033 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1034 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1035 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1036 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1037 | mutex_lock(&uuid_mutex); |
| 1038 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1039 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1040 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1041 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1042 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1043 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1044 | &device->dev_state) && |
| 1045 | (!latest_dev || |
| 1046 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1047 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1048 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1049 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1050 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1051 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1052 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1053 | /* |
| 1054 | * In the first step, keep the device which has |
| 1055 | * the correct fsid and the devid that is used |
| 1056 | * for the dev_replace procedure. |
| 1057 | * In the second step, the dev_replace state is |
| 1058 | * read from the device tree and it is known |
| 1059 | * whether the procedure is really active or |
| 1060 | * not, which means whether this device is |
| 1061 | * used or whether it should be removed. |
| 1062 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1063 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1064 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1065 | continue; |
| 1066 | } |
| 1067 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1068 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1069 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1070 | device->bdev = NULL; |
| 1071 | fs_devices->open_devices--; |
| 1072 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1073 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1074 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1075 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1076 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1077 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1078 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1079 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1080 | list_del_init(&device->dev_list); |
| 1081 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1082 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1083 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1084 | |
| 1085 | if (fs_devices->seed) { |
| 1086 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1087 | goto again; |
| 1088 | } |
| 1089 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1090 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1091 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1092 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1093 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1094 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1095 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1096 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1097 | if (!device->bdev) |
| 1098 | return; |
| 1099 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1100 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1101 | sync_blockdev(device->bdev); |
| 1102 | invalidate_bdev(device->bdev); |
| 1103 | } |
| 1104 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1105 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1106 | } |
| 1107 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1108 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1109 | { |
| 1110 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1111 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1112 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1113 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1114 | list_del_init(&device->dev_alloc_list); |
| 1115 | fs_devices->rw_devices--; |
| 1116 | } |
| 1117 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1118 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1119 | fs_devices->missing_devices--; |
| 1120 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1121 | btrfs_close_bdev(device); |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1122 | if (device->bdev) { |
Johannes Thumshirn | 3fff397 | 2019-11-26 09:40:05 +0100 | [diff] [blame] | 1123 | fs_devices->open_devices--; |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1124 | device->bdev = NULL; |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1125 | } |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1126 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1127 | |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1128 | device->fs_info = NULL; |
| 1129 | atomic_set(&device->dev_stats_ccnt, 0); |
| 1130 | extent_io_tree_release(&device->alloc_state); |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1131 | |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1132 | /* Verify the device is back in a pristine state */ |
| 1133 | ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)); |
| 1134 | ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
| 1135 | ASSERT(list_empty(&device->dev_alloc_list)); |
| 1136 | ASSERT(list_empty(&device->post_commit_list)); |
| 1137 | ASSERT(atomic_read(&device->reada_in_flight) == 0); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1140 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1141 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1142 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1143 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1144 | if (--fs_devices->opened > 0) |
| 1145 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1146 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1147 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1148 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1149 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1150 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1151 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1152 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1153 | WARN_ON(fs_devices->open_devices); |
| 1154 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1155 | fs_devices->opened = 0; |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 1156 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1157 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
| 1160 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1161 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1162 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1163 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1164 | int ret; |
| 1165 | |
| 1166 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1167 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1168 | if (!fs_devices->opened) { |
| 1169 | seed_devices = fs_devices->seed; |
| 1170 | fs_devices->seed = NULL; |
| 1171 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1172 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1173 | |
| 1174 | while (seed_devices) { |
| 1175 | fs_devices = seed_devices; |
| 1176 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1177 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1178 | free_fs_devices(fs_devices); |
| 1179 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1180 | return ret; |
| 1181 | } |
| 1182 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1183 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1184 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1185 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1186 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1187 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1188 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1189 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1190 | flags |= FMODE_EXCL; |
| 1191 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1192 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1193 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1194 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1195 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1196 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1197 | if (!latest_dev || |
| 1198 | device->generation > latest_dev->generation) |
| 1199 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1200 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1201 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1202 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1203 | goto out; |
| 1204 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1205 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1206 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1207 | fs_devices->total_rw_bytes = 0; |
Naohiro Aota | c4a816c | 2020-02-25 12:56:08 +0900 | [diff] [blame] | 1208 | fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1209 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1210 | return ret; |
| 1211 | } |
| 1212 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1213 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1214 | { |
| 1215 | struct btrfs_device *dev1, *dev2; |
| 1216 | |
| 1217 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1218 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1219 | |
| 1220 | if (dev1->devid < dev2->devid) |
| 1221 | return -1; |
| 1222 | else if (dev1->devid > dev2->devid) |
| 1223 | return 1; |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1227 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1228 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1229 | { |
| 1230 | int ret; |
| 1231 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1232 | lockdep_assert_held(&uuid_mutex); |
| 1233 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1234 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1235 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1236 | fs_devices->opened++; |
| 1237 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1238 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1239 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1240 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1241 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1242 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1243 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1244 | return ret; |
| 1245 | } |
| 1246 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1247 | void btrfs_release_disk_super(struct btrfs_super_block *super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1248 | { |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1249 | struct page *page = virt_to_page(super); |
| 1250 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1251 | put_page(page); |
| 1252 | } |
| 1253 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1254 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1255 | struct page **page, |
| 1256 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1257 | { |
| 1258 | void *p; |
| 1259 | pgoff_t index; |
| 1260 | |
| 1261 | /* make sure our super fits in the device */ |
| 1262 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1263 | return 1; |
| 1264 | |
| 1265 | /* make sure our super fits in the page */ |
| 1266 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1267 | return 1; |
| 1268 | |
| 1269 | /* make sure our super doesn't straddle pages on disk */ |
| 1270 | index = bytenr >> PAGE_SHIFT; |
| 1271 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1272 | return 1; |
| 1273 | |
| 1274 | /* pull in the page with our super */ |
| 1275 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1276 | index, GFP_KERNEL); |
| 1277 | |
Johannes Thumshirn | c514c9b | 2020-02-14 00:24:30 +0900 | [diff] [blame] | 1278 | if (IS_ERR(*page)) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1279 | return 1; |
| 1280 | |
Johannes Thumshirn | c514c9b | 2020-02-14 00:24:30 +0900 | [diff] [blame] | 1281 | p = page_address(*page); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1282 | |
| 1283 | /* align our pointer to the offset of the super block */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 1284 | *disk_super = p + offset_in_page(bytenr); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1285 | |
| 1286 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1287 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1288 | btrfs_release_disk_super(p); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1289 | return 1; |
| 1290 | } |
| 1291 | |
| 1292 | if ((*disk_super)->label[0] && |
| 1293 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1294 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1295 | |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
Anand Jain | 228a73a | 2019-01-04 13:31:54 +0800 | [diff] [blame] | 1299 | int btrfs_forget_devices(const char *path) |
| 1300 | { |
| 1301 | int ret; |
| 1302 | |
| 1303 | mutex_lock(&uuid_mutex); |
| 1304 | ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| 1305 | mutex_unlock(&uuid_mutex); |
| 1306 | |
| 1307 | return ret; |
| 1308 | } |
| 1309 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1312 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1313 | * is read via pagecache |
| 1314 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1315 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1316 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1317 | { |
| 1318 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1319 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1320 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1321 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1322 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1323 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1324 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1325 | lockdep_assert_held(&uuid_mutex); |
| 1326 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * we would like to check all the supers, but that would make |
| 1329 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1330 | * So, we need to add a special mount option to scan for |
| 1331 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1332 | */ |
| 1333 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1334 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1335 | |
| 1336 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1337 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1338 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1339 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1340 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1341 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1342 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1343 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1344 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1345 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1346 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1347 | if (new_device_added) |
| 1348 | btrfs_free_stale_devices(path, device); |
| 1349 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1350 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1351 | btrfs_release_disk_super(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1352 | |
| 1353 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1354 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1355 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1356 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1357 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1358 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1359 | /* |
| 1360 | * Try to find a chunk that intersects [start, start + len] range and when one |
| 1361 | * such is found, record the end of it in *start |
| 1362 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1363 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| 1364 | u64 len) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1365 | { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1366 | u64 physical_start, physical_end; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1367 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1368 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1369 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1370 | if (!find_first_extent_bit(&device->alloc_state, *start, |
| 1371 | &physical_start, &physical_end, |
| 1372 | CHUNK_ALLOCATED, NULL)) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1373 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1374 | if (in_range(physical_start, *start, len) || |
| 1375 | in_range(*start, physical_start, |
| 1376 | physical_end - physical_start)) { |
| 1377 | *start = physical_end + 1; |
| 1378 | return true; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1379 | } |
| 1380 | } |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1381 | return false; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1382 | } |
| 1383 | |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1384 | static u64 dev_extent_search_start(struct btrfs_device *device, u64 start) |
| 1385 | { |
| 1386 | switch (device->fs_devices->chunk_alloc_policy) { |
| 1387 | case BTRFS_CHUNK_ALLOC_REGULAR: |
| 1388 | /* |
| 1389 | * We don't want to overwrite the superblock on the drive nor |
| 1390 | * any area used by the boot loader (grub for example), so we |
| 1391 | * make sure to start at an offset of at least 1MB. |
| 1392 | */ |
| 1393 | return max_t(u64, start, SZ_1M); |
| 1394 | default: |
| 1395 | BUG(); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | /** |
| 1400 | * dev_extent_hole_check - check if specified hole is suitable for allocation |
| 1401 | * @device: the device which we have the hole |
| 1402 | * @hole_start: starting position of the hole |
| 1403 | * @hole_size: the size of the hole |
| 1404 | * @num_bytes: the size of the free space that we need |
| 1405 | * |
| 1406 | * This function may modify @hole_start and @hole_end to reflect the suitable |
| 1407 | * position for allocation. Returns 1 if hole position is updated, 0 otherwise. |
| 1408 | */ |
| 1409 | static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start, |
| 1410 | u64 *hole_size, u64 num_bytes) |
| 1411 | { |
| 1412 | bool changed = false; |
| 1413 | u64 hole_end = *hole_start + *hole_size; |
| 1414 | |
| 1415 | /* |
| 1416 | * Check before we set max_hole_start, otherwise we could end up |
| 1417 | * sending back this offset anyway. |
| 1418 | */ |
| 1419 | if (contains_pending_extent(device, hole_start, *hole_size)) { |
| 1420 | if (hole_end >= *hole_start) |
| 1421 | *hole_size = hole_end - *hole_start; |
| 1422 | else |
| 1423 | *hole_size = 0; |
| 1424 | changed = true; |
| 1425 | } |
| 1426 | |
| 1427 | switch (device->fs_devices->chunk_alloc_policy) { |
| 1428 | case BTRFS_CHUNK_ALLOC_REGULAR: |
| 1429 | /* No extra check */ |
| 1430 | break; |
| 1431 | default: |
| 1432 | BUG(); |
| 1433 | } |
| 1434 | |
| 1435 | return changed; |
| 1436 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1437 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1438 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1439 | * find_free_dev_extent_start - find free space in the specified device |
| 1440 | * @device: the device which we search the free space in |
| 1441 | * @num_bytes: the size of the free space that we need |
| 1442 | * @search_start: the position from which to begin the search |
| 1443 | * @start: store the start of the free space. |
| 1444 | * @len: the size of the free space. that we find, or the size |
| 1445 | * 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] | 1446 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1447 | * this uses a pretty simple search, the expectation is that it is |
| 1448 | * called very infrequently and that a given device has a small number |
| 1449 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1450 | * |
| 1451 | * @start is used to store the start of the free space if we find. But if we |
| 1452 | * don't find suitable free space, it will be used to store the start position |
| 1453 | * of the max free space. |
| 1454 | * |
| 1455 | * @len is used to store the size of the free space that we find. |
| 1456 | * But if we don't find suitable free space, it is used to store the size of |
| 1457 | * the max free space. |
Qu Wenruo | 135da97 | 2019-07-19 14:51:42 +0800 | [diff] [blame] | 1458 | * |
| 1459 | * NOTE: This function will search *commit* root of device tree, and does extra |
| 1460 | * check to ensure dev extents are not double allocated. |
| 1461 | * This makes the function safe to allocate dev extents but may not report |
| 1462 | * correct usable device space, as device extent freed in current transaction |
| 1463 | * is not reported as avaiable. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1464 | */ |
Qu Wenruo | 9e3246a | 2019-07-19 14:51:41 +0800 | [diff] [blame] | 1465 | static int find_free_dev_extent_start(struct btrfs_device *device, |
| 1466 | u64 num_bytes, u64 search_start, u64 *start, |
| 1467 | u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1468 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1469 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1470 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1471 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1472 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1473 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1474 | u64 hole_size; |
| 1475 | u64 max_hole_start; |
| 1476 | u64 max_hole_size; |
| 1477 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1478 | u64 search_end = device->total_bytes; |
| 1479 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1480 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1481 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1482 | |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1483 | search_start = dev_extent_search_start(device, search_start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1484 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1485 | path = btrfs_alloc_path(); |
| 1486 | if (!path) |
| 1487 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1488 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1489 | max_hole_start = search_start; |
| 1490 | max_hole_size = 0; |
| 1491 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1492 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1493 | if (search_start >= search_end || |
| 1494 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1495 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1496 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1499 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1500 | path->search_commit_root = 1; |
| 1501 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1502 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1503 | key.objectid = device->devid; |
| 1504 | key.offset = search_start; |
| 1505 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1506 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1507 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1508 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1509 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1510 | if (ret > 0) { |
| 1511 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1512 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1513 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1514 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1515 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1516 | while (1) { |
| 1517 | l = path->nodes[0]; |
| 1518 | slot = path->slots[0]; |
| 1519 | if (slot >= btrfs_header_nritems(l)) { |
| 1520 | ret = btrfs_next_leaf(root, path); |
| 1521 | if (ret == 0) |
| 1522 | continue; |
| 1523 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1524 | goto out; |
| 1525 | |
| 1526 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1527 | } |
| 1528 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1529 | |
| 1530 | if (key.objectid < device->devid) |
| 1531 | goto next; |
| 1532 | |
| 1533 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1534 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1535 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1536 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1537 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1538 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1539 | if (key.offset > search_start) { |
| 1540 | hole_size = key.offset - search_start; |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1541 | dev_extent_hole_check(device, &search_start, &hole_size, |
| 1542 | num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1543 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1544 | if (hole_size > max_hole_size) { |
| 1545 | max_hole_start = search_start; |
| 1546 | max_hole_size = hole_size; |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * If this free space is greater than which we need, |
| 1551 | * it must be the max free space that we have found |
| 1552 | * until now, so max_hole_start must point to the start |
| 1553 | * of this free space and the length of this free space |
| 1554 | * is stored in max_hole_size. Thus, we return |
| 1555 | * max_hole_start and max_hole_size and go back to the |
| 1556 | * caller. |
| 1557 | */ |
| 1558 | if (hole_size >= num_bytes) { |
| 1559 | ret = 0; |
| 1560 | goto out; |
| 1561 | } |
| 1562 | } |
| 1563 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1564 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1565 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1566 | dev_extent); |
| 1567 | if (extent_end > search_start) |
| 1568 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1569 | next: |
| 1570 | path->slots[0]++; |
| 1571 | cond_resched(); |
| 1572 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1573 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1574 | /* |
| 1575 | * At this point, search_start should be the end of |
| 1576 | * allocated dev extents, and when shrinking the device, |
| 1577 | * search_end may be smaller than search_start. |
| 1578 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1579 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1580 | hole_size = search_end - search_start; |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1581 | if (dev_extent_hole_check(device, &search_start, &hole_size, |
| 1582 | num_bytes)) { |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1583 | btrfs_release_path(path); |
| 1584 | goto again; |
| 1585 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1586 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1587 | if (hole_size > max_hole_size) { |
| 1588 | max_hole_start = search_start; |
| 1589 | max_hole_size = hole_size; |
| 1590 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1591 | } |
| 1592 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1593 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1594 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1595 | ret = -ENOSPC; |
| 1596 | else |
| 1597 | ret = 0; |
| 1598 | |
| 1599 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1600 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1601 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1602 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1603 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1604 | return ret; |
| 1605 | } |
| 1606 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1607 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1608 | u64 *start, u64 *len) |
| 1609 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1610 | /* FIXME use last free of some kind */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1611 | return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1612 | } |
| 1613 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1614 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1615 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1616 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1617 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1618 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1619 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1620 | int ret; |
| 1621 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1622 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1623 | struct btrfs_key found_key; |
| 1624 | struct extent_buffer *leaf = NULL; |
| 1625 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1626 | |
| 1627 | path = btrfs_alloc_path(); |
| 1628 | if (!path) |
| 1629 | return -ENOMEM; |
| 1630 | |
| 1631 | key.objectid = device->devid; |
| 1632 | key.offset = start; |
| 1633 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1634 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1635 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1636 | if (ret > 0) { |
| 1637 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1638 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1639 | if (ret) |
| 1640 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1641 | leaf = path->nodes[0]; |
| 1642 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1643 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1644 | struct btrfs_dev_extent); |
| 1645 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1646 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1647 | key = found_key; |
| 1648 | btrfs_release_path(path); |
| 1649 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1650 | } else if (ret == 0) { |
| 1651 | leaf = path->nodes[0]; |
| 1652 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1653 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1654 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1655 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1656 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1657 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1658 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1659 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1660 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1661 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1662 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1663 | btrfs_handle_fs_error(fs_info, ret, |
| 1664 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1665 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1666 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1667 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1668 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1669 | btrfs_free_path(path); |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1673 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1674 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1675 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1676 | { |
| 1677 | int ret; |
| 1678 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1679 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1680 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1681 | struct btrfs_dev_extent *extent; |
| 1682 | struct extent_buffer *leaf; |
| 1683 | struct btrfs_key key; |
| 1684 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1685 | 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] | 1686 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1687 | path = btrfs_alloc_path(); |
| 1688 | if (!path) |
| 1689 | return -ENOMEM; |
| 1690 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1691 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1692 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1693 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1694 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1695 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1696 | if (ret) |
| 1697 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1698 | |
| 1699 | leaf = path->nodes[0]; |
| 1700 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1701 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1702 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1703 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1704 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1705 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1706 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1707 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1708 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1709 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1710 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1711 | btrfs_free_path(path); |
| 1712 | return ret; |
| 1713 | } |
| 1714 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1715 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1716 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1717 | struct extent_map_tree *em_tree; |
| 1718 | struct extent_map *em; |
| 1719 | struct rb_node *n; |
| 1720 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1721 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 1722 | em_tree = &fs_info->mapping_tree; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1723 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1724 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1725 | if (n) { |
| 1726 | em = rb_entry(n, struct extent_map, rb_node); |
| 1727 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1728 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1729 | read_unlock(&em_tree->lock); |
| 1730 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1731 | return ret; |
| 1732 | } |
| 1733 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1734 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1735 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1736 | { |
| 1737 | int ret; |
| 1738 | struct btrfs_key key; |
| 1739 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1740 | struct btrfs_path *path; |
| 1741 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1742 | path = btrfs_alloc_path(); |
| 1743 | if (!path) |
| 1744 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1745 | |
| 1746 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1747 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1748 | key.offset = (u64)-1; |
| 1749 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1750 | 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] | 1751 | if (ret < 0) |
| 1752 | goto error; |
| 1753 | |
Anand Jain | a06dee4d | 2019-08-27 15:40:44 +0800 | [diff] [blame] | 1754 | if (ret == 0) { |
| 1755 | /* Corruption */ |
| 1756 | btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); |
| 1757 | ret = -EUCLEAN; |
| 1758 | goto error; |
| 1759 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1760 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1761 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1762 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1763 | BTRFS_DEV_ITEM_KEY); |
| 1764 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1765 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1766 | } else { |
| 1767 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1768 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1769 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1770 | } |
| 1771 | ret = 0; |
| 1772 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1773 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1774 | return ret; |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * the device information is stored in the chunk root |
| 1779 | * the btrfs_device struct should be fully filled in |
| 1780 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1781 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1782 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1783 | { |
| 1784 | int ret; |
| 1785 | struct btrfs_path *path; |
| 1786 | struct btrfs_dev_item *dev_item; |
| 1787 | struct extent_buffer *leaf; |
| 1788 | struct btrfs_key key; |
| 1789 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1790 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1791 | path = btrfs_alloc_path(); |
| 1792 | if (!path) |
| 1793 | return -ENOMEM; |
| 1794 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1795 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1796 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1797 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1798 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1799 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1800 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1801 | if (ret) |
| 1802 | goto out; |
| 1803 | |
| 1804 | leaf = path->nodes[0]; |
| 1805 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1806 | |
| 1807 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1808 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1809 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1810 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1811 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1812 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1813 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1814 | btrfs_device_get_disk_total_bytes(device)); |
| 1815 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1816 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1817 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1818 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1819 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1820 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1821 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1822 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1823 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1824 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1825 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1826 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1827 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1828 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1829 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1830 | out: |
| 1831 | btrfs_free_path(path); |
| 1832 | return ret; |
| 1833 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1834 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1835 | /* |
| 1836 | * Function to update ctime/mtime for a given device path. |
| 1837 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1838 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1839 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1840 | { |
| 1841 | struct file *filp; |
| 1842 | |
| 1843 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1844 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1845 | return; |
| 1846 | file_update_time(filp); |
| 1847 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1848 | } |
| 1849 | |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1850 | static int btrfs_rm_dev_item(struct btrfs_device *device) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1851 | { |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1852 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1853 | int ret; |
| 1854 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1855 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1856 | struct btrfs_trans_handle *trans; |
| 1857 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1858 | path = btrfs_alloc_path(); |
| 1859 | if (!path) |
| 1860 | return -ENOMEM; |
| 1861 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1862 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1863 | if (IS_ERR(trans)) { |
| 1864 | btrfs_free_path(path); |
| 1865 | return PTR_ERR(trans); |
| 1866 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1867 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1868 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1869 | key.offset = device->devid; |
| 1870 | |
| 1871 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1872 | if (ret) { |
| 1873 | if (ret > 0) |
| 1874 | ret = -ENOENT; |
| 1875 | btrfs_abort_transaction(trans, ret); |
| 1876 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1877 | goto out; |
| 1878 | } |
| 1879 | |
| 1880 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1881 | if (ret) { |
| 1882 | btrfs_abort_transaction(trans, ret); |
| 1883 | btrfs_end_transaction(trans); |
| 1884 | } |
| 1885 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1886 | out: |
| 1887 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1888 | if (!ret) |
| 1889 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1890 | return ret; |
| 1891 | } |
| 1892 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1893 | /* |
| 1894 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 1895 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 1896 | * replace. |
| 1897 | */ |
| 1898 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 1899 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1900 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1901 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1902 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1903 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1904 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1905 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1906 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1907 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1908 | all_avail = fs_info->avail_data_alloc_bits | |
| 1909 | fs_info->avail_system_alloc_bits | |
| 1910 | fs_info->avail_metadata_alloc_bits; |
| 1911 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1912 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1913 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 1914 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1915 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1916 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1917 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 1918 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1919 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1920 | if (ret) |
| 1921 | return ret; |
| 1922 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1926 | } |
| 1927 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1928 | static struct btrfs_device * btrfs_find_next_active_device( |
| 1929 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1930 | { |
| 1931 | struct btrfs_device *next_device; |
| 1932 | |
| 1933 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 1934 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1935 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 1936 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1937 | return next_device; |
| 1938 | } |
| 1939 | |
| 1940 | return NULL; |
| 1941 | } |
| 1942 | |
| 1943 | /* |
| 1944 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 1945 | * and replace it with the provided or the next active device, in the context |
| 1946 | * where this function called, there should be always be another device (or |
| 1947 | * this_dev) which is active. |
| 1948 | */ |
David Sterba | b105e92 | 2019-10-01 19:57:35 +0200 | [diff] [blame] | 1949 | void __cold btrfs_assign_next_active_device(struct btrfs_device *device, |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1950 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1951 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1952 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1953 | struct btrfs_device *next_device; |
| 1954 | |
| 1955 | if (this_dev) |
| 1956 | next_device = this_dev; |
| 1957 | else |
| 1958 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 1959 | device); |
| 1960 | ASSERT(next_device); |
| 1961 | |
| 1962 | if (fs_info->sb->s_bdev && |
| 1963 | (fs_info->sb->s_bdev == device->bdev)) |
| 1964 | fs_info->sb->s_bdev = next_device->bdev; |
| 1965 | |
| 1966 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 1967 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1968 | } |
| 1969 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1970 | /* |
| 1971 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 1972 | * currently replaced. |
| 1973 | */ |
| 1974 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 1975 | { |
| 1976 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 1977 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1978 | down_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1979 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 1980 | ASSERT(num_devices > 1); |
| 1981 | num_devices--; |
| 1982 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1983 | up_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1984 | |
| 1985 | return num_devices; |
| 1986 | } |
| 1987 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1988 | static void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, |
| 1989 | struct block_device *bdev, |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 1990 | const char *device_path) |
| 1991 | { |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 1992 | struct btrfs_super_block *disk_super; |
| 1993 | int copy_num; |
| 1994 | |
| 1995 | if (!bdev) |
| 1996 | return; |
| 1997 | |
| 1998 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) { |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1999 | struct page *page; |
| 2000 | int ret; |
| 2001 | |
| 2002 | disk_super = btrfs_read_dev_one_super(bdev, copy_num); |
| 2003 | if (IS_ERR(disk_super)) |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2004 | continue; |
| 2005 | |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2006 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2007 | |
| 2008 | page = virt_to_page(disk_super); |
| 2009 | set_page_dirty(page); |
| 2010 | lock_page(page); |
| 2011 | /* write_on_page() unlocks the page */ |
| 2012 | ret = write_one_page(page); |
| 2013 | if (ret) |
| 2014 | btrfs_warn(fs_info, |
| 2015 | "error clearing superblock number %d (%d)", |
| 2016 | copy_num, ret); |
| 2017 | btrfs_release_disk_super(disk_super); |
| 2018 | |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | /* Notify udev that device has changed */ |
| 2022 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 2023 | |
| 2024 | /* Update ctime/mtime for device path for libblkid */ |
| 2025 | update_dev_time(device_path); |
| 2026 | } |
| 2027 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2028 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 2029 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2030 | { |
| 2031 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2032 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2033 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2034 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2035 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2036 | |
| 2037 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2038 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2039 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2040 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2041 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2042 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2043 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2044 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2045 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 2046 | |
| 2047 | if (IS_ERR(device)) { |
| 2048 | if (PTR_ERR(device) == -ENOENT && |
| 2049 | strcmp(device_path, "missing") == 0) |
| 2050 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 2051 | else |
| 2052 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2053 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2054 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2055 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 2056 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 2057 | btrfs_warn_in_rcu(fs_info, |
| 2058 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 2059 | rcu_str_deref(device->name), device->devid); |
| 2060 | ret = -ETXTBSY; |
| 2061 | goto out; |
| 2062 | } |
| 2063 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2064 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2065 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2066 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2067 | } |
| 2068 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2069 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 2070 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2071 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2072 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2073 | } |
| 2074 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2075 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2076 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2077 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2078 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2079 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2080 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2081 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2082 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2083 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2084 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2085 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2086 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2087 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2088 | /* |
| 2089 | * TODO: the superblock still includes this device in its num_devices |
| 2090 | * counter although write_all_supers() is not locked out. This |
| 2091 | * could give a filesystem state which requires a degraded mount. |
| 2092 | */ |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 2093 | ret = btrfs_rm_dev_item(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2094 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2095 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2096 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2097 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
David Sterba | 163e97e | 2019-03-20 16:32:55 +0100 | [diff] [blame] | 2098 | btrfs_scrub_cancel_dev(device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2099 | |
| 2100 | /* |
| 2101 | * the device list mutex makes sure that we don't change |
| 2102 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2103 | * the device supers. Whoever is writing all supers, should |
| 2104 | * lock the device list mutex before getting the number of |
| 2105 | * devices in the super block (super_copy). Conversely, |
| 2106 | * whoever updates the number of devices in the super block |
| 2107 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2108 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2109 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2110 | /* |
| 2111 | * In normal cases the cur_devices == fs_devices. But in case |
| 2112 | * of deleting a seed device, the cur_devices should point to |
| 2113 | * its own fs_devices listed under the fs_devices->seed. |
| 2114 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2115 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2116 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2117 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2118 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2119 | cur_devices->num_devices--; |
| 2120 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2121 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2122 | if (cur_devices != fs_devices) |
| 2123 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2124 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2125 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2126 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2127 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2128 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2129 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2130 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2131 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2132 | /* remove sysfs entry */ |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2133 | btrfs_sysfs_remove_devices_dir(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2134 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2135 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2136 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2137 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2138 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2139 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2140 | /* |
| 2141 | * at this point, the device is zero sized and detached from |
| 2142 | * the devices list. All that's left is to zero out the old |
| 2143 | * supers and free the device. |
| 2144 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2145 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2146 | btrfs_scratch_superblocks(fs_info, device->bdev, |
| 2147 | device->name->str); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2148 | |
| 2149 | btrfs_close_bdev(device); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2150 | synchronize_rcu(); |
| 2151 | btrfs_free_device(device); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2152 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2153 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2154 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2155 | if (fs_devices->seed == cur_devices) { |
| 2156 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2157 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2158 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2159 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2160 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2161 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2162 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2163 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2164 | } |
| 2165 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2166 | out: |
| 2167 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2168 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2169 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2170 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2171 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2172 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2173 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2174 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2175 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2176 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2177 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2178 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2179 | } |
| 2180 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2181 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2182 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2183 | struct btrfs_fs_devices *fs_devices; |
| 2184 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2185 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2186 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2187 | /* |
| 2188 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2189 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2190 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2191 | * srcdev will have its correct fs_devices in both the cases. |
| 2192 | */ |
| 2193 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2194 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2195 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2196 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2197 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2198 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2199 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2200 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2201 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2202 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2203 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2204 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2205 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2206 | } |
| 2207 | |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2208 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2209 | { |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2210 | struct btrfs_fs_info *fs_info = srcdev->fs_info; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2211 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2212 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2213 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2214 | /* zero out the old super if it is writable */ |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2215 | btrfs_scratch_superblocks(fs_info, srcdev->bdev, |
| 2216 | srcdev->name->str); |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2217 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2218 | |
| 2219 | btrfs_close_bdev(srcdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2220 | synchronize_rcu(); |
| 2221 | btrfs_free_device(srcdev); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2222 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2223 | /* if this is no devs we rather delete the fs_devices */ |
| 2224 | if (!fs_devices->num_devices) { |
| 2225 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2226 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2227 | /* |
| 2228 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2229 | * seed. In case of a seed device being replaced, the replace |
| 2230 | * target added to the sprout FS, so there will be no more |
| 2231 | * device left under the seed FS. |
| 2232 | */ |
| 2233 | ASSERT(fs_devices->seeding); |
| 2234 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2235 | tmp_fs_devices = fs_info->fs_devices; |
| 2236 | while (tmp_fs_devices) { |
| 2237 | if (tmp_fs_devices->seed == fs_devices) { |
| 2238 | tmp_fs_devices->seed = fs_devices->seed; |
| 2239 | break; |
| 2240 | } |
| 2241 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2242 | } |
| 2243 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2244 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2245 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2246 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2247 | } |
| 2248 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2249 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2250 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2251 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2252 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2253 | mutex_lock(&fs_devices->device_list_mutex); |
| 2254 | |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2255 | btrfs_sysfs_remove_devices_dir(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2256 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2257 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2258 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2259 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2260 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2261 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2262 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2263 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2264 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2265 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2266 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2267 | |
| 2268 | /* |
| 2269 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2270 | * may lead to a call to btrfs_show_devname() which will try |
| 2271 | * to hold device_list_mutex. And here this device |
| 2272 | * is already out of device list, so we don't have to hold |
| 2273 | * the device_list_mutex lock. |
| 2274 | */ |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2275 | btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev, |
| 2276 | tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2277 | |
| 2278 | btrfs_close_bdev(tgtdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2279 | synchronize_rcu(); |
| 2280 | btrfs_free_device(tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2281 | } |
| 2282 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2283 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2284 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2285 | { |
| 2286 | int ret = 0; |
| 2287 | struct btrfs_super_block *disk_super; |
| 2288 | u64 devid; |
| 2289 | u8 *dev_uuid; |
| 2290 | struct block_device *bdev; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2291 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2292 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2293 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2294 | fs_info->bdev_holder, 0, &bdev, &disk_super); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2295 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2296 | return ERR_PTR(ret); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2297 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2298 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2299 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2300 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2301 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2302 | disk_super->metadata_uuid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2303 | else |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2304 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2305 | disk_super->fsid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2306 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2307 | btrfs_release_disk_super(disk_super); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2308 | if (!device) |
| 2309 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2310 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2311 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2314 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2315 | * Lookup a device given by device id, or the path if the id is 0. |
| 2316 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2317 | struct btrfs_device *btrfs_find_device_by_devspec( |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2318 | struct btrfs_fs_info *fs_info, u64 devid, |
| 2319 | const char *device_path) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2320 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2321 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2322 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2323 | if (devid) { |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2324 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2325 | NULL, true); |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2326 | if (!device) |
| 2327 | return ERR_PTR(-ENOENT); |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2328 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2329 | } |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2330 | |
| 2331 | if (!device_path || !device_path[0]) |
| 2332 | return ERR_PTR(-EINVAL); |
| 2333 | |
| 2334 | if (strcmp(device_path, "missing") == 0) { |
| 2335 | /* Find first missing device */ |
| 2336 | list_for_each_entry(device, &fs_info->fs_devices->devices, |
| 2337 | dev_list) { |
| 2338 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2339 | &device->dev_state) && !device->bdev) |
| 2340 | return device; |
| 2341 | } |
| 2342 | return ERR_PTR(-ENOENT); |
| 2343 | } |
| 2344 | |
| 2345 | return btrfs_find_device_by_path(fs_info, device_path); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2346 | } |
| 2347 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2348 | /* |
| 2349 | * does all the dirty work required for changing file system's UUID. |
| 2350 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2351 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2352 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2353 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2354 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2355 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2356 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2357 | struct btrfs_device *device; |
| 2358 | u64 super_flags; |
| 2359 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2360 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2361 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2362 | return -EINVAL; |
| 2363 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2364 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2365 | if (IS_ERR(seed_devices)) |
| 2366 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2367 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2368 | old_devices = clone_fs_devices(fs_devices); |
| 2369 | if (IS_ERR(old_devices)) { |
| 2370 | kfree(seed_devices); |
| 2371 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2372 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2373 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2374 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2375 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2376 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2377 | seed_devices->opened = 1; |
| 2378 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2379 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2380 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2381 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2382 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2383 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2384 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2385 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2386 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2387 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2388 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2389 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2390 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2391 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 2392 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2393 | fs_devices->num_devices = 0; |
| 2394 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2395 | fs_devices->missing_devices = 0; |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2396 | fs_devices->rotating = false; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2397 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2398 | |
| 2399 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2400 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2401 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2402 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2403 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2404 | super_flags = btrfs_super_flags(disk_super) & |
| 2405 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2406 | btrfs_set_super_flags(disk_super, super_flags); |
| 2407 | |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
| 2411 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2412 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2413 | */ |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2414 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2415 | { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2416 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2417 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2418 | struct btrfs_path *path; |
| 2419 | struct extent_buffer *leaf; |
| 2420 | struct btrfs_dev_item *dev_item; |
| 2421 | struct btrfs_device *device; |
| 2422 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2423 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2424 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2425 | u64 devid; |
| 2426 | int ret; |
| 2427 | |
| 2428 | path = btrfs_alloc_path(); |
| 2429 | if (!path) |
| 2430 | return -ENOMEM; |
| 2431 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2432 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2433 | key.offset = 0; |
| 2434 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2435 | |
| 2436 | while (1) { |
| 2437 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2438 | if (ret < 0) |
| 2439 | goto error; |
| 2440 | |
| 2441 | leaf = path->nodes[0]; |
| 2442 | next_slot: |
| 2443 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2444 | ret = btrfs_next_leaf(root, path); |
| 2445 | if (ret > 0) |
| 2446 | break; |
| 2447 | if (ret < 0) |
| 2448 | goto error; |
| 2449 | leaf = path->nodes[0]; |
| 2450 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2451 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2452 | continue; |
| 2453 | } |
| 2454 | |
| 2455 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2456 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2457 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2458 | break; |
| 2459 | |
| 2460 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2461 | struct btrfs_dev_item); |
| 2462 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2463 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2464 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2465 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2466 | BTRFS_FSID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2467 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2468 | fs_uuid, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2469 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2470 | |
| 2471 | if (device->fs_devices->seeding) { |
| 2472 | btrfs_set_device_generation(leaf, dev_item, |
| 2473 | device->generation); |
| 2474 | btrfs_mark_buffer_dirty(leaf); |
| 2475 | } |
| 2476 | |
| 2477 | path->slots[0]++; |
| 2478 | goto next_slot; |
| 2479 | } |
| 2480 | ret = 0; |
| 2481 | error: |
| 2482 | btrfs_free_path(path); |
| 2483 | return ret; |
| 2484 | } |
| 2485 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2486 | 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] | 2487 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2488 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2489 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2490 | struct btrfs_trans_handle *trans; |
| 2491 | struct btrfs_device *device; |
| 2492 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2493 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2494 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2495 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2496 | u64 orig_super_total_bytes; |
| 2497 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2498 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2499 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2500 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2501 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2502 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2503 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2504 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2505 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2506 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2507 | if (IS_ERR(bdev)) |
| 2508 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2509 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2510 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2511 | seeding_dev = 1; |
| 2512 | down_write(&sb->s_umount); |
| 2513 | mutex_lock(&uuid_mutex); |
| 2514 | } |
| 2515 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2516 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2517 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2518 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2519 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2520 | if (device->bdev == bdev) { |
| 2521 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2522 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2523 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2524 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2525 | } |
| 2526 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2527 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2528 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2529 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2530 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2531 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2532 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2533 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2534 | } |
| 2535 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2536 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2537 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2538 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2539 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2540 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2541 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2542 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2543 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2544 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2545 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2546 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2549 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2550 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2551 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2552 | device->io_width = fs_info->sectorsize; |
| 2553 | device->io_align = fs_info->sectorsize; |
| 2554 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2555 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2556 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2557 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2558 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2559 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2560 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2561 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2562 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2563 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2564 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2565 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2566 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2567 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2568 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2569 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2570 | if (ret) { |
| 2571 | btrfs_abort_transaction(trans, ret); |
| 2572 | goto error_trans; |
| 2573 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2574 | } |
| 2575 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2576 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2577 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2578 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2579 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2580 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2581 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2582 | fs_devices->num_devices++; |
| 2583 | fs_devices->open_devices++; |
| 2584 | fs_devices->rw_devices++; |
| 2585 | fs_devices->total_devices++; |
| 2586 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2587 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2588 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2589 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2590 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2591 | fs_devices->rotating = true; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2592 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2593 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2594 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2595 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2596 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2597 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2598 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2599 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2600 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2601 | |
| 2602 | /* add sysfs device entry */ |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2603 | btrfs_sysfs_add_devices_dir(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2604 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2605 | /* |
| 2606 | * we've got more storage, clear any full flags on the space |
| 2607 | * infos |
| 2608 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2609 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2610 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2611 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2612 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2613 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2614 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2615 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 2616 | ret = init_first_rw_device(trans); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2617 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2618 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2619 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2620 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2621 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2622 | } |
| 2623 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2624 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2625 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2626 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2627 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | if (seeding_dev) { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2631 | ret = btrfs_finish_sprout(trans); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2632 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2633 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2634 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2635 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2636 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 2637 | btrfs_sysfs_update_sprout_fsid(fs_devices, |
| 2638 | fs_info->fs_devices->fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2639 | } |
| 2640 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2641 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2642 | |
| 2643 | if (seeding_dev) { |
| 2644 | mutex_unlock(&uuid_mutex); |
| 2645 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2646 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2647 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2648 | if (ret) /* transaction commit */ |
| 2649 | return ret; |
| 2650 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2651 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2652 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2653 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2654 | "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] | 2655 | trans = btrfs_attach_transaction(root); |
| 2656 | if (IS_ERR(trans)) { |
| 2657 | if (PTR_ERR(trans) == -ENOENT) |
| 2658 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2659 | ret = PTR_ERR(trans); |
| 2660 | trans = NULL; |
| 2661 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2662 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2663 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2664 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2665 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2666 | /* Update ctime/mtime for libblkid */ |
| 2667 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2668 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2669 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2670 | error_sysfs: |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2671 | btrfs_sysfs_remove_devices_dir(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2672 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2673 | mutex_lock(&fs_info->chunk_mutex); |
| 2674 | list_del_rcu(&device->dev_list); |
| 2675 | list_del(&device->dev_alloc_list); |
| 2676 | fs_info->fs_devices->num_devices--; |
| 2677 | fs_info->fs_devices->open_devices--; |
| 2678 | fs_info->fs_devices->rw_devices--; |
| 2679 | fs_info->fs_devices->total_devices--; |
| 2680 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2681 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2682 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2683 | orig_super_total_bytes); |
| 2684 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2685 | orig_super_num_devices); |
| 2686 | mutex_unlock(&fs_info->chunk_mutex); |
| 2687 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2688 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2689 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2690 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2691 | if (trans) |
| 2692 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2693 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2694 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2695 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2696 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2697 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2698 | mutex_unlock(&uuid_mutex); |
| 2699 | up_write(&sb->s_umount); |
| 2700 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2701 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2702 | } |
| 2703 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2704 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2705 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2706 | { |
| 2707 | int ret; |
| 2708 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2709 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2710 | struct btrfs_dev_item *dev_item; |
| 2711 | struct extent_buffer *leaf; |
| 2712 | struct btrfs_key key; |
| 2713 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2714 | path = btrfs_alloc_path(); |
| 2715 | if (!path) |
| 2716 | return -ENOMEM; |
| 2717 | |
| 2718 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2719 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2720 | key.offset = device->devid; |
| 2721 | |
| 2722 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2723 | if (ret < 0) |
| 2724 | goto out; |
| 2725 | |
| 2726 | if (ret > 0) { |
| 2727 | ret = -ENOENT; |
| 2728 | goto out; |
| 2729 | } |
| 2730 | |
| 2731 | leaf = path->nodes[0]; |
| 2732 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2733 | |
| 2734 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2735 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2736 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2737 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2738 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2739 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2740 | btrfs_device_get_disk_total_bytes(device)); |
| 2741 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2742 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2743 | btrfs_mark_buffer_dirty(leaf); |
| 2744 | |
| 2745 | out: |
| 2746 | btrfs_free_path(path); |
| 2747 | return ret; |
| 2748 | } |
| 2749 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2750 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2751 | struct btrfs_device *device, u64 new_size) |
| 2752 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2753 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2754 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2755 | u64 old_total; |
| 2756 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2757 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2758 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2759 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2760 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2761 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2762 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2763 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2764 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2765 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2766 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2767 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2768 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2769 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2770 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2771 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2772 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2773 | btrfs_set_super_total_bytes(super_copy, |
| 2774 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2775 | device->fs_devices->total_rw_bytes += diff; |
| 2776 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2777 | btrfs_device_set_total_bytes(device, new_size); |
| 2778 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2779 | btrfs_clear_space_info_full(device->fs_info); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 2780 | if (list_empty(&device->post_commit_list)) |
| 2781 | list_add_tail(&device->post_commit_list, |
| 2782 | &trans->transaction->dev_update_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2783 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2784 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2785 | return btrfs_update_device(trans, device); |
| 2786 | } |
| 2787 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2788 | 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] | 2789 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2790 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2791 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2792 | int ret; |
| 2793 | struct btrfs_path *path; |
| 2794 | struct btrfs_key key; |
| 2795 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2796 | path = btrfs_alloc_path(); |
| 2797 | if (!path) |
| 2798 | return -ENOMEM; |
| 2799 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2800 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2801 | key.offset = chunk_offset; |
| 2802 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2803 | |
| 2804 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2805 | if (ret < 0) |
| 2806 | goto out; |
| 2807 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2808 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2809 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2810 | ret = -ENOENT; |
| 2811 | goto out; |
| 2812 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2813 | |
| 2814 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2815 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2816 | btrfs_handle_fs_error(fs_info, ret, |
| 2817 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2818 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2819 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2820 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2821 | } |
| 2822 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2823 | 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] | 2824 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2825 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2826 | struct btrfs_disk_key *disk_key; |
| 2827 | struct btrfs_chunk *chunk; |
| 2828 | u8 *ptr; |
| 2829 | int ret = 0; |
| 2830 | u32 num_stripes; |
| 2831 | u32 array_size; |
| 2832 | u32 len = 0; |
| 2833 | u32 cur; |
| 2834 | struct btrfs_key key; |
| 2835 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2836 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2837 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2838 | |
| 2839 | ptr = super_copy->sys_chunk_array; |
| 2840 | cur = 0; |
| 2841 | |
| 2842 | while (cur < array_size) { |
| 2843 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2844 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2845 | |
| 2846 | len = sizeof(*disk_key); |
| 2847 | |
| 2848 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2849 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2850 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2851 | len += btrfs_chunk_item_size(num_stripes); |
| 2852 | } else { |
| 2853 | ret = -EIO; |
| 2854 | break; |
| 2855 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2856 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2857 | key.offset == chunk_offset) { |
| 2858 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2859 | array_size -= len; |
| 2860 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2861 | } else { |
| 2862 | ptr += len; |
| 2863 | cur += len; |
| 2864 | } |
| 2865 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2866 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2867 | return ret; |
| 2868 | } |
| 2869 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2870 | /* |
| 2871 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2872 | * @logical: Logical block offset in bytes. |
| 2873 | * @length: Length of extent in bytes. |
| 2874 | * |
| 2875 | * Return: Chunk mapping or ERR_PTR. |
| 2876 | */ |
| 2877 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2878 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2879 | { |
| 2880 | struct extent_map_tree *em_tree; |
| 2881 | struct extent_map *em; |
| 2882 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 2883 | em_tree = &fs_info->mapping_tree; |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2884 | read_lock(&em_tree->lock); |
| 2885 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2886 | read_unlock(&em_tree->lock); |
| 2887 | |
| 2888 | if (!em) { |
| 2889 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2890 | logical, length); |
| 2891 | return ERR_PTR(-EINVAL); |
| 2892 | } |
| 2893 | |
| 2894 | if (em->start > logical || em->start + em->len < logical) { |
| 2895 | btrfs_crit(fs_info, |
| 2896 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2897 | logical, length, em->start, em->start + em->len); |
| 2898 | free_extent_map(em); |
| 2899 | return ERR_PTR(-EINVAL); |
| 2900 | } |
| 2901 | |
| 2902 | /* callers are responsible for dropping em's ref. */ |
| 2903 | return em; |
| 2904 | } |
| 2905 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2906 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2907 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2908 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2909 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2910 | struct map_lookup *map; |
| 2911 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2912 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2913 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2914 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2915 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2916 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2917 | /* |
| 2918 | * 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] | 2919 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2920 | * do anything we still error out. |
| 2921 | */ |
| 2922 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2923 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2924 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2925 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2926 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2927 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2928 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2929 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2930 | /* |
| 2931 | * Take the device list mutex to prevent races with the final phase of |
| 2932 | * a device replace operation that replaces the device object associated |
| 2933 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2934 | */ |
| 2935 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2936 | for (i = 0; i < map->num_stripes; i++) { |
| 2937 | struct btrfs_device *device = map->stripes[i].dev; |
| 2938 | ret = btrfs_free_dev_extent(trans, device, |
| 2939 | map->stripes[i].physical, |
| 2940 | &dev_extent_len); |
| 2941 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2942 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2943 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2944 | goto out; |
| 2945 | } |
| 2946 | |
| 2947 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2948 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2949 | btrfs_device_set_bytes_used(device, |
| 2950 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2951 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2952 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2953 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2954 | } |
| 2955 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 2956 | ret = btrfs_update_device(trans, device); |
| 2957 | if (ret) { |
| 2958 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2959 | btrfs_abort_transaction(trans, ret); |
| 2960 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2961 | } |
| 2962 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2963 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2964 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2965 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2966 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2967 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2968 | goto out; |
| 2969 | } |
| 2970 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2971 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2972 | |
| 2973 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2974 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2975 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2976 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2977 | goto out; |
| 2978 | } |
| 2979 | } |
| 2980 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 2981 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2982 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2983 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2984 | goto out; |
| 2985 | } |
| 2986 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2987 | out: |
| 2988 | /* once for us */ |
| 2989 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2990 | return ret; |
| 2991 | } |
| 2992 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2993 | 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] | 2994 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2995 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2996 | struct btrfs_trans_handle *trans; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2997 | struct btrfs_block_group *block_group; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2998 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2999 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3000 | /* |
| 3001 | * Prevent races with automatic removal of unused block groups. |
| 3002 | * After we relocate and before we remove the chunk with offset |
| 3003 | * chunk_offset, automatic removal of the block group can kick in, |
| 3004 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 3005 | * |
| 3006 | * Make sure to acquire this mutex before doing a tree search (dev |
| 3007 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 3008 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 3009 | * we release the path used to search the chunk/dev tree and before |
| 3010 | * the current task acquires this mutex and calls us. |
| 3011 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 3012 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3013 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3014 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3015 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3016 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3017 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3018 | if (ret) |
| 3019 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3020 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3021 | block_group = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3022 | if (!block_group) |
| 3023 | return -ENOENT; |
| 3024 | btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); |
| 3025 | btrfs_put_block_group(block_group); |
| 3026 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3027 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 3028 | chunk_offset); |
| 3029 | if (IS_ERR(trans)) { |
| 3030 | ret = PTR_ERR(trans); |
| 3031 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 3032 | return ret; |
| 3033 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 3034 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3035 | /* |
| 3036 | * step two, delete the device extents and the |
| 3037 | * chunk tree entries |
| 3038 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3039 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3040 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3041 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3042 | } |
| 3043 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3044 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3045 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3046 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3047 | struct btrfs_path *path; |
| 3048 | struct extent_buffer *leaf; |
| 3049 | struct btrfs_chunk *chunk; |
| 3050 | struct btrfs_key key; |
| 3051 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3052 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3053 | bool retried = false; |
| 3054 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3055 | int ret; |
| 3056 | |
| 3057 | path = btrfs_alloc_path(); |
| 3058 | if (!path) |
| 3059 | return -ENOMEM; |
| 3060 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3061 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3062 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3063 | key.offset = (u64)-1; |
| 3064 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3065 | |
| 3066 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3067 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3068 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3069 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3070 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3071 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3072 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3073 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3074 | |
| 3075 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3076 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3077 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3078 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3079 | if (ret < 0) |
| 3080 | goto error; |
| 3081 | if (ret > 0) |
| 3082 | break; |
| 3083 | |
| 3084 | leaf = path->nodes[0]; |
| 3085 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3086 | |
| 3087 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3088 | struct btrfs_chunk); |
| 3089 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3090 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3091 | |
| 3092 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3093 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3094 | if (ret == -ENOSPC) |
| 3095 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3096 | else |
| 3097 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3098 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3099 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3100 | |
| 3101 | if (found_key.offset == 0) |
| 3102 | break; |
| 3103 | key.offset = found_key.offset - 1; |
| 3104 | } |
| 3105 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3106 | if (failed && !retried) { |
| 3107 | failed = 0; |
| 3108 | retried = true; |
| 3109 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3110 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3111 | ret = -ENOSPC; |
| 3112 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3113 | error: |
| 3114 | btrfs_free_path(path); |
| 3115 | return ret; |
| 3116 | } |
| 3117 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3118 | /* |
| 3119 | * return 1 : allocate a data chunk successfully, |
| 3120 | * return <0: errors during allocating a data chunk, |
| 3121 | * return 0 : no need to allocate a data chunk. |
| 3122 | */ |
| 3123 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3124 | u64 chunk_offset) |
| 3125 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3126 | struct btrfs_block_group *cache; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3127 | u64 bytes_used; |
| 3128 | u64 chunk_type; |
| 3129 | |
| 3130 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3131 | ASSERT(cache); |
| 3132 | chunk_type = cache->flags; |
| 3133 | btrfs_put_block_group(cache); |
| 3134 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3135 | if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA)) |
| 3136 | return 0; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3137 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3138 | spin_lock(&fs_info->data_sinfo->lock); |
| 3139 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3140 | spin_unlock(&fs_info->data_sinfo->lock); |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3141 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3142 | if (!bytes_used) { |
| 3143 | struct btrfs_trans_handle *trans; |
| 3144 | int ret; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3145 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3146 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3147 | if (IS_ERR(trans)) |
| 3148 | return PTR_ERR(trans); |
| 3149 | |
| 3150 | ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA); |
| 3151 | btrfs_end_transaction(trans); |
| 3152 | if (ret < 0) |
| 3153 | return ret; |
| 3154 | return 1; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3155 | } |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3156 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3157 | return 0; |
| 3158 | } |
| 3159 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3160 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3161 | struct btrfs_balance_control *bctl) |
| 3162 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3163 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3164 | struct btrfs_trans_handle *trans; |
| 3165 | struct btrfs_balance_item *item; |
| 3166 | struct btrfs_disk_balance_args disk_bargs; |
| 3167 | struct btrfs_path *path; |
| 3168 | struct extent_buffer *leaf; |
| 3169 | struct btrfs_key key; |
| 3170 | int ret, err; |
| 3171 | |
| 3172 | path = btrfs_alloc_path(); |
| 3173 | if (!path) |
| 3174 | return -ENOMEM; |
| 3175 | |
| 3176 | trans = btrfs_start_transaction(root, 0); |
| 3177 | if (IS_ERR(trans)) { |
| 3178 | btrfs_free_path(path); |
| 3179 | return PTR_ERR(trans); |
| 3180 | } |
| 3181 | |
| 3182 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3183 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3184 | key.offset = 0; |
| 3185 | |
| 3186 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3187 | sizeof(*item)); |
| 3188 | if (ret) |
| 3189 | goto out; |
| 3190 | |
| 3191 | leaf = path->nodes[0]; |
| 3192 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3193 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3194 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3195 | |
| 3196 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3197 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3198 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3199 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3200 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3201 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3202 | |
| 3203 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3204 | |
| 3205 | btrfs_mark_buffer_dirty(leaf); |
| 3206 | out: |
| 3207 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3208 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3209 | if (err && !ret) |
| 3210 | ret = err; |
| 3211 | return ret; |
| 3212 | } |
| 3213 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3214 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3215 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3216 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3217 | struct btrfs_trans_handle *trans; |
| 3218 | struct btrfs_path *path; |
| 3219 | struct btrfs_key key; |
| 3220 | int ret, err; |
| 3221 | |
| 3222 | path = btrfs_alloc_path(); |
| 3223 | if (!path) |
| 3224 | return -ENOMEM; |
| 3225 | |
| 3226 | trans = btrfs_start_transaction(root, 0); |
| 3227 | if (IS_ERR(trans)) { |
| 3228 | btrfs_free_path(path); |
| 3229 | return PTR_ERR(trans); |
| 3230 | } |
| 3231 | |
| 3232 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3233 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3234 | key.offset = 0; |
| 3235 | |
| 3236 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3237 | if (ret < 0) |
| 3238 | goto out; |
| 3239 | if (ret > 0) { |
| 3240 | ret = -ENOENT; |
| 3241 | goto out; |
| 3242 | } |
| 3243 | |
| 3244 | ret = btrfs_del_item(trans, root, path); |
| 3245 | out: |
| 3246 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3247 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3248 | if (err && !ret) |
| 3249 | ret = err; |
| 3250 | return ret; |
| 3251 | } |
| 3252 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3253 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3254 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3255 | * resume after balance was interrupted. |
| 3256 | */ |
| 3257 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3258 | { |
| 3259 | /* |
| 3260 | * Turn on soft mode for chunk types that were being converted. |
| 3261 | */ |
| 3262 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3263 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3264 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3265 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3266 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3267 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3268 | |
| 3269 | /* |
| 3270 | * Turn on usage filter if is not already used. The idea is |
| 3271 | * that chunks that we have already balanced should be |
| 3272 | * reasonably full. Don't do it for chunks that are being |
| 3273 | * converted - that will keep us from relocating unconverted |
| 3274 | * (albeit full) chunks. |
| 3275 | */ |
| 3276 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3277 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3278 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3279 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3280 | bctl->data.usage = 90; |
| 3281 | } |
| 3282 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3283 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3284 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3285 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3286 | bctl->sys.usage = 90; |
| 3287 | } |
| 3288 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3289 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3290 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3291 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3292 | bctl->meta.usage = 90; |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3297 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3298 | */ |
| 3299 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3300 | { |
| 3301 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3302 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3303 | |
| 3304 | BUG_ON(!fs_info->balance_ctl); |
| 3305 | |
| 3306 | spin_lock(&fs_info->balance_lock); |
| 3307 | fs_info->balance_ctl = NULL; |
| 3308 | spin_unlock(&fs_info->balance_lock); |
| 3309 | |
| 3310 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3311 | ret = del_balance_item(fs_info); |
| 3312 | if (ret) |
| 3313 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3314 | } |
| 3315 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3316 | /* |
| 3317 | * Balance filters. Return 1 if chunk should be filtered out |
| 3318 | * (should not be balanced). |
| 3319 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3320 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3321 | struct btrfs_balance_args *bargs) |
| 3322 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3323 | chunk_type = chunk_to_extended(chunk_type) & |
| 3324 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3325 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3326 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3327 | return 0; |
| 3328 | |
| 3329 | return 1; |
| 3330 | } |
| 3331 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3332 | 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] | 3333 | struct btrfs_balance_args *bargs) |
| 3334 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3335 | struct btrfs_block_group *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3336 | u64 chunk_used; |
| 3337 | u64 user_thresh_min; |
| 3338 | u64 user_thresh_max; |
| 3339 | int ret = 1; |
| 3340 | |
| 3341 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3342 | chunk_used = cache->used; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3343 | |
| 3344 | if (bargs->usage_min == 0) |
| 3345 | user_thresh_min = 0; |
| 3346 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3347 | user_thresh_min = div_factor_fine(cache->length, |
| 3348 | bargs->usage_min); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3349 | |
| 3350 | if (bargs->usage_max == 0) |
| 3351 | user_thresh_max = 1; |
| 3352 | else if (bargs->usage_max > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3353 | user_thresh_max = cache->length; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3354 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3355 | user_thresh_max = div_factor_fine(cache->length, |
| 3356 | bargs->usage_max); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3357 | |
| 3358 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3359 | ret = 0; |
| 3360 | |
| 3361 | btrfs_put_block_group(cache); |
| 3362 | return ret; |
| 3363 | } |
| 3364 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3365 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3366 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3367 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3368 | struct btrfs_block_group *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3369 | u64 chunk_used, user_thresh; |
| 3370 | int ret = 1; |
| 3371 | |
| 3372 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3373 | chunk_used = cache->used; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3374 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3375 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3376 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3377 | else if (bargs->usage > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3378 | user_thresh = cache->length; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3379 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3380 | user_thresh = div_factor_fine(cache->length, bargs->usage); |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3381 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3382 | if (chunk_used < user_thresh) |
| 3383 | ret = 0; |
| 3384 | |
| 3385 | btrfs_put_block_group(cache); |
| 3386 | return ret; |
| 3387 | } |
| 3388 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3389 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3390 | struct btrfs_chunk *chunk, |
| 3391 | struct btrfs_balance_args *bargs) |
| 3392 | { |
| 3393 | struct btrfs_stripe *stripe; |
| 3394 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3395 | int i; |
| 3396 | |
| 3397 | for (i = 0; i < num_stripes; i++) { |
| 3398 | stripe = btrfs_stripe_nr(chunk, i); |
| 3399 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | return 1; |
| 3404 | } |
| 3405 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3406 | static u64 calc_data_stripes(u64 type, int num_stripes) |
| 3407 | { |
| 3408 | const int index = btrfs_bg_flags_to_raid_index(type); |
| 3409 | const int ncopies = btrfs_raid_array[index].ncopies; |
| 3410 | const int nparity = btrfs_raid_array[index].nparity; |
| 3411 | |
| 3412 | if (nparity) |
| 3413 | return num_stripes - nparity; |
| 3414 | else |
| 3415 | return num_stripes / ncopies; |
| 3416 | } |
| 3417 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3418 | /* [pstart, pend) */ |
| 3419 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3420 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3421 | struct btrfs_balance_args *bargs) |
| 3422 | { |
| 3423 | struct btrfs_stripe *stripe; |
| 3424 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3425 | u64 stripe_offset; |
| 3426 | u64 stripe_length; |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3427 | u64 type; |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3428 | int factor; |
| 3429 | int i; |
| 3430 | |
| 3431 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3432 | return 0; |
| 3433 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3434 | type = btrfs_chunk_type(leaf, chunk); |
| 3435 | factor = calc_data_stripes(type, num_stripes); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3436 | |
| 3437 | for (i = 0; i < num_stripes; i++) { |
| 3438 | stripe = btrfs_stripe_nr(chunk, i); |
| 3439 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3440 | continue; |
| 3441 | |
| 3442 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3443 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3444 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3445 | |
| 3446 | if (stripe_offset < bargs->pend && |
| 3447 | stripe_offset + stripe_length > bargs->pstart) |
| 3448 | return 0; |
| 3449 | } |
| 3450 | |
| 3451 | return 1; |
| 3452 | } |
| 3453 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3454 | /* [vstart, vend) */ |
| 3455 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3456 | struct btrfs_chunk *chunk, |
| 3457 | u64 chunk_offset, |
| 3458 | struct btrfs_balance_args *bargs) |
| 3459 | { |
| 3460 | if (chunk_offset < bargs->vend && |
| 3461 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3462 | /* at least part of the chunk is inside this vrange */ |
| 3463 | return 0; |
| 3464 | |
| 3465 | return 1; |
| 3466 | } |
| 3467 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3468 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3469 | struct btrfs_chunk *chunk, |
| 3470 | struct btrfs_balance_args *bargs) |
| 3471 | { |
| 3472 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3473 | |
| 3474 | if (bargs->stripes_min <= num_stripes |
| 3475 | && num_stripes <= bargs->stripes_max) |
| 3476 | return 0; |
| 3477 | |
| 3478 | return 1; |
| 3479 | } |
| 3480 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3481 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3482 | struct btrfs_balance_args *bargs) |
| 3483 | { |
| 3484 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3485 | return 0; |
| 3486 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3487 | chunk_type = chunk_to_extended(chunk_type) & |
| 3488 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3489 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3490 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3491 | return 1; |
| 3492 | |
| 3493 | return 0; |
| 3494 | } |
| 3495 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3496 | static int should_balance_chunk(struct extent_buffer *leaf, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3497 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3498 | { |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3499 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3500 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3501 | struct btrfs_balance_args *bargs = NULL; |
| 3502 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3503 | |
| 3504 | /* type filter */ |
| 3505 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3506 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3507 | return 0; |
| 3508 | } |
| 3509 | |
| 3510 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3511 | bargs = &bctl->data; |
| 3512 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3513 | bargs = &bctl->sys; |
| 3514 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3515 | bargs = &bctl->meta; |
| 3516 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3517 | /* profiles filter */ |
| 3518 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3519 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3520 | return 0; |
| 3521 | } |
| 3522 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3523 | /* usage filter */ |
| 3524 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3525 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3526 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3527 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3528 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3529 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3530 | } |
| 3531 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3532 | /* devid filter */ |
| 3533 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3534 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3538 | /* drange filter, makes sense only with devid filter */ |
| 3539 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3540 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3541 | return 0; |
| 3542 | } |
| 3543 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3544 | /* vrange filter */ |
| 3545 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3546 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3547 | return 0; |
| 3548 | } |
| 3549 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3550 | /* stripes filter */ |
| 3551 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3552 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3553 | return 0; |
| 3554 | } |
| 3555 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3556 | /* soft profile changing mode */ |
| 3557 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3558 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3559 | return 0; |
| 3560 | } |
| 3561 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3562 | /* |
| 3563 | * limited by count, must be the last filter |
| 3564 | */ |
| 3565 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3566 | if (bargs->limit == 0) |
| 3567 | return 0; |
| 3568 | else |
| 3569 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3570 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3571 | /* |
| 3572 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3573 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3574 | * about the count of all chunks that satisfy the filters. |
| 3575 | */ |
| 3576 | if (bargs->limit_max == 0) |
| 3577 | return 0; |
| 3578 | else |
| 3579 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3580 | } |
| 3581 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3582 | return 1; |
| 3583 | } |
| 3584 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3585 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3586 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3587 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3588 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3589 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3590 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3591 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3592 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3593 | struct btrfs_key found_key; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3594 | struct extent_buffer *leaf; |
| 3595 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3596 | int ret; |
| 3597 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3598 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3599 | /* 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] | 3600 | u64 limit_data = bctl->data.limit; |
| 3601 | u64 limit_meta = bctl->meta.limit; |
| 3602 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3603 | u32 count_data = 0; |
| 3604 | u32 count_meta = 0; |
| 3605 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3606 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3607 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3608 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3609 | if (!path) { |
| 3610 | ret = -ENOMEM; |
| 3611 | goto error; |
| 3612 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3613 | |
| 3614 | /* zero out stat counters */ |
| 3615 | spin_lock(&fs_info->balance_lock); |
| 3616 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3617 | spin_unlock(&fs_info->balance_lock); |
| 3618 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3619 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3620 | /* |
| 3621 | * The single value limit and min/max limits use the same bytes |
| 3622 | * in the |
| 3623 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3624 | bctl->data.limit = limit_data; |
| 3625 | bctl->meta.limit = limit_meta; |
| 3626 | bctl->sys.limit = limit_sys; |
| 3627 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3628 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3629 | key.offset = (u64)-1; |
| 3630 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3631 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3632 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3633 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3634 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3635 | ret = -ECANCELED; |
| 3636 | goto error; |
| 3637 | } |
| 3638 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3639 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3640 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3641 | if (ret < 0) { |
| 3642 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3643 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3644 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3645 | |
| 3646 | /* |
| 3647 | * this shouldn't happen, it means the last relocate |
| 3648 | * failed |
| 3649 | */ |
| 3650 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3651 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3652 | |
| 3653 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3654 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3655 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3656 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3657 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3658 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3659 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3660 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3661 | leaf = path->nodes[0]; |
| 3662 | slot = path->slots[0]; |
| 3663 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3664 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3665 | if (found_key.objectid != key.objectid) { |
| 3666 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3667 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3668 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3669 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3670 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3671 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3672 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3673 | if (!counting) { |
| 3674 | spin_lock(&fs_info->balance_lock); |
| 3675 | bctl->stat.considered++; |
| 3676 | spin_unlock(&fs_info->balance_lock); |
| 3677 | } |
| 3678 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3679 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3680 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3681 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3682 | if (!ret) { |
| 3683 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3684 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3685 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3686 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3687 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3688 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3689 | spin_lock(&fs_info->balance_lock); |
| 3690 | bctl->stat.expected++; |
| 3691 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3692 | |
| 3693 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3694 | count_data++; |
| 3695 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3696 | count_sys++; |
| 3697 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3698 | count_meta++; |
| 3699 | |
| 3700 | goto loop; |
| 3701 | } |
| 3702 | |
| 3703 | /* |
| 3704 | * Apply limit_min filter, no need to check if the LIMITS |
| 3705 | * filter is used, limit_min is 0 by default |
| 3706 | */ |
| 3707 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3708 | count_data < bctl->data.limit_min) |
| 3709 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3710 | count_meta < bctl->meta.limit_min) |
| 3711 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3712 | count_sys < bctl->sys.limit_min)) { |
| 3713 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3714 | goto loop; |
| 3715 | } |
| 3716 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3717 | if (!chunk_reserved) { |
| 3718 | /* |
| 3719 | * We may be relocating the only data chunk we have, |
| 3720 | * which could potentially end up with losing data's |
| 3721 | * raid profile, so lets allocate an empty one in |
| 3722 | * advance. |
| 3723 | */ |
| 3724 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3725 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3726 | if (ret < 0) { |
| 3727 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3728 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3729 | } else if (ret == 1) { |
| 3730 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3731 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3732 | } |
| 3733 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3734 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3735 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3736 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3737 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3738 | } else if (ret == -ETXTBSY) { |
| 3739 | btrfs_info(fs_info, |
| 3740 | "skipping relocation of block group %llu due to active swapfile", |
| 3741 | found_key.offset); |
| 3742 | ret = 0; |
| 3743 | } else if (ret) { |
| 3744 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3745 | } else { |
| 3746 | spin_lock(&fs_info->balance_lock); |
| 3747 | bctl->stat.completed++; |
| 3748 | spin_unlock(&fs_info->balance_lock); |
| 3749 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3750 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3751 | if (found_key.offset == 0) |
| 3752 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3753 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3754 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3755 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3756 | if (counting) { |
| 3757 | btrfs_release_path(path); |
| 3758 | counting = false; |
| 3759 | goto again; |
| 3760 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3761 | error: |
| 3762 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3763 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3764 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3765 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3766 | if (!ret) |
| 3767 | ret = -ENOSPC; |
| 3768 | } |
| 3769 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3770 | return ret; |
| 3771 | } |
| 3772 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3773 | /** |
| 3774 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3775 | * @flags: profile to validate |
| 3776 | * @extended: if true @flags is treated as an extended profile |
| 3777 | */ |
| 3778 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3779 | { |
| 3780 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3781 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3782 | |
| 3783 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3784 | |
| 3785 | /* 1) check that all other bits are zeroed */ |
| 3786 | if (flags & ~mask) |
| 3787 | return 0; |
| 3788 | |
| 3789 | /* 2) see if profile is reduced */ |
| 3790 | if (flags == 0) |
| 3791 | return !extended; /* "0" is valid for usual profiles */ |
| 3792 | |
David Sterba | c149916 | 2019-10-01 19:44:42 +0200 | [diff] [blame] | 3793 | return has_single_bit_set(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3794 | } |
| 3795 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3796 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3797 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3798 | /* cancel requested || normal exit path */ |
| 3799 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3800 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3801 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3802 | } |
| 3803 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3804 | /* Non-zero return value signifies invalidity */ |
| 3805 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3806 | u64 allowed) |
| 3807 | { |
| 3808 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3809 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3810 | (bctl_arg->target & ~allowed))); |
| 3811 | } |
| 3812 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3813 | /* |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3814 | * Fill @buf with textual description of balance filter flags @bargs, up to |
| 3815 | * @size_buf including the terminating null. The output may be trimmed if it |
| 3816 | * does not fit into the provided buffer. |
| 3817 | */ |
| 3818 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, |
| 3819 | u32 size_buf) |
| 3820 | { |
| 3821 | int ret; |
| 3822 | u32 size_bp = size_buf; |
| 3823 | char *bp = buf; |
| 3824 | u64 flags = bargs->flags; |
| 3825 | char tmp_buf[128] = {'\0'}; |
| 3826 | |
| 3827 | if (!flags) |
| 3828 | return; |
| 3829 | |
| 3830 | #define CHECK_APPEND_NOARG(a) \ |
| 3831 | do { \ |
| 3832 | ret = snprintf(bp, size_bp, (a)); \ |
| 3833 | if (ret < 0 || ret >= size_bp) \ |
| 3834 | goto out_overflow; \ |
| 3835 | size_bp -= ret; \ |
| 3836 | bp += ret; \ |
| 3837 | } while (0) |
| 3838 | |
| 3839 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3840 | do { \ |
| 3841 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3842 | if (ret < 0 || ret >= size_bp) \ |
| 3843 | goto out_overflow; \ |
| 3844 | size_bp -= ret; \ |
| 3845 | bp += ret; \ |
| 3846 | } while (0) |
| 3847 | |
| 3848 | #define CHECK_APPEND_2ARG(a, v1, v2) \ |
| 3849 | do { \ |
| 3850 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ |
| 3851 | if (ret < 0 || ret >= size_bp) \ |
| 3852 | goto out_overflow; \ |
| 3853 | size_bp -= ret; \ |
| 3854 | bp += ret; \ |
| 3855 | } while (0) |
| 3856 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3857 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3858 | CHECK_APPEND_1ARG("convert=%s,", |
| 3859 | btrfs_bg_type_to_raid_name(bargs->target)); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3860 | |
| 3861 | if (flags & BTRFS_BALANCE_ARGS_SOFT) |
| 3862 | CHECK_APPEND_NOARG("soft,"); |
| 3863 | |
| 3864 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { |
| 3865 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, |
| 3866 | sizeof(tmp_buf)); |
| 3867 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); |
| 3868 | } |
| 3869 | |
| 3870 | if (flags & BTRFS_BALANCE_ARGS_USAGE) |
| 3871 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); |
| 3872 | |
| 3873 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) |
| 3874 | CHECK_APPEND_2ARG("usage=%u..%u,", |
| 3875 | bargs->usage_min, bargs->usage_max); |
| 3876 | |
| 3877 | if (flags & BTRFS_BALANCE_ARGS_DEVID) |
| 3878 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); |
| 3879 | |
| 3880 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) |
| 3881 | CHECK_APPEND_2ARG("drange=%llu..%llu,", |
| 3882 | bargs->pstart, bargs->pend); |
| 3883 | |
| 3884 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) |
| 3885 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", |
| 3886 | bargs->vstart, bargs->vend); |
| 3887 | |
| 3888 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) |
| 3889 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); |
| 3890 | |
| 3891 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) |
| 3892 | CHECK_APPEND_2ARG("limit=%u..%u,", |
| 3893 | bargs->limit_min, bargs->limit_max); |
| 3894 | |
| 3895 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) |
| 3896 | CHECK_APPEND_2ARG("stripes=%u..%u,", |
| 3897 | bargs->stripes_min, bargs->stripes_max); |
| 3898 | |
| 3899 | #undef CHECK_APPEND_2ARG |
| 3900 | #undef CHECK_APPEND_1ARG |
| 3901 | #undef CHECK_APPEND_NOARG |
| 3902 | |
| 3903 | out_overflow: |
| 3904 | |
| 3905 | if (size_bp < size_buf) |
| 3906 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ |
| 3907 | else |
| 3908 | buf[0] = '\0'; |
| 3909 | } |
| 3910 | |
| 3911 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) |
| 3912 | { |
| 3913 | u32 size_buf = 1024; |
| 3914 | char tmp_buf[192] = {'\0'}; |
| 3915 | char *buf; |
| 3916 | char *bp; |
| 3917 | u32 size_bp = size_buf; |
| 3918 | int ret; |
| 3919 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3920 | |
| 3921 | buf = kzalloc(size_buf, GFP_KERNEL); |
| 3922 | if (!buf) |
| 3923 | return; |
| 3924 | |
| 3925 | bp = buf; |
| 3926 | |
| 3927 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3928 | do { \ |
| 3929 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3930 | if (ret < 0 || ret >= size_bp) \ |
| 3931 | goto out_overflow; \ |
| 3932 | size_bp -= ret; \ |
| 3933 | bp += ret; \ |
| 3934 | } while (0) |
| 3935 | |
| 3936 | if (bctl->flags & BTRFS_BALANCE_FORCE) |
| 3937 | CHECK_APPEND_1ARG("%s", "-f "); |
| 3938 | |
| 3939 | if (bctl->flags & BTRFS_BALANCE_DATA) { |
| 3940 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); |
| 3941 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); |
| 3942 | } |
| 3943 | |
| 3944 | if (bctl->flags & BTRFS_BALANCE_METADATA) { |
| 3945 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); |
| 3946 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); |
| 3947 | } |
| 3948 | |
| 3949 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { |
| 3950 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); |
| 3951 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); |
| 3952 | } |
| 3953 | |
| 3954 | #undef CHECK_APPEND_1ARG |
| 3955 | |
| 3956 | out_overflow: |
| 3957 | |
| 3958 | if (size_bp < size_buf) |
| 3959 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ |
| 3960 | btrfs_info(fs_info, "balance: %s %s", |
| 3961 | (bctl->flags & BTRFS_BALANCE_RESUME) ? |
| 3962 | "resume" : "start", buf); |
| 3963 | |
| 3964 | kfree(buf); |
| 3965 | } |
| 3966 | |
| 3967 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3968 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3969 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3970 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 3971 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3972 | struct btrfs_ioctl_balance_args *bargs) |
| 3973 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3974 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3975 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3976 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3977 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3978 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3979 | unsigned seq; |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3980 | bool reducing_redundancy; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 3981 | int i; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3982 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3983 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3984 | atomic_read(&fs_info->balance_pause_req) || |
| 3985 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3986 | ret = -EINVAL; |
| 3987 | goto out; |
| 3988 | } |
| 3989 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3990 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3991 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3992 | mixed = 1; |
| 3993 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3994 | /* |
| 3995 | * In case of mixed groups both data and meta should be picked, |
| 3996 | * and identical options should be given for both of them. |
| 3997 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3998 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3999 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4000 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 4001 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 4002 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4003 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4004 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4005 | ret = -EINVAL; |
| 4006 | goto out; |
| 4007 | } |
| 4008 | } |
| 4009 | |
Josef Bacik | b35cf1f | 2020-01-10 11:11:24 -0500 | [diff] [blame] | 4010 | /* |
| 4011 | * rw_devices will not change at the moment, device add/delete/replace |
| 4012 | * are excluded by EXCL_OP |
| 4013 | */ |
| 4014 | num_devices = fs_info->fs_devices->rw_devices; |
Qu Wenruo | fab2735 | 2019-09-25 10:13:27 +0800 | [diff] [blame] | 4015 | |
| 4016 | /* |
| 4017 | * SINGLE profile on-disk has no profile bit, but in-memory we have a |
| 4018 | * special bit for it, to make it easier to distinguish. Thus we need |
| 4019 | * to set it manually, or balance would refuse the profile. |
| 4020 | */ |
| 4021 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4022 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
| 4023 | if (num_devices >= btrfs_raid_array[i].devs_min) |
| 4024 | allowed |= btrfs_raid_array[i].bg_flag; |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4025 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4026 | if (validate_convert_profile(&bctl->data, allowed)) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4027 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4028 | "balance: invalid convert data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4029 | btrfs_bg_type_to_raid_name(bctl->data.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4030 | ret = -EINVAL; |
| 4031 | goto out; |
| 4032 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4033 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4034 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4035 | "balance: invalid convert metadata profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4036 | btrfs_bg_type_to_raid_name(bctl->meta.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4037 | ret = -EINVAL; |
| 4038 | goto out; |
| 4039 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 4040 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4041 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4042 | "balance: invalid convert system profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4043 | btrfs_bg_type_to_raid_name(bctl->sys.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4044 | ret = -EINVAL; |
| 4045 | goto out; |
| 4046 | } |
| 4047 | |
David Sterba | 6079e12 | 2019-05-17 11:43:29 +0200 | [diff] [blame] | 4048 | /* |
| 4049 | * Allow to reduce metadata or system integrity only if force set for |
| 4050 | * profiles with redundancy (copies, parity) |
| 4051 | */ |
| 4052 | allowed = 0; |
| 4053 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { |
| 4054 | if (btrfs_raid_array[i].ncopies >= 2 || |
| 4055 | btrfs_raid_array[i].tolerated_failures >= 1) |
| 4056 | allowed |= btrfs_raid_array[i].bg_flag; |
| 4057 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4058 | do { |
| 4059 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 4060 | |
| 4061 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4062 | (fs_info->avail_system_alloc_bits & allowed) && |
| 4063 | !(bctl->sys.target & allowed)) || |
| 4064 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4065 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4066 | !(bctl->meta.target & allowed))) |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4067 | reducing_redundancy = true; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4068 | else |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4069 | reducing_redundancy = false; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4070 | |
| 4071 | /* if we're not converting, the target field is uninitialized */ |
| 4072 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4073 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 4074 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4075 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4076 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4077 | |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4078 | if (reducing_redundancy) { |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4079 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 4080 | btrfs_info(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4081 | "balance: force reducing metadata redundancy"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4082 | } else { |
| 4083 | btrfs_err(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4084 | "balance: reduces metadata redundancy, use --force if you want this"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4085 | ret = -EINVAL; |
| 4086 | goto out; |
| 4087 | } |
| 4088 | } |
| 4089 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4090 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 4091 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4092 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4093 | "balance: metadata profile %s has lower redundancy than data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4094 | btrfs_bg_type_to_raid_name(meta_target), |
| 4095 | btrfs_bg_type_to_raid_name(data_target)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 4098 | if (fs_info->send_in_progress) { |
| 4099 | btrfs_warn_rl(fs_info, |
| 4100 | "cannot run balance while send operations are in progress (%d in progress)", |
| 4101 | fs_info->send_in_progress); |
| 4102 | ret = -EAGAIN; |
| 4103 | goto out; |
| 4104 | } |
| 4105 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4106 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4107 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4108 | goto out; |
| 4109 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4110 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 4111 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4112 | BUG_ON(fs_info->balance_ctl); |
| 4113 | spin_lock(&fs_info->balance_lock); |
| 4114 | fs_info->balance_ctl = bctl; |
| 4115 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4116 | } else { |
| 4117 | BUG_ON(ret != -EEXIST); |
| 4118 | spin_lock(&fs_info->balance_lock); |
| 4119 | update_balance_args(bctl); |
| 4120 | spin_unlock(&fs_info->balance_lock); |
| 4121 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4122 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4123 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 4124 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4125 | describe_balance_start_or_resume(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4126 | mutex_unlock(&fs_info->balance_mutex); |
| 4127 | |
| 4128 | ret = __btrfs_balance(fs_info); |
| 4129 | |
| 4130 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4131 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) |
| 4132 | btrfs_info(fs_info, "balance: paused"); |
| 4133 | else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req)) |
| 4134 | btrfs_info(fs_info, "balance: canceled"); |
| 4135 | else |
| 4136 | btrfs_info(fs_info, "balance: ended with status: %d", ret); |
| 4137 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4138 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4139 | |
| 4140 | if (bargs) { |
| 4141 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4142 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4143 | } |
| 4144 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4145 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4146 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4147 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4148 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4149 | } |
| 4150 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4151 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4152 | |
| 4153 | return ret; |
| 4154 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4155 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4156 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4157 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4158 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4159 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4160 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4161 | return ret; |
| 4162 | } |
| 4163 | |
| 4164 | static int balance_kthread(void *data) |
| 4165 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4166 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4167 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4168 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4169 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4170 | if (fs_info->balance_ctl) |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4171 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4172 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4173 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4174 | return ret; |
| 4175 | } |
| 4176 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4177 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4178 | { |
| 4179 | struct task_struct *tsk; |
| 4180 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4181 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4182 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4183 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4184 | return 0; |
| 4185 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4186 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4187 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4188 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4189 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4190 | return 0; |
| 4191 | } |
| 4192 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4193 | /* |
| 4194 | * A ro->rw remount sequence should continue with the paused balance |
| 4195 | * regardless of who pauses it, system or the user as of now, so set |
| 4196 | * the resume flag. |
| 4197 | */ |
| 4198 | spin_lock(&fs_info->balance_lock); |
| 4199 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4200 | spin_unlock(&fs_info->balance_lock); |
| 4201 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4202 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4203 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4204 | } |
| 4205 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4206 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4207 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4208 | struct btrfs_balance_control *bctl; |
| 4209 | struct btrfs_balance_item *item; |
| 4210 | struct btrfs_disk_balance_args disk_bargs; |
| 4211 | struct btrfs_path *path; |
| 4212 | struct extent_buffer *leaf; |
| 4213 | struct btrfs_key key; |
| 4214 | int ret; |
| 4215 | |
| 4216 | path = btrfs_alloc_path(); |
| 4217 | if (!path) |
| 4218 | return -ENOMEM; |
| 4219 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4220 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4221 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4222 | key.offset = 0; |
| 4223 | |
| 4224 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4225 | if (ret < 0) |
| 4226 | goto out; |
| 4227 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4228 | ret = 0; |
| 4229 | goto out; |
| 4230 | } |
| 4231 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4232 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4233 | if (!bctl) { |
| 4234 | ret = -ENOMEM; |
| 4235 | goto out; |
| 4236 | } |
| 4237 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4238 | leaf = path->nodes[0]; |
| 4239 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4240 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4241 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4242 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4243 | |
| 4244 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4245 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4246 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4247 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4248 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4249 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4250 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4251 | /* |
| 4252 | * This should never happen, as the paused balance state is recovered |
| 4253 | * during mount without any chance of other exclusive ops to collide. |
| 4254 | * |
| 4255 | * This gives the exclusive op status to balance and keeps in paused |
| 4256 | * state until user intervention (cancel or umount). If the ownership |
| 4257 | * cannot be assigned, show a message but do not fail. The balance |
| 4258 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4259 | * set up. |
| 4260 | */ |
| 4261 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4262 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4263 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4264 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4265 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4266 | BUG_ON(fs_info->balance_ctl); |
| 4267 | spin_lock(&fs_info->balance_lock); |
| 4268 | fs_info->balance_ctl = bctl; |
| 4269 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4270 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4271 | out: |
| 4272 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4273 | return ret; |
| 4274 | } |
| 4275 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4276 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4277 | { |
| 4278 | int ret = 0; |
| 4279 | |
| 4280 | mutex_lock(&fs_info->balance_mutex); |
| 4281 | if (!fs_info->balance_ctl) { |
| 4282 | mutex_unlock(&fs_info->balance_mutex); |
| 4283 | return -ENOTCONN; |
| 4284 | } |
| 4285 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4286 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4287 | atomic_inc(&fs_info->balance_pause_req); |
| 4288 | mutex_unlock(&fs_info->balance_mutex); |
| 4289 | |
| 4290 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4291 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4292 | |
| 4293 | mutex_lock(&fs_info->balance_mutex); |
| 4294 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4295 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4296 | atomic_dec(&fs_info->balance_pause_req); |
| 4297 | } else { |
| 4298 | ret = -ENOTCONN; |
| 4299 | } |
| 4300 | |
| 4301 | mutex_unlock(&fs_info->balance_mutex); |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4305 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4306 | { |
| 4307 | mutex_lock(&fs_info->balance_mutex); |
| 4308 | if (!fs_info->balance_ctl) { |
| 4309 | mutex_unlock(&fs_info->balance_mutex); |
| 4310 | return -ENOTCONN; |
| 4311 | } |
| 4312 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4313 | /* |
| 4314 | * A paused balance with the item stored on disk can be resumed at |
| 4315 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4316 | * and we must not allow cancelling as it deletes the item. |
| 4317 | */ |
| 4318 | if (sb_rdonly(fs_info->sb)) { |
| 4319 | mutex_unlock(&fs_info->balance_mutex); |
| 4320 | return -EROFS; |
| 4321 | } |
| 4322 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4323 | atomic_inc(&fs_info->balance_cancel_req); |
| 4324 | /* |
| 4325 | * if we are running just wait and return, balance item is |
| 4326 | * deleted in btrfs_balance in this case |
| 4327 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4328 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4329 | mutex_unlock(&fs_info->balance_mutex); |
| 4330 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4331 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4332 | mutex_lock(&fs_info->balance_mutex); |
| 4333 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4334 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4335 | /* |
| 4336 | * Lock released to allow other waiters to continue, we'll |
| 4337 | * reexamine the status again. |
| 4338 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4339 | mutex_lock(&fs_info->balance_mutex); |
| 4340 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4341 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4342 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4343 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4344 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4345 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4346 | } |
| 4347 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4348 | BUG_ON(fs_info->balance_ctl || |
| 4349 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4350 | atomic_dec(&fs_info->balance_cancel_req); |
| 4351 | mutex_unlock(&fs_info->balance_mutex); |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
Nikolay Borisov | 97f4dd0 | 2020-02-18 16:56:08 +0200 | [diff] [blame] | 4355 | int btrfs_uuid_scan_kthread(void *data) |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4356 | { |
| 4357 | struct btrfs_fs_info *fs_info = data; |
| 4358 | struct btrfs_root *root = fs_info->tree_root; |
| 4359 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4360 | struct btrfs_path *path = NULL; |
| 4361 | int ret = 0; |
| 4362 | struct extent_buffer *eb; |
| 4363 | int slot; |
| 4364 | struct btrfs_root_item root_item; |
| 4365 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4366 | struct btrfs_trans_handle *trans = NULL; |
Josef Bacik | c94bec2 | 2020-02-14 15:05:01 -0500 | [diff] [blame] | 4367 | bool closing = false; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4368 | |
| 4369 | path = btrfs_alloc_path(); |
| 4370 | if (!path) { |
| 4371 | ret = -ENOMEM; |
| 4372 | goto out; |
| 4373 | } |
| 4374 | |
| 4375 | key.objectid = 0; |
| 4376 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4377 | key.offset = 0; |
| 4378 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4379 | while (1) { |
Josef Bacik | c94bec2 | 2020-02-14 15:05:01 -0500 | [diff] [blame] | 4380 | if (btrfs_fs_closing(fs_info)) { |
| 4381 | closing = true; |
| 4382 | break; |
| 4383 | } |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4384 | ret = btrfs_search_forward(root, &key, path, |
| 4385 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4386 | if (ret) { |
| 4387 | if (ret > 0) |
| 4388 | ret = 0; |
| 4389 | break; |
| 4390 | } |
| 4391 | |
| 4392 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4393 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4394 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4395 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4396 | goto skip; |
| 4397 | |
| 4398 | eb = path->nodes[0]; |
| 4399 | slot = path->slots[0]; |
| 4400 | item_size = btrfs_item_size_nr(eb, slot); |
| 4401 | if (item_size < sizeof(root_item)) |
| 4402 | goto skip; |
| 4403 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4404 | read_extent_buffer(eb, &root_item, |
| 4405 | btrfs_item_ptr_offset(eb, slot), |
| 4406 | (int)sizeof(root_item)); |
| 4407 | if (btrfs_root_refs(&root_item) == 0) |
| 4408 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4409 | |
| 4410 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4411 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4412 | if (trans) |
| 4413 | goto update_tree; |
| 4414 | |
| 4415 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4416 | /* |
| 4417 | * 1 - subvol uuid item |
| 4418 | * 1 - received_subvol uuid item |
| 4419 | */ |
| 4420 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4421 | if (IS_ERR(trans)) { |
| 4422 | ret = PTR_ERR(trans); |
| 4423 | break; |
| 4424 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4425 | continue; |
| 4426 | } else { |
| 4427 | goto skip; |
| 4428 | } |
| 4429 | update_tree: |
| 4430 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4431 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4432 | BTRFS_UUID_KEY_SUBVOL, |
| 4433 | key.objectid); |
| 4434 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4435 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4436 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4437 | break; |
| 4438 | } |
| 4439 | } |
| 4440 | |
| 4441 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4442 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4443 | root_item.received_uuid, |
| 4444 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4445 | key.objectid); |
| 4446 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4447 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4448 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4449 | break; |
| 4450 | } |
| 4451 | } |
| 4452 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4453 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4454 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4455 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4456 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4457 | if (ret) |
| 4458 | break; |
| 4459 | } |
| 4460 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4461 | btrfs_release_path(path); |
| 4462 | if (key.offset < (u64)-1) { |
| 4463 | key.offset++; |
| 4464 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4465 | key.offset = 0; |
| 4466 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4467 | } else if (key.objectid < (u64)-1) { |
| 4468 | key.offset = 0; |
| 4469 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4470 | key.objectid++; |
| 4471 | } else { |
| 4472 | break; |
| 4473 | } |
| 4474 | cond_resched(); |
| 4475 | } |
| 4476 | |
| 4477 | out: |
| 4478 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4479 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4480 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4481 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4482 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Josef Bacik | c94bec2 | 2020-02-14 15:05:01 -0500 | [diff] [blame] | 4483 | else if (!closing) |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4484 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4485 | up(&fs_info->uuid_tree_rescan_sem); |
| 4486 | return 0; |
| 4487 | } |
| 4488 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4489 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4490 | { |
| 4491 | struct btrfs_trans_handle *trans; |
| 4492 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4493 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4494 | struct task_struct *task; |
| 4495 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4496 | |
| 4497 | /* |
| 4498 | * 1 - root node |
| 4499 | * 1 - root item |
| 4500 | */ |
| 4501 | trans = btrfs_start_transaction(tree_root, 2); |
| 4502 | if (IS_ERR(trans)) |
| 4503 | return PTR_ERR(trans); |
| 4504 | |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 4505 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4506 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4507 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4508 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4509 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4510 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | fs_info->uuid_root = uuid_root; |
| 4514 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4515 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4516 | if (ret) |
| 4517 | return ret; |
| 4518 | |
| 4519 | down(&fs_info->uuid_tree_rescan_sem); |
| 4520 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4521 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4522 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4523 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4524 | up(&fs_info->uuid_tree_rescan_sem); |
| 4525 | return PTR_ERR(task); |
| 4526 | } |
| 4527 | |
| 4528 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4529 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4530 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4531 | /* |
| 4532 | * shrinking a device means finding all of the device extents past |
| 4533 | * the new size, and then following the back refs to the chunks. |
| 4534 | * The chunk relocation code actually frees the device extent |
| 4535 | */ |
| 4536 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4537 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4538 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4539 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4540 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4541 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4542 | struct btrfs_path *path; |
| 4543 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4544 | u64 chunk_offset; |
| 4545 | int ret; |
| 4546 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4547 | int failed = 0; |
| 4548 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4549 | struct extent_buffer *l; |
| 4550 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4551 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4552 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4553 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4554 | u64 diff; |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4555 | u64 start; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4556 | |
| 4557 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4558 | start = new_size; |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4559 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4560 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4561 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4562 | return -EINVAL; |
| 4563 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4564 | path = btrfs_alloc_path(); |
| 4565 | if (!path) |
| 4566 | return -ENOMEM; |
| 4567 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4568 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4569 | |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4570 | trans = btrfs_start_transaction(root, 0); |
| 4571 | if (IS_ERR(trans)) { |
| 4572 | btrfs_free_path(path); |
| 4573 | return PTR_ERR(trans); |
| 4574 | } |
| 4575 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4576 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4577 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4578 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4579 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4580 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4581 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4582 | } |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4583 | |
| 4584 | /* |
| 4585 | * Once the device's size has been set to the new size, ensure all |
| 4586 | * in-memory chunks are synced to disk so that the loop below sees them |
| 4587 | * and relocates them accordingly. |
| 4588 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 4589 | if (contains_pending_extent(device, &start, diff)) { |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4590 | mutex_unlock(&fs_info->chunk_mutex); |
| 4591 | ret = btrfs_commit_transaction(trans); |
| 4592 | if (ret) |
| 4593 | goto done; |
| 4594 | } else { |
| 4595 | mutex_unlock(&fs_info->chunk_mutex); |
| 4596 | btrfs_end_transaction(trans); |
| 4597 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4598 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4599 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4600 | key.objectid = device->devid; |
| 4601 | key.offset = (u64)-1; |
| 4602 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4603 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4604 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4605 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4606 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4607 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4608 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4609 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4610 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4611 | |
| 4612 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4613 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4614 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4615 | if (ret < 0) |
| 4616 | goto done; |
| 4617 | if (ret) { |
| 4618 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4619 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4620 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | l = path->nodes[0]; |
| 4624 | slot = path->slots[0]; |
| 4625 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4626 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4627 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4628 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4629 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4630 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4631 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4632 | |
| 4633 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4634 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4635 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4636 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4637 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4638 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4639 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4640 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4641 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4642 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4643 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4644 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4645 | /* |
| 4646 | * We may be relocating the only data chunk we have, |
| 4647 | * which could potentially end up with losing data's |
| 4648 | * raid profile, so lets allocate an empty one in |
| 4649 | * advance. |
| 4650 | */ |
| 4651 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4652 | if (ret < 0) { |
| 4653 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4654 | goto done; |
| 4655 | } |
| 4656 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4657 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4658 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4659 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4660 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4661 | } else if (ret) { |
| 4662 | if (ret == -ETXTBSY) { |
| 4663 | btrfs_warn(fs_info, |
| 4664 | "could not shrink block group %llu due to active swapfile", |
| 4665 | chunk_offset); |
| 4666 | } |
| 4667 | goto done; |
| 4668 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4669 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4670 | |
| 4671 | if (failed && !retried) { |
| 4672 | failed = 0; |
| 4673 | retried = true; |
| 4674 | goto again; |
| 4675 | } else if (failed && retried) { |
| 4676 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4677 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4678 | } |
| 4679 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4680 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4681 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4682 | if (IS_ERR(trans)) { |
| 4683 | ret = PTR_ERR(trans); |
| 4684 | goto done; |
| 4685 | } |
| 4686 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4687 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4688 | btrfs_device_set_disk_total_bytes(device, new_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4689 | if (list_empty(&device->post_commit_list)) |
| 4690 | list_add_tail(&device->post_commit_list, |
| 4691 | &trans->transaction->dev_update_list); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4692 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4693 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4694 | btrfs_set_super_total_bytes(super_copy, |
| 4695 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4696 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4697 | |
| 4698 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4699 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4700 | if (ret < 0) { |
| 4701 | btrfs_abort_transaction(trans, ret); |
| 4702 | btrfs_end_transaction(trans); |
| 4703 | } else { |
| 4704 | ret = btrfs_commit_transaction(trans); |
| 4705 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4706 | done: |
| 4707 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4708 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4709 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4710 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4711 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4712 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4713 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4714 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4715 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4716 | return ret; |
| 4717 | } |
| 4718 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4719 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4720 | struct btrfs_key *key, |
| 4721 | struct btrfs_chunk *chunk, int item_size) |
| 4722 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4723 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4724 | struct btrfs_disk_key disk_key; |
| 4725 | u32 array_size; |
| 4726 | u8 *ptr; |
| 4727 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4728 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4729 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4730 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4731 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4732 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4733 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4734 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4735 | |
| 4736 | ptr = super_copy->sys_chunk_array + array_size; |
| 4737 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4738 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4739 | ptr += sizeof(disk_key); |
| 4740 | memcpy(ptr, chunk, item_size); |
| 4741 | item_size += sizeof(disk_key); |
| 4742 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4743 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4744 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4745 | return 0; |
| 4746 | } |
| 4747 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4748 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4749 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4750 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4751 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4752 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4753 | const struct btrfs_device_info *di_a = a; |
| 4754 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4755 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4756 | if (di_a->max_avail > di_b->max_avail) |
| 4757 | return -1; |
| 4758 | if (di_a->max_avail < di_b->max_avail) |
| 4759 | return 1; |
| 4760 | if (di_a->total_avail > di_b->total_avail) |
| 4761 | return -1; |
| 4762 | if (di_a->total_avail < di_b->total_avail) |
| 4763 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4764 | return 0; |
| 4765 | } |
| 4766 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4767 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4768 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4769 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4770 | return; |
| 4771 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4772 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4773 | } |
| 4774 | |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 4775 | static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4776 | { |
| 4777 | if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4))) |
| 4778 | return; |
| 4779 | |
| 4780 | btrfs_set_fs_incompat(info, RAID1C34); |
| 4781 | } |
| 4782 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 4783 | /* |
| 4784 | * Structure used internally for __btrfs_alloc_chunk() function. |
| 4785 | * Wraps needed parameters. |
| 4786 | */ |
| 4787 | struct alloc_chunk_ctl { |
| 4788 | u64 start; |
| 4789 | u64 type; |
| 4790 | /* Total number of stripes to allocate */ |
| 4791 | int num_stripes; |
| 4792 | /* sub_stripes info for map */ |
| 4793 | int sub_stripes; |
| 4794 | /* Stripes per device */ |
| 4795 | int dev_stripes; |
| 4796 | /* Maximum number of devices to use */ |
| 4797 | int devs_max; |
| 4798 | /* Minimum number of devices to use */ |
| 4799 | int devs_min; |
| 4800 | /* ndevs has to be a multiple of this */ |
| 4801 | int devs_increment; |
| 4802 | /* Number of copies */ |
| 4803 | int ncopies; |
| 4804 | /* Number of stripes worth of bytes to store parity information */ |
| 4805 | int nparity; |
| 4806 | u64 max_stripe_size; |
| 4807 | u64 max_chunk_size; |
| 4808 | u64 stripe_size; |
| 4809 | u64 chunk_size; |
| 4810 | int ndevs; |
| 4811 | }; |
| 4812 | |
Naohiro Aota | 27c314d | 2020-02-25 12:56:11 +0900 | [diff] [blame] | 4813 | static void init_alloc_chunk_ctl_policy_regular( |
| 4814 | struct btrfs_fs_devices *fs_devices, |
| 4815 | struct alloc_chunk_ctl *ctl) |
| 4816 | { |
| 4817 | u64 type = ctl->type; |
| 4818 | |
| 4819 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4820 | ctl->max_stripe_size = SZ_1G; |
| 4821 | ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
| 4822 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
| 4823 | /* For larger filesystems, use larger metadata chunks */ |
| 4824 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4825 | ctl->max_stripe_size = SZ_1G; |
| 4826 | else |
| 4827 | ctl->max_stripe_size = SZ_256M; |
| 4828 | ctl->max_chunk_size = ctl->max_stripe_size; |
| 4829 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4830 | ctl->max_stripe_size = SZ_32M; |
| 4831 | ctl->max_chunk_size = 2 * ctl->max_stripe_size; |
| 4832 | ctl->devs_max = min_t(int, ctl->devs_max, |
| 4833 | BTRFS_MAX_DEVS_SYS_CHUNK); |
| 4834 | } else { |
| 4835 | BUG(); |
| 4836 | } |
| 4837 | |
| 4838 | /* We don't want a chunk larger than 10% of writable space */ |
| 4839 | ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4840 | ctl->max_chunk_size); |
| 4841 | } |
| 4842 | |
| 4843 | static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices, |
| 4844 | struct alloc_chunk_ctl *ctl) |
| 4845 | { |
| 4846 | int index = btrfs_bg_flags_to_raid_index(ctl->type); |
| 4847 | |
| 4848 | ctl->sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4849 | ctl->dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4850 | ctl->devs_max = btrfs_raid_array[index].devs_max; |
| 4851 | if (!ctl->devs_max) |
| 4852 | ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info); |
| 4853 | ctl->devs_min = btrfs_raid_array[index].devs_min; |
| 4854 | ctl->devs_increment = btrfs_raid_array[index].devs_increment; |
| 4855 | ctl->ncopies = btrfs_raid_array[index].ncopies; |
| 4856 | ctl->nparity = btrfs_raid_array[index].nparity; |
| 4857 | ctl->ndevs = 0; |
| 4858 | |
| 4859 | switch (fs_devices->chunk_alloc_policy) { |
| 4860 | case BTRFS_CHUNK_ALLOC_REGULAR: |
| 4861 | init_alloc_chunk_ctl_policy_regular(fs_devices, ctl); |
| 4862 | break; |
| 4863 | default: |
| 4864 | BUG(); |
| 4865 | } |
| 4866 | } |
| 4867 | |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame^] | 4868 | static int gather_device_info(struct btrfs_fs_devices *fs_devices, |
| 4869 | struct alloc_chunk_ctl *ctl, |
| 4870 | struct btrfs_device_info *devices_info) |
| 4871 | { |
| 4872 | struct btrfs_fs_info *info = fs_devices->fs_info; |
| 4873 | struct btrfs_device *device; |
| 4874 | u64 total_avail; |
| 4875 | u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes; |
| 4876 | u64 dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes; |
| 4877 | int ret; |
| 4878 | int ndevs = 0; |
| 4879 | u64 max_avail; |
| 4880 | u64 dev_offset; |
| 4881 | |
| 4882 | /* |
| 4883 | * in the first pass through the devices list, we gather information |
| 4884 | * about the available holes on each device. |
| 4885 | */ |
| 4886 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 4887 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| 4888 | WARN(1, KERN_ERR |
| 4889 | "BTRFS: read-only device in alloc_list\n"); |
| 4890 | continue; |
| 4891 | } |
| 4892 | |
| 4893 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 4894 | &device->dev_state) || |
| 4895 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
| 4896 | continue; |
| 4897 | |
| 4898 | if (device->total_bytes > device->bytes_used) |
| 4899 | total_avail = device->total_bytes - device->bytes_used; |
| 4900 | else |
| 4901 | total_avail = 0; |
| 4902 | |
| 4903 | /* If there is no space on this device, skip it. */ |
| 4904 | if (total_avail == 0) |
| 4905 | continue; |
| 4906 | |
| 4907 | ret = find_free_dev_extent(device, dev_extent_want, &dev_offset, |
| 4908 | &max_avail); |
| 4909 | if (ret && ret != -ENOSPC) |
| 4910 | return ret; |
| 4911 | |
| 4912 | if (ret == 0) |
| 4913 | max_avail = dev_extent_want; |
| 4914 | |
| 4915 | if (max_avail < dev_extent_min) { |
| 4916 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4917 | btrfs_debug(info, |
| 4918 | "%s: devid %llu has no free space, have=%llu want=%llu", |
| 4919 | __func__, device->devid, max_avail, |
| 4920 | dev_extent_min); |
| 4921 | continue; |
| 4922 | } |
| 4923 | |
| 4924 | if (ndevs == fs_devices->rw_devices) { |
| 4925 | WARN(1, "%s: found more than %llu devices\n", |
| 4926 | __func__, fs_devices->rw_devices); |
| 4927 | break; |
| 4928 | } |
| 4929 | devices_info[ndevs].dev_offset = dev_offset; |
| 4930 | devices_info[ndevs].max_avail = max_avail; |
| 4931 | devices_info[ndevs].total_avail = total_avail; |
| 4932 | devices_info[ndevs].dev = device; |
| 4933 | ++ndevs; |
| 4934 | } |
| 4935 | ctl->ndevs = ndevs; |
| 4936 | |
| 4937 | /* |
| 4938 | * now sort the devices by hole size / available space |
| 4939 | */ |
| 4940 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4941 | btrfs_cmp_device_info, NULL); |
| 4942 | |
| 4943 | return 0; |
| 4944 | } |
| 4945 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4946 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4947 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4948 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4949 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4950 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4951 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4952 | struct extent_map_tree *em_tree; |
| 4953 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4954 | struct btrfs_device_info *devices_info = NULL; |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 4955 | struct alloc_chunk_ctl ctl; |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame^] | 4956 | /* Number of stripes that count for block group size */ |
| 4957 | int data_stripes; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4958 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4959 | int i; |
| 4960 | int j; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4961 | |
Naohiro Aota | b25c19f | 2020-02-25 12:56:07 +0900 | [diff] [blame] | 4962 | if (!alloc_profile_is_valid(type, 0)) { |
| 4963 | ASSERT(0); |
| 4964 | return -EINVAL; |
| 4965 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4966 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4967 | if (list_empty(&fs_devices->alloc_list)) { |
| 4968 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4969 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4970 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4971 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4972 | |
Naohiro Aota | 27c314d | 2020-02-25 12:56:11 +0900 | [diff] [blame] | 4973 | if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { |
| 4974 | btrfs_err(info, "invalid chunk type 0x%llx requested", type); |
| 4975 | ASSERT(0); |
| 4976 | return -EINVAL; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4977 | } |
| 4978 | |
Naohiro Aota | 27c314d | 2020-02-25 12:56:11 +0900 | [diff] [blame] | 4979 | ctl.start = start; |
| 4980 | ctl.type = type; |
| 4981 | init_alloc_chunk_ctl(fs_devices, &ctl); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4982 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4983 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4984 | GFP_NOFS); |
| 4985 | if (!devices_info) |
| 4986 | return -ENOMEM; |
| 4987 | |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame^] | 4988 | ret = gather_device_info(fs_devices, &ctl, devices_info); |
| 4989 | if (ret < 0) |
| 4990 | goto error; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4991 | |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 4992 | /* |
| 4993 | * Round down to number of usable stripes, devs_increment can be any |
| 4994 | * number so we can't use round_down() |
| 4995 | */ |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 4996 | ctl.ndevs -= ctl.ndevs % ctl.devs_increment; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4997 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 4998 | if (ctl.ndevs < ctl.devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4999 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5000 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 5001 | btrfs_debug(info, |
| 5002 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5003 | __func__, ctl.ndevs, ctl.devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5004 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5005 | goto error; |
| 5006 | } |
| 5007 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5008 | ctl.ndevs = min(ctl.ndevs, ctl.devs_max); |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 5009 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5010 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 5011 | * The primary goal is to maximize the number of stripes, so use as |
| 5012 | * many devices as possible, even if the stripes are not maximum sized. |
| 5013 | * |
| 5014 | * The DUP profile stores more than one stripe per device, the |
| 5015 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5016 | */ |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5017 | ctl.stripe_size = div_u64(devices_info[ctl.ndevs - 1].max_avail, |
| 5018 | ctl.dev_stripes); |
| 5019 | ctl.num_stripes = ctl.ndevs * ctl.dev_stripes; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5020 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5021 | /* |
| 5022 | * this will have to be fixed for RAID1 and RAID10 over |
| 5023 | * more drives |
| 5024 | */ |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5025 | data_stripes = (ctl.num_stripes - ctl.nparity) / ctl.ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5026 | |
| 5027 | /* |
| 5028 | * Use the number of data stripes to figure out how big this chunk |
| 5029 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5030 | * and compare that answer with the max chunk size. If it's higher, |
| 5031 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5032 | */ |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5033 | if (ctl.stripe_size * data_stripes > ctl.max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5034 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5035 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 5036 | * then use it, unless it ends up being even bigger than the |
| 5037 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5038 | */ |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5039 | ctl.stripe_size = |
| 5040 | min(round_up(div_u64(ctl.max_chunk_size, data_stripes), |
| 5041 | SZ_16M), |
| 5042 | ctl.stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5043 | } |
| 5044 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 5045 | /* align to BTRFS_STRIPE_LEN */ |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5046 | ctl.stripe_size = round_down(ctl.stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5047 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5048 | map = kmalloc(map_lookup_size(ctl.num_stripes), GFP_NOFS); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5049 | if (!map) { |
| 5050 | ret = -ENOMEM; |
| 5051 | goto error; |
| 5052 | } |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 5053 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5054 | map->num_stripes = ctl.num_stripes; |
| 5055 | |
| 5056 | for (i = 0; i < ctl.ndevs; ++i) { |
| 5057 | for (j = 0; j < ctl.dev_stripes; ++j) { |
| 5058 | int s = i * ctl.dev_stripes + j; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5059 | map->stripes[s].dev = devices_info[i].dev; |
| 5060 | map->stripes[s].physical = devices_info[i].dev_offset + |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5061 | j * ctl.stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 5062 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5063 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5064 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 5065 | map->io_align = BTRFS_STRIPE_LEN; |
| 5066 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5067 | map->type = type; |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5068 | map->sub_stripes = ctl.sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5069 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5070 | ctl.chunk_size = ctl.stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5071 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5072 | trace_btrfs_chunk_alloc(info, map, start, ctl.chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5073 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5074 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5075 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5076 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5077 | ret = -ENOMEM; |
| 5078 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5079 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5080 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5081 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5082 | em->start = start; |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5083 | em->len = ctl.chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5084 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5085 | em->block_len = em->len; |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5086 | em->orig_block_len = ctl.stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5087 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5088 | em_tree = &info->mapping_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5089 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5090 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5091 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5092 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5093 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 5094 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5095 | } |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5096 | write_unlock(&em_tree->lock); |
| 5097 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5098 | ret = btrfs_make_block_group(trans, 0, type, start, ctl.chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5099 | if (ret) |
| 5100 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5101 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 5102 | for (i = 0; i < map->num_stripes; i++) { |
| 5103 | struct btrfs_device *dev = map->stripes[i].dev; |
| 5104 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5105 | btrfs_device_set_bytes_used(dev, |
| 5106 | dev->bytes_used + ctl.stripe_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 5107 | if (list_empty(&dev->post_commit_list)) |
| 5108 | list_add_tail(&dev->post_commit_list, |
| 5109 | &trans->transaction->dev_update_list); |
| 5110 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 5111 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5112 | atomic64_sub(ctl.stripe_size * map->num_stripes, |
| 5113 | &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 5114 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5115 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5116 | check_raid56_incompat_flag(info, type); |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 5117 | check_raid1c34_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5118 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5119 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5120 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5121 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5122 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5123 | write_lock(&em_tree->lock); |
| 5124 | remove_extent_mapping(em_tree, em); |
| 5125 | write_unlock(&em_tree->lock); |
| 5126 | |
| 5127 | /* One for our allocation */ |
| 5128 | free_extent_map(em); |
| 5129 | /* One for the tree reference */ |
| 5130 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5131 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5132 | kfree(devices_info); |
| 5133 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5134 | } |
| 5135 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5136 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5137 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5138 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5139 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5140 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5141 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5142 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5143 | struct btrfs_device *device; |
| 5144 | struct btrfs_chunk *chunk; |
| 5145 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5146 | struct extent_map *em; |
| 5147 | struct map_lookup *map; |
| 5148 | size_t item_size; |
| 5149 | u64 dev_offset; |
| 5150 | u64 stripe_size; |
| 5151 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5152 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5153 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5154 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5155 | if (IS_ERR(em)) |
| 5156 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5157 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5158 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5159 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5160 | stripe_size = em->orig_block_len; |
| 5161 | |
| 5162 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5163 | if (!chunk) { |
| 5164 | ret = -ENOMEM; |
| 5165 | goto out; |
| 5166 | } |
| 5167 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5168 | /* |
| 5169 | * Take the device list mutex to prevent races with the final phase of |
| 5170 | * a device replace operation that replaces the device object associated |
| 5171 | * with the map's stripes, because the device object's id can change |
| 5172 | * at any time during that final phase of the device replace operation |
| 5173 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5174 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5175 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5176 | for (i = 0; i < map->num_stripes; i++) { |
| 5177 | device = map->stripes[i].dev; |
| 5178 | dev_offset = map->stripes[i].physical; |
| 5179 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5180 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5181 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5182 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5183 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5184 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5185 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5186 | break; |
| 5187 | } |
| 5188 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5189 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5190 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5191 | } |
| 5192 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5193 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5194 | for (i = 0; i < map->num_stripes; i++) { |
| 5195 | device = map->stripes[i].dev; |
| 5196 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5197 | |
| 5198 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5199 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5200 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5201 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5202 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5203 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5204 | |
| 5205 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5206 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5207 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5208 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5209 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5210 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5211 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5212 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5213 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5214 | |
| 5215 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5216 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5217 | key.offset = chunk_offset; |
| 5218 | |
| 5219 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5220 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5221 | /* |
| 5222 | * TODO: Cleanup of inserted chunk root in case of |
| 5223 | * failure. |
| 5224 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5225 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5226 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5227 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5228 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5229 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5230 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5231 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5232 | } |
| 5233 | |
| 5234 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5235 | * Chunk allocation falls into two parts. The first part does work |
| 5236 | * that makes the new allocated chunk usable, but does not do any operation |
| 5237 | * that modifies the chunk tree. The second part does the work that |
| 5238 | * requires modifying the chunk tree. This division is important for the |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5239 | * bootstrap process of adding storage to a seed btrfs. |
| 5240 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5241 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5242 | { |
| 5243 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5244 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5245 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5246 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5247 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5248 | } |
| 5249 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5250 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5251 | { |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5252 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5253 | u64 chunk_offset; |
| 5254 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5255 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5256 | int ret; |
| 5257 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5258 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5259 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5260 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5261 | if (ret) |
| 5262 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5263 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5264 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5265 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5266 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5267 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5268 | } |
| 5269 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5270 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5271 | { |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5272 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5273 | |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5274 | return btrfs_raid_array[index].tolerated_failures; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5275 | } |
| 5276 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5277 | 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] | 5278 | { |
| 5279 | struct extent_map *em; |
| 5280 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5281 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5282 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5283 | int i; |
| 5284 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5285 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5286 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5287 | return 1; |
| 5288 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5289 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5290 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5291 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5292 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5293 | miss_ndevs++; |
| 5294 | continue; |
| 5295 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5296 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5297 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5298 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5299 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5300 | } |
| 5301 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5302 | |
| 5303 | /* |
| 5304 | * If the number of missing devices is larger than max errors, |
| 5305 | * we can not write the data into that chunk successfully, so |
| 5306 | * set it readonly. |
| 5307 | */ |
| 5308 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5309 | readonly = 1; |
| 5310 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5311 | free_extent_map(em); |
| 5312 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5313 | } |
| 5314 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5315 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5316 | { |
| 5317 | struct extent_map *em; |
| 5318 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5319 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5320 | write_lock(&tree->lock); |
| 5321 | em = lookup_extent_mapping(tree, 0, (u64)-1); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5322 | if (em) |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5323 | remove_extent_mapping(tree, em); |
| 5324 | write_unlock(&tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5325 | if (!em) |
| 5326 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5327 | /* once for us */ |
| 5328 | free_extent_map(em); |
| 5329 | /* once for the tree */ |
| 5330 | free_extent_map(em); |
| 5331 | } |
| 5332 | } |
| 5333 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5334 | 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] | 5335 | { |
| 5336 | struct extent_map *em; |
| 5337 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5338 | int ret; |
| 5339 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5340 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5341 | if (IS_ERR(em)) |
| 5342 | /* |
| 5343 | * We could return errors for these cases, but that could get |
| 5344 | * ugly and we'd probably do the same thing which is just not do |
| 5345 | * anything else and exit, so return 1 so the callers don't try |
| 5346 | * to use other copies. |
| 5347 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5348 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5349 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5350 | map = em->map_lookup; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5351 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK)) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5352 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5353 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5354 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5355 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5356 | ret = 2; |
| 5357 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5358 | /* |
| 5359 | * There could be two corrupted data stripes, we need |
| 5360 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5361 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5362 | * Fail a stripe at a time on every retry except the |
| 5363 | * stripe under reconstruction. |
| 5364 | */ |
| 5365 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5366 | else |
| 5367 | ret = 1; |
| 5368 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5369 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5370 | down_read(&fs_info->dev_replace.rwsem); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5371 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5372 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5373 | ret++; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5374 | up_read(&fs_info->dev_replace.rwsem); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5375 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5376 | return ret; |
| 5377 | } |
| 5378 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5379 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5380 | u64 logical) |
| 5381 | { |
| 5382 | struct extent_map *em; |
| 5383 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5384 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5385 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5386 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5387 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5388 | if (!WARN_ON(IS_ERR(em))) { |
| 5389 | map = em->map_lookup; |
| 5390 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5391 | len = map->stripe_len * nr_data_stripes(map); |
| 5392 | free_extent_map(em); |
| 5393 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5394 | return len; |
| 5395 | } |
| 5396 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5397 | 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] | 5398 | { |
| 5399 | struct extent_map *em; |
| 5400 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5401 | int ret = 0; |
| 5402 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5403 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5404 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5405 | if(!WARN_ON(IS_ERR(em))) { |
| 5406 | map = em->map_lookup; |
| 5407 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5408 | ret = 1; |
| 5409 | free_extent_map(em); |
| 5410 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5411 | return ret; |
| 5412 | } |
| 5413 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5414 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5415 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5416 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5417 | { |
| 5418 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5419 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5420 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5421 | int tolerance; |
| 5422 | struct btrfs_device *srcdev; |
| 5423 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5424 | ASSERT((map->type & |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5425 | (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10))); |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5426 | |
| 5427 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5428 | num_stripes = map->sub_stripes; |
| 5429 | else |
| 5430 | num_stripes = map->num_stripes; |
| 5431 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5432 | preferred_mirror = first + current->pid % num_stripes; |
| 5433 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5434 | if (dev_replace_is_ongoing && |
| 5435 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5436 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5437 | srcdev = fs_info->dev_replace.srcdev; |
| 5438 | else |
| 5439 | srcdev = NULL; |
| 5440 | |
| 5441 | /* |
| 5442 | * try to avoid the drive that is the source drive for a |
| 5443 | * dev-replace procedure, only choose it if no other non-missing |
| 5444 | * mirror is available |
| 5445 | */ |
| 5446 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5447 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5448 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5449 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5450 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5451 | if (map->stripes[i].dev->bdev && |
| 5452 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5453 | return i; |
| 5454 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5455 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5456 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5457 | /* we couldn't find one that doesn't fail. Just return something |
| 5458 | * and the io error handling code will clean up eventually |
| 5459 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5460 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5461 | } |
| 5462 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5463 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5464 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5465 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5466 | int i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5467 | int again = 1; |
| 5468 | |
| 5469 | while (again) { |
| 5470 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5471 | for (i = 0; i < num_stripes - 1; i++) { |
David Sterba | eeb6f17 | 2019-11-28 15:31:17 +0100 | [diff] [blame] | 5472 | /* Swap if parity is on a smaller index */ |
| 5473 | if (bbio->raid_map[i] > bbio->raid_map[i + 1]) { |
| 5474 | swap(bbio->stripes[i], bbio->stripes[i + 1]); |
| 5475 | swap(bbio->raid_map[i], bbio->raid_map[i + 1]); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5476 | again = 1; |
| 5477 | } |
| 5478 | } |
| 5479 | } |
| 5480 | } |
| 5481 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5482 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5483 | { |
| 5484 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5485 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5486 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5487 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5488 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5489 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5490 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5491 | /* |
| 5492 | * plus the raid_map, which includes both the tgt dev |
| 5493 | * and the stripes |
| 5494 | */ |
| 5495 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5496 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5497 | |
| 5498 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5499 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5500 | |
| 5501 | return bbio; |
| 5502 | } |
| 5503 | |
| 5504 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5505 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5506 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5507 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5508 | } |
| 5509 | |
| 5510 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5511 | { |
| 5512 | if (!bbio) |
| 5513 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5514 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5515 | kfree(bbio); |
| 5516 | } |
| 5517 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5518 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5519 | /* |
| 5520 | * Please note that, discard won't be sent to target device of device |
| 5521 | * replace. |
| 5522 | */ |
| 5523 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5524 | u64 logical, u64 *length_ret, |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5525 | struct btrfs_bio **bbio_ret) |
| 5526 | { |
| 5527 | struct extent_map *em; |
| 5528 | struct map_lookup *map; |
| 5529 | struct btrfs_bio *bbio; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5530 | u64 length = *length_ret; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5531 | u64 offset; |
| 5532 | u64 stripe_nr; |
| 5533 | u64 stripe_nr_end; |
| 5534 | u64 stripe_end_offset; |
| 5535 | u64 stripe_cnt; |
| 5536 | u64 stripe_len; |
| 5537 | u64 stripe_offset; |
| 5538 | u64 num_stripes; |
| 5539 | u32 stripe_index; |
| 5540 | u32 factor = 0; |
| 5541 | u32 sub_stripes = 0; |
| 5542 | u64 stripes_per_dev = 0; |
| 5543 | u32 remaining_stripes = 0; |
| 5544 | u32 last_stripe = 0; |
| 5545 | int ret = 0; |
| 5546 | int i; |
| 5547 | |
| 5548 | /* discard always return a bbio */ |
| 5549 | ASSERT(bbio_ret); |
| 5550 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5551 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5552 | if (IS_ERR(em)) |
| 5553 | return PTR_ERR(em); |
| 5554 | |
| 5555 | map = em->map_lookup; |
| 5556 | /* we don't discard raid56 yet */ |
| 5557 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5558 | ret = -EOPNOTSUPP; |
| 5559 | goto out; |
| 5560 | } |
| 5561 | |
| 5562 | offset = logical - em->start; |
Qu Wenruo | 2d97461 | 2019-10-23 21:57:26 +0800 | [diff] [blame] | 5563 | length = min_t(u64, em->start + em->len - logical, length); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5564 | *length_ret = length; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5565 | |
| 5566 | stripe_len = map->stripe_len; |
| 5567 | /* |
| 5568 | * stripe_nr counts the total number of stripes we have to stride |
| 5569 | * to get to this block |
| 5570 | */ |
| 5571 | stripe_nr = div64_u64(offset, stripe_len); |
| 5572 | |
| 5573 | /* stripe_offset is the offset of this block in its stripe */ |
| 5574 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5575 | |
| 5576 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5577 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5578 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5579 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5580 | (offset + length); |
| 5581 | /* |
| 5582 | * after this, stripe_nr is the number of stripes on this |
| 5583 | * device we have to walk to find the data, and stripe_index is |
| 5584 | * the number of our device in the stripe array |
| 5585 | */ |
| 5586 | num_stripes = 1; |
| 5587 | stripe_index = 0; |
| 5588 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5589 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5590 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5591 | sub_stripes = 1; |
| 5592 | else |
| 5593 | sub_stripes = map->sub_stripes; |
| 5594 | |
| 5595 | factor = map->num_stripes / sub_stripes; |
| 5596 | num_stripes = min_t(u64, map->num_stripes, |
| 5597 | sub_stripes * stripe_cnt); |
| 5598 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5599 | stripe_index *= sub_stripes; |
| 5600 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5601 | &remaining_stripes); |
| 5602 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5603 | last_stripe *= sub_stripes; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5604 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5605 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5606 | num_stripes = map->num_stripes; |
| 5607 | } else { |
| 5608 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5609 | &stripe_index); |
| 5610 | } |
| 5611 | |
| 5612 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5613 | if (!bbio) { |
| 5614 | ret = -ENOMEM; |
| 5615 | goto out; |
| 5616 | } |
| 5617 | |
| 5618 | for (i = 0; i < num_stripes; i++) { |
| 5619 | bbio->stripes[i].physical = |
| 5620 | map->stripes[stripe_index].physical + |
| 5621 | stripe_offset + stripe_nr * map->stripe_len; |
| 5622 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5623 | |
| 5624 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5625 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5626 | bbio->stripes[i].length = stripes_per_dev * |
| 5627 | map->stripe_len; |
| 5628 | |
| 5629 | if (i / sub_stripes < remaining_stripes) |
| 5630 | bbio->stripes[i].length += |
| 5631 | map->stripe_len; |
| 5632 | |
| 5633 | /* |
| 5634 | * Special for the first stripe and |
| 5635 | * the last stripe: |
| 5636 | * |
| 5637 | * |-------|...|-------| |
| 5638 | * |----------| |
| 5639 | * off end_off |
| 5640 | */ |
| 5641 | if (i < sub_stripes) |
| 5642 | bbio->stripes[i].length -= |
| 5643 | stripe_offset; |
| 5644 | |
| 5645 | if (stripe_index >= last_stripe && |
| 5646 | stripe_index <= (last_stripe + |
| 5647 | sub_stripes - 1)) |
| 5648 | bbio->stripes[i].length -= |
| 5649 | stripe_end_offset; |
| 5650 | |
| 5651 | if (i == sub_stripes - 1) |
| 5652 | stripe_offset = 0; |
| 5653 | } else { |
| 5654 | bbio->stripes[i].length = length; |
| 5655 | } |
| 5656 | |
| 5657 | stripe_index++; |
| 5658 | if (stripe_index == map->num_stripes) { |
| 5659 | stripe_index = 0; |
| 5660 | stripe_nr++; |
| 5661 | } |
| 5662 | } |
| 5663 | |
| 5664 | *bbio_ret = bbio; |
| 5665 | bbio->map_type = map->type; |
| 5666 | bbio->num_stripes = num_stripes; |
| 5667 | out: |
| 5668 | free_extent_map(em); |
| 5669 | return ret; |
| 5670 | } |
| 5671 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5672 | /* |
| 5673 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5674 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5675 | * left cursor can also be read from the target drive. |
| 5676 | * |
| 5677 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5678 | * array of stripes. |
| 5679 | * For READ, it also needs to be supported using the same mirror number. |
| 5680 | * |
| 5681 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5682 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5683 | * case. |
| 5684 | */ |
| 5685 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5686 | u64 logical, u64 length, |
| 5687 | u64 srcdev_devid, int *mirror_num, |
| 5688 | u64 *physical) |
| 5689 | { |
| 5690 | struct btrfs_bio *bbio = NULL; |
| 5691 | int num_stripes; |
| 5692 | int index_srcdev = 0; |
| 5693 | int found = 0; |
| 5694 | u64 physical_of_found = 0; |
| 5695 | int i; |
| 5696 | int ret = 0; |
| 5697 | |
| 5698 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5699 | logical, &length, &bbio, 0, 0); |
| 5700 | if (ret) { |
| 5701 | ASSERT(bbio == NULL); |
| 5702 | return ret; |
| 5703 | } |
| 5704 | |
| 5705 | num_stripes = bbio->num_stripes; |
| 5706 | if (*mirror_num > num_stripes) { |
| 5707 | /* |
| 5708 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5709 | * that means that the requested area is not left of the left |
| 5710 | * cursor |
| 5711 | */ |
| 5712 | btrfs_put_bbio(bbio); |
| 5713 | return -EIO; |
| 5714 | } |
| 5715 | |
| 5716 | /* |
| 5717 | * process the rest of the function using the mirror_num of the source |
| 5718 | * drive. Therefore look it up first. At the end, patch the device |
| 5719 | * pointer to the one of the target drive. |
| 5720 | */ |
| 5721 | for (i = 0; i < num_stripes; i++) { |
| 5722 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5723 | continue; |
| 5724 | |
| 5725 | /* |
| 5726 | * In case of DUP, in order to keep it simple, only add the |
| 5727 | * mirror with the lowest physical address |
| 5728 | */ |
| 5729 | if (found && |
| 5730 | physical_of_found <= bbio->stripes[i].physical) |
| 5731 | continue; |
| 5732 | |
| 5733 | index_srcdev = i; |
| 5734 | found = 1; |
| 5735 | physical_of_found = bbio->stripes[i].physical; |
| 5736 | } |
| 5737 | |
| 5738 | btrfs_put_bbio(bbio); |
| 5739 | |
| 5740 | ASSERT(found); |
| 5741 | if (!found) |
| 5742 | return -EIO; |
| 5743 | |
| 5744 | *mirror_num = index_srcdev + 1; |
| 5745 | *physical = physical_of_found; |
| 5746 | return ret; |
| 5747 | } |
| 5748 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5749 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5750 | struct btrfs_bio **bbio_ret, |
| 5751 | struct btrfs_dev_replace *dev_replace, |
| 5752 | int *num_stripes_ret, int *max_errors_ret) |
| 5753 | { |
| 5754 | struct btrfs_bio *bbio = *bbio_ret; |
| 5755 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5756 | int tgtdev_indexes = 0; |
| 5757 | int num_stripes = *num_stripes_ret; |
| 5758 | int max_errors = *max_errors_ret; |
| 5759 | int i; |
| 5760 | |
| 5761 | if (op == BTRFS_MAP_WRITE) { |
| 5762 | int index_where_to_add; |
| 5763 | |
| 5764 | /* |
| 5765 | * duplicate the write operations while the dev replace |
| 5766 | * procedure is running. Since the copying of the old disk to |
| 5767 | * the new disk takes place at run time while the filesystem is |
| 5768 | * mounted writable, the regular write operations to the old |
| 5769 | * disk have to be duplicated to go to the new disk as well. |
| 5770 | * |
| 5771 | * Note that device->missing is handled by the caller, and that |
| 5772 | * the write to the old disk is already set up in the stripes |
| 5773 | * array. |
| 5774 | */ |
| 5775 | index_where_to_add = num_stripes; |
| 5776 | for (i = 0; i < num_stripes; i++) { |
| 5777 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5778 | /* write to new disk, too */ |
| 5779 | struct btrfs_bio_stripe *new = |
| 5780 | bbio->stripes + index_where_to_add; |
| 5781 | struct btrfs_bio_stripe *old = |
| 5782 | bbio->stripes + i; |
| 5783 | |
| 5784 | new->physical = old->physical; |
| 5785 | new->length = old->length; |
| 5786 | new->dev = dev_replace->tgtdev; |
| 5787 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5788 | index_where_to_add++; |
| 5789 | max_errors++; |
| 5790 | tgtdev_indexes++; |
| 5791 | } |
| 5792 | } |
| 5793 | num_stripes = index_where_to_add; |
| 5794 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5795 | int index_srcdev = 0; |
| 5796 | int found = 0; |
| 5797 | u64 physical_of_found = 0; |
| 5798 | |
| 5799 | /* |
| 5800 | * During the dev-replace procedure, the target drive can also |
| 5801 | * be used to read data in case it is needed to repair a corrupt |
| 5802 | * block elsewhere. This is possible if the requested area is |
| 5803 | * left of the left cursor. In this area, the target drive is a |
| 5804 | * full copy of the source drive. |
| 5805 | */ |
| 5806 | for (i = 0; i < num_stripes; i++) { |
| 5807 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5808 | /* |
| 5809 | * In case of DUP, in order to keep it simple, |
| 5810 | * only add the mirror with the lowest physical |
| 5811 | * address |
| 5812 | */ |
| 5813 | if (found && |
| 5814 | physical_of_found <= |
| 5815 | bbio->stripes[i].physical) |
| 5816 | continue; |
| 5817 | index_srcdev = i; |
| 5818 | found = 1; |
| 5819 | physical_of_found = bbio->stripes[i].physical; |
| 5820 | } |
| 5821 | } |
| 5822 | if (found) { |
| 5823 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5824 | bbio->stripes + num_stripes; |
| 5825 | |
| 5826 | tgtdev_stripe->physical = physical_of_found; |
| 5827 | tgtdev_stripe->length = |
| 5828 | bbio->stripes[index_srcdev].length; |
| 5829 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5830 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5831 | |
| 5832 | tgtdev_indexes++; |
| 5833 | num_stripes++; |
| 5834 | } |
| 5835 | } |
| 5836 | |
| 5837 | *num_stripes_ret = num_stripes; |
| 5838 | *max_errors_ret = max_errors; |
| 5839 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5840 | *bbio_ret = bbio; |
| 5841 | } |
| 5842 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5843 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5844 | { |
| 5845 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5846 | } |
| 5847 | |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5848 | /* |
| 5849 | * btrfs_get_io_geometry - calculates the geomery of a particular (address, len) |
| 5850 | * tuple. This information is used to calculate how big a |
| 5851 | * particular bio can get before it straddles a stripe. |
| 5852 | * |
| 5853 | * @fs_info - the filesystem |
| 5854 | * @logical - address that we want to figure out the geometry of |
| 5855 | * @len - the length of IO we are going to perform, starting at @logical |
| 5856 | * @op - type of operation - write or read |
| 5857 | * @io_geom - pointer used to return values |
| 5858 | * |
| 5859 | * Returns < 0 in case a chunk for the given logical address cannot be found, |
| 5860 | * usually shouldn't happen unless @logical is corrupted, 0 otherwise. |
| 5861 | */ |
| 5862 | 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] | 5863 | u64 logical, u64 len, struct btrfs_io_geometry *io_geom) |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5864 | { |
| 5865 | struct extent_map *em; |
| 5866 | struct map_lookup *map; |
| 5867 | u64 offset; |
| 5868 | u64 stripe_offset; |
| 5869 | u64 stripe_nr; |
| 5870 | u64 stripe_len; |
| 5871 | u64 raid56_full_stripe_start = (u64)-1; |
| 5872 | int data_stripes; |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5873 | int ret = 0; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5874 | |
| 5875 | ASSERT(op != BTRFS_MAP_DISCARD); |
| 5876 | |
| 5877 | em = btrfs_get_chunk_map(fs_info, logical, len); |
| 5878 | if (IS_ERR(em)) |
| 5879 | return PTR_ERR(em); |
| 5880 | |
| 5881 | map = em->map_lookup; |
| 5882 | /* Offset of this logical address in the chunk */ |
| 5883 | offset = logical - em->start; |
| 5884 | /* Len of a stripe in a chunk */ |
| 5885 | stripe_len = map->stripe_len; |
| 5886 | /* Stripe wher this block falls in */ |
| 5887 | stripe_nr = div64_u64(offset, stripe_len); |
| 5888 | /* Offset of stripe in the chunk */ |
| 5889 | stripe_offset = stripe_nr * stripe_len; |
| 5890 | if (offset < stripe_offset) { |
| 5891 | btrfs_crit(fs_info, |
| 5892 | "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu", |
| 5893 | stripe_offset, offset, em->start, logical, stripe_len); |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5894 | ret = -EINVAL; |
| 5895 | goto out; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5896 | } |
| 5897 | |
| 5898 | /* stripe_offset is the offset of this block in its stripe */ |
| 5899 | stripe_offset = offset - stripe_offset; |
| 5900 | data_stripes = nr_data_stripes(map); |
| 5901 | |
| 5902 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5903 | u64 max_len = stripe_len - stripe_offset; |
| 5904 | |
| 5905 | /* |
| 5906 | * In case of raid56, we need to know the stripe aligned start |
| 5907 | */ |
| 5908 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5909 | unsigned long full_stripe_len = stripe_len * data_stripes; |
| 5910 | raid56_full_stripe_start = offset; |
| 5911 | |
| 5912 | /* |
| 5913 | * Allow a write of a full stripe, but make sure we |
| 5914 | * don't allow straddling of stripes |
| 5915 | */ |
| 5916 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5917 | full_stripe_len); |
| 5918 | raid56_full_stripe_start *= full_stripe_len; |
| 5919 | |
| 5920 | /* |
| 5921 | * For writes to RAID[56], allow a full stripeset across |
| 5922 | * all disks. For other RAID types and for RAID[56] |
| 5923 | * reads, just allow a single stripe (on a single disk). |
| 5924 | */ |
| 5925 | if (op == BTRFS_MAP_WRITE) { |
| 5926 | max_len = stripe_len * data_stripes - |
| 5927 | (offset - raid56_full_stripe_start); |
| 5928 | } |
| 5929 | } |
| 5930 | len = min_t(u64, em->len - offset, max_len); |
| 5931 | } else { |
| 5932 | len = em->len - offset; |
| 5933 | } |
| 5934 | |
| 5935 | io_geom->len = len; |
| 5936 | io_geom->offset = offset; |
| 5937 | io_geom->stripe_len = stripe_len; |
| 5938 | io_geom->stripe_nr = stripe_nr; |
| 5939 | io_geom->stripe_offset = stripe_offset; |
| 5940 | io_geom->raid56_stripe_offset = raid56_full_stripe_start; |
| 5941 | |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5942 | out: |
| 5943 | /* once for us */ |
| 5944 | free_extent_map(em); |
| 5945 | return ret; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5946 | } |
| 5947 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5948 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 5949 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5950 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5951 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5952 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5953 | { |
| 5954 | struct extent_map *em; |
| 5955 | struct map_lookup *map; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5956 | u64 stripe_offset; |
| 5957 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5958 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5959 | u32 stripe_index; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5960 | int data_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5961 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5962 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5963 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5964 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5965 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5966 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5967 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5968 | int dev_replace_is_ongoing = 0; |
| 5969 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5970 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5971 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5972 | u64 raid56_full_stripe_start = (u64)-1; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5973 | struct btrfs_io_geometry geom; |
| 5974 | |
| 5975 | ASSERT(bbio_ret); |
David Sterba | 75fb2e9 | 2018-08-03 00:36:29 +0200 | [diff] [blame] | 5976 | ASSERT(op != BTRFS_MAP_DISCARD); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5977 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5978 | ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom); |
| 5979 | if (ret < 0) |
| 5980 | return ret; |
| 5981 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5982 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Dan Carpenter | f113698 | 2019-08-09 17:07:39 +0300 | [diff] [blame] | 5983 | ASSERT(!IS_ERR(em)); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5984 | map = em->map_lookup; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5985 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5986 | *length = geom.len; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5987 | stripe_len = geom.stripe_len; |
| 5988 | stripe_nr = geom.stripe_nr; |
| 5989 | stripe_offset = geom.stripe_offset; |
| 5990 | raid56_full_stripe_start = geom.raid56_stripe_offset; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5991 | data_stripes = nr_data_stripes(map); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5992 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5993 | down_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5994 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 5995 | /* |
| 5996 | * Hold the semaphore for read during the whole operation, write is |
| 5997 | * requested at commit time but must wait. |
| 5998 | */ |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5999 | if (!dev_replace_is_ongoing) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6000 | up_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6001 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6002 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6003 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6004 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 6005 | dev_replace->srcdev->devid, |
| 6006 | &mirror_num, |
| 6007 | &physical_to_patch_in_first_stripe); |
| 6008 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6009 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6010 | else |
| 6011 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6012 | } else if (mirror_num > map->num_stripes) { |
| 6013 | mirror_num = 0; |
| 6014 | } |
| 6015 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6016 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6017 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6018 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6019 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6020 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6021 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6022 | mirror_num = 1; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 6023 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6024 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6025 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6026 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6027 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6028 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6029 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6030 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6031 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6032 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6033 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6034 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6035 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6036 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6037 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6038 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6039 | } else { |
| 6040 | mirror_num = 1; |
| 6041 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6042 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6043 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6044 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6045 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6046 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6047 | stripe_index *= map->sub_stripes; |
| 6048 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6049 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6050 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6051 | else if (mirror_num) |
| 6052 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6053 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6054 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6055 | stripe_index = find_live_mirror(fs_info, map, |
| 6056 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6057 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6058 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6059 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6060 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6061 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6062 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6063 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6064 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6065 | stripe_len * data_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6066 | |
| 6067 | /* RAID[56] write or recovery. Return all stripes */ |
| 6068 | num_stripes = map->num_stripes; |
| 6069 | max_errors = nr_parity_stripes(map); |
| 6070 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6071 | *length = map->stripe_len; |
| 6072 | stripe_index = 0; |
| 6073 | stripe_offset = 0; |
| 6074 | } else { |
| 6075 | /* |
| 6076 | * Mirror #0 or #1 means the original data block. |
| 6077 | * Mirror #2 is RAID5 parity block. |
| 6078 | * Mirror #3 is RAID6 Q block. |
| 6079 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6080 | stripe_nr = div_u64_rem(stripe_nr, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6081 | data_stripes, &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6082 | if (mirror_num > 1) |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6083 | stripe_index = data_stripes + mirror_num - 2; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6084 | |
| 6085 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6086 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 6087 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6088 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6089 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6090 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6091 | } else { |
| 6092 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6093 | * after this, stripe_nr is the number of stripes on this |
| 6094 | * device we have to walk to find the data, and stripe_index is |
| 6095 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6096 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6097 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6098 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6099 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6100 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6101 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6102 | btrfs_crit(fs_info, |
| 6103 | "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] | 6104 | stripe_index, map->num_stripes); |
| 6105 | ret = -EINVAL; |
| 6106 | goto out; |
| 6107 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6108 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6109 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6110 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6111 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6112 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6113 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6114 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6115 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6116 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6117 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6118 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6119 | if (!bbio) { |
| 6120 | ret = -ENOMEM; |
| 6121 | goto out; |
| 6122 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6123 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6124 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6125 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6126 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6127 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6128 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6129 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6130 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6131 | |
| 6132 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 6133 | sizeof(struct btrfs_bio_stripe) * |
| 6134 | num_alloc_stripes + |
| 6135 | sizeof(int) * tgtdev_indexes); |
| 6136 | |
| 6137 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6138 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6139 | |
| 6140 | /* Fill in the logical address of each stripe */ |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6141 | tmp = stripe_nr * data_stripes; |
| 6142 | for (i = 0; i < data_stripes; i++) |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6143 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6144 | em->start + (tmp + i) * map->stripe_len; |
| 6145 | |
| 6146 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6147 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6148 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6149 | RAID6_Q_STRIPE; |
| 6150 | } |
| 6151 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 6152 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6153 | for (i = 0; i < num_stripes; i++) { |
| 6154 | bbio->stripes[i].physical = |
| 6155 | map->stripes[stripe_index].physical + |
| 6156 | stripe_offset + |
| 6157 | stripe_nr * map->stripe_len; |
| 6158 | bbio->stripes[i].dev = |
| 6159 | map->stripes[stripe_index].dev; |
| 6160 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6161 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6162 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6163 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6164 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6165 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6166 | if (bbio->raid_map) |
| 6167 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 6168 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6169 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6170 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6171 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6172 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6173 | } |
| 6174 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6175 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6176 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6177 | bbio->num_stripes = num_stripes; |
| 6178 | bbio->max_errors = max_errors; |
| 6179 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6180 | |
| 6181 | /* |
| 6182 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6183 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6184 | * available as a mirror |
| 6185 | */ |
| 6186 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6187 | WARN_ON(num_stripes > 1); |
| 6188 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6189 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6190 | bbio->mirror_num = map->num_stripes + 1; |
| 6191 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6192 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6193 | if (dev_replace_is_ongoing) { |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6194 | lockdep_assert_held(&dev_replace->rwsem); |
| 6195 | /* Unlock and let waiting writers proceed */ |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6196 | up_read(&dev_replace->rwsem); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6197 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6198 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6199 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6200 | } |
| 6201 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6202 | 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] | 6203 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6204 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6205 | { |
David Sterba | 75fb2e9 | 2018-08-03 00:36:29 +0200 | [diff] [blame] | 6206 | if (op == BTRFS_MAP_DISCARD) |
| 6207 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 6208 | length, bbio_ret); |
| 6209 | |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6210 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6211 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6212 | } |
| 6213 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6214 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6215 | 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] | 6216 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6217 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6218 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6219 | 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] | 6220 | } |
| 6221 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6222 | 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] | 6223 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6224 | bio->bi_private = bbio->private; |
| 6225 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6226 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6227 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6228 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6229 | } |
| 6230 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6231 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6232 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6233 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6234 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6235 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6236 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6237 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6238 | if (bio->bi_status == BLK_STS_IOERR || |
| 6239 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6240 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6241 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6242 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6243 | |
| 6244 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6245 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6246 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6247 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6248 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6249 | BTRFS_DEV_STAT_WRITE_ERRS); |
David Sterba | 0cc068e | 2019-03-07 15:40:50 +0100 | [diff] [blame] | 6250 | else if (!(bio->bi_opf & REQ_RAHEAD)) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6251 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6252 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6253 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6254 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6255 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6256 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6257 | } |
| 6258 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6259 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6260 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6261 | is_orig_bio = 1; |
| 6262 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6263 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6264 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6265 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6266 | if (!is_orig_bio) { |
| 6267 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6268 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6269 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6270 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6271 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6272 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6273 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6274 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6275 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6276 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6277 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6278 | /* |
| 6279 | * this bio is actually up to date, we didn't |
| 6280 | * go over the max number of errors |
| 6281 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6282 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6283 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6284 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6285 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6286 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6287 | bio_put(bio); |
| 6288 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6289 | } |
| 6290 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6291 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6292 | u64 physical, int dev_nr) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6293 | { |
| 6294 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6295 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6296 | |
| 6297 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6298 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6299 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6300 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6301 | btrfs_debug_in_rcu(fs_info, |
| 6302 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6303 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
David Sterba | 1db45a3 | 2019-11-28 15:31:46 +0100 | [diff] [blame] | 6304 | (unsigned long)dev->bdev->bd_dev, rcu_str_deref(dev->name), |
| 6305 | dev->devid, bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6306 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6307 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6308 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6309 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6310 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6311 | } |
| 6312 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6313 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6314 | { |
| 6315 | atomic_inc(&bbio->error); |
| 6316 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6317 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6318 | WARN_ON(bio != bbio->orig_bio); |
| 6319 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6320 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6321 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6322 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6323 | bio->bi_status = BLK_STS_IOERR; |
| 6324 | else |
| 6325 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6326 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6327 | } |
| 6328 | } |
| 6329 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6330 | 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] | 6331 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6332 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6333 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6334 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6335 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6336 | u64 length = 0; |
| 6337 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6338 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6339 | int dev_nr; |
| 6340 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6341 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6342 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6343 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6344 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6345 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6346 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6347 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6348 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6349 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6350 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6351 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6352 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6353 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6354 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6355 | bbio->orig_bio = first_bio; |
| 6356 | bbio->private = first_bio->bi_private; |
| 6357 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6358 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6359 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6360 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6361 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6362 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6363 | /* In this case, map_length has been set to the length of |
| 6364 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6365 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6366 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6367 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6368 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6369 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6370 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6371 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6372 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6373 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6374 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6375 | } |
| 6376 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6377 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6378 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6379 | "mapping failed logical %llu bio len %llu len %llu", |
| 6380 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6381 | BUG(); |
| 6382 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6383 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6384 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6385 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6386 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6387 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6388 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6389 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6390 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6391 | continue; |
| 6392 | } |
| 6393 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6394 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6395 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6396 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6397 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6398 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6399 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6400 | dev_nr); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6401 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6402 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6403 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6404 | } |
| 6405 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6406 | /* |
| 6407 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 6408 | * return NULL. |
| 6409 | * |
| 6410 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 6411 | * only devid is used. |
| 6412 | * |
| 6413 | * If @seed is true, traverse through the seed devices. |
| 6414 | */ |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6415 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6416 | u64 devid, u8 *uuid, u8 *fsid, |
| 6417 | bool seed) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6418 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6419 | struct btrfs_device *device; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6420 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6421 | while (fs_devices) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6422 | if (!fsid || |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6423 | !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6424 | list_for_each_entry(device, &fs_devices->devices, |
| 6425 | dev_list) { |
| 6426 | if (device->devid == devid && |
| 6427 | (!uuid || memcmp(device->uuid, uuid, |
| 6428 | BTRFS_UUID_SIZE) == 0)) |
| 6429 | return device; |
| 6430 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6431 | } |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6432 | if (seed) |
| 6433 | fs_devices = fs_devices->seed; |
| 6434 | else |
| 6435 | return NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6436 | } |
| 6437 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6438 | } |
| 6439 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6440 | 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] | 6441 | u64 devid, u8 *dev_uuid) |
| 6442 | { |
| 6443 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6444 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6445 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6446 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6447 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6448 | |
| 6449 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6450 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6451 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6452 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6453 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6454 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6455 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6456 | return device; |
| 6457 | } |
| 6458 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6459 | /** |
| 6460 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6461 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6462 | * devid is provided (i.e. @devid != NULL). |
| 6463 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6464 | * is generated. |
| 6465 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6466 | * is generated. |
| 6467 | * |
| 6468 | * 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] | 6469 | * 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] | 6470 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6471 | */ |
| 6472 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6473 | const u64 *devid, |
| 6474 | const u8 *uuid) |
| 6475 | { |
| 6476 | struct btrfs_device *dev; |
| 6477 | u64 tmp; |
| 6478 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6479 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6480 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6481 | |
| 6482 | dev = __alloc_device(); |
| 6483 | if (IS_ERR(dev)) |
| 6484 | return dev; |
| 6485 | |
| 6486 | if (devid) |
| 6487 | tmp = *devid; |
| 6488 | else { |
| 6489 | int ret; |
| 6490 | |
| 6491 | ret = find_next_devid(fs_info, &tmp); |
| 6492 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6493 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6494 | return ERR_PTR(ret); |
| 6495 | } |
| 6496 | } |
| 6497 | dev->devid = tmp; |
| 6498 | |
| 6499 | if (uuid) |
| 6500 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6501 | else |
| 6502 | generate_random_uuid(dev->uuid); |
| 6503 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6504 | return dev; |
| 6505 | } |
| 6506 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6507 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6508 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6509 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6510 | if (error) |
| 6511 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6512 | devid, uuid); |
| 6513 | else |
| 6514 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6515 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6516 | } |
| 6517 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6518 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 6519 | { |
| 6520 | int index = btrfs_bg_flags_to_raid_index(type); |
| 6521 | int ncopies = btrfs_raid_array[index].ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6522 | const int nparity = btrfs_raid_array[index].nparity; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6523 | int data_stripes; |
| 6524 | |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6525 | if (nparity) |
| 6526 | data_stripes = num_stripes - nparity; |
| 6527 | else |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6528 | data_stripes = num_stripes / ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6529 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6530 | return div_u64(chunk_len, data_stripes); |
| 6531 | } |
| 6532 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6533 | 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] | 6534 | struct btrfs_chunk *chunk) |
| 6535 | { |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6536 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6537 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6538 | struct map_lookup *map; |
| 6539 | struct extent_map *em; |
| 6540 | u64 logical; |
| 6541 | u64 length; |
| 6542 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6543 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6544 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6545 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6546 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6547 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6548 | logical = key->offset; |
| 6549 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6550 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6551 | |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6552 | /* |
| 6553 | * Only need to verify chunk item if we're reading from sys chunk array, |
| 6554 | * as chunk item in tree block is already verified by tree-checker. |
| 6555 | */ |
| 6556 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { |
David Sterba | ddaf1d5 | 2019-03-20 16:40:48 +0100 | [diff] [blame] | 6557 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6558 | if (ret) |
| 6559 | return ret; |
| 6560 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6561 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6562 | read_lock(&map_tree->lock); |
| 6563 | em = lookup_extent_mapping(map_tree, logical, 1); |
| 6564 | read_unlock(&map_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6565 | |
| 6566 | /* already mapped? */ |
| 6567 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6568 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6569 | return 0; |
| 6570 | } else if (em) { |
| 6571 | free_extent_map(em); |
| 6572 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6573 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6574 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6575 | if (!em) |
| 6576 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6577 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6578 | if (!map) { |
| 6579 | free_extent_map(em); |
| 6580 | return -ENOMEM; |
| 6581 | } |
| 6582 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6583 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6584 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6585 | em->start = logical; |
| 6586 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6587 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6588 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6589 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6590 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6591 | map->num_stripes = num_stripes; |
| 6592 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6593 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6594 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6595 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6596 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6597 | map->verified_stripes = 0; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6598 | em->orig_block_len = calc_stripe_length(map->type, em->len, |
| 6599 | map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6600 | for (i = 0; i < num_stripes; i++) { |
| 6601 | map->stripes[i].physical = |
| 6602 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6603 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6604 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6605 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6606 | BTRFS_UUID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6607 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6608 | devid, uuid, NULL, true); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6609 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6610 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6611 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6612 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6613 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6614 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6615 | if (!map->stripes[i].dev) { |
| 6616 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6617 | add_missing_dev(fs_info->fs_devices, devid, |
| 6618 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6619 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6620 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6621 | btrfs_err(fs_info, |
| 6622 | "failed to init missing dev %llu: %ld", |
| 6623 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6624 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6625 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6626 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6627 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6628 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6629 | &(map->stripes[i].dev->dev_state)); |
| 6630 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6631 | } |
| 6632 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6633 | write_lock(&map_tree->lock); |
| 6634 | ret = add_extent_mapping(map_tree, em, 0); |
| 6635 | write_unlock(&map_tree->lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6636 | if (ret < 0) { |
| 6637 | btrfs_err(fs_info, |
| 6638 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6639 | em->start, em->len, ret); |
| 6640 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6641 | free_extent_map(em); |
| 6642 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6643 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6644 | } |
| 6645 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6646 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6647 | struct btrfs_dev_item *dev_item, |
| 6648 | struct btrfs_device *device) |
| 6649 | { |
| 6650 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6651 | |
| 6652 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6653 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6654 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6655 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6656 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6657 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6658 | device->type = btrfs_device_type(leaf, dev_item); |
| 6659 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6660 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6661 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6662 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6663 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6664 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6665 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6666 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6667 | } |
| 6668 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6669 | 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] | 6670 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6671 | { |
| 6672 | struct btrfs_fs_devices *fs_devices; |
| 6673 | int ret; |
| 6674 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6675 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6676 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6677 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6678 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6679 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6680 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6681 | return fs_devices; |
| 6682 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6683 | fs_devices = fs_devices->seed; |
| 6684 | } |
| 6685 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6686 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6687 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6688 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6689 | return ERR_PTR(-ENOENT); |
| 6690 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6691 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6692 | if (IS_ERR(fs_devices)) |
| 6693 | return fs_devices; |
| 6694 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 6695 | fs_devices->seeding = true; |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6696 | fs_devices->opened = 1; |
| 6697 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6698 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6699 | |
| 6700 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6701 | if (IS_ERR(fs_devices)) |
| 6702 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6703 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6704 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6705 | if (ret) { |
| 6706 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6707 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6708 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6709 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6710 | |
| 6711 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6712 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6713 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6714 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6715 | goto out; |
| 6716 | } |
| 6717 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6718 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6719 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6720 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6721 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6722 | } |
| 6723 | |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6724 | static int read_one_dev(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6725 | struct btrfs_dev_item *dev_item) |
| 6726 | { |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6727 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6728 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6729 | struct btrfs_device *device; |
| 6730 | u64 devid; |
| 6731 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6732 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6733 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6734 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6735 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6736 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6737 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6738 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6739 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6740 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6741 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6742 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6743 | if (IS_ERR(fs_devices)) |
| 6744 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6745 | } |
| 6746 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6747 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6748 | fs_uuid, true); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6749 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6750 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6751 | btrfs_report_missing_device(fs_info, devid, |
| 6752 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6753 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6754 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6755 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6756 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6757 | if (IS_ERR(device)) { |
| 6758 | btrfs_err(fs_info, |
| 6759 | "failed to add missing dev %llu: %ld", |
| 6760 | devid, PTR_ERR(device)); |
| 6761 | return PTR_ERR(device); |
| 6762 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6763 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6764 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6765 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6766 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6767 | btrfs_report_missing_device(fs_info, |
| 6768 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6769 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6770 | } |
| 6771 | btrfs_report_missing_device(fs_info, devid, |
| 6772 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6773 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6774 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6775 | if (!device->bdev && |
| 6776 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6777 | /* |
| 6778 | * this happens when a device that was properly setup |
| 6779 | * in the device info lists suddenly goes bad. |
| 6780 | * device->bdev is NULL, and so we have to set |
| 6781 | * device->missing to one here |
| 6782 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6783 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6784 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6785 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6786 | |
| 6787 | /* Move the device to its own fs_devices */ |
| 6788 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6789 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6790 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6791 | |
| 6792 | list_move(&device->dev_list, &fs_devices->devices); |
| 6793 | device->fs_devices->num_devices--; |
| 6794 | fs_devices->num_devices++; |
| 6795 | |
| 6796 | device->fs_devices->missing_devices--; |
| 6797 | fs_devices->missing_devices++; |
| 6798 | |
| 6799 | device->fs_devices = fs_devices; |
| 6800 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6801 | } |
| 6802 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6803 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6804 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6805 | if (device->generation != |
| 6806 | btrfs_device_generation(leaf, dev_item)) |
| 6807 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6808 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6809 | |
| 6810 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6811 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6812 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6813 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6814 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6815 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6816 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6817 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6818 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6819 | return ret; |
| 6820 | } |
| 6821 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6822 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6823 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6824 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6825 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6826 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6827 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6828 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6829 | u8 *array_ptr; |
| 6830 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6831 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6832 | u32 num_stripes; |
| 6833 | u32 array_size; |
| 6834 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6835 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6836 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6837 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6838 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6839 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6840 | /* |
| 6841 | * This will create extent buffer of nodesize, superblock size is |
| 6842 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6843 | * overallocate but we can keep it as-is, only the first page is used. |
| 6844 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6845 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6846 | if (IS_ERR(sb)) |
| 6847 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6848 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6849 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6850 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6851 | * 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] | 6852 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6853 | * pages up-to-date when the page is larger: extent does not cover the |
| 6854 | * whole page and consequently check_page_uptodate does not find all |
| 6855 | * the page's extents up-to-date (the hole beyond sb), |
| 6856 | * write_extent_buffer then triggers a WARN_ON. |
| 6857 | * |
| 6858 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6859 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6860 | * to silence the warning eg. on PowerPC 64. |
| 6861 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6862 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6863 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6864 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6865 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6866 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6867 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6868 | array_ptr = super_copy->sys_chunk_array; |
| 6869 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6870 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6871 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6872 | while (cur_offset < array_size) { |
| 6873 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6874 | len = sizeof(*disk_key); |
| 6875 | if (cur_offset + len > array_size) |
| 6876 | goto out_short_read; |
| 6877 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6878 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6879 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6880 | array_ptr += len; |
| 6881 | sb_array_offset += len; |
| 6882 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6883 | |
Johannes Thumshirn | 32ab3d1 | 2019-10-18 11:58:23 +0200 | [diff] [blame] | 6884 | if (key.type != BTRFS_CHUNK_ITEM_KEY) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6885 | btrfs_err(fs_info, |
| 6886 | "unexpected item type %u in sys_array at offset %u", |
| 6887 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6888 | ret = -EIO; |
| 6889 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6890 | } |
Johannes Thumshirn | 32ab3d1 | 2019-10-18 11:58:23 +0200 | [diff] [blame] | 6891 | |
| 6892 | chunk = (struct btrfs_chunk *)sb_array_offset; |
| 6893 | /* |
| 6894 | * At least one btrfs_chunk with one stripe must be present, |
| 6895 | * exact stripe count check comes afterwards |
| 6896 | */ |
| 6897 | len = btrfs_chunk_item_size(1); |
| 6898 | if (cur_offset + len > array_size) |
| 6899 | goto out_short_read; |
| 6900 | |
| 6901 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6902 | if (!num_stripes) { |
| 6903 | btrfs_err(fs_info, |
| 6904 | "invalid number of stripes %u in sys_array at offset %u", |
| 6905 | num_stripes, cur_offset); |
| 6906 | ret = -EIO; |
| 6907 | break; |
| 6908 | } |
| 6909 | |
| 6910 | type = btrfs_chunk_type(sb, chunk); |
| 6911 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
| 6912 | btrfs_err(fs_info, |
| 6913 | "invalid chunk type %llu in sys_array at offset %u", |
| 6914 | type, cur_offset); |
| 6915 | ret = -EIO; |
| 6916 | break; |
| 6917 | } |
| 6918 | |
| 6919 | len = btrfs_chunk_item_size(num_stripes); |
| 6920 | if (cur_offset + len > array_size) |
| 6921 | goto out_short_read; |
| 6922 | |
| 6923 | ret = read_one_chunk(&key, sb, chunk); |
| 6924 | if (ret) |
| 6925 | break; |
| 6926 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6927 | array_ptr += len; |
| 6928 | sb_array_offset += len; |
| 6929 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6930 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6931 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6932 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6933 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6934 | |
| 6935 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6936 | 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] | 6937 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6938 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6939 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6940 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6941 | } |
| 6942 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6943 | /* |
| 6944 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 6945 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6946 | * If the @failing_dev is specified, it's accounted as missing. |
| 6947 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6948 | * Return true if all chunks meet the minimal RW mount requirements. |
| 6949 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 6950 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6951 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 6952 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6953 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6954 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6955 | struct extent_map *em; |
| 6956 | u64 next_start = 0; |
| 6957 | bool ret = true; |
| 6958 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6959 | read_lock(&map_tree->lock); |
| 6960 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); |
| 6961 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6962 | /* No chunk at all? Return false anyway */ |
| 6963 | if (!em) { |
| 6964 | ret = false; |
| 6965 | goto out; |
| 6966 | } |
| 6967 | while (em) { |
| 6968 | struct map_lookup *map; |
| 6969 | int missing = 0; |
| 6970 | int max_tolerated; |
| 6971 | int i; |
| 6972 | |
| 6973 | map = em->map_lookup; |
| 6974 | max_tolerated = |
| 6975 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 6976 | map->type); |
| 6977 | for (i = 0; i < map->num_stripes; i++) { |
| 6978 | struct btrfs_device *dev = map->stripes[i].dev; |
| 6979 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6980 | if (!dev || !dev->bdev || |
| 6981 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6982 | dev->last_flush_error) |
| 6983 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6984 | else if (failing_dev && failing_dev == dev) |
| 6985 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6986 | } |
| 6987 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6988 | if (!failing_dev) |
| 6989 | btrfs_warn(fs_info, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 6990 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6991 | em->start, missing, max_tolerated); |
| 6992 | free_extent_map(em); |
| 6993 | ret = false; |
| 6994 | goto out; |
| 6995 | } |
| 6996 | next_start = extent_map_end(em); |
| 6997 | free_extent_map(em); |
| 6998 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6999 | read_lock(&map_tree->lock); |
| 7000 | em = lookup_extent_mapping(map_tree, next_start, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7001 | (u64)(-1) - next_start); |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7002 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7003 | } |
| 7004 | out: |
| 7005 | return ret; |
| 7006 | } |
| 7007 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7008 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7009 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7010 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7011 | struct btrfs_path *path; |
| 7012 | struct extent_buffer *leaf; |
| 7013 | struct btrfs_key key; |
| 7014 | struct btrfs_key found_key; |
| 7015 | int ret; |
| 7016 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7017 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7018 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7019 | path = btrfs_alloc_path(); |
| 7020 | if (!path) |
| 7021 | return -ENOMEM; |
| 7022 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 7023 | /* |
| 7024 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 7025 | * otherwise we don't need it. |
| 7026 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7027 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7028 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7029 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7030 | /* |
| 7031 | * Read all device items, and then all the chunk items. All |
| 7032 | * device items are found before any chunk item (their object id |
| 7033 | * is smaller than the lowest possible object id for a chunk |
| 7034 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7035 | */ |
| 7036 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7037 | key.offset = 0; |
| 7038 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7039 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7040 | if (ret < 0) |
| 7041 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7042 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7043 | leaf = path->nodes[0]; |
| 7044 | slot = path->slots[0]; |
| 7045 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7046 | ret = btrfs_next_leaf(root, path); |
| 7047 | if (ret == 0) |
| 7048 | continue; |
| 7049 | if (ret < 0) |
| 7050 | goto error; |
| 7051 | break; |
| 7052 | } |
| 7053 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7054 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7055 | struct btrfs_dev_item *dev_item; |
| 7056 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7057 | struct btrfs_dev_item); |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 7058 | ret = read_one_dev(leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7059 | if (ret) |
| 7060 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7061 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7062 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7063 | struct btrfs_chunk *chunk; |
| 7064 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7065 | ret = read_one_chunk(&found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7066 | if (ret) |
| 7067 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7068 | } |
| 7069 | path->slots[0]++; |
| 7070 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7071 | |
| 7072 | /* |
| 7073 | * After loading chunk tree, we've got all device information, |
| 7074 | * do another round of validation checks. |
| 7075 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7076 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7077 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7078 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7079 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7080 | total_dev); |
| 7081 | ret = -EINVAL; |
| 7082 | goto error; |
| 7083 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7084 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7085 | fs_info->fs_devices->total_rw_bytes) { |
| 7086 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7087 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7088 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7089 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7090 | ret = -EINVAL; |
| 7091 | goto error; |
| 7092 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7093 | ret = 0; |
| 7094 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7095 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7096 | mutex_unlock(&uuid_mutex); |
| 7097 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7098 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7099 | return ret; |
| 7100 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7101 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7102 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7103 | { |
| 7104 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7105 | struct btrfs_device *device; |
| 7106 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7107 | while (fs_devices) { |
| 7108 | mutex_lock(&fs_devices->device_list_mutex); |
| 7109 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7110 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7111 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7112 | |
| 7113 | fs_devices = fs_devices->seed; |
| 7114 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7115 | } |
| 7116 | |
David Sterba | 1dc990d | 2019-08-21 20:05:32 +0200 | [diff] [blame] | 7117 | static u64 btrfs_dev_stats_value(const struct extent_buffer *eb, |
| 7118 | const struct btrfs_dev_stats_item *ptr, |
| 7119 | int index) |
| 7120 | { |
| 7121 | u64 val; |
| 7122 | |
| 7123 | read_extent_buffer(eb, &val, |
| 7124 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7125 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7126 | sizeof(val)); |
| 7127 | return val; |
| 7128 | } |
| 7129 | |
| 7130 | static void btrfs_set_dev_stats_value(struct extent_buffer *eb, |
| 7131 | struct btrfs_dev_stats_item *ptr, |
| 7132 | int index, u64 val) |
| 7133 | { |
| 7134 | write_extent_buffer(eb, &val, |
| 7135 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7136 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7137 | sizeof(val)); |
| 7138 | } |
| 7139 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7140 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7141 | { |
| 7142 | struct btrfs_key key; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7143 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7144 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7145 | struct extent_buffer *eb; |
| 7146 | int slot; |
| 7147 | int ret = 0; |
| 7148 | struct btrfs_device *device; |
| 7149 | struct btrfs_path *path = NULL; |
| 7150 | int i; |
| 7151 | |
| 7152 | path = btrfs_alloc_path(); |
Anand Jain | 3b80a984 | 2019-08-21 17:26:32 +0800 | [diff] [blame] | 7153 | if (!path) |
| 7154 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7155 | |
| 7156 | mutex_lock(&fs_devices->device_list_mutex); |
| 7157 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7158 | int item_size; |
| 7159 | struct btrfs_dev_stats_item *ptr; |
| 7160 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7161 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7162 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7163 | key.offset = device->devid; |
| 7164 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7165 | if (ret) { |
Anand Jain | ae4b9b4 | 2019-08-07 16:21:20 +0800 | [diff] [blame] | 7166 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7167 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7168 | device->dev_stats_valid = 1; |
| 7169 | btrfs_release_path(path); |
| 7170 | continue; |
| 7171 | } |
| 7172 | slot = path->slots[0]; |
| 7173 | eb = path->nodes[0]; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7174 | item_size = btrfs_item_size_nr(eb, slot); |
| 7175 | |
| 7176 | ptr = btrfs_item_ptr(eb, slot, |
| 7177 | struct btrfs_dev_stats_item); |
| 7178 | |
| 7179 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7180 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7181 | btrfs_dev_stat_set(device, i, |
| 7182 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7183 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7184 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7185 | } |
| 7186 | |
| 7187 | device->dev_stats_valid = 1; |
| 7188 | btrfs_dev_stat_print_on_load(device); |
| 7189 | btrfs_release_path(path); |
| 7190 | } |
| 7191 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7192 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7193 | btrfs_free_path(path); |
| 7194 | return ret < 0 ? ret : 0; |
| 7195 | } |
| 7196 | |
| 7197 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7198 | struct btrfs_device *device) |
| 7199 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7200 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7201 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7202 | struct btrfs_path *path; |
| 7203 | struct btrfs_key key; |
| 7204 | struct extent_buffer *eb; |
| 7205 | struct btrfs_dev_stats_item *ptr; |
| 7206 | int ret; |
| 7207 | int i; |
| 7208 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7209 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7210 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7211 | key.offset = device->devid; |
| 7212 | |
| 7213 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7214 | if (!path) |
| 7215 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7216 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7217 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7218 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7219 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7220 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7221 | goto out; |
| 7222 | } |
| 7223 | |
| 7224 | if (ret == 0 && |
| 7225 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7226 | /* need to delete old one and insert a new one */ |
| 7227 | ret = btrfs_del_item(trans, dev_root, path); |
| 7228 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7229 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7230 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7231 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7232 | goto out; |
| 7233 | } |
| 7234 | ret = 1; |
| 7235 | } |
| 7236 | |
| 7237 | if (ret == 1) { |
| 7238 | /* need to insert a new item */ |
| 7239 | btrfs_release_path(path); |
| 7240 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7241 | &key, sizeof(*ptr)); |
| 7242 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7243 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7244 | "insert dev_stats item for device %s failed %d", |
| 7245 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7246 | goto out; |
| 7247 | } |
| 7248 | } |
| 7249 | |
| 7250 | eb = path->nodes[0]; |
| 7251 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7252 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7253 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7254 | btrfs_dev_stat_read(device, i)); |
| 7255 | btrfs_mark_buffer_dirty(eb); |
| 7256 | |
| 7257 | out: |
| 7258 | btrfs_free_path(path); |
| 7259 | return ret; |
| 7260 | } |
| 7261 | |
| 7262 | /* |
| 7263 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7264 | */ |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7265 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7266 | { |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7267 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7268 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7269 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7270 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7271 | int ret = 0; |
| 7272 | |
| 7273 | mutex_lock(&fs_devices->device_list_mutex); |
| 7274 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7275 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7276 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7277 | continue; |
| 7278 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7279 | |
| 7280 | /* |
| 7281 | * There is a LOAD-LOAD control dependency between the value of |
| 7282 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7283 | * reading the in-memory counters. Such control dependencies |
| 7284 | * require explicit read memory barriers. |
| 7285 | * |
| 7286 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7287 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7288 | * barrier implied by atomic_xchg in |
| 7289 | * btrfs_dev_stats_read_and_reset |
| 7290 | */ |
| 7291 | smp_rmb(); |
| 7292 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7293 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7294 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7295 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7296 | } |
| 7297 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7298 | |
| 7299 | return ret; |
| 7300 | } |
| 7301 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7302 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7303 | { |
| 7304 | btrfs_dev_stat_inc(dev, index); |
| 7305 | btrfs_dev_stat_print_on_error(dev); |
| 7306 | } |
| 7307 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7308 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7309 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7310 | if (!dev->dev_stats_valid) |
| 7311 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7312 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7313 | "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] | 7314 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7315 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7316 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7317 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7318 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7319 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7320 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7321 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7322 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7323 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7324 | int i; |
| 7325 | |
| 7326 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7327 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7328 | break; |
| 7329 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7330 | return; /* all values == 0, suppress message */ |
| 7331 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7332 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7333 | "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] | 7334 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7335 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7336 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7337 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7338 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7339 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7340 | } |
| 7341 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7342 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7343 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7344 | { |
| 7345 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7346 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7347 | int i; |
| 7348 | |
| 7349 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7350 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
| 7351 | true); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7352 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7353 | |
| 7354 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7355 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7356 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7357 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7358 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7359 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7360 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7361 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7362 | if (stats->nr_items > i) |
| 7363 | stats->values[i] = |
| 7364 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7365 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7366 | btrfs_dev_stat_set(dev, i, 0); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7367 | } |
Anand Jain | a69976b | 2020-01-10 12:26:34 +0800 | [diff] [blame] | 7368 | btrfs_info(fs_info, "device stats zeroed by %s (%d)", |
| 7369 | current->comm, task_pid_nr(current)); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7370 | } else { |
| 7371 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7372 | if (stats->nr_items > i) |
| 7373 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7374 | } |
| 7375 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7376 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7377 | return 0; |
| 7378 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7379 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7380 | /* |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7381 | * Update the size and bytes used for each device where it changed. This is |
| 7382 | * delayed since we would otherwise get errors while writing out the |
| 7383 | * superblocks. |
| 7384 | * |
| 7385 | * Must be invoked during transaction commit. |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7386 | */ |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7387 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7388 | { |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7389 | struct btrfs_device *curr, *next; |
| 7390 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7391 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
| 7392 | |
| 7393 | if (list_empty(&trans->dev_update_list)) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7394 | return; |
| 7395 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7396 | /* |
| 7397 | * We don't need the device_list_mutex here. This list is owned by the |
| 7398 | * transaction and the transaction must complete before the device is |
| 7399 | * released. |
| 7400 | */ |
| 7401 | mutex_lock(&trans->fs_info->chunk_mutex); |
| 7402 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, |
| 7403 | post_commit_list) { |
| 7404 | list_del_init(&curr->post_commit_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7405 | curr->commit_total_bytes = curr->disk_total_bytes; |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7406 | curr->commit_bytes_used = curr->bytes_used; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7407 | } |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7408 | mutex_unlock(&trans->fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7409 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7410 | |
| 7411 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7412 | { |
| 7413 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7414 | while (fs_devices) { |
| 7415 | fs_devices->fs_info = fs_info; |
| 7416 | fs_devices = fs_devices->seed; |
| 7417 | } |
| 7418 | } |
| 7419 | |
| 7420 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7421 | { |
| 7422 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7423 | while (fs_devices) { |
| 7424 | fs_devices->fs_info = NULL; |
| 7425 | fs_devices = fs_devices->seed; |
| 7426 | } |
| 7427 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7428 | |
| 7429 | /* |
| 7430 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7431 | */ |
| 7432 | int btrfs_bg_type_to_factor(u64 flags) |
| 7433 | { |
David Sterba | 44b28ad | 2019-05-17 11:43:31 +0200 | [diff] [blame] | 7434 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 7435 | |
| 7436 | return btrfs_raid_array[index].ncopies; |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7437 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7438 | |
| 7439 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7440 | |
| 7441 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7442 | u64 chunk_offset, u64 devid, |
| 7443 | u64 physical_offset, u64 physical_len) |
| 7444 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7445 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7446 | struct extent_map *em; |
| 7447 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7448 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7449 | u64 stripe_len; |
| 7450 | bool found = false; |
| 7451 | int ret = 0; |
| 7452 | int i; |
| 7453 | |
| 7454 | read_lock(&em_tree->lock); |
| 7455 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7456 | read_unlock(&em_tree->lock); |
| 7457 | |
| 7458 | if (!em) { |
| 7459 | btrfs_err(fs_info, |
| 7460 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7461 | physical_offset, devid); |
| 7462 | ret = -EUCLEAN; |
| 7463 | goto out; |
| 7464 | } |
| 7465 | |
| 7466 | map = em->map_lookup; |
| 7467 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7468 | if (physical_len != stripe_len) { |
| 7469 | btrfs_err(fs_info, |
| 7470 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7471 | physical_offset, devid, em->start, physical_len, |
| 7472 | stripe_len); |
| 7473 | ret = -EUCLEAN; |
| 7474 | goto out; |
| 7475 | } |
| 7476 | |
| 7477 | for (i = 0; i < map->num_stripes; i++) { |
| 7478 | if (map->stripes[i].dev->devid == devid && |
| 7479 | map->stripes[i].physical == physical_offset) { |
| 7480 | found = true; |
| 7481 | if (map->verified_stripes >= map->num_stripes) { |
| 7482 | btrfs_err(fs_info, |
| 7483 | "too many dev extents for chunk %llu found", |
| 7484 | em->start); |
| 7485 | ret = -EUCLEAN; |
| 7486 | goto out; |
| 7487 | } |
| 7488 | map->verified_stripes++; |
| 7489 | break; |
| 7490 | } |
| 7491 | } |
| 7492 | if (!found) { |
| 7493 | btrfs_err(fs_info, |
| 7494 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7495 | physical_offset, devid); |
| 7496 | ret = -EUCLEAN; |
| 7497 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7498 | |
| 7499 | /* Make sure no dev extent is beyond device bondary */ |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7500 | dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7501 | if (!dev) { |
| 7502 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7503 | ret = -EUCLEAN; |
| 7504 | goto out; |
| 7505 | } |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7506 | |
| 7507 | /* It's possible this device is a dummy for seed device */ |
| 7508 | if (dev->disk_total_bytes == 0) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7509 | dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL, |
| 7510 | NULL, false); |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7511 | if (!dev) { |
| 7512 | btrfs_err(fs_info, "failed to find seed devid %llu", |
| 7513 | devid); |
| 7514 | ret = -EUCLEAN; |
| 7515 | goto out; |
| 7516 | } |
| 7517 | } |
| 7518 | |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7519 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7520 | btrfs_err(fs_info, |
| 7521 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7522 | devid, physical_offset, physical_len, |
| 7523 | dev->disk_total_bytes); |
| 7524 | ret = -EUCLEAN; |
| 7525 | goto out; |
| 7526 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7527 | out: |
| 7528 | free_extent_map(em); |
| 7529 | return ret; |
| 7530 | } |
| 7531 | |
| 7532 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7533 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7534 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7535 | struct extent_map *em; |
| 7536 | struct rb_node *node; |
| 7537 | int ret = 0; |
| 7538 | |
| 7539 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7540 | 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] | 7541 | em = rb_entry(node, struct extent_map, rb_node); |
| 7542 | if (em->map_lookup->num_stripes != |
| 7543 | em->map_lookup->verified_stripes) { |
| 7544 | btrfs_err(fs_info, |
| 7545 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7546 | em->start, em->map_lookup->verified_stripes, |
| 7547 | em->map_lookup->num_stripes); |
| 7548 | ret = -EUCLEAN; |
| 7549 | goto out; |
| 7550 | } |
| 7551 | } |
| 7552 | out: |
| 7553 | read_unlock(&em_tree->lock); |
| 7554 | return ret; |
| 7555 | } |
| 7556 | |
| 7557 | /* |
| 7558 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7559 | * later chunk allocation/free would cause unexpected behavior. |
| 7560 | * |
| 7561 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7562 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7563 | */ |
| 7564 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7565 | { |
| 7566 | struct btrfs_path *path; |
| 7567 | struct btrfs_root *root = fs_info->dev_root; |
| 7568 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7569 | u64 prev_devid = 0; |
| 7570 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7571 | int ret = 0; |
| 7572 | |
| 7573 | key.objectid = 1; |
| 7574 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7575 | key.offset = 0; |
| 7576 | |
| 7577 | path = btrfs_alloc_path(); |
| 7578 | if (!path) |
| 7579 | return -ENOMEM; |
| 7580 | |
| 7581 | path->reada = READA_FORWARD; |
| 7582 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7583 | if (ret < 0) |
| 7584 | goto out; |
| 7585 | |
| 7586 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7587 | ret = btrfs_next_item(root, path); |
| 7588 | if (ret < 0) |
| 7589 | goto out; |
| 7590 | /* No dev extents at all? Not good */ |
| 7591 | if (ret > 0) { |
| 7592 | ret = -EUCLEAN; |
| 7593 | goto out; |
| 7594 | } |
| 7595 | } |
| 7596 | while (1) { |
| 7597 | struct extent_buffer *leaf = path->nodes[0]; |
| 7598 | struct btrfs_dev_extent *dext; |
| 7599 | int slot = path->slots[0]; |
| 7600 | u64 chunk_offset; |
| 7601 | u64 physical_offset; |
| 7602 | u64 physical_len; |
| 7603 | u64 devid; |
| 7604 | |
| 7605 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7606 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7607 | break; |
| 7608 | devid = key.objectid; |
| 7609 | physical_offset = key.offset; |
| 7610 | |
| 7611 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7612 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7613 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7614 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7615 | /* Check if this dev extent overlaps with the previous one */ |
| 7616 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7617 | btrfs_err(fs_info, |
| 7618 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7619 | devid, physical_offset, prev_dev_ext_end); |
| 7620 | ret = -EUCLEAN; |
| 7621 | goto out; |
| 7622 | } |
| 7623 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7624 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7625 | physical_offset, physical_len); |
| 7626 | if (ret < 0) |
| 7627 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7628 | prev_devid = devid; |
| 7629 | prev_dev_ext_end = physical_offset + physical_len; |
| 7630 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7631 | ret = btrfs_next_item(root, path); |
| 7632 | if (ret < 0) |
| 7633 | goto out; |
| 7634 | if (ret > 0) { |
| 7635 | ret = 0; |
| 7636 | break; |
| 7637 | } |
| 7638 | } |
| 7639 | |
| 7640 | /* Ensure all chunks have corresponding dev extents */ |
| 7641 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7642 | out: |
| 7643 | btrfs_free_path(path); |
| 7644 | return ret; |
| 7645 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7646 | |
| 7647 | /* |
| 7648 | * Check whether the given block group or device is pinned by any inode being |
| 7649 | * used as a swapfile. |
| 7650 | */ |
| 7651 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7652 | { |
| 7653 | struct btrfs_swapfile_pin *sp; |
| 7654 | struct rb_node *node; |
| 7655 | |
| 7656 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7657 | node = fs_info->swapfile_pins.rb_node; |
| 7658 | while (node) { |
| 7659 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7660 | if (ptr < sp->ptr) |
| 7661 | node = node->rb_left; |
| 7662 | else if (ptr > sp->ptr) |
| 7663 | node = node->rb_right; |
| 7664 | else |
| 7665 | break; |
| 7666 | } |
| 7667 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7668 | return node != NULL; |
| 7669 | } |