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> |
Josef Bacik | fccc000 | 2020-08-31 10:52:42 -0400 | [diff] [blame] | 7 | #include <linux/sched/mm.h> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 10 | #include <linux/blkdev.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 12 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 13 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 14 | #include <linux/semaphore.h> |
Andy Shevchenko | 8da4b8c | 2016-05-20 17:01:00 -0700 | [diff] [blame] | 15 | #include <linux/uuid.h> |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 16 | #include <linux/list_sort.h> |
David Sterba | 784352f | 2019-08-21 18:54:28 +0200 | [diff] [blame] | 17 | #include "misc.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 18 | #include "ctree.h" |
| 19 | #include "extent_map.h" |
| 20 | #include "disk-io.h" |
| 21 | #include "transaction.h" |
| 22 | #include "print-tree.h" |
| 23 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 24 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 25 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 26 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 27 | #include "rcu-string.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 28 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 29 | #include "sysfs.h" |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 30 | #include "tree-checker.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 31 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 32 | #include "block-group.h" |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 33 | #include "discard.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 34 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 35 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 36 | [BTRFS_RAID_RAID10] = { |
| 37 | .sub_stripes = 2, |
| 38 | .dev_stripes = 1, |
| 39 | .devs_max = 0, /* 0 == as many as possible */ |
| 40 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 41 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 42 | .devs_increment = 2, |
| 43 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 44 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 45 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 46 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 47 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 48 | }, |
| 49 | [BTRFS_RAID_RAID1] = { |
| 50 | .sub_stripes = 1, |
| 51 | .dev_stripes = 1, |
| 52 | .devs_max = 2, |
| 53 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 54 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 55 | .devs_increment = 2, |
| 56 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 57 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 58 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 59 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 60 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 61 | }, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 62 | [BTRFS_RAID_RAID1C3] = { |
| 63 | .sub_stripes = 1, |
| 64 | .dev_stripes = 1, |
David Sterba | cf93e15 | 2019-11-27 16:10:54 +0100 | [diff] [blame] | 65 | .devs_max = 3, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 66 | .devs_min = 3, |
| 67 | .tolerated_failures = 2, |
| 68 | .devs_increment = 3, |
| 69 | .ncopies = 3, |
David Sterba | db26a02 | 2019-11-25 15:34:48 +0100 | [diff] [blame] | 70 | .nparity = 0, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 71 | .raid_name = "raid1c3", |
| 72 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3, |
| 73 | .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET, |
| 74 | }, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 75 | [BTRFS_RAID_RAID1C4] = { |
| 76 | .sub_stripes = 1, |
| 77 | .dev_stripes = 1, |
David Sterba | cf93e15 | 2019-11-27 16:10:54 +0100 | [diff] [blame] | 78 | .devs_max = 4, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 79 | .devs_min = 4, |
| 80 | .tolerated_failures = 3, |
| 81 | .devs_increment = 4, |
| 82 | .ncopies = 4, |
David Sterba | db26a02 | 2019-11-25 15:34:48 +0100 | [diff] [blame] | 83 | .nparity = 0, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 84 | .raid_name = "raid1c4", |
| 85 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4, |
| 86 | .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET, |
| 87 | }, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 88 | [BTRFS_RAID_DUP] = { |
| 89 | .sub_stripes = 1, |
| 90 | .dev_stripes = 2, |
| 91 | .devs_max = 1, |
| 92 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 93 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 94 | .devs_increment = 1, |
| 95 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 96 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 97 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 98 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 99 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 100 | }, |
| 101 | [BTRFS_RAID_RAID0] = { |
| 102 | .sub_stripes = 1, |
| 103 | .dev_stripes = 1, |
| 104 | .devs_max = 0, |
| 105 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 106 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 107 | .devs_increment = 1, |
| 108 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 109 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 110 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 111 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 112 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 113 | }, |
| 114 | [BTRFS_RAID_SINGLE] = { |
| 115 | .sub_stripes = 1, |
| 116 | .dev_stripes = 1, |
| 117 | .devs_max = 1, |
| 118 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 119 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 120 | .devs_increment = 1, |
| 121 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 122 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 123 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 124 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 125 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 126 | }, |
| 127 | [BTRFS_RAID_RAID5] = { |
| 128 | .sub_stripes = 1, |
| 129 | .dev_stripes = 1, |
| 130 | .devs_max = 0, |
| 131 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 132 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 133 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 134 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 135 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 136 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 137 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 138 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 139 | }, |
| 140 | [BTRFS_RAID_RAID6] = { |
| 141 | .sub_stripes = 1, |
| 142 | .dev_stripes = 1, |
| 143 | .devs_max = 0, |
| 144 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 145 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 146 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 147 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 148 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 149 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 150 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 151 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 152 | }, |
| 153 | }; |
| 154 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 155 | const char *btrfs_bg_type_to_raid_name(u64 flags) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 156 | { |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 157 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 158 | |
| 159 | if (index >= BTRFS_NR_RAID_TYPES) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 160 | return NULL; |
| 161 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 162 | return btrfs_raid_array[index].raid_name; |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Anand Jain | f89e09c | 2018-11-20 16:12:55 +0800 | [diff] [blame] | 165 | /* |
| 166 | * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| 167 | * bytes including terminating null byte. |
| 168 | */ |
| 169 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| 170 | { |
| 171 | int i; |
| 172 | int ret; |
| 173 | char *bp = buf; |
| 174 | u64 flags = bg_flags; |
| 175 | u32 size_bp = size_buf; |
| 176 | |
| 177 | if (!flags) { |
| 178 | strcpy(bp, "NONE"); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | #define DESCRIBE_FLAG(flag, desc) \ |
| 183 | do { \ |
| 184 | if (flags & (flag)) { \ |
| 185 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| 186 | if (ret < 0 || ret >= size_bp) \ |
| 187 | goto out_overflow; \ |
| 188 | size_bp -= ret; \ |
| 189 | bp += ret; \ |
| 190 | flags &= ~(flag); \ |
| 191 | } \ |
| 192 | } while (0) |
| 193 | |
| 194 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| 195 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| 196 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| 197 | |
| 198 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| 199 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 200 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| 201 | btrfs_raid_array[i].raid_name); |
| 202 | #undef DESCRIBE_FLAG |
| 203 | |
| 204 | if (flags) { |
| 205 | ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| 206 | size_bp -= ret; |
| 207 | } |
| 208 | |
| 209 | if (size_bp < size_buf) |
| 210 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| 211 | |
| 212 | /* |
| 213 | * The text is trimmed, it's up to the caller to provide sufficiently |
| 214 | * large buffer |
| 215 | */ |
| 216 | out_overflow:; |
| 217 | } |
| 218 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 219 | static int init_first_rw_device(struct btrfs_trans_handle *trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 220 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 221 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 222 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 223 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 224 | enum btrfs_map_op op, |
| 225 | u64 logical, u64 *length, |
| 226 | struct btrfs_bio **bbio_ret, |
| 227 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 228 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 229 | /* |
| 230 | * Device locking |
| 231 | * ============== |
| 232 | * |
| 233 | * There are several mutexes that protect manipulation of devices and low-level |
| 234 | * structures like chunks but not block groups, extents or files |
| 235 | * |
| 236 | * uuid_mutex (global lock) |
| 237 | * ------------------------ |
| 238 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 239 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 240 | * device) or requested by the device= mount option |
| 241 | * |
| 242 | * the mutex can be very coarse and can cover long-running operations |
| 243 | * |
| 244 | * protects: updates to fs_devices counters like missing devices, rw devices, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 245 | * seeding, structure cloning, opening/closing devices at mount/umount time |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 246 | * |
| 247 | * global::fs_devs - add, remove, updates to the global list |
| 248 | * |
Josef Bacik | 18c850f | 2020-07-17 15:12:27 -0400 | [diff] [blame] | 249 | * does not protect: manipulation of the fs_devices::devices list in general |
| 250 | * but in mount context it could be used to exclude list modifications by eg. |
| 251 | * scan ioctl |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 252 | * |
| 253 | * btrfs_device::name - renames (write side), read is RCU |
| 254 | * |
| 255 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 256 | * ------------------------------------------------ |
| 257 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 258 | * |
| 259 | * simple list traversal with read-only actions can be done with RCU protection |
| 260 | * |
| 261 | * may be used to exclude some operations from running concurrently without any |
| 262 | * modifications to the list (see write_all_supers) |
| 263 | * |
Josef Bacik | 18c850f | 2020-07-17 15:12:27 -0400 | [diff] [blame] | 264 | * Is not required at mount and close times, because our device list is |
| 265 | * protected by the uuid_mutex at that point. |
| 266 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 267 | * balance_mutex |
| 268 | * ------------- |
| 269 | * protects balance structures (status, state) and context accessed from |
| 270 | * several places (internally, ioctl) |
| 271 | * |
| 272 | * chunk_mutex |
| 273 | * ----------- |
| 274 | * protects chunks, adding or removing during allocation, trim or when a new |
Nikolay Borisov | 0b6f5d4 | 2019-05-09 18:11:11 +0300 | [diff] [blame] | 275 | * device is added/removed. Additionally it also protects post_commit_list of |
| 276 | * individual devices, since they can be added to the transaction's |
| 277 | * post_commit_list only with chunk_mutex held. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 278 | * |
| 279 | * cleaner_mutex |
| 280 | * ------------- |
| 281 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 282 | * cleaning together with relocation or delayed iputs |
| 283 | * |
| 284 | * |
| 285 | * Lock nesting |
| 286 | * ============ |
| 287 | * |
| 288 | * uuid_mutex |
Anand Jain | ae3e715 | 2020-05-14 01:42:45 +0800 | [diff] [blame] | 289 | * device_list_mutex |
| 290 | * chunk_mutex |
| 291 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 292 | * |
| 293 | * |
| 294 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 295 | * ====================================== |
| 296 | * |
| 297 | * Maintains the exclusivity of the following operations that apply to the |
| 298 | * whole filesystem and cannot run in parallel. |
| 299 | * |
| 300 | * - Balance (*) |
| 301 | * - Device add |
| 302 | * - Device remove |
| 303 | * - Device replace (*) |
| 304 | * - Resize |
| 305 | * |
| 306 | * The device operations (as above) can be in one of the following states: |
| 307 | * |
| 308 | * - Running state |
| 309 | * - Paused state |
| 310 | * - Completed state |
| 311 | * |
| 312 | * Only device operations marked with (*) can go into the Paused state for the |
| 313 | * following reasons: |
| 314 | * |
| 315 | * - ioctl (only Balance can be Paused through ioctl) |
| 316 | * - filesystem remounted as read-only |
| 317 | * - filesystem unmounted and mounted as read-only |
| 318 | * - system power-cycle and filesystem mounted as read-only |
| 319 | * - filesystem or device errors leading to forced read-only |
| 320 | * |
| 321 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 322 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 323 | * A device operation in Paused or Running state can be canceled or resumed |
| 324 | * either by ioctl (Balance only) or when remounted as read-write. |
| 325 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 326 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 327 | */ |
| 328 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 329 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 330 | static LIST_HEAD(fs_uuids); |
David Sterba | 4143cb8 | 2019-10-01 19:57:37 +0200 | [diff] [blame] | 331 | struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 332 | { |
| 333 | return &fs_uuids; |
| 334 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 335 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 336 | /* |
| 337 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 338 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 339 | * @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] | 340 | * |
| 341 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 342 | * The returned struct is not linked onto any lists and can be destroyed with |
| 343 | * kfree() right away. |
| 344 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 345 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 346 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 347 | { |
| 348 | struct btrfs_fs_devices *fs_devs; |
| 349 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 350 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 351 | if (!fs_devs) |
| 352 | return ERR_PTR(-ENOMEM); |
| 353 | |
| 354 | mutex_init(&fs_devs->device_list_mutex); |
| 355 | |
| 356 | INIT_LIST_HEAD(&fs_devs->devices); |
| 357 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 358 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 359 | if (fsid) |
| 360 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 361 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 362 | if (metadata_fsid) |
| 363 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 364 | else if (fsid) |
| 365 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 366 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 367 | return fs_devs; |
| 368 | } |
| 369 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 370 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 371 | { |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 372 | WARN_ON(!list_empty(&device->post_commit_list)); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 373 | rcu_string_free(device->name); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 374 | extent_io_tree_release(&device->alloc_state); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 375 | bio_put(device->flush_bio); |
| 376 | kfree(device); |
| 377 | } |
| 378 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 379 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 380 | { |
| 381 | struct btrfs_device *device; |
| 382 | WARN_ON(fs_devices->opened); |
| 383 | while (!list_empty(&fs_devices->devices)) { |
| 384 | device = list_entry(fs_devices->devices.next, |
| 385 | struct btrfs_device, dev_list); |
| 386 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 387 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 388 | } |
| 389 | kfree(fs_devices); |
| 390 | } |
| 391 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 392 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 393 | { |
| 394 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 395 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 396 | while (!list_empty(&fs_uuids)) { |
| 397 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 398 | struct btrfs_fs_devices, fs_list); |
| 399 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 400 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 401 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 402 | } |
| 403 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 404 | /* |
| 405 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 406 | * 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] | 407 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 408 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 409 | static struct btrfs_device *__alloc_device(void) |
| 410 | { |
| 411 | struct btrfs_device *dev; |
| 412 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 413 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 414 | if (!dev) |
| 415 | return ERR_PTR(-ENOMEM); |
| 416 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 417 | /* |
| 418 | * Preallocate a bio that's always going to be used for flushing device |
| 419 | * barriers and matches the device lifespan |
| 420 | */ |
| 421 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 422 | if (!dev->flush_bio) { |
| 423 | kfree(dev); |
| 424 | return ERR_PTR(-ENOMEM); |
| 425 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 426 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 427 | INIT_LIST_HEAD(&dev->dev_list); |
| 428 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 429 | INIT_LIST_HEAD(&dev->post_commit_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 430 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 431 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 432 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 433 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 434 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 435 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 436 | extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 437 | |
| 438 | return dev; |
| 439 | } |
| 440 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 441 | static noinline struct btrfs_fs_devices *find_fsid( |
| 442 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 443 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 444 | struct btrfs_fs_devices *fs_devices; |
| 445 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 446 | ASSERT(fsid); |
| 447 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 448 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 449 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 450 | if (metadata_fsid) { |
| 451 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 452 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 453 | BTRFS_FSID_SIZE) == 0) |
| 454 | return fs_devices; |
| 455 | } else { |
| 456 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 457 | return fs_devices; |
| 458 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 459 | } |
| 460 | return NULL; |
| 461 | } |
| 462 | |
Su Yue | c6730a0 | 2020-01-10 14:11:33 +0200 | [diff] [blame] | 463 | static struct btrfs_fs_devices *find_fsid_with_metadata_uuid( |
| 464 | struct btrfs_super_block *disk_super) |
| 465 | { |
| 466 | |
| 467 | struct btrfs_fs_devices *fs_devices; |
| 468 | |
| 469 | /* |
| 470 | * Handle scanned device having completed its fsid change but |
| 471 | * belonging to a fs_devices that was created by first scanning |
| 472 | * a device which didn't have its fsid/metadata_uuid changed |
| 473 | * at all and the CHANGING_FSID_V2 flag set. |
| 474 | */ |
| 475 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 476 | if (fs_devices->fsid_change && |
| 477 | memcmp(disk_super->metadata_uuid, fs_devices->fsid, |
| 478 | BTRFS_FSID_SIZE) == 0 && |
| 479 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 480 | BTRFS_FSID_SIZE) == 0) { |
| 481 | return fs_devices; |
| 482 | } |
| 483 | } |
| 484 | /* |
| 485 | * Handle scanned device having completed its fsid change but |
| 486 | * belonging to a fs_devices that was created by a device that |
| 487 | * has an outdated pair of fsid/metadata_uuid and |
| 488 | * CHANGING_FSID_V2 flag set. |
| 489 | */ |
| 490 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 491 | if (fs_devices->fsid_change && |
| 492 | memcmp(fs_devices->metadata_uuid, |
| 493 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 494 | memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid, |
| 495 | BTRFS_FSID_SIZE) == 0) { |
| 496 | return fs_devices; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return find_fsid(disk_super->fsid, disk_super->metadata_uuid); |
| 501 | } |
| 502 | |
| 503 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 504 | static int |
| 505 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 506 | int flush, struct block_device **bdev, |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 507 | struct btrfs_super_block **disk_super) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 508 | { |
| 509 | int ret; |
| 510 | |
| 511 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 512 | |
| 513 | if (IS_ERR(*bdev)) { |
| 514 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 515 | goto error; |
| 516 | } |
| 517 | |
| 518 | if (flush) |
| 519 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 520 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 521 | if (ret) { |
| 522 | blkdev_put(*bdev, flags); |
| 523 | goto error; |
| 524 | } |
| 525 | invalidate_bdev(*bdev); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 526 | *disk_super = btrfs_read_dev_super(*bdev); |
| 527 | if (IS_ERR(*disk_super)) { |
| 528 | ret = PTR_ERR(*disk_super); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 529 | blkdev_put(*bdev, flags); |
| 530 | goto error; |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | |
| 535 | error: |
| 536 | *bdev = NULL; |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 540 | static bool device_path_matched(const char *path, struct btrfs_device *device) |
| 541 | { |
| 542 | int found; |
| 543 | |
| 544 | rcu_read_lock(); |
| 545 | found = strcmp(rcu_str_deref(device->name), path); |
| 546 | rcu_read_unlock(); |
| 547 | |
| 548 | return found == 0; |
| 549 | } |
| 550 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 551 | /* |
| 552 | * Search and remove all stale (devices which are not mounted) devices. |
| 553 | * When both inputs are NULL, it will search and release all stale devices. |
| 554 | * path: Optional. When provided will it release all unmounted devices |
| 555 | * matching this path only. |
| 556 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 557 | * devices. |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 558 | * Return: 0 for success or if @path is NULL. |
| 559 | * -EBUSY if @path is a mounted device. |
| 560 | * -ENOENT if @path does not match any device in the list. |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 561 | */ |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 562 | static int btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 563 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 564 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 565 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 566 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 567 | int ret = 0; |
| 568 | |
| 569 | if (path) |
| 570 | ret = -ENOENT; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 571 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 572 | 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] | 573 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 574 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 575 | list_for_each_entry_safe(device, tmp_device, |
| 576 | &fs_devices->devices, dev_list) { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 577 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 578 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 579 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 580 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 581 | if (path && !device_path_matched(path, device)) |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 582 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 583 | if (fs_devices->opened) { |
| 584 | /* for an already deleted device return 0 */ |
| 585 | if (path && ret != 0) |
| 586 | ret = -EBUSY; |
| 587 | break; |
| 588 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 589 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 590 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 591 | fs_devices->num_devices--; |
| 592 | list_del(&device->dev_list); |
| 593 | btrfs_free_device(device); |
| 594 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 595 | ret = 0; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 596 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 597 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 598 | } |
| 599 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 600 | |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 601 | if (fs_devices->num_devices == 0) { |
| 602 | btrfs_sysfs_remove_fsid(fs_devices); |
| 603 | list_del(&fs_devices->fs_list); |
| 604 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 605 | } |
| 606 | } |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 607 | |
| 608 | return ret; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 609 | } |
| 610 | |
Josef Bacik | 18c850f | 2020-07-17 15:12:27 -0400 | [diff] [blame] | 611 | /* |
| 612 | * This is only used on mount, and we are protected from competing things |
| 613 | * messing with our fs_devices by the uuid_mutex, thus we do not need the |
| 614 | * fs_devices->device_list_mutex here. |
| 615 | */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 616 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 617 | struct btrfs_device *device, fmode_t flags, |
| 618 | void *holder) |
| 619 | { |
| 620 | struct request_queue *q; |
| 621 | struct block_device *bdev; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 622 | struct btrfs_super_block *disk_super; |
| 623 | u64 devid; |
| 624 | int ret; |
| 625 | |
| 626 | if (device->bdev) |
| 627 | return -EINVAL; |
| 628 | if (!device->name) |
| 629 | return -EINVAL; |
| 630 | |
| 631 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 632 | &bdev, &disk_super); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 633 | if (ret) |
| 634 | return ret; |
| 635 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 636 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 637 | if (devid != device->devid) |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 638 | goto error_free_page; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 639 | |
| 640 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 641 | goto error_free_page; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 642 | |
| 643 | device->generation = btrfs_super_generation(disk_super); |
| 644 | |
| 645 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 646 | if (btrfs_super_incompat_flags(disk_super) & |
| 647 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 648 | pr_err( |
| 649 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 650 | goto error_free_page; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 653 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 654 | fs_devices->seeding = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 655 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 656 | if (bdev_read_only(bdev)) |
| 657 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 658 | else |
| 659 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 663 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 664 | fs_devices->rotating = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 665 | |
| 666 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 667 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 668 | device->mode = flags; |
| 669 | |
| 670 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 671 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 672 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 673 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 674 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 675 | } |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 676 | btrfs_release_disk_super(disk_super); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 677 | |
| 678 | return 0; |
| 679 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 680 | error_free_page: |
| 681 | btrfs_release_disk_super(disk_super); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 682 | blkdev_put(bdev, flags); |
| 683 | |
| 684 | return -EINVAL; |
| 685 | } |
| 686 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 687 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 688 | * 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] | 689 | * being created with a disk that has already completed its fsid change. Such |
| 690 | * disk can belong to an fs which has its FSID changed or to one which doesn't. |
| 691 | * Handle both cases here. |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 692 | */ |
| 693 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 694 | struct btrfs_super_block *disk_super) |
| 695 | { |
| 696 | struct btrfs_fs_devices *fs_devices; |
| 697 | |
| 698 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 699 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 700 | BTRFS_FSID_SIZE) != 0 && |
| 701 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 702 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 703 | return fs_devices; |
| 704 | } |
| 705 | } |
| 706 | |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 707 | return find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 708 | } |
| 709 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 710 | |
| 711 | static struct btrfs_fs_devices *find_fsid_changed( |
| 712 | struct btrfs_super_block *disk_super) |
| 713 | { |
| 714 | struct btrfs_fs_devices *fs_devices; |
| 715 | |
| 716 | /* |
| 717 | * Handles the case where scanned device is part of an fs that had |
| 718 | * multiple successful changes of FSID but curently device didn't |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 719 | * observe it. Meaning our fsid will be different than theirs. We need |
| 720 | * to handle two subcases : |
| 721 | * 1 - The fs still continues to have different METADATA/FSID uuids. |
| 722 | * 2 - The fs is switched back to its original FSID (METADATA/FSID |
| 723 | * are equal). |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 724 | */ |
| 725 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 726 | /* Changed UUIDs */ |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 727 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 728 | BTRFS_FSID_SIZE) != 0 && |
| 729 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 730 | BTRFS_FSID_SIZE) == 0 && |
| 731 | memcmp(fs_devices->fsid, disk_super->fsid, |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 732 | BTRFS_FSID_SIZE) != 0) |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 733 | return fs_devices; |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 734 | |
| 735 | /* Unchanged UUIDs */ |
| 736 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 737 | BTRFS_FSID_SIZE) == 0 && |
| 738 | memcmp(fs_devices->fsid, disk_super->metadata_uuid, |
| 739 | BTRFS_FSID_SIZE) == 0) |
| 740 | return fs_devices; |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | return NULL; |
| 744 | } |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 745 | |
| 746 | static struct btrfs_fs_devices *find_fsid_reverted_metadata( |
| 747 | struct btrfs_super_block *disk_super) |
| 748 | { |
| 749 | struct btrfs_fs_devices *fs_devices; |
| 750 | |
| 751 | /* |
| 752 | * Handle the case where the scanned device is part of an fs whose last |
| 753 | * metadata UUID change reverted it to the original FSID. At the same |
| 754 | * time * fs_devices was first created by another constitutent device |
| 755 | * which didn't fully observe the operation. This results in an |
| 756 | * btrfs_fs_devices created with metadata/fsid different AND |
| 757 | * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the |
| 758 | * fs_devices equal to the FSID of the disk. |
| 759 | */ |
| 760 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 761 | if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 762 | BTRFS_FSID_SIZE) != 0 && |
| 763 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 764 | BTRFS_FSID_SIZE) == 0 && |
| 765 | fs_devices->fsid_change) |
| 766 | return fs_devices; |
| 767 | } |
| 768 | |
| 769 | return NULL; |
| 770 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 771 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 772 | * Add new device to list of registered devices |
| 773 | * |
| 774 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 775 | * device pointer which was just added or updated when successful |
| 776 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 777 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 778 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 779 | struct btrfs_super_block *disk_super, |
| 780 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 781 | { |
| 782 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 783 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 784 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 785 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 786 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 787 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 788 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 789 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 790 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 791 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 792 | if (fsid_change_in_progress) { |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 793 | if (!has_metadata_uuid) |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 794 | fs_devices = find_fsid_inprogress(disk_super); |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 795 | else |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 796 | fs_devices = find_fsid_changed(disk_super); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 797 | } else if (has_metadata_uuid) { |
Su Yue | c6730a0 | 2020-01-10 14:11:33 +0200 | [diff] [blame] | 798 | fs_devices = find_fsid_with_metadata_uuid(disk_super); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 799 | } else { |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 800 | fs_devices = find_fsid_reverted_metadata(disk_super); |
| 801 | if (!fs_devices) |
| 802 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 805 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 806 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 807 | if (has_metadata_uuid) |
| 808 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 809 | disk_super->metadata_uuid); |
| 810 | else |
| 811 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 812 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 813 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 814 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 815 | |
Al Viro | 92900e5 | 2019-01-27 04:58:00 +0000 | [diff] [blame] | 816 | fs_devices->fsid_change = fsid_change_in_progress; |
| 817 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 818 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 819 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 820 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 821 | device = NULL; |
| 822 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 823 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 824 | device = btrfs_find_device(fs_devices, devid, |
| 825 | disk_super->dev_item.uuid, NULL, false); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 826 | |
| 827 | /* |
| 828 | * If this disk has been pulled into an fs devices created by |
| 829 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 830 | * metadata_uuid/fsid values of the fs_devices. |
| 831 | */ |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 832 | if (fs_devices->fsid_change && |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 833 | found_transid > fs_devices->latest_generation) { |
| 834 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 835 | BTRFS_FSID_SIZE); |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 836 | |
| 837 | if (has_metadata_uuid) |
| 838 | memcpy(fs_devices->metadata_uuid, |
| 839 | disk_super->metadata_uuid, |
| 840 | BTRFS_FSID_SIZE); |
| 841 | else |
| 842 | memcpy(fs_devices->metadata_uuid, |
| 843 | disk_super->fsid, BTRFS_FSID_SIZE); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 844 | |
| 845 | fs_devices->fsid_change = false; |
| 846 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 847 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 848 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 849 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 850 | if (fs_devices->opened) { |
| 851 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 852 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 853 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 854 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 855 | device = btrfs_alloc_device(NULL, &devid, |
| 856 | disk_super->dev_item.uuid); |
| 857 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 858 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 859 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 860 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 861 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 862 | |
| 863 | name = rcu_string_strdup(path, GFP_NOFS); |
| 864 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 865 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 866 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 867 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 868 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 869 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 870 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 871 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 872 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 873 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 874 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 875 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 876 | |
| 877 | if (disk_super->label[0]) |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 878 | pr_info( |
| 879 | "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n", |
| 880 | disk_super->label, devid, found_transid, path, |
| 881 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 882 | else |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 883 | pr_info( |
| 884 | "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n", |
| 885 | disk_super->fsid, devid, found_transid, path, |
| 886 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 887 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 888 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 889 | /* |
| 890 | * When FS is already mounted. |
| 891 | * 1. If you are here and if the device->name is NULL that |
| 892 | * means this device was missing at time of FS mount. |
| 893 | * 2. If you are here and if the device->name is different |
| 894 | * from 'path' that means either |
| 895 | * a. The same device disappeared and reappeared with |
| 896 | * different name. or |
| 897 | * b. The missing-disk-which-was-replaced, has |
| 898 | * reappeared now. |
| 899 | * |
| 900 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 901 | * and unintentional. |
| 902 | * |
| 903 | * Further in case of 1 and 2a above, the disk at 'path' |
| 904 | * would have missed some transaction when it was away and |
| 905 | * in case of 2a the stale bdev has to be updated as well. |
| 906 | * 2b must not be allowed at all time. |
| 907 | */ |
| 908 | |
| 909 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 910 | * For now, we do allow update to btrfs_fs_device through the |
| 911 | * btrfs dev scan cli after FS has been mounted. We're still |
| 912 | * tracking a problem where systems fail mount by subvolume id |
| 913 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 914 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 915 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 916 | /* |
| 917 | * That is if the FS is _not_ mounted and if you |
| 918 | * are here, that means there is more than one |
| 919 | * disk with same uuid and devid.We keep the one |
| 920 | * with larger generation number or the last-in if |
| 921 | * generation are equal. |
| 922 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 923 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 924 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 925 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 926 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 927 | /* |
| 928 | * We are going to replace the device path for a given devid, |
| 929 | * make sure it's the same device if the device is mounted |
| 930 | */ |
| 931 | if (device->bdev) { |
| 932 | struct block_device *path_bdev; |
| 933 | |
| 934 | path_bdev = lookup_bdev(path); |
| 935 | if (IS_ERR(path_bdev)) { |
| 936 | mutex_unlock(&fs_devices->device_list_mutex); |
| 937 | return ERR_CAST(path_bdev); |
| 938 | } |
| 939 | |
| 940 | if (device->bdev != path_bdev) { |
| 941 | bdput(path_bdev); |
| 942 | mutex_unlock(&fs_devices->device_list_mutex); |
| 943 | btrfs_warn_in_rcu(device->fs_info, |
| 944 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 945 | disk_super->fsid, devid, |
| 946 | rcu_str_deref(device->name), path); |
| 947 | return ERR_PTR(-EEXIST); |
| 948 | } |
| 949 | bdput(path_bdev); |
| 950 | btrfs_info_in_rcu(device->fs_info, |
| 951 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 952 | disk_super->fsid, devid, |
| 953 | rcu_str_deref(device->name), path); |
| 954 | } |
| 955 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 956 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 957 | if (!name) { |
| 958 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 959 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 960 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 961 | rcu_string_free(device->name); |
| 962 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 963 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 964 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 965 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 966 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 967 | } |
| 968 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 969 | /* |
| 970 | * Unmount does not free the btrfs_device struct but would zero |
| 971 | * generation along with most of the other members. So just update |
| 972 | * it back. We need it to pick the disk with largest generation |
| 973 | * (as above). |
| 974 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 975 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 976 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 977 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 978 | fs_devices->latest_generation); |
| 979 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 980 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 981 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 982 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 983 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 984 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 985 | } |
| 986 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 987 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 988 | { |
| 989 | struct btrfs_fs_devices *fs_devices; |
| 990 | struct btrfs_device *device; |
| 991 | struct btrfs_device *orig_dev; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 992 | int ret = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 993 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 994 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 995 | if (IS_ERR(fs_devices)) |
| 996 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 997 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 998 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 999 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1000 | |
| 1001 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1002 | struct rcu_string *name; |
| 1003 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 1004 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 1005 | orig_dev->uuid); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1006 | if (IS_ERR(device)) { |
| 1007 | ret = PTR_ERR(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1008 | goto error; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1009 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1010 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1011 | /* |
| 1012 | * This is ok to do without rcu read locked because we hold the |
| 1013 | * uuid mutex so nothing we touch in here is going to disappear. |
| 1014 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1015 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 1016 | name = rcu_string_strdup(orig_dev->name->str, |
| 1017 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1018 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1019 | btrfs_free_device(device); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1020 | ret = -ENOMEM; |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1021 | goto error; |
| 1022 | } |
| 1023 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 1024 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1025 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1026 | list_add(&device->dev_list, &fs_devices->devices); |
| 1027 | device->fs_devices = fs_devices; |
| 1028 | fs_devices->num_devices++; |
| 1029 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1030 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1031 | return fs_devices; |
| 1032 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1033 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1034 | free_fs_devices(fs_devices); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1035 | return ERR_PTR(ret); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1036 | } |
| 1037 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1038 | /* |
| 1039 | * After we have read the system tree and know devids belonging to |
| 1040 | * this filesystem, remove the device which does not belong there. |
| 1041 | */ |
| 1042 | 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] | 1043 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1044 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1045 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1046 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1047 | mutex_lock(&uuid_mutex); |
| 1048 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1049 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1050 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1051 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1052 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1053 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1054 | &device->dev_state) && |
Anand Jain | 998a067 | 2020-05-05 02:58:25 +0800 | [diff] [blame] | 1055 | !test_bit(BTRFS_DEV_STATE_MISSING, |
| 1056 | &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1057 | (!latest_dev || |
| 1058 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1059 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1060 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1061 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1062 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1063 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1064 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1065 | /* |
| 1066 | * In the first step, keep the device which has |
| 1067 | * the correct fsid and the devid that is used |
| 1068 | * for the dev_replace procedure. |
| 1069 | * In the second step, the dev_replace state is |
| 1070 | * read from the device tree and it is known |
| 1071 | * whether the procedure is really active or |
| 1072 | * not, which means whether this device is |
| 1073 | * used or whether it should be removed. |
| 1074 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1075 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1076 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1077 | continue; |
| 1078 | } |
| 1079 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1080 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1081 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1082 | device->bdev = NULL; |
| 1083 | fs_devices->open_devices--; |
| 1084 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1085 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1086 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1087 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1088 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1089 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1090 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1091 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1092 | list_del_init(&device->dev_list); |
| 1093 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1094 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1095 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1096 | |
| 1097 | if (fs_devices->seed) { |
| 1098 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1099 | goto again; |
| 1100 | } |
| 1101 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1102 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1103 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1104 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1105 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1106 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1107 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1108 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1109 | if (!device->bdev) |
| 1110 | return; |
| 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 | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1113 | sync_blockdev(device->bdev); |
| 1114 | invalidate_bdev(device->bdev); |
| 1115 | } |
| 1116 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1117 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1118 | } |
| 1119 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1120 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1121 | { |
| 1122 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1123 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1124 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1125 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1126 | list_del_init(&device->dev_alloc_list); |
| 1127 | fs_devices->rw_devices--; |
| 1128 | } |
| 1129 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1130 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1131 | fs_devices->missing_devices--; |
| 1132 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1133 | btrfs_close_bdev(device); |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1134 | if (device->bdev) { |
Johannes Thumshirn | 3fff397 | 2019-11-26 09:40:05 +0100 | [diff] [blame] | 1135 | fs_devices->open_devices--; |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1136 | device->bdev = NULL; |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1137 | } |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1138 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1139 | |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1140 | device->fs_info = NULL; |
| 1141 | atomic_set(&device->dev_stats_ccnt, 0); |
| 1142 | extent_io_tree_release(&device->alloc_state); |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1143 | |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1144 | /* Verify the device is back in a pristine state */ |
| 1145 | ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)); |
| 1146 | ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
| 1147 | ASSERT(list_empty(&device->dev_alloc_list)); |
| 1148 | ASSERT(list_empty(&device->post_commit_list)); |
| 1149 | ASSERT(atomic_read(&device->reada_in_flight) == 0); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1152 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1153 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1154 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1155 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1156 | if (--fs_devices->opened > 0) |
| 1157 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1158 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1159 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1160 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1161 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1162 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1163 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1164 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1165 | WARN_ON(fs_devices->open_devices); |
| 1166 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1167 | fs_devices->opened = 0; |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 1168 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1169 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1170 | return 0; |
| 1171 | } |
| 1172 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1173 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1174 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1175 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1176 | int ret; |
| 1177 | |
| 1178 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1179 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1180 | if (!fs_devices->opened) { |
| 1181 | seed_devices = fs_devices->seed; |
| 1182 | fs_devices->seed = NULL; |
| 1183 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1184 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1185 | |
| 1186 | while (seed_devices) { |
| 1187 | fs_devices = seed_devices; |
| 1188 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1189 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1190 | free_fs_devices(fs_devices); |
| 1191 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1192 | return ret; |
| 1193 | } |
| 1194 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1195 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1196 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1197 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1198 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1199 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1200 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1201 | flags |= FMODE_EXCL; |
| 1202 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1203 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1204 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1205 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1206 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1207 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1208 | if (!latest_dev || |
| 1209 | device->generation > latest_dev->generation) |
| 1210 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1211 | } |
Anand Jain | 1ed802c | 2020-04-28 23:22:25 +0800 | [diff] [blame] | 1212 | if (fs_devices->open_devices == 0) |
| 1213 | return -EINVAL; |
| 1214 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1215 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1216 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1217 | fs_devices->total_rw_bytes = 0; |
Naohiro Aota | c4a816c | 2020-02-25 12:56:08 +0900 | [diff] [blame] | 1218 | fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR; |
Anand Jain | 1ed802c | 2020-04-28 23:22:25 +0800 | [diff] [blame] | 1219 | |
| 1220 | return 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1221 | } |
| 1222 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1223 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1224 | { |
| 1225 | struct btrfs_device *dev1, *dev2; |
| 1226 | |
| 1227 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1228 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1229 | |
| 1230 | if (dev1->devid < dev2->devid) |
| 1231 | return -1; |
| 1232 | else if (dev1->devid > dev2->devid) |
| 1233 | return 1; |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1237 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1238 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1239 | { |
| 1240 | int ret; |
| 1241 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1242 | lockdep_assert_held(&uuid_mutex); |
Josef Bacik | 18c850f | 2020-07-17 15:12:27 -0400 | [diff] [blame] | 1243 | /* |
| 1244 | * The device_list_mutex cannot be taken here in case opening the |
| 1245 | * underlying device takes further locks like bd_mutex. |
| 1246 | * |
| 1247 | * We also don't need the lock here as this is called during mount and |
| 1248 | * exclusion is provided by uuid_mutex |
| 1249 | */ |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1250 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1251 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1252 | fs_devices->opened++; |
| 1253 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1254 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1255 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1256 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1257 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1258 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1259 | return ret; |
| 1260 | } |
| 1261 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1262 | void btrfs_release_disk_super(struct btrfs_super_block *super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1263 | { |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1264 | struct page *page = virt_to_page(super); |
| 1265 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1266 | put_page(page); |
| 1267 | } |
| 1268 | |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1269 | static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev, |
| 1270 | u64 bytenr) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1271 | { |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1272 | struct btrfs_super_block *disk_super; |
| 1273 | struct page *page; |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1274 | void *p; |
| 1275 | pgoff_t index; |
| 1276 | |
| 1277 | /* make sure our super fits in the device */ |
| 1278 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1279 | return ERR_PTR(-EINVAL); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1280 | |
| 1281 | /* make sure our super fits in the page */ |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1282 | if (sizeof(*disk_super) > PAGE_SIZE) |
| 1283 | return ERR_PTR(-EINVAL); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1284 | |
| 1285 | /* make sure our super doesn't straddle pages on disk */ |
| 1286 | index = bytenr >> PAGE_SHIFT; |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1287 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index) |
| 1288 | return ERR_PTR(-EINVAL); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1289 | |
| 1290 | /* pull in the page with our super */ |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1291 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1292 | |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1293 | if (IS_ERR(page)) |
| 1294 | return ERR_CAST(page); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1295 | |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1296 | p = page_address(page); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1297 | |
| 1298 | /* align our pointer to the offset of the super block */ |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1299 | disk_super = p + offset_in_page(bytenr); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1300 | |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1301 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1302 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1303 | btrfs_release_disk_super(p); |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1304 | return ERR_PTR(-EINVAL); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1305 | } |
| 1306 | |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1307 | if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 1308 | disk_super->label[BTRFS_LABEL_SIZE - 1] = 0; |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1309 | |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1310 | return disk_super; |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Anand Jain | 228a73a | 2019-01-04 13:31:54 +0800 | [diff] [blame] | 1313 | int btrfs_forget_devices(const char *path) |
| 1314 | { |
| 1315 | int ret; |
| 1316 | |
| 1317 | mutex_lock(&uuid_mutex); |
| 1318 | ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| 1319 | mutex_unlock(&uuid_mutex); |
| 1320 | |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1324 | /* |
| 1325 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1326 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1327 | * is read via pagecache |
| 1328 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1329 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1330 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1331 | { |
| 1332 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1333 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1334 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1335 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1336 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1337 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1338 | lockdep_assert_held(&uuid_mutex); |
| 1339 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1340 | /* |
| 1341 | * we would like to check all the supers, but that would make |
| 1342 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1343 | * So, we need to add a special mount option to scan for |
| 1344 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1345 | */ |
| 1346 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1347 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1348 | |
| 1349 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1350 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1351 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1352 | |
Nikolay Borisov | b335eab | 2020-04-15 15:53:46 +0300 | [diff] [blame] | 1353 | disk_super = btrfs_read_disk_super(bdev, bytenr); |
| 1354 | if (IS_ERR(disk_super)) { |
| 1355 | device = ERR_CAST(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1356 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1357 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1358 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1359 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1360 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1361 | if (new_device_added) |
| 1362 | btrfs_free_stale_devices(path, device); |
| 1363 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1364 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 1365 | btrfs_release_disk_super(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1366 | |
| 1367 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1368 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1369 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1370 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1371 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1372 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1373 | /* |
| 1374 | * Try to find a chunk that intersects [start, start + len] range and when one |
| 1375 | * such is found, record the end of it in *start |
| 1376 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1377 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| 1378 | u64 len) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1379 | { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1380 | u64 physical_start, physical_end; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1381 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1382 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1383 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1384 | if (!find_first_extent_bit(&device->alloc_state, *start, |
| 1385 | &physical_start, &physical_end, |
| 1386 | CHUNK_ALLOCATED, NULL)) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1387 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1388 | if (in_range(physical_start, *start, len) || |
| 1389 | in_range(*start, physical_start, |
| 1390 | physical_end - physical_start)) { |
| 1391 | *start = physical_end + 1; |
| 1392 | return true; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1393 | } |
| 1394 | } |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1395 | return false; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1396 | } |
| 1397 | |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1398 | static u64 dev_extent_search_start(struct btrfs_device *device, u64 start) |
| 1399 | { |
| 1400 | switch (device->fs_devices->chunk_alloc_policy) { |
| 1401 | case BTRFS_CHUNK_ALLOC_REGULAR: |
| 1402 | /* |
| 1403 | * We don't want to overwrite the superblock on the drive nor |
| 1404 | * any area used by the boot loader (grub for example), so we |
| 1405 | * make sure to start at an offset of at least 1MB. |
| 1406 | */ |
| 1407 | return max_t(u64, start, SZ_1M); |
| 1408 | default: |
| 1409 | BUG(); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * dev_extent_hole_check - check if specified hole is suitable for allocation |
| 1415 | * @device: the device which we have the hole |
| 1416 | * @hole_start: starting position of the hole |
| 1417 | * @hole_size: the size of the hole |
| 1418 | * @num_bytes: the size of the free space that we need |
| 1419 | * |
| 1420 | * This function may modify @hole_start and @hole_end to reflect the suitable |
| 1421 | * position for allocation. Returns 1 if hole position is updated, 0 otherwise. |
| 1422 | */ |
| 1423 | static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start, |
| 1424 | u64 *hole_size, u64 num_bytes) |
| 1425 | { |
| 1426 | bool changed = false; |
| 1427 | u64 hole_end = *hole_start + *hole_size; |
| 1428 | |
| 1429 | /* |
| 1430 | * Check before we set max_hole_start, otherwise we could end up |
| 1431 | * sending back this offset anyway. |
| 1432 | */ |
| 1433 | if (contains_pending_extent(device, hole_start, *hole_size)) { |
| 1434 | if (hole_end >= *hole_start) |
| 1435 | *hole_size = hole_end - *hole_start; |
| 1436 | else |
| 1437 | *hole_size = 0; |
| 1438 | changed = true; |
| 1439 | } |
| 1440 | |
| 1441 | switch (device->fs_devices->chunk_alloc_policy) { |
| 1442 | case BTRFS_CHUNK_ALLOC_REGULAR: |
| 1443 | /* No extra check */ |
| 1444 | break; |
| 1445 | default: |
| 1446 | BUG(); |
| 1447 | } |
| 1448 | |
| 1449 | return changed; |
| 1450 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1451 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1452 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1453 | * find_free_dev_extent_start - find free space in the specified device |
| 1454 | * @device: the device which we search the free space in |
| 1455 | * @num_bytes: the size of the free space that we need |
| 1456 | * @search_start: the position from which to begin the search |
| 1457 | * @start: store the start of the free space. |
| 1458 | * @len: the size of the free space. that we find, or the size |
| 1459 | * 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] | 1460 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1461 | * this uses a pretty simple search, the expectation is that it is |
| 1462 | * called very infrequently and that a given device has a small number |
| 1463 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1464 | * |
| 1465 | * @start is used to store the start of the free space if we find. But if we |
| 1466 | * don't find suitable free space, it will be used to store the start position |
| 1467 | * of the max free space. |
| 1468 | * |
| 1469 | * @len is used to store the size of the free space that we find. |
| 1470 | * But if we don't find suitable free space, it is used to store the size of |
| 1471 | * the max free space. |
Qu Wenruo | 135da97 | 2019-07-19 14:51:42 +0800 | [diff] [blame] | 1472 | * |
| 1473 | * NOTE: This function will search *commit* root of device tree, and does extra |
| 1474 | * check to ensure dev extents are not double allocated. |
| 1475 | * This makes the function safe to allocate dev extents but may not report |
| 1476 | * correct usable device space, as device extent freed in current transaction |
| 1477 | * is not reported as avaiable. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1478 | */ |
Qu Wenruo | 9e3246a | 2019-07-19 14:51:41 +0800 | [diff] [blame] | 1479 | static int find_free_dev_extent_start(struct btrfs_device *device, |
| 1480 | u64 num_bytes, u64 search_start, u64 *start, |
| 1481 | u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1482 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1483 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1484 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1485 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1486 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1487 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1488 | u64 hole_size; |
| 1489 | u64 max_hole_start; |
| 1490 | u64 max_hole_size; |
| 1491 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1492 | u64 search_end = device->total_bytes; |
| 1493 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1494 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1495 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1496 | |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1497 | search_start = dev_extent_search_start(device, search_start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1498 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1499 | path = btrfs_alloc_path(); |
| 1500 | if (!path) |
| 1501 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1502 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1503 | max_hole_start = search_start; |
| 1504 | max_hole_size = 0; |
| 1505 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1506 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1507 | if (search_start >= search_end || |
| 1508 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1509 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1510 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1513 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1514 | path->search_commit_root = 1; |
| 1515 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1516 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1517 | key.objectid = device->devid; |
| 1518 | key.offset = search_start; |
| 1519 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1520 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1521 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1522 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1523 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1524 | if (ret > 0) { |
| 1525 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1526 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1527 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1528 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1529 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1530 | while (1) { |
| 1531 | l = path->nodes[0]; |
| 1532 | slot = path->slots[0]; |
| 1533 | if (slot >= btrfs_header_nritems(l)) { |
| 1534 | ret = btrfs_next_leaf(root, path); |
| 1535 | if (ret == 0) |
| 1536 | continue; |
| 1537 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1538 | goto out; |
| 1539 | |
| 1540 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1541 | } |
| 1542 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1543 | |
| 1544 | if (key.objectid < device->devid) |
| 1545 | goto next; |
| 1546 | |
| 1547 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1548 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1549 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1550 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1551 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1552 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1553 | if (key.offset > search_start) { |
| 1554 | hole_size = key.offset - search_start; |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1555 | dev_extent_hole_check(device, &search_start, &hole_size, |
| 1556 | num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1557 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1558 | if (hole_size > max_hole_size) { |
| 1559 | max_hole_start = search_start; |
| 1560 | max_hole_size = hole_size; |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * If this free space is greater than which we need, |
| 1565 | * it must be the max free space that we have found |
| 1566 | * until now, so max_hole_start must point to the start |
| 1567 | * of this free space and the length of this free space |
| 1568 | * is stored in max_hole_size. Thus, we return |
| 1569 | * max_hole_start and max_hole_size and go back to the |
| 1570 | * caller. |
| 1571 | */ |
| 1572 | if (hole_size >= num_bytes) { |
| 1573 | ret = 0; |
| 1574 | goto out; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1578 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1579 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1580 | dev_extent); |
| 1581 | if (extent_end > search_start) |
| 1582 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1583 | next: |
| 1584 | path->slots[0]++; |
| 1585 | cond_resched(); |
| 1586 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1587 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1588 | /* |
| 1589 | * At this point, search_start should be the end of |
| 1590 | * allocated dev extents, and when shrinking the device, |
| 1591 | * search_end may be smaller than search_start. |
| 1592 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1593 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1594 | hole_size = search_end - search_start; |
Naohiro Aota | 3b4ffa4 | 2020-02-25 12:56:09 +0900 | [diff] [blame] | 1595 | if (dev_extent_hole_check(device, &search_start, &hole_size, |
| 1596 | num_bytes)) { |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1597 | btrfs_release_path(path); |
| 1598 | goto again; |
| 1599 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1600 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1601 | if (hole_size > max_hole_size) { |
| 1602 | max_hole_start = search_start; |
| 1603 | max_hole_size = hole_size; |
| 1604 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1605 | } |
| 1606 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1607 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1608 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1609 | ret = -ENOSPC; |
| 1610 | else |
| 1611 | ret = 0; |
| 1612 | |
| 1613 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1614 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1615 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1616 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1617 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1618 | return ret; |
| 1619 | } |
| 1620 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1621 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1622 | u64 *start, u64 *len) |
| 1623 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1624 | /* FIXME use last free of some kind */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1625 | return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1628 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1629 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1630 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1631 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1632 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1633 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1634 | int ret; |
| 1635 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1636 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1637 | struct btrfs_key found_key; |
| 1638 | struct extent_buffer *leaf = NULL; |
| 1639 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1640 | |
| 1641 | path = btrfs_alloc_path(); |
| 1642 | if (!path) |
| 1643 | return -ENOMEM; |
| 1644 | |
| 1645 | key.objectid = device->devid; |
| 1646 | key.offset = start; |
| 1647 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1648 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1649 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1650 | if (ret > 0) { |
| 1651 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1652 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1653 | if (ret) |
| 1654 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1655 | leaf = path->nodes[0]; |
| 1656 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1657 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1658 | struct btrfs_dev_extent); |
| 1659 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1660 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1661 | key = found_key; |
| 1662 | btrfs_release_path(path); |
| 1663 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1664 | } else if (ret == 0) { |
| 1665 | leaf = path->nodes[0]; |
| 1666 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1667 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1668 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1669 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1670 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1671 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1672 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1673 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1674 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1675 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1676 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1677 | btrfs_handle_fs_error(fs_info, ret, |
| 1678 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1679 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1680 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1681 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1682 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1683 | btrfs_free_path(path); |
| 1684 | return ret; |
| 1685 | } |
| 1686 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1687 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1688 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1689 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1690 | { |
| 1691 | int ret; |
| 1692 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1693 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1694 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1695 | struct btrfs_dev_extent *extent; |
| 1696 | struct extent_buffer *leaf; |
| 1697 | struct btrfs_key key; |
| 1698 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1699 | 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] | 1700 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1701 | path = btrfs_alloc_path(); |
| 1702 | if (!path) |
| 1703 | return -ENOMEM; |
| 1704 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1705 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1706 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1707 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1708 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1709 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1710 | if (ret) |
| 1711 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1712 | |
| 1713 | leaf = path->nodes[0]; |
| 1714 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1715 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1716 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1717 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1718 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1719 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1720 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1721 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1722 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1723 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1724 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1725 | btrfs_free_path(path); |
| 1726 | return ret; |
| 1727 | } |
| 1728 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1729 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1730 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1731 | struct extent_map_tree *em_tree; |
| 1732 | struct extent_map *em; |
| 1733 | struct rb_node *n; |
| 1734 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1735 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 1736 | em_tree = &fs_info->mapping_tree; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1737 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1738 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1739 | if (n) { |
| 1740 | em = rb_entry(n, struct extent_map, rb_node); |
| 1741 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1742 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1743 | read_unlock(&em_tree->lock); |
| 1744 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1745 | return ret; |
| 1746 | } |
| 1747 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1748 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1749 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1750 | { |
| 1751 | int ret; |
| 1752 | struct btrfs_key key; |
| 1753 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1754 | struct btrfs_path *path; |
| 1755 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1756 | path = btrfs_alloc_path(); |
| 1757 | if (!path) |
| 1758 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1759 | |
| 1760 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1761 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1762 | key.offset = (u64)-1; |
| 1763 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1764 | 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] | 1765 | if (ret < 0) |
| 1766 | goto error; |
| 1767 | |
Anand Jain | a06dee4d | 2019-08-27 15:40:44 +0800 | [diff] [blame] | 1768 | if (ret == 0) { |
| 1769 | /* Corruption */ |
| 1770 | btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); |
| 1771 | ret = -EUCLEAN; |
| 1772 | goto error; |
| 1773 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1774 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1775 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1776 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1777 | BTRFS_DEV_ITEM_KEY); |
| 1778 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1779 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1780 | } else { |
| 1781 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1782 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1783 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1784 | } |
| 1785 | ret = 0; |
| 1786 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1787 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1788 | return ret; |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * the device information is stored in the chunk root |
| 1793 | * the btrfs_device struct should be fully filled in |
| 1794 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1795 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1796 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1797 | { |
| 1798 | int ret; |
| 1799 | struct btrfs_path *path; |
| 1800 | struct btrfs_dev_item *dev_item; |
| 1801 | struct extent_buffer *leaf; |
| 1802 | struct btrfs_key key; |
| 1803 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1804 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1805 | path = btrfs_alloc_path(); |
| 1806 | if (!path) |
| 1807 | return -ENOMEM; |
| 1808 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1809 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1810 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1811 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1812 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1813 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1814 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1815 | if (ret) |
| 1816 | goto out; |
| 1817 | |
| 1818 | leaf = path->nodes[0]; |
| 1819 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1820 | |
| 1821 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1822 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1823 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1824 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1825 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1826 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1827 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1828 | btrfs_device_get_disk_total_bytes(device)); |
| 1829 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1830 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1831 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1832 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1833 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1834 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1835 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1836 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1837 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1838 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1839 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1840 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1841 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1842 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1843 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1844 | out: |
| 1845 | btrfs_free_path(path); |
| 1846 | return ret; |
| 1847 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1848 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1849 | /* |
| 1850 | * Function to update ctime/mtime for a given device path. |
| 1851 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1852 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1853 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1854 | { |
| 1855 | struct file *filp; |
| 1856 | |
| 1857 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1858 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1859 | return; |
| 1860 | file_update_time(filp); |
| 1861 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1862 | } |
| 1863 | |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1864 | static int btrfs_rm_dev_item(struct btrfs_device *device) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1865 | { |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1866 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1867 | int ret; |
| 1868 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1869 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1870 | struct btrfs_trans_handle *trans; |
| 1871 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1872 | path = btrfs_alloc_path(); |
| 1873 | if (!path) |
| 1874 | return -ENOMEM; |
| 1875 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1876 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1877 | if (IS_ERR(trans)) { |
| 1878 | btrfs_free_path(path); |
| 1879 | return PTR_ERR(trans); |
| 1880 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1881 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1882 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1883 | key.offset = device->devid; |
| 1884 | |
| 1885 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1886 | if (ret) { |
| 1887 | if (ret > 0) |
| 1888 | ret = -ENOENT; |
| 1889 | btrfs_abort_transaction(trans, ret); |
| 1890 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1891 | goto out; |
| 1892 | } |
| 1893 | |
| 1894 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1895 | if (ret) { |
| 1896 | btrfs_abort_transaction(trans, ret); |
| 1897 | btrfs_end_transaction(trans); |
| 1898 | } |
| 1899 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1900 | out: |
| 1901 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1902 | if (!ret) |
| 1903 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1904 | return ret; |
| 1905 | } |
| 1906 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1907 | /* |
| 1908 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 1909 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 1910 | * replace. |
| 1911 | */ |
| 1912 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 1913 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1914 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1915 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1916 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1917 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1918 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1919 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1920 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1921 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1922 | all_avail = fs_info->avail_data_alloc_bits | |
| 1923 | fs_info->avail_system_alloc_bits | |
| 1924 | fs_info->avail_metadata_alloc_bits; |
| 1925 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1926 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1927 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 1928 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1929 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1930 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1931 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 1932 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1933 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1934 | if (ret) |
| 1935 | return ret; |
| 1936 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1940 | } |
| 1941 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1942 | static struct btrfs_device * btrfs_find_next_active_device( |
| 1943 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1944 | { |
| 1945 | struct btrfs_device *next_device; |
| 1946 | |
| 1947 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 1948 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1949 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 1950 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1951 | return next_device; |
| 1952 | } |
| 1953 | |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | |
| 1957 | /* |
| 1958 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 1959 | * and replace it with the provided or the next active device, in the context |
| 1960 | * where this function called, there should be always be another device (or |
| 1961 | * this_dev) which is active. |
| 1962 | */ |
David Sterba | b105e92 | 2019-10-01 19:57:35 +0200 | [diff] [blame] | 1963 | void __cold btrfs_assign_next_active_device(struct btrfs_device *device, |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1964 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1965 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1966 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1967 | struct btrfs_device *next_device; |
| 1968 | |
| 1969 | if (this_dev) |
| 1970 | next_device = this_dev; |
| 1971 | else |
| 1972 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 1973 | device); |
| 1974 | ASSERT(next_device); |
| 1975 | |
| 1976 | if (fs_info->sb->s_bdev && |
| 1977 | (fs_info->sb->s_bdev == device->bdev)) |
| 1978 | fs_info->sb->s_bdev = next_device->bdev; |
| 1979 | |
| 1980 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 1981 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1982 | } |
| 1983 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1984 | /* |
| 1985 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 1986 | * currently replaced. |
| 1987 | */ |
| 1988 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 1989 | { |
| 1990 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 1991 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1992 | down_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1993 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 1994 | ASSERT(num_devices > 1); |
| 1995 | num_devices--; |
| 1996 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1997 | up_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1998 | |
| 1999 | return num_devices; |
| 2000 | } |
| 2001 | |
Josef Bacik | 313b085 | 2020-08-20 11:18:26 -0400 | [diff] [blame^] | 2002 | void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info, |
| 2003 | struct block_device *bdev, |
| 2004 | const char *device_path) |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2005 | { |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2006 | struct btrfs_super_block *disk_super; |
| 2007 | int copy_num; |
| 2008 | |
| 2009 | if (!bdev) |
| 2010 | return; |
| 2011 | |
| 2012 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) { |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2013 | struct page *page; |
| 2014 | int ret; |
| 2015 | |
| 2016 | disk_super = btrfs_read_dev_one_super(bdev, copy_num); |
| 2017 | if (IS_ERR(disk_super)) |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2018 | continue; |
| 2019 | |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2020 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2021 | |
| 2022 | page = virt_to_page(disk_super); |
| 2023 | set_page_dirty(page); |
| 2024 | lock_page(page); |
| 2025 | /* write_on_page() unlocks the page */ |
| 2026 | ret = write_one_page(page); |
| 2027 | if (ret) |
| 2028 | btrfs_warn(fs_info, |
| 2029 | "error clearing superblock number %d (%d)", |
| 2030 | copy_num, ret); |
| 2031 | btrfs_release_disk_super(disk_super); |
| 2032 | |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | /* Notify udev that device has changed */ |
| 2036 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 2037 | |
| 2038 | /* Update ctime/mtime for device path for libblkid */ |
| 2039 | update_dev_time(device_path); |
| 2040 | } |
| 2041 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2042 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 2043 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2044 | { |
| 2045 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2046 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2047 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2048 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2049 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2050 | |
| 2051 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2052 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 2053 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2054 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2055 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 2056 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2057 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2058 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2059 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 2060 | |
| 2061 | if (IS_ERR(device)) { |
| 2062 | if (PTR_ERR(device) == -ENOENT && |
| 2063 | strcmp(device_path, "missing") == 0) |
| 2064 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 2065 | else |
| 2066 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2067 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2068 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2069 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 2070 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 2071 | btrfs_warn_in_rcu(fs_info, |
| 2072 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 2073 | rcu_str_deref(device->name), device->devid); |
| 2074 | ret = -ETXTBSY; |
| 2075 | goto out; |
| 2076 | } |
| 2077 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2078 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2079 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2080 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2081 | } |
| 2082 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2083 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 2084 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2085 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2086 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2087 | } |
| 2088 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2089 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2090 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2091 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2092 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2093 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2094 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2095 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2096 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2097 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2098 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2099 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2100 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2101 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2102 | /* |
| 2103 | * TODO: the superblock still includes this device in its num_devices |
| 2104 | * counter although write_all_supers() is not locked out. This |
| 2105 | * could give a filesystem state which requires a degraded mount. |
| 2106 | */ |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 2107 | ret = btrfs_rm_dev_item(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2108 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2109 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2110 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2111 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
David Sterba | 163e97e | 2019-03-20 16:32:55 +0100 | [diff] [blame] | 2112 | btrfs_scrub_cancel_dev(device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2113 | |
| 2114 | /* |
| 2115 | * the device list mutex makes sure that we don't change |
| 2116 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2117 | * the device supers. Whoever is writing all supers, should |
| 2118 | * lock the device list mutex before getting the number of |
| 2119 | * devices in the super block (super_copy). Conversely, |
| 2120 | * whoever updates the number of devices in the super block |
| 2121 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2122 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2123 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2124 | /* |
| 2125 | * In normal cases the cur_devices == fs_devices. But in case |
| 2126 | * of deleting a seed device, the cur_devices should point to |
| 2127 | * its own fs_devices listed under the fs_devices->seed. |
| 2128 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2129 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2130 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2131 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2132 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2133 | cur_devices->num_devices--; |
| 2134 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2135 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2136 | if (cur_devices != fs_devices) |
| 2137 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2138 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2139 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2140 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2141 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2142 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2143 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2144 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2145 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2146 | /* remove sysfs entry */ |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2147 | btrfs_sysfs_remove_devices_dir(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2148 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2149 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2150 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2151 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2152 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2153 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2154 | /* |
| 2155 | * at this point, the device is zero sized and detached from |
| 2156 | * the devices list. All that's left is to zero out the old |
| 2157 | * supers and free the device. |
| 2158 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2159 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2160 | btrfs_scratch_superblocks(fs_info, device->bdev, |
| 2161 | device->name->str); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2162 | |
| 2163 | btrfs_close_bdev(device); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2164 | synchronize_rcu(); |
| 2165 | btrfs_free_device(device); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2166 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2167 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2168 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2169 | if (fs_devices->seed == cur_devices) { |
| 2170 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2171 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2172 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2173 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2174 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2175 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2176 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2177 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2178 | } |
| 2179 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2180 | out: |
| 2181 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2182 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2183 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2184 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2185 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2186 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2187 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2188 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2189 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2190 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2191 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2192 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2193 | } |
| 2194 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2195 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2196 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2197 | struct btrfs_fs_devices *fs_devices; |
| 2198 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2199 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2200 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2201 | /* |
| 2202 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2203 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2204 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2205 | * srcdev will have its correct fs_devices in both the cases. |
| 2206 | */ |
| 2207 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2208 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2209 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2210 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2211 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2212 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2213 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2214 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2215 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2216 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2217 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2218 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2219 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2220 | } |
| 2221 | |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2222 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2223 | { |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2224 | struct btrfs_fs_info *fs_info = srcdev->fs_info; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2225 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2226 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2227 | btrfs_close_bdev(srcdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2228 | synchronize_rcu(); |
| 2229 | btrfs_free_device(srcdev); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2230 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2231 | /* if this is no devs we rather delete the fs_devices */ |
| 2232 | if (!fs_devices->num_devices) { |
| 2233 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2234 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2235 | /* |
| 2236 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2237 | * seed. In case of a seed device being replaced, the replace |
| 2238 | * target added to the sprout FS, so there will be no more |
| 2239 | * device left under the seed FS. |
| 2240 | */ |
| 2241 | ASSERT(fs_devices->seeding); |
| 2242 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2243 | tmp_fs_devices = fs_info->fs_devices; |
| 2244 | while (tmp_fs_devices) { |
| 2245 | if (tmp_fs_devices->seed == fs_devices) { |
| 2246 | tmp_fs_devices->seed = fs_devices->seed; |
| 2247 | break; |
| 2248 | } |
| 2249 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2250 | } |
| 2251 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2252 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2253 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2254 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2257 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2258 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2259 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2260 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2261 | mutex_lock(&fs_devices->device_list_mutex); |
| 2262 | |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2263 | btrfs_sysfs_remove_devices_dir(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2264 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2265 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2266 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2267 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2268 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2269 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2270 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2271 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2272 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2273 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2274 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2275 | |
| 2276 | /* |
| 2277 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2278 | * may lead to a call to btrfs_show_devname() which will try |
| 2279 | * to hold device_list_mutex. And here this device |
| 2280 | * is already out of device list, so we don't have to hold |
| 2281 | * the device_list_mutex lock. |
| 2282 | */ |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2283 | btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev, |
| 2284 | tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2285 | |
| 2286 | btrfs_close_bdev(tgtdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2287 | synchronize_rcu(); |
| 2288 | btrfs_free_device(tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2289 | } |
| 2290 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2291 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2292 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2293 | { |
| 2294 | int ret = 0; |
| 2295 | struct btrfs_super_block *disk_super; |
| 2296 | u64 devid; |
| 2297 | u8 *dev_uuid; |
| 2298 | struct block_device *bdev; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2299 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2300 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2301 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2302 | fs_info->bdev_holder, 0, &bdev, &disk_super); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2303 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2304 | return ERR_PTR(ret); |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2305 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2306 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2307 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2308 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2309 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2310 | disk_super->metadata_uuid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2311 | else |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2312 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2313 | disk_super->fsid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2314 | |
Johannes Thumshirn | 8f32380 | 2020-02-14 00:24:32 +0900 | [diff] [blame] | 2315 | btrfs_release_disk_super(disk_super); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2316 | if (!device) |
| 2317 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2318 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2319 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2320 | } |
| 2321 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2322 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2323 | * Lookup a device given by device id, or the path if the id is 0. |
| 2324 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2325 | struct btrfs_device *btrfs_find_device_by_devspec( |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2326 | struct btrfs_fs_info *fs_info, u64 devid, |
| 2327 | const char *device_path) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2328 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2329 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2330 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2331 | if (devid) { |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2332 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2333 | NULL, true); |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2334 | if (!device) |
| 2335 | return ERR_PTR(-ENOENT); |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2336 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2337 | } |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2338 | |
| 2339 | if (!device_path || !device_path[0]) |
| 2340 | return ERR_PTR(-EINVAL); |
| 2341 | |
| 2342 | if (strcmp(device_path, "missing") == 0) { |
| 2343 | /* Find first missing device */ |
| 2344 | list_for_each_entry(device, &fs_info->fs_devices->devices, |
| 2345 | dev_list) { |
| 2346 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2347 | &device->dev_state) && !device->bdev) |
| 2348 | return device; |
| 2349 | } |
| 2350 | return ERR_PTR(-ENOENT); |
| 2351 | } |
| 2352 | |
| 2353 | return btrfs_find_device_by_path(fs_info, device_path); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2354 | } |
| 2355 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2356 | /* |
| 2357 | * does all the dirty work required for changing file system's UUID. |
| 2358 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2359 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2360 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2361 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2362 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2363 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2364 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2365 | struct btrfs_device *device; |
| 2366 | u64 super_flags; |
| 2367 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2368 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2369 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2370 | return -EINVAL; |
| 2371 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2372 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2373 | if (IS_ERR(seed_devices)) |
| 2374 | return PTR_ERR(seed_devices); |
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 | old_devices = clone_fs_devices(fs_devices); |
| 2377 | if (IS_ERR(old_devices)) { |
| 2378 | kfree(seed_devices); |
| 2379 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2380 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2381 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2382 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2383 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2384 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2385 | seed_devices->opened = 1; |
| 2386 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2387 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2388 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2389 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2390 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2391 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2392 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2393 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2394 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2395 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2396 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2397 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2398 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2399 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 2400 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2401 | fs_devices->num_devices = 0; |
| 2402 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2403 | fs_devices->missing_devices = 0; |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2404 | fs_devices->rotating = false; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2405 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2406 | |
| 2407 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2408 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2409 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2410 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2411 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2412 | super_flags = btrfs_super_flags(disk_super) & |
| 2413 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2414 | btrfs_set_super_flags(disk_super, super_flags); |
| 2415 | |
| 2416 | return 0; |
| 2417 | } |
| 2418 | |
| 2419 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2420 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2421 | */ |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2422 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2423 | { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2424 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2425 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2426 | struct btrfs_path *path; |
| 2427 | struct extent_buffer *leaf; |
| 2428 | struct btrfs_dev_item *dev_item; |
| 2429 | struct btrfs_device *device; |
| 2430 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2431 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2432 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2433 | u64 devid; |
| 2434 | int ret; |
| 2435 | |
| 2436 | path = btrfs_alloc_path(); |
| 2437 | if (!path) |
| 2438 | return -ENOMEM; |
| 2439 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2440 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2441 | key.offset = 0; |
| 2442 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2443 | |
| 2444 | while (1) { |
| 2445 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2446 | if (ret < 0) |
| 2447 | goto error; |
| 2448 | |
| 2449 | leaf = path->nodes[0]; |
| 2450 | next_slot: |
| 2451 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2452 | ret = btrfs_next_leaf(root, path); |
| 2453 | if (ret > 0) |
| 2454 | break; |
| 2455 | if (ret < 0) |
| 2456 | goto error; |
| 2457 | leaf = path->nodes[0]; |
| 2458 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2459 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2460 | continue; |
| 2461 | } |
| 2462 | |
| 2463 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2464 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2465 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2466 | break; |
| 2467 | |
| 2468 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2469 | struct btrfs_dev_item); |
| 2470 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2471 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2472 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2473 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2474 | BTRFS_FSID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2475 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2476 | fs_uuid, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2477 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2478 | |
| 2479 | if (device->fs_devices->seeding) { |
| 2480 | btrfs_set_device_generation(leaf, dev_item, |
| 2481 | device->generation); |
| 2482 | btrfs_mark_buffer_dirty(leaf); |
| 2483 | } |
| 2484 | |
| 2485 | path->slots[0]++; |
| 2486 | goto next_slot; |
| 2487 | } |
| 2488 | ret = 0; |
| 2489 | error: |
| 2490 | btrfs_free_path(path); |
| 2491 | return ret; |
| 2492 | } |
| 2493 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2494 | 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] | 2495 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2496 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2497 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2498 | struct btrfs_trans_handle *trans; |
| 2499 | struct btrfs_device *device; |
| 2500 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2501 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2502 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2503 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2504 | u64 orig_super_total_bytes; |
| 2505 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2506 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2507 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2508 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2509 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2510 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2511 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2512 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2513 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2514 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2515 | if (IS_ERR(bdev)) |
| 2516 | return PTR_ERR(bdev); |
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 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2519 | seeding_dev = 1; |
| 2520 | down_write(&sb->s_umount); |
| 2521 | mutex_lock(&uuid_mutex); |
| 2522 | } |
| 2523 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2524 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2525 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2526 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2527 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2528 | if (device->bdev == bdev) { |
| 2529 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2530 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2531 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2532 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2533 | } |
| 2534 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2535 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2536 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2537 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2538 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2539 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2540 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2541 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2542 | } |
| 2543 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2544 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2545 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2546 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2547 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2548 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2549 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2550 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2551 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2552 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2553 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2554 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2557 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2558 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2559 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2560 | device->io_width = fs_info->sectorsize; |
| 2561 | device->io_align = fs_info->sectorsize; |
| 2562 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2563 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2564 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2565 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2566 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2567 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2568 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2569 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2570 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2571 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2572 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2573 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2574 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2575 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2576 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2577 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2578 | if (ret) { |
| 2579 | btrfs_abort_transaction(trans, ret); |
| 2580 | goto error_trans; |
| 2581 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2582 | } |
| 2583 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2584 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2585 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2586 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2587 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2588 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2589 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2590 | fs_devices->num_devices++; |
| 2591 | fs_devices->open_devices++; |
| 2592 | fs_devices->rw_devices++; |
| 2593 | fs_devices->total_devices++; |
| 2594 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2595 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2596 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2597 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2598 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2599 | fs_devices->rotating = true; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2600 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2601 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2602 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2603 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2604 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2605 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2606 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2607 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2608 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2609 | |
| 2610 | /* add sysfs device entry */ |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2611 | btrfs_sysfs_add_devices_dir(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2612 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2613 | /* |
| 2614 | * we've got more storage, clear any full flags on the space |
| 2615 | * infos |
| 2616 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2617 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2618 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2619 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2620 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2621 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2622 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2623 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 2624 | ret = init_first_rw_device(trans); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2625 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2626 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2627 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2628 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2629 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2630 | } |
| 2631 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2632 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2633 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2634 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2635 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | if (seeding_dev) { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2639 | ret = btrfs_finish_sprout(trans); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2640 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2641 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2642 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2643 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2644 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 2645 | btrfs_sysfs_update_sprout_fsid(fs_devices, |
| 2646 | fs_info->fs_devices->fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2647 | } |
| 2648 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2649 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2650 | |
| 2651 | if (seeding_dev) { |
| 2652 | mutex_unlock(&uuid_mutex); |
| 2653 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2654 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2655 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2656 | if (ret) /* transaction commit */ |
| 2657 | return ret; |
| 2658 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2659 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2660 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2661 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2662 | "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] | 2663 | trans = btrfs_attach_transaction(root); |
| 2664 | if (IS_ERR(trans)) { |
| 2665 | if (PTR_ERR(trans) == -ENOENT) |
| 2666 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2667 | ret = PTR_ERR(trans); |
| 2668 | trans = NULL; |
| 2669 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2670 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2671 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2672 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2673 | |
Anand Jain | 7f551d9 | 2020-05-05 02:58:26 +0800 | [diff] [blame] | 2674 | /* |
| 2675 | * Now that we have written a new super block to this device, check all |
| 2676 | * other fs_devices list if device_path alienates any other scanned |
| 2677 | * device. |
| 2678 | * We can ignore the return value as it typically returns -EINVAL and |
| 2679 | * only succeeds if the device was an alien. |
| 2680 | */ |
| 2681 | btrfs_forget_devices(device_path); |
| 2682 | |
| 2683 | /* Update ctime/mtime for blkid or udev */ |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2684 | update_dev_time(device_path); |
Anand Jain | 7f551d9 | 2020-05-05 02:58:26 +0800 | [diff] [blame] | 2685 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2686 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2687 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2688 | error_sysfs: |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2689 | btrfs_sysfs_remove_devices_dir(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2690 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2691 | mutex_lock(&fs_info->chunk_mutex); |
| 2692 | list_del_rcu(&device->dev_list); |
| 2693 | list_del(&device->dev_alloc_list); |
| 2694 | fs_info->fs_devices->num_devices--; |
| 2695 | fs_info->fs_devices->open_devices--; |
| 2696 | fs_info->fs_devices->rw_devices--; |
| 2697 | fs_info->fs_devices->total_devices--; |
| 2698 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2699 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2700 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2701 | orig_super_total_bytes); |
| 2702 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2703 | orig_super_num_devices); |
| 2704 | mutex_unlock(&fs_info->chunk_mutex); |
| 2705 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2706 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2707 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2708 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2709 | if (trans) |
| 2710 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2711 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2712 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2713 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2714 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2715 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2716 | mutex_unlock(&uuid_mutex); |
| 2717 | up_write(&sb->s_umount); |
| 2718 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2719 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2720 | } |
| 2721 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2722 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2723 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2724 | { |
| 2725 | int ret; |
| 2726 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2727 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2728 | struct btrfs_dev_item *dev_item; |
| 2729 | struct extent_buffer *leaf; |
| 2730 | struct btrfs_key key; |
| 2731 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2732 | path = btrfs_alloc_path(); |
| 2733 | if (!path) |
| 2734 | return -ENOMEM; |
| 2735 | |
| 2736 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2737 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2738 | key.offset = device->devid; |
| 2739 | |
| 2740 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2741 | if (ret < 0) |
| 2742 | goto out; |
| 2743 | |
| 2744 | if (ret > 0) { |
| 2745 | ret = -ENOENT; |
| 2746 | goto out; |
| 2747 | } |
| 2748 | |
| 2749 | leaf = path->nodes[0]; |
| 2750 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2751 | |
| 2752 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2753 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2754 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2755 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2756 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2757 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2758 | btrfs_device_get_disk_total_bytes(device)); |
| 2759 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2760 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2761 | btrfs_mark_buffer_dirty(leaf); |
| 2762 | |
| 2763 | out: |
| 2764 | btrfs_free_path(path); |
| 2765 | return ret; |
| 2766 | } |
| 2767 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2768 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2769 | struct btrfs_device *device, u64 new_size) |
| 2770 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2771 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2772 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2773 | u64 old_total; |
| 2774 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2775 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2776 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2777 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2778 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2779 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2780 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2781 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2782 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2783 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2784 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2785 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2786 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2787 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2788 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2789 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2790 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2791 | btrfs_set_super_total_bytes(super_copy, |
| 2792 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2793 | device->fs_devices->total_rw_bytes += diff; |
| 2794 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2795 | btrfs_device_set_total_bytes(device, new_size); |
| 2796 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2797 | btrfs_clear_space_info_full(device->fs_info); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 2798 | if (list_empty(&device->post_commit_list)) |
| 2799 | list_add_tail(&device->post_commit_list, |
| 2800 | &trans->transaction->dev_update_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2801 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2802 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2803 | return btrfs_update_device(trans, device); |
| 2804 | } |
| 2805 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2806 | 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] | 2807 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2808 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2809 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2810 | int ret; |
| 2811 | struct btrfs_path *path; |
| 2812 | struct btrfs_key key; |
| 2813 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2814 | path = btrfs_alloc_path(); |
| 2815 | if (!path) |
| 2816 | return -ENOMEM; |
| 2817 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2818 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2819 | key.offset = chunk_offset; |
| 2820 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2821 | |
| 2822 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2823 | if (ret < 0) |
| 2824 | goto out; |
| 2825 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2826 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2827 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2828 | ret = -ENOENT; |
| 2829 | goto out; |
| 2830 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2831 | |
| 2832 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2833 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2834 | btrfs_handle_fs_error(fs_info, ret, |
| 2835 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2836 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2837 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2838 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2839 | } |
| 2840 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2841 | 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] | 2842 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2843 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2844 | struct btrfs_disk_key *disk_key; |
| 2845 | struct btrfs_chunk *chunk; |
| 2846 | u8 *ptr; |
| 2847 | int ret = 0; |
| 2848 | u32 num_stripes; |
| 2849 | u32 array_size; |
| 2850 | u32 len = 0; |
| 2851 | u32 cur; |
| 2852 | struct btrfs_key key; |
| 2853 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2854 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2855 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2856 | |
| 2857 | ptr = super_copy->sys_chunk_array; |
| 2858 | cur = 0; |
| 2859 | |
| 2860 | while (cur < array_size) { |
| 2861 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2862 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2863 | |
| 2864 | len = sizeof(*disk_key); |
| 2865 | |
| 2866 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2867 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2868 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2869 | len += btrfs_chunk_item_size(num_stripes); |
| 2870 | } else { |
| 2871 | ret = -EIO; |
| 2872 | break; |
| 2873 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2874 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2875 | key.offset == chunk_offset) { |
| 2876 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2877 | array_size -= len; |
| 2878 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2879 | } else { |
| 2880 | ptr += len; |
| 2881 | cur += len; |
| 2882 | } |
| 2883 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2884 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2885 | return ret; |
| 2886 | } |
| 2887 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2888 | /* |
| 2889 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2890 | * @logical: Logical block offset in bytes. |
| 2891 | * @length: Length of extent in bytes. |
| 2892 | * |
| 2893 | * Return: Chunk mapping or ERR_PTR. |
| 2894 | */ |
| 2895 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2896 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2897 | { |
| 2898 | struct extent_map_tree *em_tree; |
| 2899 | struct extent_map *em; |
| 2900 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 2901 | em_tree = &fs_info->mapping_tree; |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2902 | read_lock(&em_tree->lock); |
| 2903 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2904 | read_unlock(&em_tree->lock); |
| 2905 | |
| 2906 | if (!em) { |
| 2907 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2908 | logical, length); |
| 2909 | return ERR_PTR(-EINVAL); |
| 2910 | } |
| 2911 | |
| 2912 | if (em->start > logical || em->start + em->len < logical) { |
| 2913 | btrfs_crit(fs_info, |
| 2914 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2915 | logical, length, em->start, em->start + em->len); |
| 2916 | free_extent_map(em); |
| 2917 | return ERR_PTR(-EINVAL); |
| 2918 | } |
| 2919 | |
| 2920 | /* callers are responsible for dropping em's ref. */ |
| 2921 | return em; |
| 2922 | } |
| 2923 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2924 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2925 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2926 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2927 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2928 | struct map_lookup *map; |
| 2929 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2930 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2931 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2932 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2933 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2934 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2935 | /* |
| 2936 | * 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] | 2937 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2938 | * do anything we still error out. |
| 2939 | */ |
| 2940 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2941 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2942 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2943 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2944 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2945 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2946 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2947 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2948 | /* |
| 2949 | * Take the device list mutex to prevent races with the final phase of |
| 2950 | * a device replace operation that replaces the device object associated |
| 2951 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2952 | */ |
| 2953 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2954 | for (i = 0; i < map->num_stripes; i++) { |
| 2955 | struct btrfs_device *device = map->stripes[i].dev; |
| 2956 | ret = btrfs_free_dev_extent(trans, device, |
| 2957 | map->stripes[i].physical, |
| 2958 | &dev_extent_len); |
| 2959 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2960 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2961 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2962 | goto out; |
| 2963 | } |
| 2964 | |
| 2965 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2966 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2967 | btrfs_device_set_bytes_used(device, |
| 2968 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2969 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2970 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2971 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2972 | } |
| 2973 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 2974 | ret = btrfs_update_device(trans, device); |
| 2975 | if (ret) { |
| 2976 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2977 | btrfs_abort_transaction(trans, ret); |
| 2978 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2979 | } |
| 2980 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2981 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2982 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2983 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2984 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2985 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2986 | goto out; |
| 2987 | } |
| 2988 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2989 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2990 | |
| 2991 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2992 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2993 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2994 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2995 | goto out; |
| 2996 | } |
| 2997 | } |
| 2998 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 2999 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3000 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3001 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3002 | goto out; |
| 3003 | } |
| 3004 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 3005 | out: |
| 3006 | /* once for us */ |
| 3007 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3008 | return ret; |
| 3009 | } |
| 3010 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3011 | 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] | 3012 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3013 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3014 | struct btrfs_trans_handle *trans; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3015 | struct btrfs_block_group *block_group; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3016 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3017 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3018 | /* |
| 3019 | * Prevent races with automatic removal of unused block groups. |
| 3020 | * After we relocate and before we remove the chunk with offset |
| 3021 | * chunk_offset, automatic removal of the block group can kick in, |
| 3022 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 3023 | * |
| 3024 | * Make sure to acquire this mutex before doing a tree search (dev |
| 3025 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 3026 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 3027 | * we release the path used to search the chunk/dev tree and before |
| 3028 | * the current task acquires this mutex and calls us. |
| 3029 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 3030 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3031 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3032 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3033 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3034 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3035 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3036 | if (ret) |
| 3037 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3038 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 3039 | block_group = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3040 | if (!block_group) |
| 3041 | return -ENOENT; |
| 3042 | btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); |
| 3043 | btrfs_put_block_group(block_group); |
| 3044 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3045 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 3046 | chunk_offset); |
| 3047 | if (IS_ERR(trans)) { |
| 3048 | ret = PTR_ERR(trans); |
| 3049 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 3050 | return ret; |
| 3051 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 3052 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3053 | /* |
| 3054 | * step two, delete the device extents and the |
| 3055 | * chunk tree entries |
| 3056 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 3057 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3058 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 3059 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3060 | } |
| 3061 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3062 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3063 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3064 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3065 | struct btrfs_path *path; |
| 3066 | struct extent_buffer *leaf; |
| 3067 | struct btrfs_chunk *chunk; |
| 3068 | struct btrfs_key key; |
| 3069 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3070 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3071 | bool retried = false; |
| 3072 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3073 | int ret; |
| 3074 | |
| 3075 | path = btrfs_alloc_path(); |
| 3076 | if (!path) |
| 3077 | return -ENOMEM; |
| 3078 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3079 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3080 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3081 | key.offset = (u64)-1; |
| 3082 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3083 | |
| 3084 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3085 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3086 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3087 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3088 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3089 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3090 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3091 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3092 | |
| 3093 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3094 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3095 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3096 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3097 | if (ret < 0) |
| 3098 | goto error; |
| 3099 | if (ret > 0) |
| 3100 | break; |
| 3101 | |
| 3102 | leaf = path->nodes[0]; |
| 3103 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3104 | |
| 3105 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3106 | struct btrfs_chunk); |
| 3107 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3108 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3109 | |
| 3110 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3111 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3112 | if (ret == -ENOSPC) |
| 3113 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3114 | else |
| 3115 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3116 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3117 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3118 | |
| 3119 | if (found_key.offset == 0) |
| 3120 | break; |
| 3121 | key.offset = found_key.offset - 1; |
| 3122 | } |
| 3123 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3124 | if (failed && !retried) { |
| 3125 | failed = 0; |
| 3126 | retried = true; |
| 3127 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3128 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3129 | ret = -ENOSPC; |
| 3130 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3131 | error: |
| 3132 | btrfs_free_path(path); |
| 3133 | return ret; |
| 3134 | } |
| 3135 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3136 | /* |
| 3137 | * return 1 : allocate a data chunk successfully, |
| 3138 | * return <0: errors during allocating a data chunk, |
| 3139 | * return 0 : no need to allocate a data chunk. |
| 3140 | */ |
| 3141 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3142 | u64 chunk_offset) |
| 3143 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3144 | struct btrfs_block_group *cache; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3145 | u64 bytes_used; |
| 3146 | u64 chunk_type; |
| 3147 | |
| 3148 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3149 | ASSERT(cache); |
| 3150 | chunk_type = cache->flags; |
| 3151 | btrfs_put_block_group(cache); |
| 3152 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3153 | if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA)) |
| 3154 | return 0; |
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 | spin_lock(&fs_info->data_sinfo->lock); |
| 3157 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3158 | spin_unlock(&fs_info->data_sinfo->lock); |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3159 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3160 | if (!bytes_used) { |
| 3161 | struct btrfs_trans_handle *trans; |
| 3162 | int ret; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3163 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3164 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3165 | if (IS_ERR(trans)) |
| 3166 | return PTR_ERR(trans); |
| 3167 | |
| 3168 | ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA); |
| 3169 | btrfs_end_transaction(trans); |
| 3170 | if (ret < 0) |
| 3171 | return ret; |
| 3172 | return 1; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3173 | } |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3174 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3175 | return 0; |
| 3176 | } |
| 3177 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3178 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3179 | struct btrfs_balance_control *bctl) |
| 3180 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3181 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3182 | struct btrfs_trans_handle *trans; |
| 3183 | struct btrfs_balance_item *item; |
| 3184 | struct btrfs_disk_balance_args disk_bargs; |
| 3185 | struct btrfs_path *path; |
| 3186 | struct extent_buffer *leaf; |
| 3187 | struct btrfs_key key; |
| 3188 | int ret, err; |
| 3189 | |
| 3190 | path = btrfs_alloc_path(); |
| 3191 | if (!path) |
| 3192 | return -ENOMEM; |
| 3193 | |
| 3194 | trans = btrfs_start_transaction(root, 0); |
| 3195 | if (IS_ERR(trans)) { |
| 3196 | btrfs_free_path(path); |
| 3197 | return PTR_ERR(trans); |
| 3198 | } |
| 3199 | |
| 3200 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3201 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3202 | key.offset = 0; |
| 3203 | |
| 3204 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3205 | sizeof(*item)); |
| 3206 | if (ret) |
| 3207 | goto out; |
| 3208 | |
| 3209 | leaf = path->nodes[0]; |
| 3210 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3211 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3212 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3213 | |
| 3214 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3215 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3216 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3217 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3218 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3219 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3220 | |
| 3221 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3222 | |
| 3223 | btrfs_mark_buffer_dirty(leaf); |
| 3224 | out: |
| 3225 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3226 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3227 | if (err && !ret) |
| 3228 | ret = err; |
| 3229 | return ret; |
| 3230 | } |
| 3231 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3232 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3233 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3234 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3235 | struct btrfs_trans_handle *trans; |
| 3236 | struct btrfs_path *path; |
| 3237 | struct btrfs_key key; |
| 3238 | int ret, err; |
| 3239 | |
| 3240 | path = btrfs_alloc_path(); |
| 3241 | if (!path) |
| 3242 | return -ENOMEM; |
| 3243 | |
David Sterba | 3502a8c | 2020-06-25 12:35:28 +0200 | [diff] [blame] | 3244 | trans = btrfs_start_transaction_fallback_global_rsv(root, 0); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3245 | if (IS_ERR(trans)) { |
| 3246 | btrfs_free_path(path); |
| 3247 | return PTR_ERR(trans); |
| 3248 | } |
| 3249 | |
| 3250 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3251 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3252 | key.offset = 0; |
| 3253 | |
| 3254 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3255 | if (ret < 0) |
| 3256 | goto out; |
| 3257 | if (ret > 0) { |
| 3258 | ret = -ENOENT; |
| 3259 | goto out; |
| 3260 | } |
| 3261 | |
| 3262 | ret = btrfs_del_item(trans, root, path); |
| 3263 | out: |
| 3264 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3265 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3266 | if (err && !ret) |
| 3267 | ret = err; |
| 3268 | return ret; |
| 3269 | } |
| 3270 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3271 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3272 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3273 | * resume after balance was interrupted. |
| 3274 | */ |
| 3275 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3276 | { |
| 3277 | /* |
| 3278 | * Turn on soft mode for chunk types that were being converted. |
| 3279 | */ |
| 3280 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3281 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3282 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3283 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3284 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3285 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3286 | |
| 3287 | /* |
| 3288 | * Turn on usage filter if is not already used. The idea is |
| 3289 | * that chunks that we have already balanced should be |
| 3290 | * reasonably full. Don't do it for chunks that are being |
| 3291 | * converted - that will keep us from relocating unconverted |
| 3292 | * (albeit full) chunks. |
| 3293 | */ |
| 3294 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3295 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3296 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3297 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3298 | bctl->data.usage = 90; |
| 3299 | } |
| 3300 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3301 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3302 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3303 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3304 | bctl->sys.usage = 90; |
| 3305 | } |
| 3306 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3307 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3308 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3309 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3310 | bctl->meta.usage = 90; |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3315 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3316 | */ |
| 3317 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3318 | { |
| 3319 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3320 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3321 | |
| 3322 | BUG_ON(!fs_info->balance_ctl); |
| 3323 | |
| 3324 | spin_lock(&fs_info->balance_lock); |
| 3325 | fs_info->balance_ctl = NULL; |
| 3326 | spin_unlock(&fs_info->balance_lock); |
| 3327 | |
| 3328 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3329 | ret = del_balance_item(fs_info); |
| 3330 | if (ret) |
| 3331 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3332 | } |
| 3333 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3334 | /* |
| 3335 | * Balance filters. Return 1 if chunk should be filtered out |
| 3336 | * (should not be balanced). |
| 3337 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3338 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3339 | struct btrfs_balance_args *bargs) |
| 3340 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3341 | chunk_type = chunk_to_extended(chunk_type) & |
| 3342 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3343 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3344 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3345 | return 0; |
| 3346 | |
| 3347 | return 1; |
| 3348 | } |
| 3349 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3350 | 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] | 3351 | struct btrfs_balance_args *bargs) |
| 3352 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3353 | struct btrfs_block_group *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3354 | u64 chunk_used; |
| 3355 | u64 user_thresh_min; |
| 3356 | u64 user_thresh_max; |
| 3357 | int ret = 1; |
| 3358 | |
| 3359 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3360 | chunk_used = cache->used; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3361 | |
| 3362 | if (bargs->usage_min == 0) |
| 3363 | user_thresh_min = 0; |
| 3364 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3365 | user_thresh_min = div_factor_fine(cache->length, |
| 3366 | bargs->usage_min); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3367 | |
| 3368 | if (bargs->usage_max == 0) |
| 3369 | user_thresh_max = 1; |
| 3370 | else if (bargs->usage_max > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3371 | user_thresh_max = cache->length; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3372 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3373 | user_thresh_max = div_factor_fine(cache->length, |
| 3374 | bargs->usage_max); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3375 | |
| 3376 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3377 | ret = 0; |
| 3378 | |
| 3379 | btrfs_put_block_group(cache); |
| 3380 | return ret; |
| 3381 | } |
| 3382 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3383 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3384 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3385 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3386 | struct btrfs_block_group *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3387 | u64 chunk_used, user_thresh; |
| 3388 | int ret = 1; |
| 3389 | |
| 3390 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3391 | chunk_used = cache->used; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3392 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3393 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3394 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3395 | else if (bargs->usage > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3396 | user_thresh = cache->length; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3397 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3398 | user_thresh = div_factor_fine(cache->length, bargs->usage); |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3399 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3400 | if (chunk_used < user_thresh) |
| 3401 | ret = 0; |
| 3402 | |
| 3403 | btrfs_put_block_group(cache); |
| 3404 | return ret; |
| 3405 | } |
| 3406 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3407 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3408 | struct btrfs_chunk *chunk, |
| 3409 | struct btrfs_balance_args *bargs) |
| 3410 | { |
| 3411 | struct btrfs_stripe *stripe; |
| 3412 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3413 | int i; |
| 3414 | |
| 3415 | for (i = 0; i < num_stripes; i++) { |
| 3416 | stripe = btrfs_stripe_nr(chunk, i); |
| 3417 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3418 | return 0; |
| 3419 | } |
| 3420 | |
| 3421 | return 1; |
| 3422 | } |
| 3423 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3424 | static u64 calc_data_stripes(u64 type, int num_stripes) |
| 3425 | { |
| 3426 | const int index = btrfs_bg_flags_to_raid_index(type); |
| 3427 | const int ncopies = btrfs_raid_array[index].ncopies; |
| 3428 | const int nparity = btrfs_raid_array[index].nparity; |
| 3429 | |
| 3430 | if (nparity) |
| 3431 | return num_stripes - nparity; |
| 3432 | else |
| 3433 | return num_stripes / ncopies; |
| 3434 | } |
| 3435 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3436 | /* [pstart, pend) */ |
| 3437 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3438 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3439 | struct btrfs_balance_args *bargs) |
| 3440 | { |
| 3441 | struct btrfs_stripe *stripe; |
| 3442 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3443 | u64 stripe_offset; |
| 3444 | u64 stripe_length; |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3445 | u64 type; |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3446 | int factor; |
| 3447 | int i; |
| 3448 | |
| 3449 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3450 | return 0; |
| 3451 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3452 | type = btrfs_chunk_type(leaf, chunk); |
| 3453 | factor = calc_data_stripes(type, num_stripes); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3454 | |
| 3455 | for (i = 0; i < num_stripes; i++) { |
| 3456 | stripe = btrfs_stripe_nr(chunk, i); |
| 3457 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3458 | continue; |
| 3459 | |
| 3460 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3461 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3462 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3463 | |
| 3464 | if (stripe_offset < bargs->pend && |
| 3465 | stripe_offset + stripe_length > bargs->pstart) |
| 3466 | return 0; |
| 3467 | } |
| 3468 | |
| 3469 | return 1; |
| 3470 | } |
| 3471 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3472 | /* [vstart, vend) */ |
| 3473 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3474 | struct btrfs_chunk *chunk, |
| 3475 | u64 chunk_offset, |
| 3476 | struct btrfs_balance_args *bargs) |
| 3477 | { |
| 3478 | if (chunk_offset < bargs->vend && |
| 3479 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3480 | /* at least part of the chunk is inside this vrange */ |
| 3481 | return 0; |
| 3482 | |
| 3483 | return 1; |
| 3484 | } |
| 3485 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3486 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3487 | struct btrfs_chunk *chunk, |
| 3488 | struct btrfs_balance_args *bargs) |
| 3489 | { |
| 3490 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3491 | |
| 3492 | if (bargs->stripes_min <= num_stripes |
| 3493 | && num_stripes <= bargs->stripes_max) |
| 3494 | return 0; |
| 3495 | |
| 3496 | return 1; |
| 3497 | } |
| 3498 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3499 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3500 | struct btrfs_balance_args *bargs) |
| 3501 | { |
| 3502 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3503 | return 0; |
| 3504 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3505 | chunk_type = chunk_to_extended(chunk_type) & |
| 3506 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3507 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3508 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3509 | return 1; |
| 3510 | |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3514 | static int should_balance_chunk(struct extent_buffer *leaf, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3515 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3516 | { |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3517 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3518 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3519 | struct btrfs_balance_args *bargs = NULL; |
| 3520 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3521 | |
| 3522 | /* type filter */ |
| 3523 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3524 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3525 | return 0; |
| 3526 | } |
| 3527 | |
| 3528 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3529 | bargs = &bctl->data; |
| 3530 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3531 | bargs = &bctl->sys; |
| 3532 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3533 | bargs = &bctl->meta; |
| 3534 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3535 | /* profiles filter */ |
| 3536 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3537 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3538 | return 0; |
| 3539 | } |
| 3540 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3541 | /* usage filter */ |
| 3542 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3543 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3544 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3545 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3546 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3547 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3548 | } |
| 3549 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3550 | /* devid filter */ |
| 3551 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3552 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3553 | return 0; |
| 3554 | } |
| 3555 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3556 | /* drange filter, makes sense only with devid filter */ |
| 3557 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3558 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3559 | return 0; |
| 3560 | } |
| 3561 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3562 | /* vrange filter */ |
| 3563 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3564 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3565 | return 0; |
| 3566 | } |
| 3567 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3568 | /* stripes filter */ |
| 3569 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3570 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3571 | return 0; |
| 3572 | } |
| 3573 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3574 | /* soft profile changing mode */ |
| 3575 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3576 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3577 | return 0; |
| 3578 | } |
| 3579 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3580 | /* |
| 3581 | * limited by count, must be the last filter |
| 3582 | */ |
| 3583 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3584 | if (bargs->limit == 0) |
| 3585 | return 0; |
| 3586 | else |
| 3587 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3588 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3589 | /* |
| 3590 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3591 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3592 | * about the count of all chunks that satisfy the filters. |
| 3593 | */ |
| 3594 | if (bargs->limit_max == 0) |
| 3595 | return 0; |
| 3596 | else |
| 3597 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3598 | } |
| 3599 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3600 | return 1; |
| 3601 | } |
| 3602 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3603 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3604 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3605 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3606 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3607 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3608 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3609 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3610 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3611 | struct btrfs_key found_key; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3612 | struct extent_buffer *leaf; |
| 3613 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3614 | int ret; |
| 3615 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3616 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3617 | /* 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] | 3618 | u64 limit_data = bctl->data.limit; |
| 3619 | u64 limit_meta = bctl->meta.limit; |
| 3620 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3621 | u32 count_data = 0; |
| 3622 | u32 count_meta = 0; |
| 3623 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3624 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3625 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3626 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3627 | if (!path) { |
| 3628 | ret = -ENOMEM; |
| 3629 | goto error; |
| 3630 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3631 | |
| 3632 | /* zero out stat counters */ |
| 3633 | spin_lock(&fs_info->balance_lock); |
| 3634 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3635 | spin_unlock(&fs_info->balance_lock); |
| 3636 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3637 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3638 | /* |
| 3639 | * The single value limit and min/max limits use the same bytes |
| 3640 | * in the |
| 3641 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3642 | bctl->data.limit = limit_data; |
| 3643 | bctl->meta.limit = limit_meta; |
| 3644 | bctl->sys.limit = limit_sys; |
| 3645 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3646 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3647 | key.offset = (u64)-1; |
| 3648 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3649 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3650 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3651 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3652 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3653 | ret = -ECANCELED; |
| 3654 | goto error; |
| 3655 | } |
| 3656 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3657 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3658 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3659 | if (ret < 0) { |
| 3660 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3661 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3662 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3663 | |
| 3664 | /* |
| 3665 | * this shouldn't happen, it means the last relocate |
| 3666 | * failed |
| 3667 | */ |
| 3668 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3669 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3670 | |
| 3671 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3672 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3673 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3674 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3675 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3676 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3677 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3678 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3679 | leaf = path->nodes[0]; |
| 3680 | slot = path->slots[0]; |
| 3681 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3682 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3683 | if (found_key.objectid != key.objectid) { |
| 3684 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3685 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3686 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3687 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3688 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3689 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3690 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3691 | if (!counting) { |
| 3692 | spin_lock(&fs_info->balance_lock); |
| 3693 | bctl->stat.considered++; |
| 3694 | spin_unlock(&fs_info->balance_lock); |
| 3695 | } |
| 3696 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3697 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3698 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3699 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3700 | if (!ret) { |
| 3701 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3702 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3703 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3704 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3705 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3706 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3707 | spin_lock(&fs_info->balance_lock); |
| 3708 | bctl->stat.expected++; |
| 3709 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3710 | |
| 3711 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3712 | count_data++; |
| 3713 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3714 | count_sys++; |
| 3715 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3716 | count_meta++; |
| 3717 | |
| 3718 | goto loop; |
| 3719 | } |
| 3720 | |
| 3721 | /* |
| 3722 | * Apply limit_min filter, no need to check if the LIMITS |
| 3723 | * filter is used, limit_min is 0 by default |
| 3724 | */ |
| 3725 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3726 | count_data < bctl->data.limit_min) |
| 3727 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3728 | count_meta < bctl->meta.limit_min) |
| 3729 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3730 | count_sys < bctl->sys.limit_min)) { |
| 3731 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3732 | goto loop; |
| 3733 | } |
| 3734 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3735 | if (!chunk_reserved) { |
| 3736 | /* |
| 3737 | * We may be relocating the only data chunk we have, |
| 3738 | * which could potentially end up with losing data's |
| 3739 | * raid profile, so lets allocate an empty one in |
| 3740 | * advance. |
| 3741 | */ |
| 3742 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3743 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3744 | if (ret < 0) { |
| 3745 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3746 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3747 | } else if (ret == 1) { |
| 3748 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3749 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3750 | } |
| 3751 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3752 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3753 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3754 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3755 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3756 | } else if (ret == -ETXTBSY) { |
| 3757 | btrfs_info(fs_info, |
| 3758 | "skipping relocation of block group %llu due to active swapfile", |
| 3759 | found_key.offset); |
| 3760 | ret = 0; |
| 3761 | } else if (ret) { |
| 3762 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3763 | } else { |
| 3764 | spin_lock(&fs_info->balance_lock); |
| 3765 | bctl->stat.completed++; |
| 3766 | spin_unlock(&fs_info->balance_lock); |
| 3767 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3768 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3769 | if (found_key.offset == 0) |
| 3770 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3771 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3772 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3773 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3774 | if (counting) { |
| 3775 | btrfs_release_path(path); |
| 3776 | counting = false; |
| 3777 | goto again; |
| 3778 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3779 | error: |
| 3780 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3781 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3782 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3783 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3784 | if (!ret) |
| 3785 | ret = -ENOSPC; |
| 3786 | } |
| 3787 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3788 | return ret; |
| 3789 | } |
| 3790 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3791 | /** |
| 3792 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3793 | * @flags: profile to validate |
| 3794 | * @extended: if true @flags is treated as an extended profile |
| 3795 | */ |
| 3796 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3797 | { |
| 3798 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3799 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3800 | |
| 3801 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3802 | |
| 3803 | /* 1) check that all other bits are zeroed */ |
| 3804 | if (flags & ~mask) |
| 3805 | return 0; |
| 3806 | |
| 3807 | /* 2) see if profile is reduced */ |
| 3808 | if (flags == 0) |
| 3809 | return !extended; /* "0" is valid for usual profiles */ |
| 3810 | |
David Sterba | c149916 | 2019-10-01 19:44:42 +0200 | [diff] [blame] | 3811 | return has_single_bit_set(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3812 | } |
| 3813 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3814 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3815 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3816 | /* cancel requested || normal exit path */ |
| 3817 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3818 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3819 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3820 | } |
| 3821 | |
David Sterba | 5ba366c | 2020-02-27 21:00:52 +0100 | [diff] [blame] | 3822 | /* |
| 3823 | * Validate target profile against allowed profiles and return true if it's OK. |
| 3824 | * Otherwise print the error message and return false. |
| 3825 | */ |
| 3826 | static inline int validate_convert_profile(struct btrfs_fs_info *fs_info, |
| 3827 | const struct btrfs_balance_args *bargs, |
| 3828 | u64 allowed, const char *type) |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3829 | { |
David Sterba | 5ba366c | 2020-02-27 21:00:52 +0100 | [diff] [blame] | 3830 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3831 | return true; |
| 3832 | |
| 3833 | /* Profile is valid and does not have bits outside of the allowed set */ |
| 3834 | if (alloc_profile_is_valid(bargs->target, 1) && |
| 3835 | (bargs->target & ~allowed) == 0) |
| 3836 | return true; |
| 3837 | |
| 3838 | btrfs_err(fs_info, "balance: invalid convert %s profile %s", |
| 3839 | type, btrfs_bg_type_to_raid_name(bargs->target)); |
| 3840 | return false; |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3843 | /* |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3844 | * Fill @buf with textual description of balance filter flags @bargs, up to |
| 3845 | * @size_buf including the terminating null. The output may be trimmed if it |
| 3846 | * does not fit into the provided buffer. |
| 3847 | */ |
| 3848 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, |
| 3849 | u32 size_buf) |
| 3850 | { |
| 3851 | int ret; |
| 3852 | u32 size_bp = size_buf; |
| 3853 | char *bp = buf; |
| 3854 | u64 flags = bargs->flags; |
| 3855 | char tmp_buf[128] = {'\0'}; |
| 3856 | |
| 3857 | if (!flags) |
| 3858 | return; |
| 3859 | |
| 3860 | #define CHECK_APPEND_NOARG(a) \ |
| 3861 | do { \ |
| 3862 | ret = snprintf(bp, size_bp, (a)); \ |
| 3863 | if (ret < 0 || ret >= size_bp) \ |
| 3864 | goto out_overflow; \ |
| 3865 | size_bp -= ret; \ |
| 3866 | bp += ret; \ |
| 3867 | } while (0) |
| 3868 | |
| 3869 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3870 | do { \ |
| 3871 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3872 | if (ret < 0 || ret >= size_bp) \ |
| 3873 | goto out_overflow; \ |
| 3874 | size_bp -= ret; \ |
| 3875 | bp += ret; \ |
| 3876 | } while (0) |
| 3877 | |
| 3878 | #define CHECK_APPEND_2ARG(a, v1, v2) \ |
| 3879 | do { \ |
| 3880 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ |
| 3881 | if (ret < 0 || ret >= size_bp) \ |
| 3882 | goto out_overflow; \ |
| 3883 | size_bp -= ret; \ |
| 3884 | bp += ret; \ |
| 3885 | } while (0) |
| 3886 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3887 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3888 | CHECK_APPEND_1ARG("convert=%s,", |
| 3889 | btrfs_bg_type_to_raid_name(bargs->target)); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3890 | |
| 3891 | if (flags & BTRFS_BALANCE_ARGS_SOFT) |
| 3892 | CHECK_APPEND_NOARG("soft,"); |
| 3893 | |
| 3894 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { |
| 3895 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, |
| 3896 | sizeof(tmp_buf)); |
| 3897 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); |
| 3898 | } |
| 3899 | |
| 3900 | if (flags & BTRFS_BALANCE_ARGS_USAGE) |
| 3901 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); |
| 3902 | |
| 3903 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) |
| 3904 | CHECK_APPEND_2ARG("usage=%u..%u,", |
| 3905 | bargs->usage_min, bargs->usage_max); |
| 3906 | |
| 3907 | if (flags & BTRFS_BALANCE_ARGS_DEVID) |
| 3908 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); |
| 3909 | |
| 3910 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) |
| 3911 | CHECK_APPEND_2ARG("drange=%llu..%llu,", |
| 3912 | bargs->pstart, bargs->pend); |
| 3913 | |
| 3914 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) |
| 3915 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", |
| 3916 | bargs->vstart, bargs->vend); |
| 3917 | |
| 3918 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) |
| 3919 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); |
| 3920 | |
| 3921 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) |
| 3922 | CHECK_APPEND_2ARG("limit=%u..%u,", |
| 3923 | bargs->limit_min, bargs->limit_max); |
| 3924 | |
| 3925 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) |
| 3926 | CHECK_APPEND_2ARG("stripes=%u..%u,", |
| 3927 | bargs->stripes_min, bargs->stripes_max); |
| 3928 | |
| 3929 | #undef CHECK_APPEND_2ARG |
| 3930 | #undef CHECK_APPEND_1ARG |
| 3931 | #undef CHECK_APPEND_NOARG |
| 3932 | |
| 3933 | out_overflow: |
| 3934 | |
| 3935 | if (size_bp < size_buf) |
| 3936 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ |
| 3937 | else |
| 3938 | buf[0] = '\0'; |
| 3939 | } |
| 3940 | |
| 3941 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) |
| 3942 | { |
| 3943 | u32 size_buf = 1024; |
| 3944 | char tmp_buf[192] = {'\0'}; |
| 3945 | char *buf; |
| 3946 | char *bp; |
| 3947 | u32 size_bp = size_buf; |
| 3948 | int ret; |
| 3949 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3950 | |
| 3951 | buf = kzalloc(size_buf, GFP_KERNEL); |
| 3952 | if (!buf) |
| 3953 | return; |
| 3954 | |
| 3955 | bp = buf; |
| 3956 | |
| 3957 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3958 | do { \ |
| 3959 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3960 | if (ret < 0 || ret >= size_bp) \ |
| 3961 | goto out_overflow; \ |
| 3962 | size_bp -= ret; \ |
| 3963 | bp += ret; \ |
| 3964 | } while (0) |
| 3965 | |
| 3966 | if (bctl->flags & BTRFS_BALANCE_FORCE) |
| 3967 | CHECK_APPEND_1ARG("%s", "-f "); |
| 3968 | |
| 3969 | if (bctl->flags & BTRFS_BALANCE_DATA) { |
| 3970 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); |
| 3971 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); |
| 3972 | } |
| 3973 | |
| 3974 | if (bctl->flags & BTRFS_BALANCE_METADATA) { |
| 3975 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); |
| 3976 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); |
| 3977 | } |
| 3978 | |
| 3979 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { |
| 3980 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); |
| 3981 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); |
| 3982 | } |
| 3983 | |
| 3984 | #undef CHECK_APPEND_1ARG |
| 3985 | |
| 3986 | out_overflow: |
| 3987 | |
| 3988 | if (size_bp < size_buf) |
| 3989 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ |
| 3990 | btrfs_info(fs_info, "balance: %s %s", |
| 3991 | (bctl->flags & BTRFS_BALANCE_RESUME) ? |
| 3992 | "resume" : "start", buf); |
| 3993 | |
| 3994 | kfree(buf); |
| 3995 | } |
| 3996 | |
| 3997 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3998 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3999 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4000 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 4001 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4002 | struct btrfs_ioctl_balance_args *bargs) |
| 4003 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4004 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4005 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4006 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4007 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 4008 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4009 | unsigned seq; |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4010 | bool reducing_redundancy; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4011 | int i; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4012 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4013 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4014 | atomic_read(&fs_info->balance_pause_req) || |
Qu Wenruo | 726a342 | 2020-02-17 14:16:52 +0800 | [diff] [blame] | 4015 | btrfs_should_cancel_balance(fs_info)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4016 | ret = -EINVAL; |
| 4017 | goto out; |
| 4018 | } |
| 4019 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4020 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 4021 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 4022 | mixed = 1; |
| 4023 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4024 | /* |
| 4025 | * In case of mixed groups both data and meta should be picked, |
| 4026 | * and identical options should be given for both of them. |
| 4027 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4028 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 4029 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4030 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 4031 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 4032 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4033 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4034 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4035 | ret = -EINVAL; |
| 4036 | goto out; |
| 4037 | } |
| 4038 | } |
| 4039 | |
Josef Bacik | b35cf1f | 2020-01-10 11:11:24 -0500 | [diff] [blame] | 4040 | /* |
| 4041 | * rw_devices will not change at the moment, device add/delete/replace |
| 4042 | * are excluded by EXCL_OP |
| 4043 | */ |
| 4044 | num_devices = fs_info->fs_devices->rw_devices; |
Qu Wenruo | fab2735 | 2019-09-25 10:13:27 +0800 | [diff] [blame] | 4045 | |
| 4046 | /* |
| 4047 | * SINGLE profile on-disk has no profile bit, but in-memory we have a |
| 4048 | * special bit for it, to make it easier to distinguish. Thus we need |
| 4049 | * to set it manually, or balance would refuse the profile. |
| 4050 | */ |
| 4051 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 4052 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
| 4053 | if (num_devices >= btrfs_raid_array[i].devs_min) |
| 4054 | allowed |= btrfs_raid_array[i].bg_flag; |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 4055 | |
David Sterba | 5ba366c | 2020-02-27 21:00:52 +0100 | [diff] [blame] | 4056 | if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") || |
| 4057 | !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") || |
| 4058 | !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4059 | ret = -EINVAL; |
| 4060 | goto out; |
| 4061 | } |
| 4062 | |
David Sterba | 6079e12 | 2019-05-17 11:43:29 +0200 | [diff] [blame] | 4063 | /* |
| 4064 | * Allow to reduce metadata or system integrity only if force set for |
| 4065 | * profiles with redundancy (copies, parity) |
| 4066 | */ |
| 4067 | allowed = 0; |
| 4068 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { |
| 4069 | if (btrfs_raid_array[i].ncopies >= 2 || |
| 4070 | btrfs_raid_array[i].tolerated_failures >= 1) |
| 4071 | allowed |= btrfs_raid_array[i].bg_flag; |
| 4072 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4073 | do { |
| 4074 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 4075 | |
| 4076 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4077 | (fs_info->avail_system_alloc_bits & allowed) && |
| 4078 | !(bctl->sys.target & allowed)) || |
| 4079 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4080 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4081 | !(bctl->meta.target & allowed))) |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4082 | reducing_redundancy = true; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4083 | else |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4084 | reducing_redundancy = false; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4085 | |
| 4086 | /* if we're not converting, the target field is uninitialized */ |
| 4087 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4088 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 4089 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4090 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4091 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4092 | |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4093 | if (reducing_redundancy) { |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4094 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 4095 | btrfs_info(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4096 | "balance: force reducing metadata redundancy"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4097 | } else { |
| 4098 | btrfs_err(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4099 | "balance: reduces metadata redundancy, use --force if you want this"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4100 | ret = -EINVAL; |
| 4101 | goto out; |
| 4102 | } |
| 4103 | } |
| 4104 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4105 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 4106 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4107 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4108 | "balance: metadata profile %s has lower redundancy than data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4109 | btrfs_bg_type_to_raid_name(meta_target), |
| 4110 | btrfs_bg_type_to_raid_name(data_target)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 4113 | if (fs_info->send_in_progress) { |
| 4114 | btrfs_warn_rl(fs_info, |
| 4115 | "cannot run balance while send operations are in progress (%d in progress)", |
| 4116 | fs_info->send_in_progress); |
| 4117 | ret = -EAGAIN; |
| 4118 | goto out; |
| 4119 | } |
| 4120 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4121 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4122 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4123 | goto out; |
| 4124 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4125 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 4126 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4127 | BUG_ON(fs_info->balance_ctl); |
| 4128 | spin_lock(&fs_info->balance_lock); |
| 4129 | fs_info->balance_ctl = bctl; |
| 4130 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4131 | } else { |
| 4132 | BUG_ON(ret != -EEXIST); |
| 4133 | spin_lock(&fs_info->balance_lock); |
| 4134 | update_balance_args(bctl); |
| 4135 | spin_unlock(&fs_info->balance_lock); |
| 4136 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4137 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4138 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 4139 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4140 | describe_balance_start_or_resume(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4141 | mutex_unlock(&fs_info->balance_mutex); |
| 4142 | |
| 4143 | ret = __btrfs_balance(fs_info); |
| 4144 | |
| 4145 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4146 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) |
| 4147 | btrfs_info(fs_info, "balance: paused"); |
Qu Wenruo | 44d354a | 2020-07-13 09:03:21 +0800 | [diff] [blame] | 4148 | /* |
| 4149 | * Balance can be canceled by: |
| 4150 | * |
| 4151 | * - Regular cancel request |
| 4152 | * Then ret == -ECANCELED and balance_cancel_req > 0 |
| 4153 | * |
| 4154 | * - Fatal signal to "btrfs" process |
| 4155 | * Either the signal caught by wait_reserve_ticket() and callers |
| 4156 | * got -EINTR, or caught by btrfs_should_cancel_balance() and |
| 4157 | * got -ECANCELED. |
| 4158 | * Either way, in this case balance_cancel_req = 0, and |
| 4159 | * ret == -EINTR or ret == -ECANCELED. |
| 4160 | * |
| 4161 | * So here we only check the return value to catch canceled balance. |
| 4162 | */ |
| 4163 | else if (ret == -ECANCELED || ret == -EINTR) |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4164 | btrfs_info(fs_info, "balance: canceled"); |
| 4165 | else |
| 4166 | btrfs_info(fs_info, "balance: ended with status: %d", ret); |
| 4167 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4168 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4169 | |
| 4170 | if (bargs) { |
| 4171 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4172 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4173 | } |
| 4174 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4175 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4176 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4177 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4178 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4179 | } |
| 4180 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4181 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4182 | |
| 4183 | return ret; |
| 4184 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4185 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4186 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4187 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4188 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4189 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4190 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4191 | return ret; |
| 4192 | } |
| 4193 | |
| 4194 | static int balance_kthread(void *data) |
| 4195 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4196 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4197 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4198 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4199 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4200 | if (fs_info->balance_ctl) |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4201 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4202 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4203 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4204 | return ret; |
| 4205 | } |
| 4206 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4207 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4208 | { |
| 4209 | struct task_struct *tsk; |
| 4210 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4211 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4212 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4213 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4214 | return 0; |
| 4215 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4216 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4217 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4218 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4219 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4220 | return 0; |
| 4221 | } |
| 4222 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4223 | /* |
| 4224 | * A ro->rw remount sequence should continue with the paused balance |
| 4225 | * regardless of who pauses it, system or the user as of now, so set |
| 4226 | * the resume flag. |
| 4227 | */ |
| 4228 | spin_lock(&fs_info->balance_lock); |
| 4229 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4230 | spin_unlock(&fs_info->balance_lock); |
| 4231 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4232 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4233 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4234 | } |
| 4235 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4236 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4237 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4238 | struct btrfs_balance_control *bctl; |
| 4239 | struct btrfs_balance_item *item; |
| 4240 | struct btrfs_disk_balance_args disk_bargs; |
| 4241 | struct btrfs_path *path; |
| 4242 | struct extent_buffer *leaf; |
| 4243 | struct btrfs_key key; |
| 4244 | int ret; |
| 4245 | |
| 4246 | path = btrfs_alloc_path(); |
| 4247 | if (!path) |
| 4248 | return -ENOMEM; |
| 4249 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4250 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4251 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4252 | key.offset = 0; |
| 4253 | |
| 4254 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4255 | if (ret < 0) |
| 4256 | goto out; |
| 4257 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4258 | ret = 0; |
| 4259 | goto out; |
| 4260 | } |
| 4261 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4262 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4263 | if (!bctl) { |
| 4264 | ret = -ENOMEM; |
| 4265 | goto out; |
| 4266 | } |
| 4267 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4268 | leaf = path->nodes[0]; |
| 4269 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4270 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4271 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4272 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4273 | |
| 4274 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4275 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4276 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4277 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4278 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4279 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4280 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4281 | /* |
| 4282 | * This should never happen, as the paused balance state is recovered |
| 4283 | * during mount without any chance of other exclusive ops to collide. |
| 4284 | * |
| 4285 | * This gives the exclusive op status to balance and keeps in paused |
| 4286 | * state until user intervention (cancel or umount). If the ownership |
| 4287 | * cannot be assigned, show a message but do not fail. The balance |
| 4288 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4289 | * set up. |
| 4290 | */ |
| 4291 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4292 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4293 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4294 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4295 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4296 | BUG_ON(fs_info->balance_ctl); |
| 4297 | spin_lock(&fs_info->balance_lock); |
| 4298 | fs_info->balance_ctl = bctl; |
| 4299 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4300 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4301 | out: |
| 4302 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4303 | return ret; |
| 4304 | } |
| 4305 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4306 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4307 | { |
| 4308 | int ret = 0; |
| 4309 | |
| 4310 | mutex_lock(&fs_info->balance_mutex); |
| 4311 | if (!fs_info->balance_ctl) { |
| 4312 | mutex_unlock(&fs_info->balance_mutex); |
| 4313 | return -ENOTCONN; |
| 4314 | } |
| 4315 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4316 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4317 | atomic_inc(&fs_info->balance_pause_req); |
| 4318 | mutex_unlock(&fs_info->balance_mutex); |
| 4319 | |
| 4320 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4321 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4322 | |
| 4323 | mutex_lock(&fs_info->balance_mutex); |
| 4324 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4325 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4326 | atomic_dec(&fs_info->balance_pause_req); |
| 4327 | } else { |
| 4328 | ret = -ENOTCONN; |
| 4329 | } |
| 4330 | |
| 4331 | mutex_unlock(&fs_info->balance_mutex); |
| 4332 | return ret; |
| 4333 | } |
| 4334 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4335 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4336 | { |
| 4337 | mutex_lock(&fs_info->balance_mutex); |
| 4338 | if (!fs_info->balance_ctl) { |
| 4339 | mutex_unlock(&fs_info->balance_mutex); |
| 4340 | return -ENOTCONN; |
| 4341 | } |
| 4342 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4343 | /* |
| 4344 | * A paused balance with the item stored on disk can be resumed at |
| 4345 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4346 | * and we must not allow cancelling as it deletes the item. |
| 4347 | */ |
| 4348 | if (sb_rdonly(fs_info->sb)) { |
| 4349 | mutex_unlock(&fs_info->balance_mutex); |
| 4350 | return -EROFS; |
| 4351 | } |
| 4352 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4353 | atomic_inc(&fs_info->balance_cancel_req); |
| 4354 | /* |
| 4355 | * if we are running just wait and return, balance item is |
| 4356 | * deleted in btrfs_balance in this case |
| 4357 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4358 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4359 | mutex_unlock(&fs_info->balance_mutex); |
| 4360 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4361 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4362 | mutex_lock(&fs_info->balance_mutex); |
| 4363 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4364 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4365 | /* |
| 4366 | * Lock released to allow other waiters to continue, we'll |
| 4367 | * reexamine the status again. |
| 4368 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4369 | mutex_lock(&fs_info->balance_mutex); |
| 4370 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4371 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4372 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4373 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4374 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4375 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4378 | BUG_ON(fs_info->balance_ctl || |
| 4379 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4380 | atomic_dec(&fs_info->balance_cancel_req); |
| 4381 | mutex_unlock(&fs_info->balance_mutex); |
| 4382 | return 0; |
| 4383 | } |
| 4384 | |
Nikolay Borisov | 97f4dd0 | 2020-02-18 16:56:08 +0200 | [diff] [blame] | 4385 | int btrfs_uuid_scan_kthread(void *data) |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4386 | { |
| 4387 | struct btrfs_fs_info *fs_info = data; |
| 4388 | struct btrfs_root *root = fs_info->tree_root; |
| 4389 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4390 | struct btrfs_path *path = NULL; |
| 4391 | int ret = 0; |
| 4392 | struct extent_buffer *eb; |
| 4393 | int slot; |
| 4394 | struct btrfs_root_item root_item; |
| 4395 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4396 | struct btrfs_trans_handle *trans = NULL; |
Josef Bacik | c94bec2 | 2020-02-14 15:05:01 -0500 | [diff] [blame] | 4397 | bool closing = false; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4398 | |
| 4399 | path = btrfs_alloc_path(); |
| 4400 | if (!path) { |
| 4401 | ret = -ENOMEM; |
| 4402 | goto out; |
| 4403 | } |
| 4404 | |
| 4405 | key.objectid = 0; |
| 4406 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4407 | key.offset = 0; |
| 4408 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4409 | while (1) { |
Josef Bacik | c94bec2 | 2020-02-14 15:05:01 -0500 | [diff] [blame] | 4410 | if (btrfs_fs_closing(fs_info)) { |
| 4411 | closing = true; |
| 4412 | break; |
| 4413 | } |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4414 | ret = btrfs_search_forward(root, &key, path, |
| 4415 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4416 | if (ret) { |
| 4417 | if (ret > 0) |
| 4418 | ret = 0; |
| 4419 | break; |
| 4420 | } |
| 4421 | |
| 4422 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4423 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4424 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4425 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4426 | goto skip; |
| 4427 | |
| 4428 | eb = path->nodes[0]; |
| 4429 | slot = path->slots[0]; |
| 4430 | item_size = btrfs_item_size_nr(eb, slot); |
| 4431 | if (item_size < sizeof(root_item)) |
| 4432 | goto skip; |
| 4433 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4434 | read_extent_buffer(eb, &root_item, |
| 4435 | btrfs_item_ptr_offset(eb, slot), |
| 4436 | (int)sizeof(root_item)); |
| 4437 | if (btrfs_root_refs(&root_item) == 0) |
| 4438 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4439 | |
| 4440 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4441 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4442 | if (trans) |
| 4443 | goto update_tree; |
| 4444 | |
| 4445 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4446 | /* |
| 4447 | * 1 - subvol uuid item |
| 4448 | * 1 - received_subvol uuid item |
| 4449 | */ |
| 4450 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4451 | if (IS_ERR(trans)) { |
| 4452 | ret = PTR_ERR(trans); |
| 4453 | break; |
| 4454 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4455 | continue; |
| 4456 | } else { |
| 4457 | goto skip; |
| 4458 | } |
| 4459 | update_tree: |
Josef Bacik | 9771a5cf | 2020-08-10 11:42:26 -0400 | [diff] [blame] | 4460 | btrfs_release_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4461 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4462 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4463 | BTRFS_UUID_KEY_SUBVOL, |
| 4464 | key.objectid); |
| 4465 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4466 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4467 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4468 | break; |
| 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4473 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4474 | root_item.received_uuid, |
| 4475 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4476 | key.objectid); |
| 4477 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4478 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4479 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4480 | break; |
| 4481 | } |
| 4482 | } |
| 4483 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4484 | skip: |
Josef Bacik | 9771a5cf | 2020-08-10 11:42:26 -0400 | [diff] [blame] | 4485 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4486 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4487 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4488 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4489 | if (ret) |
| 4490 | break; |
| 4491 | } |
| 4492 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4493 | if (key.offset < (u64)-1) { |
| 4494 | key.offset++; |
| 4495 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4496 | key.offset = 0; |
| 4497 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4498 | } else if (key.objectid < (u64)-1) { |
| 4499 | key.offset = 0; |
| 4500 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4501 | key.objectid++; |
| 4502 | } else { |
| 4503 | break; |
| 4504 | } |
| 4505 | cond_resched(); |
| 4506 | } |
| 4507 | |
| 4508 | out: |
| 4509 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4510 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4511 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4512 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4513 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Josef Bacik | c94bec2 | 2020-02-14 15:05:01 -0500 | [diff] [blame] | 4514 | else if (!closing) |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4515 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4516 | up(&fs_info->uuid_tree_rescan_sem); |
| 4517 | return 0; |
| 4518 | } |
| 4519 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4520 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4521 | { |
| 4522 | struct btrfs_trans_handle *trans; |
| 4523 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4524 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4525 | struct task_struct *task; |
| 4526 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4527 | |
| 4528 | /* |
| 4529 | * 1 - root node |
| 4530 | * 1 - root item |
| 4531 | */ |
| 4532 | trans = btrfs_start_transaction(tree_root, 2); |
| 4533 | if (IS_ERR(trans)) |
| 4534 | return PTR_ERR(trans); |
| 4535 | |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 4536 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4537 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4538 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4539 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4540 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4541 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | fs_info->uuid_root = uuid_root; |
| 4545 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4546 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4547 | if (ret) |
| 4548 | return ret; |
| 4549 | |
| 4550 | down(&fs_info->uuid_tree_rescan_sem); |
| 4551 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4552 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4553 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4554 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4555 | up(&fs_info->uuid_tree_rescan_sem); |
| 4556 | return PTR_ERR(task); |
| 4557 | } |
| 4558 | |
| 4559 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4560 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4561 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4562 | /* |
| 4563 | * shrinking a device means finding all of the device extents past |
| 4564 | * the new size, and then following the back refs to the chunks. |
| 4565 | * The chunk relocation code actually frees the device extent |
| 4566 | */ |
| 4567 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4568 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4569 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4570 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4571 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4572 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4573 | struct btrfs_path *path; |
| 4574 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4575 | u64 chunk_offset; |
| 4576 | int ret; |
| 4577 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4578 | int failed = 0; |
| 4579 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4580 | struct extent_buffer *l; |
| 4581 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4582 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4583 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4584 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4585 | u64 diff; |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4586 | u64 start; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4587 | |
| 4588 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4589 | start = new_size; |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4590 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4591 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4592 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4593 | return -EINVAL; |
| 4594 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4595 | path = btrfs_alloc_path(); |
| 4596 | if (!path) |
| 4597 | return -ENOMEM; |
| 4598 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4599 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4600 | |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4601 | trans = btrfs_start_transaction(root, 0); |
| 4602 | if (IS_ERR(trans)) { |
| 4603 | btrfs_free_path(path); |
| 4604 | return PTR_ERR(trans); |
| 4605 | } |
| 4606 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4607 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4608 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4609 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4610 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4611 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4612 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4613 | } |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4614 | |
| 4615 | /* |
| 4616 | * Once the device's size has been set to the new size, ensure all |
| 4617 | * in-memory chunks are synced to disk so that the loop below sees them |
| 4618 | * and relocates them accordingly. |
| 4619 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 4620 | if (contains_pending_extent(device, &start, diff)) { |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4621 | mutex_unlock(&fs_info->chunk_mutex); |
| 4622 | ret = btrfs_commit_transaction(trans); |
| 4623 | if (ret) |
| 4624 | goto done; |
| 4625 | } else { |
| 4626 | mutex_unlock(&fs_info->chunk_mutex); |
| 4627 | btrfs_end_transaction(trans); |
| 4628 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4629 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4630 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4631 | key.objectid = device->devid; |
| 4632 | key.offset = (u64)-1; |
| 4633 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4634 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4635 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4636 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4637 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4638 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4639 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4640 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4641 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4642 | |
| 4643 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4644 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4645 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4646 | if (ret < 0) |
| 4647 | goto done; |
| 4648 | if (ret) { |
| 4649 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4650 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4651 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | l = path->nodes[0]; |
| 4655 | slot = path->slots[0]; |
| 4656 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4657 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4658 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4659 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4660 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4661 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4662 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4663 | |
| 4664 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4665 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4666 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4667 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4668 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4669 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4670 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4671 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4672 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4673 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4674 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4675 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4676 | /* |
| 4677 | * We may be relocating the only data chunk we have, |
| 4678 | * which could potentially end up with losing data's |
| 4679 | * raid profile, so lets allocate an empty one in |
| 4680 | * advance. |
| 4681 | */ |
| 4682 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4683 | if (ret < 0) { |
| 4684 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4685 | goto done; |
| 4686 | } |
| 4687 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4688 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4689 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4690 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4691 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4692 | } else if (ret) { |
| 4693 | if (ret == -ETXTBSY) { |
| 4694 | btrfs_warn(fs_info, |
| 4695 | "could not shrink block group %llu due to active swapfile", |
| 4696 | chunk_offset); |
| 4697 | } |
| 4698 | goto done; |
| 4699 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4700 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4701 | |
| 4702 | if (failed && !retried) { |
| 4703 | failed = 0; |
| 4704 | retried = true; |
| 4705 | goto again; |
| 4706 | } else if (failed && retried) { |
| 4707 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4708 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4709 | } |
| 4710 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4711 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4712 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4713 | if (IS_ERR(trans)) { |
| 4714 | ret = PTR_ERR(trans); |
| 4715 | goto done; |
| 4716 | } |
| 4717 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4718 | mutex_lock(&fs_info->chunk_mutex); |
Qu Wenruo | c57dd1f | 2020-07-31 19:29:11 +0800 | [diff] [blame] | 4719 | /* Clear all state bits beyond the shrunk device size */ |
| 4720 | clear_extent_bits(&device->alloc_state, new_size, (u64)-1, |
| 4721 | CHUNK_STATE_MASK); |
| 4722 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4723 | btrfs_device_set_disk_total_bytes(device, new_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4724 | if (list_empty(&device->post_commit_list)) |
| 4725 | list_add_tail(&device->post_commit_list, |
| 4726 | &trans->transaction->dev_update_list); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4727 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4728 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4729 | btrfs_set_super_total_bytes(super_copy, |
| 4730 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4731 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4732 | |
| 4733 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4734 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4735 | if (ret < 0) { |
| 4736 | btrfs_abort_transaction(trans, ret); |
| 4737 | btrfs_end_transaction(trans); |
| 4738 | } else { |
| 4739 | ret = btrfs_commit_transaction(trans); |
| 4740 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4741 | done: |
| 4742 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4743 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4744 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4745 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4746 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4747 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4748 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4749 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4750 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4751 | return ret; |
| 4752 | } |
| 4753 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4754 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4755 | struct btrfs_key *key, |
| 4756 | struct btrfs_chunk *chunk, int item_size) |
| 4757 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4758 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4759 | struct btrfs_disk_key disk_key; |
| 4760 | u32 array_size; |
| 4761 | u8 *ptr; |
| 4762 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4763 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4764 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4765 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4766 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4767 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4768 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4769 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4770 | |
| 4771 | ptr = super_copy->sys_chunk_array + array_size; |
| 4772 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4773 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4774 | ptr += sizeof(disk_key); |
| 4775 | memcpy(ptr, chunk, item_size); |
| 4776 | item_size += sizeof(disk_key); |
| 4777 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4778 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4779 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4780 | return 0; |
| 4781 | } |
| 4782 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4783 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4784 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4785 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4786 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4787 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4788 | const struct btrfs_device_info *di_a = a; |
| 4789 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4790 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4791 | if (di_a->max_avail > di_b->max_avail) |
| 4792 | return -1; |
| 4793 | if (di_a->max_avail < di_b->max_avail) |
| 4794 | return 1; |
| 4795 | if (di_a->total_avail > di_b->total_avail) |
| 4796 | return -1; |
| 4797 | if (di_a->total_avail < di_b->total_avail) |
| 4798 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4799 | return 0; |
| 4800 | } |
| 4801 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4802 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4803 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4804 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4805 | return; |
| 4806 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4807 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4808 | } |
| 4809 | |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 4810 | static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4811 | { |
| 4812 | if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4))) |
| 4813 | return; |
| 4814 | |
| 4815 | btrfs_set_fs_incompat(info, RAID1C34); |
| 4816 | } |
| 4817 | |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 4818 | /* |
| 4819 | * Structure used internally for __btrfs_alloc_chunk() function. |
| 4820 | * Wraps needed parameters. |
| 4821 | */ |
| 4822 | struct alloc_chunk_ctl { |
| 4823 | u64 start; |
| 4824 | u64 type; |
| 4825 | /* Total number of stripes to allocate */ |
| 4826 | int num_stripes; |
| 4827 | /* sub_stripes info for map */ |
| 4828 | int sub_stripes; |
| 4829 | /* Stripes per device */ |
| 4830 | int dev_stripes; |
| 4831 | /* Maximum number of devices to use */ |
| 4832 | int devs_max; |
| 4833 | /* Minimum number of devices to use */ |
| 4834 | int devs_min; |
| 4835 | /* ndevs has to be a multiple of this */ |
| 4836 | int devs_increment; |
| 4837 | /* Number of copies */ |
| 4838 | int ncopies; |
| 4839 | /* Number of stripes worth of bytes to store parity information */ |
| 4840 | int nparity; |
| 4841 | u64 max_stripe_size; |
| 4842 | u64 max_chunk_size; |
Naohiro Aota | 6aafb30 | 2020-02-25 12:56:15 +0900 | [diff] [blame] | 4843 | u64 dev_extent_min; |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 4844 | u64 stripe_size; |
| 4845 | u64 chunk_size; |
| 4846 | int ndevs; |
| 4847 | }; |
| 4848 | |
Naohiro Aota | 27c314d | 2020-02-25 12:56:11 +0900 | [diff] [blame] | 4849 | static void init_alloc_chunk_ctl_policy_regular( |
| 4850 | struct btrfs_fs_devices *fs_devices, |
| 4851 | struct alloc_chunk_ctl *ctl) |
| 4852 | { |
| 4853 | u64 type = ctl->type; |
| 4854 | |
| 4855 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4856 | ctl->max_stripe_size = SZ_1G; |
| 4857 | ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
| 4858 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
| 4859 | /* For larger filesystems, use larger metadata chunks */ |
| 4860 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4861 | ctl->max_stripe_size = SZ_1G; |
| 4862 | else |
| 4863 | ctl->max_stripe_size = SZ_256M; |
| 4864 | ctl->max_chunk_size = ctl->max_stripe_size; |
| 4865 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4866 | ctl->max_stripe_size = SZ_32M; |
| 4867 | ctl->max_chunk_size = 2 * ctl->max_stripe_size; |
| 4868 | ctl->devs_max = min_t(int, ctl->devs_max, |
| 4869 | BTRFS_MAX_DEVS_SYS_CHUNK); |
| 4870 | } else { |
| 4871 | BUG(); |
| 4872 | } |
| 4873 | |
| 4874 | /* We don't want a chunk larger than 10% of writable space */ |
| 4875 | ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4876 | ctl->max_chunk_size); |
Naohiro Aota | 6aafb30 | 2020-02-25 12:56:15 +0900 | [diff] [blame] | 4877 | ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes; |
Naohiro Aota | 27c314d | 2020-02-25 12:56:11 +0900 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices, |
| 4881 | struct alloc_chunk_ctl *ctl) |
| 4882 | { |
| 4883 | int index = btrfs_bg_flags_to_raid_index(ctl->type); |
| 4884 | |
| 4885 | ctl->sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4886 | ctl->dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4887 | ctl->devs_max = btrfs_raid_array[index].devs_max; |
| 4888 | if (!ctl->devs_max) |
| 4889 | ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info); |
| 4890 | ctl->devs_min = btrfs_raid_array[index].devs_min; |
| 4891 | ctl->devs_increment = btrfs_raid_array[index].devs_increment; |
| 4892 | ctl->ncopies = btrfs_raid_array[index].ncopies; |
| 4893 | ctl->nparity = btrfs_raid_array[index].nparity; |
| 4894 | ctl->ndevs = 0; |
| 4895 | |
| 4896 | switch (fs_devices->chunk_alloc_policy) { |
| 4897 | case BTRFS_CHUNK_ALLOC_REGULAR: |
| 4898 | init_alloc_chunk_ctl_policy_regular(fs_devices, ctl); |
| 4899 | break; |
| 4900 | default: |
| 4901 | BUG(); |
| 4902 | } |
| 4903 | } |
| 4904 | |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame] | 4905 | static int gather_device_info(struct btrfs_fs_devices *fs_devices, |
| 4906 | struct alloc_chunk_ctl *ctl, |
| 4907 | struct btrfs_device_info *devices_info) |
| 4908 | { |
| 4909 | struct btrfs_fs_info *info = fs_devices->fs_info; |
| 4910 | struct btrfs_device *device; |
| 4911 | u64 total_avail; |
| 4912 | u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes; |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame] | 4913 | int ret; |
| 4914 | int ndevs = 0; |
| 4915 | u64 max_avail; |
| 4916 | u64 dev_offset; |
| 4917 | |
| 4918 | /* |
| 4919 | * in the first pass through the devices list, we gather information |
| 4920 | * about the available holes on each device. |
| 4921 | */ |
| 4922 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 4923 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
| 4924 | WARN(1, KERN_ERR |
| 4925 | "BTRFS: read-only device in alloc_list\n"); |
| 4926 | continue; |
| 4927 | } |
| 4928 | |
| 4929 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 4930 | &device->dev_state) || |
| 4931 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
| 4932 | continue; |
| 4933 | |
| 4934 | if (device->total_bytes > device->bytes_used) |
| 4935 | total_avail = device->total_bytes - device->bytes_used; |
| 4936 | else |
| 4937 | total_avail = 0; |
| 4938 | |
| 4939 | /* If there is no space on this device, skip it. */ |
Naohiro Aota | 6aafb30 | 2020-02-25 12:56:15 +0900 | [diff] [blame] | 4940 | if (total_avail < ctl->dev_extent_min) |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame] | 4941 | continue; |
| 4942 | |
| 4943 | ret = find_free_dev_extent(device, dev_extent_want, &dev_offset, |
| 4944 | &max_avail); |
| 4945 | if (ret && ret != -ENOSPC) |
| 4946 | return ret; |
| 4947 | |
| 4948 | if (ret == 0) |
| 4949 | max_avail = dev_extent_want; |
| 4950 | |
Naohiro Aota | 6aafb30 | 2020-02-25 12:56:15 +0900 | [diff] [blame] | 4951 | if (max_avail < ctl->dev_extent_min) { |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame] | 4952 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4953 | btrfs_debug(info, |
| 4954 | "%s: devid %llu has no free space, have=%llu want=%llu", |
| 4955 | __func__, device->devid, max_avail, |
Naohiro Aota | 6aafb30 | 2020-02-25 12:56:15 +0900 | [diff] [blame] | 4956 | ctl->dev_extent_min); |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame] | 4957 | continue; |
| 4958 | } |
| 4959 | |
| 4960 | if (ndevs == fs_devices->rw_devices) { |
| 4961 | WARN(1, "%s: found more than %llu devices\n", |
| 4962 | __func__, fs_devices->rw_devices); |
| 4963 | break; |
| 4964 | } |
| 4965 | devices_info[ndevs].dev_offset = dev_offset; |
| 4966 | devices_info[ndevs].max_avail = max_avail; |
| 4967 | devices_info[ndevs].total_avail = total_avail; |
| 4968 | devices_info[ndevs].dev = device; |
| 4969 | ++ndevs; |
| 4970 | } |
| 4971 | ctl->ndevs = ndevs; |
| 4972 | |
| 4973 | /* |
| 4974 | * now sort the devices by hole size / available space |
| 4975 | */ |
| 4976 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4977 | btrfs_cmp_device_info, NULL); |
| 4978 | |
| 4979 | return 0; |
| 4980 | } |
| 4981 | |
Naohiro Aota | 5badf51 | 2020-02-25 12:56:13 +0900 | [diff] [blame] | 4982 | static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl, |
| 4983 | struct btrfs_device_info *devices_info) |
| 4984 | { |
| 4985 | /* Number of stripes that count for block group size */ |
| 4986 | int data_stripes; |
| 4987 | |
| 4988 | /* |
| 4989 | * The primary goal is to maximize the number of stripes, so use as |
| 4990 | * many devices as possible, even if the stripes are not maximum sized. |
| 4991 | * |
| 4992 | * The DUP profile stores more than one stripe per device, the |
| 4993 | * max_avail is the total size so we have to adjust. |
| 4994 | */ |
| 4995 | ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail, |
| 4996 | ctl->dev_stripes); |
| 4997 | ctl->num_stripes = ctl->ndevs * ctl->dev_stripes; |
| 4998 | |
| 4999 | /* This will have to be fixed for RAID1 and RAID10 over more drives */ |
| 5000 | data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies; |
| 5001 | |
| 5002 | /* |
| 5003 | * Use the number of data stripes to figure out how big this chunk is |
| 5004 | * really going to be in terms of logical address space, and compare |
| 5005 | * that answer with the max chunk size. If it's higher, we try to |
| 5006 | * reduce stripe_size. |
| 5007 | */ |
| 5008 | if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) { |
| 5009 | /* |
| 5010 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 5011 | * then use it, unless it ends up being even bigger than the |
| 5012 | * previous value we had already. |
| 5013 | */ |
| 5014 | ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size, |
| 5015 | data_stripes), SZ_16M), |
| 5016 | ctl->stripe_size); |
| 5017 | } |
| 5018 | |
| 5019 | /* Align to BTRFS_STRIPE_LEN */ |
| 5020 | ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN); |
| 5021 | ctl->chunk_size = ctl->stripe_size * data_stripes; |
| 5022 | |
| 5023 | return 0; |
| 5024 | } |
| 5025 | |
| 5026 | static int decide_stripe_size(struct btrfs_fs_devices *fs_devices, |
| 5027 | struct alloc_chunk_ctl *ctl, |
| 5028 | struct btrfs_device_info *devices_info) |
| 5029 | { |
| 5030 | struct btrfs_fs_info *info = fs_devices->fs_info; |
| 5031 | |
| 5032 | /* |
| 5033 | * Round down to number of usable stripes, devs_increment can be any |
| 5034 | * number so we can't use round_down() that requires power of 2, while |
| 5035 | * rounddown is safe. |
| 5036 | */ |
| 5037 | ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment); |
| 5038 | |
| 5039 | if (ctl->ndevs < ctl->devs_min) { |
| 5040 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 5041 | btrfs_debug(info, |
| 5042 | "%s: not enough devices with free space: have=%d minimum required=%d", |
| 5043 | __func__, ctl->ndevs, ctl->devs_min); |
| 5044 | } |
| 5045 | return -ENOSPC; |
| 5046 | } |
| 5047 | |
| 5048 | ctl->ndevs = min(ctl->ndevs, ctl->devs_max); |
| 5049 | |
| 5050 | switch (fs_devices->chunk_alloc_policy) { |
| 5051 | case BTRFS_CHUNK_ALLOC_REGULAR: |
| 5052 | return decide_stripe_size_regular(ctl, devices_info); |
| 5053 | default: |
| 5054 | BUG(); |
| 5055 | } |
| 5056 | } |
| 5057 | |
Naohiro Aota | dce580c | 2020-02-25 12:56:14 +0900 | [diff] [blame] | 5058 | static int create_chunk(struct btrfs_trans_handle *trans, |
| 5059 | struct alloc_chunk_ctl *ctl, |
| 5060 | struct btrfs_device_info *devices_info) |
| 5061 | { |
| 5062 | struct btrfs_fs_info *info = trans->fs_info; |
| 5063 | struct map_lookup *map = NULL; |
| 5064 | struct extent_map_tree *em_tree; |
| 5065 | struct extent_map *em; |
| 5066 | u64 start = ctl->start; |
| 5067 | u64 type = ctl->type; |
| 5068 | int ret; |
| 5069 | int i; |
| 5070 | int j; |
| 5071 | |
| 5072 | map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS); |
| 5073 | if (!map) |
| 5074 | return -ENOMEM; |
| 5075 | map->num_stripes = ctl->num_stripes; |
| 5076 | |
| 5077 | for (i = 0; i < ctl->ndevs; ++i) { |
| 5078 | for (j = 0; j < ctl->dev_stripes; ++j) { |
| 5079 | int s = i * ctl->dev_stripes + j; |
| 5080 | map->stripes[s].dev = devices_info[i].dev; |
| 5081 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 5082 | j * ctl->stripe_size; |
| 5083 | } |
| 5084 | } |
| 5085 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 5086 | map->io_align = BTRFS_STRIPE_LEN; |
| 5087 | map->io_width = BTRFS_STRIPE_LEN; |
| 5088 | map->type = type; |
| 5089 | map->sub_stripes = ctl->sub_stripes; |
| 5090 | |
| 5091 | trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size); |
| 5092 | |
| 5093 | em = alloc_extent_map(); |
| 5094 | if (!em) { |
| 5095 | kfree(map); |
| 5096 | return -ENOMEM; |
| 5097 | } |
| 5098 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
| 5099 | em->map_lookup = map; |
| 5100 | em->start = start; |
| 5101 | em->len = ctl->chunk_size; |
| 5102 | em->block_start = 0; |
| 5103 | em->block_len = em->len; |
| 5104 | em->orig_block_len = ctl->stripe_size; |
| 5105 | |
| 5106 | em_tree = &info->mapping_tree; |
| 5107 | write_lock(&em_tree->lock); |
| 5108 | ret = add_extent_mapping(em_tree, em, 0); |
| 5109 | if (ret) { |
| 5110 | write_unlock(&em_tree->lock); |
| 5111 | free_extent_map(em); |
| 5112 | return ret; |
| 5113 | } |
| 5114 | write_unlock(&em_tree->lock); |
| 5115 | |
| 5116 | ret = btrfs_make_block_group(trans, 0, type, start, ctl->chunk_size); |
| 5117 | if (ret) |
| 5118 | goto error_del_extent; |
| 5119 | |
| 5120 | for (i = 0; i < map->num_stripes; i++) { |
| 5121 | struct btrfs_device *dev = map->stripes[i].dev; |
| 5122 | |
| 5123 | btrfs_device_set_bytes_used(dev, |
| 5124 | dev->bytes_used + ctl->stripe_size); |
| 5125 | if (list_empty(&dev->post_commit_list)) |
| 5126 | list_add_tail(&dev->post_commit_list, |
| 5127 | &trans->transaction->dev_update_list); |
| 5128 | } |
| 5129 | |
| 5130 | atomic64_sub(ctl->stripe_size * map->num_stripes, |
| 5131 | &info->free_chunk_space); |
| 5132 | |
| 5133 | free_extent_map(em); |
| 5134 | check_raid56_incompat_flag(info, type); |
| 5135 | check_raid1c34_incompat_flag(info, type); |
| 5136 | |
| 5137 | return 0; |
| 5138 | |
| 5139 | error_del_extent: |
| 5140 | write_lock(&em_tree->lock); |
| 5141 | remove_extent_mapping(em_tree, em); |
| 5142 | write_unlock(&em_tree->lock); |
| 5143 | |
| 5144 | /* One for our allocation */ |
| 5145 | free_extent_map(em); |
| 5146 | /* One for the tree reference */ |
| 5147 | free_extent_map(em); |
| 5148 | |
| 5149 | return ret; |
| 5150 | } |
| 5151 | |
Nikolay Borisov | 11c67b1 | 2020-03-02 12:29:25 +0200 | [diff] [blame] | 5152 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5153 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5154 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5155 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5156 | struct btrfs_device_info *devices_info = NULL; |
Naohiro Aota | 4f2bafe | 2020-02-25 12:56:10 +0900 | [diff] [blame] | 5157 | struct alloc_chunk_ctl ctl; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5158 | int ret; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5159 | |
Nikolay Borisov | 11c67b1 | 2020-03-02 12:29:25 +0200 | [diff] [blame] | 5160 | lockdep_assert_held(&info->chunk_mutex); |
| 5161 | |
Naohiro Aota | b25c19f | 2020-02-25 12:56:07 +0900 | [diff] [blame] | 5162 | if (!alloc_profile_is_valid(type, 0)) { |
| 5163 | ASSERT(0); |
| 5164 | return -EINVAL; |
| 5165 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5166 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5167 | if (list_empty(&fs_devices->alloc_list)) { |
| 5168 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 5169 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5170 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 5171 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5172 | |
Naohiro Aota | 27c314d | 2020-02-25 12:56:11 +0900 | [diff] [blame] | 5173 | if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { |
| 5174 | btrfs_err(info, "invalid chunk type 0x%llx requested", type); |
| 5175 | ASSERT(0); |
| 5176 | return -EINVAL; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5177 | } |
| 5178 | |
Nikolay Borisov | 11c67b1 | 2020-03-02 12:29:25 +0200 | [diff] [blame] | 5179 | ctl.start = find_next_chunk(info); |
Naohiro Aota | 27c314d | 2020-02-25 12:56:11 +0900 | [diff] [blame] | 5180 | ctl.type = type; |
| 5181 | init_alloc_chunk_ctl(fs_devices, &ctl); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5182 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5183 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5184 | GFP_NOFS); |
| 5185 | if (!devices_info) |
| 5186 | return -ENOMEM; |
| 5187 | |
Naohiro Aota | 560156c | 2020-02-25 12:56:12 +0900 | [diff] [blame] | 5188 | ret = gather_device_info(fs_devices, &ctl, devices_info); |
| 5189 | if (ret < 0) |
Naohiro Aota | dce580c | 2020-02-25 12:56:14 +0900 | [diff] [blame] | 5190 | goto out; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5191 | |
Naohiro Aota | 5badf51 | 2020-02-25 12:56:13 +0900 | [diff] [blame] | 5192 | ret = decide_stripe_size(fs_devices, &ctl, devices_info); |
| 5193 | if (ret < 0) |
Naohiro Aota | dce580c | 2020-02-25 12:56:14 +0900 | [diff] [blame] | 5194 | goto out; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5195 | |
Naohiro Aota | dce580c | 2020-02-25 12:56:14 +0900 | [diff] [blame] | 5196 | ret = create_chunk(trans, &ctl, devices_info); |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 5197 | |
Naohiro Aota | dce580c | 2020-02-25 12:56:14 +0900 | [diff] [blame] | 5198 | out: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5199 | kfree(devices_info); |
| 5200 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5201 | } |
| 5202 | |
Nikolay Borisov | 11c67b1 | 2020-03-02 12:29:25 +0200 | [diff] [blame] | 5203 | /* |
| 5204 | * Chunk allocation falls into two parts. The first part does work |
| 5205 | * that makes the new allocated chunk usable, but does not do any operation |
| 5206 | * that modifies the chunk tree. The second part does the work that |
| 5207 | * requires modifying the chunk tree. This division is important for the |
| 5208 | * bootstrap process of adding storage to a seed btrfs. |
| 5209 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5210 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5211 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5212 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5213 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5214 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5215 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5216 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5217 | struct btrfs_device *device; |
| 5218 | struct btrfs_chunk *chunk; |
| 5219 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5220 | struct extent_map *em; |
| 5221 | struct map_lookup *map; |
| 5222 | size_t item_size; |
| 5223 | u64 dev_offset; |
| 5224 | u64 stripe_size; |
| 5225 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5226 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5227 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5228 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5229 | if (IS_ERR(em)) |
| 5230 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5231 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5232 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5233 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5234 | stripe_size = em->orig_block_len; |
| 5235 | |
| 5236 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5237 | if (!chunk) { |
| 5238 | ret = -ENOMEM; |
| 5239 | goto out; |
| 5240 | } |
| 5241 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5242 | /* |
| 5243 | * Take the device list mutex to prevent races with the final phase of |
| 5244 | * a device replace operation that replaces the device object associated |
| 5245 | * with the map's stripes, because the device object's id can change |
| 5246 | * at any time during that final phase of the device replace operation |
| 5247 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5248 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5249 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5250 | for (i = 0; i < map->num_stripes; i++) { |
| 5251 | device = map->stripes[i].dev; |
| 5252 | dev_offset = map->stripes[i].physical; |
| 5253 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5254 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5255 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5256 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5257 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5258 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5259 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5260 | break; |
| 5261 | } |
| 5262 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5263 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5264 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5265 | } |
| 5266 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5267 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5268 | for (i = 0; i < map->num_stripes; i++) { |
| 5269 | device = map->stripes[i].dev; |
| 5270 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5271 | |
| 5272 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5273 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5274 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5275 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5276 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5277 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5278 | |
| 5279 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5280 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5281 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5282 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5283 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5284 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5285 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5286 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5287 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5288 | |
| 5289 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5290 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5291 | key.offset = chunk_offset; |
| 5292 | |
| 5293 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5294 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5295 | /* |
| 5296 | * TODO: Cleanup of inserted chunk root in case of |
| 5297 | * failure. |
| 5298 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5299 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5300 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5301 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5302 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5303 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5304 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5305 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5306 | } |
| 5307 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5308 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5309 | { |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5310 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5311 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5312 | int ret; |
| 5313 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5314 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
Nikolay Borisov | 11c67b1 | 2020-03-02 12:29:25 +0200 | [diff] [blame] | 5315 | ret = btrfs_alloc_chunk(trans, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5316 | if (ret) |
| 5317 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5318 | |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5319 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
Nikolay Borisov | 11c67b1 | 2020-03-02 12:29:25 +0200 | [diff] [blame] | 5320 | ret = btrfs_alloc_chunk(trans, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5321 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5322 | } |
| 5323 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5324 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5325 | { |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5326 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5327 | |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5328 | return btrfs_raid_array[index].tolerated_failures; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5329 | } |
| 5330 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5331 | 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] | 5332 | { |
| 5333 | struct extent_map *em; |
| 5334 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5335 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5336 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5337 | int i; |
| 5338 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5339 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5340 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5341 | return 1; |
| 5342 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5343 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5344 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5345 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5346 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5347 | miss_ndevs++; |
| 5348 | continue; |
| 5349 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5350 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5351 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5352 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5353 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5354 | } |
| 5355 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5356 | |
| 5357 | /* |
| 5358 | * If the number of missing devices is larger than max errors, |
| 5359 | * we can not write the data into that chunk successfully, so |
| 5360 | * set it readonly. |
| 5361 | */ |
| 5362 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5363 | readonly = 1; |
| 5364 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5365 | free_extent_map(em); |
| 5366 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5367 | } |
| 5368 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5369 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5370 | { |
| 5371 | struct extent_map *em; |
| 5372 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5373 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5374 | write_lock(&tree->lock); |
| 5375 | em = lookup_extent_mapping(tree, 0, (u64)-1); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5376 | if (em) |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5377 | remove_extent_mapping(tree, em); |
| 5378 | write_unlock(&tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5379 | if (!em) |
| 5380 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5381 | /* once for us */ |
| 5382 | free_extent_map(em); |
| 5383 | /* once for the tree */ |
| 5384 | free_extent_map(em); |
| 5385 | } |
| 5386 | } |
| 5387 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5388 | 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] | 5389 | { |
| 5390 | struct extent_map *em; |
| 5391 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5392 | int ret; |
| 5393 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5394 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5395 | if (IS_ERR(em)) |
| 5396 | /* |
| 5397 | * We could return errors for these cases, but that could get |
| 5398 | * ugly and we'd probably do the same thing which is just not do |
| 5399 | * anything else and exit, so return 1 so the callers don't try |
| 5400 | * to use other copies. |
| 5401 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5402 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5403 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5404 | map = em->map_lookup; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5405 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK)) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5406 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5407 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5408 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5409 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5410 | ret = 2; |
| 5411 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5412 | /* |
| 5413 | * There could be two corrupted data stripes, we need |
| 5414 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5415 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5416 | * Fail a stripe at a time on every retry except the |
| 5417 | * stripe under reconstruction. |
| 5418 | */ |
| 5419 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5420 | else |
| 5421 | ret = 1; |
| 5422 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5423 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5424 | down_read(&fs_info->dev_replace.rwsem); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5425 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5426 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5427 | ret++; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5428 | up_read(&fs_info->dev_replace.rwsem); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5429 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5430 | return ret; |
| 5431 | } |
| 5432 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5433 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5434 | u64 logical) |
| 5435 | { |
| 5436 | struct extent_map *em; |
| 5437 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5438 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5439 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5440 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5441 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5442 | if (!WARN_ON(IS_ERR(em))) { |
| 5443 | map = em->map_lookup; |
| 5444 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5445 | len = map->stripe_len * nr_data_stripes(map); |
| 5446 | free_extent_map(em); |
| 5447 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5448 | return len; |
| 5449 | } |
| 5450 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5451 | 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] | 5452 | { |
| 5453 | struct extent_map *em; |
| 5454 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5455 | int ret = 0; |
| 5456 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5457 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5458 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5459 | if(!WARN_ON(IS_ERR(em))) { |
| 5460 | map = em->map_lookup; |
| 5461 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5462 | ret = 1; |
| 5463 | free_extent_map(em); |
| 5464 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5465 | return ret; |
| 5466 | } |
| 5467 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5468 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5469 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5470 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5471 | { |
| 5472 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5473 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5474 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5475 | int tolerance; |
| 5476 | struct btrfs_device *srcdev; |
| 5477 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5478 | ASSERT((map->type & |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5479 | (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10))); |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5480 | |
| 5481 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5482 | num_stripes = map->sub_stripes; |
| 5483 | else |
| 5484 | num_stripes = map->num_stripes; |
| 5485 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5486 | preferred_mirror = first + current->pid % num_stripes; |
| 5487 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5488 | if (dev_replace_is_ongoing && |
| 5489 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5490 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5491 | srcdev = fs_info->dev_replace.srcdev; |
| 5492 | else |
| 5493 | srcdev = NULL; |
| 5494 | |
| 5495 | /* |
| 5496 | * try to avoid the drive that is the source drive for a |
| 5497 | * dev-replace procedure, only choose it if no other non-missing |
| 5498 | * mirror is available |
| 5499 | */ |
| 5500 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5501 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5502 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5503 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5504 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5505 | if (map->stripes[i].dev->bdev && |
| 5506 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5507 | return i; |
| 5508 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5509 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5510 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5511 | /* we couldn't find one that doesn't fail. Just return something |
| 5512 | * and the io error handling code will clean up eventually |
| 5513 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5514 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5515 | } |
| 5516 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5517 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5518 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5519 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5520 | int i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5521 | int again = 1; |
| 5522 | |
| 5523 | while (again) { |
| 5524 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5525 | for (i = 0; i < num_stripes - 1; i++) { |
David Sterba | eeb6f17 | 2019-11-28 15:31:17 +0100 | [diff] [blame] | 5526 | /* Swap if parity is on a smaller index */ |
| 5527 | if (bbio->raid_map[i] > bbio->raid_map[i + 1]) { |
| 5528 | swap(bbio->stripes[i], bbio->stripes[i + 1]); |
| 5529 | swap(bbio->raid_map[i], bbio->raid_map[i + 1]); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5530 | again = 1; |
| 5531 | } |
| 5532 | } |
| 5533 | } |
| 5534 | } |
| 5535 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5536 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5537 | { |
| 5538 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5539 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5540 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5541 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5542 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5543 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5544 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5545 | /* |
| 5546 | * plus the raid_map, which includes both the tgt dev |
| 5547 | * and the stripes |
| 5548 | */ |
| 5549 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5550 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5551 | |
| 5552 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5553 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5554 | |
Nikolay Borisov | 608769a | 2020-07-02 16:46:41 +0300 | [diff] [blame] | 5555 | bbio->tgtdev_map = (int *)(bbio->stripes + total_stripes); |
| 5556 | bbio->raid_map = (u64 *)(bbio->tgtdev_map + real_stripes); |
| 5557 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5558 | return bbio; |
| 5559 | } |
| 5560 | |
| 5561 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5562 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5563 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5564 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5568 | { |
| 5569 | if (!bbio) |
| 5570 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5571 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5572 | kfree(bbio); |
| 5573 | } |
| 5574 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5575 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5576 | /* |
| 5577 | * Please note that, discard won't be sent to target device of device |
| 5578 | * replace. |
| 5579 | */ |
| 5580 | 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] | 5581 | u64 logical, u64 *length_ret, |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5582 | struct btrfs_bio **bbio_ret) |
| 5583 | { |
| 5584 | struct extent_map *em; |
| 5585 | struct map_lookup *map; |
| 5586 | struct btrfs_bio *bbio; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5587 | u64 length = *length_ret; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5588 | u64 offset; |
| 5589 | u64 stripe_nr; |
| 5590 | u64 stripe_nr_end; |
| 5591 | u64 stripe_end_offset; |
| 5592 | u64 stripe_cnt; |
| 5593 | u64 stripe_len; |
| 5594 | u64 stripe_offset; |
| 5595 | u64 num_stripes; |
| 5596 | u32 stripe_index; |
| 5597 | u32 factor = 0; |
| 5598 | u32 sub_stripes = 0; |
| 5599 | u64 stripes_per_dev = 0; |
| 5600 | u32 remaining_stripes = 0; |
| 5601 | u32 last_stripe = 0; |
| 5602 | int ret = 0; |
| 5603 | int i; |
| 5604 | |
| 5605 | /* discard always return a bbio */ |
| 5606 | ASSERT(bbio_ret); |
| 5607 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5608 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5609 | if (IS_ERR(em)) |
| 5610 | return PTR_ERR(em); |
| 5611 | |
| 5612 | map = em->map_lookup; |
| 5613 | /* we don't discard raid56 yet */ |
| 5614 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5615 | ret = -EOPNOTSUPP; |
| 5616 | goto out; |
| 5617 | } |
| 5618 | |
| 5619 | offset = logical - em->start; |
Qu Wenruo | 2d97461 | 2019-10-23 21:57:26 +0800 | [diff] [blame] | 5620 | length = min_t(u64, em->start + em->len - logical, length); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5621 | *length_ret = length; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5622 | |
| 5623 | stripe_len = map->stripe_len; |
| 5624 | /* |
| 5625 | * stripe_nr counts the total number of stripes we have to stride |
| 5626 | * to get to this block |
| 5627 | */ |
| 5628 | stripe_nr = div64_u64(offset, stripe_len); |
| 5629 | |
| 5630 | /* stripe_offset is the offset of this block in its stripe */ |
| 5631 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5632 | |
| 5633 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5634 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5635 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5636 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5637 | (offset + length); |
| 5638 | /* |
| 5639 | * after this, stripe_nr is the number of stripes on this |
| 5640 | * device we have to walk to find the data, and stripe_index is |
| 5641 | * the number of our device in the stripe array |
| 5642 | */ |
| 5643 | num_stripes = 1; |
| 5644 | stripe_index = 0; |
| 5645 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5646 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5647 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5648 | sub_stripes = 1; |
| 5649 | else |
| 5650 | sub_stripes = map->sub_stripes; |
| 5651 | |
| 5652 | factor = map->num_stripes / sub_stripes; |
| 5653 | num_stripes = min_t(u64, map->num_stripes, |
| 5654 | sub_stripes * stripe_cnt); |
| 5655 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5656 | stripe_index *= sub_stripes; |
| 5657 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5658 | &remaining_stripes); |
| 5659 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5660 | last_stripe *= sub_stripes; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5661 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5662 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5663 | num_stripes = map->num_stripes; |
| 5664 | } else { |
| 5665 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5666 | &stripe_index); |
| 5667 | } |
| 5668 | |
| 5669 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5670 | if (!bbio) { |
| 5671 | ret = -ENOMEM; |
| 5672 | goto out; |
| 5673 | } |
| 5674 | |
| 5675 | for (i = 0; i < num_stripes; i++) { |
| 5676 | bbio->stripes[i].physical = |
| 5677 | map->stripes[stripe_index].physical + |
| 5678 | stripe_offset + stripe_nr * map->stripe_len; |
| 5679 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5680 | |
| 5681 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5682 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5683 | bbio->stripes[i].length = stripes_per_dev * |
| 5684 | map->stripe_len; |
| 5685 | |
| 5686 | if (i / sub_stripes < remaining_stripes) |
| 5687 | bbio->stripes[i].length += |
| 5688 | map->stripe_len; |
| 5689 | |
| 5690 | /* |
| 5691 | * Special for the first stripe and |
| 5692 | * the last stripe: |
| 5693 | * |
| 5694 | * |-------|...|-------| |
| 5695 | * |----------| |
| 5696 | * off end_off |
| 5697 | */ |
| 5698 | if (i < sub_stripes) |
| 5699 | bbio->stripes[i].length -= |
| 5700 | stripe_offset; |
| 5701 | |
| 5702 | if (stripe_index >= last_stripe && |
| 5703 | stripe_index <= (last_stripe + |
| 5704 | sub_stripes - 1)) |
| 5705 | bbio->stripes[i].length -= |
| 5706 | stripe_end_offset; |
| 5707 | |
| 5708 | if (i == sub_stripes - 1) |
| 5709 | stripe_offset = 0; |
| 5710 | } else { |
| 5711 | bbio->stripes[i].length = length; |
| 5712 | } |
| 5713 | |
| 5714 | stripe_index++; |
| 5715 | if (stripe_index == map->num_stripes) { |
| 5716 | stripe_index = 0; |
| 5717 | stripe_nr++; |
| 5718 | } |
| 5719 | } |
| 5720 | |
| 5721 | *bbio_ret = bbio; |
| 5722 | bbio->map_type = map->type; |
| 5723 | bbio->num_stripes = num_stripes; |
| 5724 | out: |
| 5725 | free_extent_map(em); |
| 5726 | return ret; |
| 5727 | } |
| 5728 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5729 | /* |
| 5730 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5731 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5732 | * left cursor can also be read from the target drive. |
| 5733 | * |
| 5734 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5735 | * array of stripes. |
| 5736 | * For READ, it also needs to be supported using the same mirror number. |
| 5737 | * |
| 5738 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5739 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5740 | * case. |
| 5741 | */ |
| 5742 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5743 | u64 logical, u64 length, |
| 5744 | u64 srcdev_devid, int *mirror_num, |
| 5745 | u64 *physical) |
| 5746 | { |
| 5747 | struct btrfs_bio *bbio = NULL; |
| 5748 | int num_stripes; |
| 5749 | int index_srcdev = 0; |
| 5750 | int found = 0; |
| 5751 | u64 physical_of_found = 0; |
| 5752 | int i; |
| 5753 | int ret = 0; |
| 5754 | |
| 5755 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5756 | logical, &length, &bbio, 0, 0); |
| 5757 | if (ret) { |
| 5758 | ASSERT(bbio == NULL); |
| 5759 | return ret; |
| 5760 | } |
| 5761 | |
| 5762 | num_stripes = bbio->num_stripes; |
| 5763 | if (*mirror_num > num_stripes) { |
| 5764 | /* |
| 5765 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5766 | * that means that the requested area is not left of the left |
| 5767 | * cursor |
| 5768 | */ |
| 5769 | btrfs_put_bbio(bbio); |
| 5770 | return -EIO; |
| 5771 | } |
| 5772 | |
| 5773 | /* |
| 5774 | * process the rest of the function using the mirror_num of the source |
| 5775 | * drive. Therefore look it up first. At the end, patch the device |
| 5776 | * pointer to the one of the target drive. |
| 5777 | */ |
| 5778 | for (i = 0; i < num_stripes; i++) { |
| 5779 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5780 | continue; |
| 5781 | |
| 5782 | /* |
| 5783 | * In case of DUP, in order to keep it simple, only add the |
| 5784 | * mirror with the lowest physical address |
| 5785 | */ |
| 5786 | if (found && |
| 5787 | physical_of_found <= bbio->stripes[i].physical) |
| 5788 | continue; |
| 5789 | |
| 5790 | index_srcdev = i; |
| 5791 | found = 1; |
| 5792 | physical_of_found = bbio->stripes[i].physical; |
| 5793 | } |
| 5794 | |
| 5795 | btrfs_put_bbio(bbio); |
| 5796 | |
| 5797 | ASSERT(found); |
| 5798 | if (!found) |
| 5799 | return -EIO; |
| 5800 | |
| 5801 | *mirror_num = index_srcdev + 1; |
| 5802 | *physical = physical_of_found; |
| 5803 | return ret; |
| 5804 | } |
| 5805 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5806 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5807 | struct btrfs_bio **bbio_ret, |
| 5808 | struct btrfs_dev_replace *dev_replace, |
| 5809 | int *num_stripes_ret, int *max_errors_ret) |
| 5810 | { |
| 5811 | struct btrfs_bio *bbio = *bbio_ret; |
| 5812 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5813 | int tgtdev_indexes = 0; |
| 5814 | int num_stripes = *num_stripes_ret; |
| 5815 | int max_errors = *max_errors_ret; |
| 5816 | int i; |
| 5817 | |
| 5818 | if (op == BTRFS_MAP_WRITE) { |
| 5819 | int index_where_to_add; |
| 5820 | |
| 5821 | /* |
| 5822 | * duplicate the write operations while the dev replace |
| 5823 | * procedure is running. Since the copying of the old disk to |
| 5824 | * the new disk takes place at run time while the filesystem is |
| 5825 | * mounted writable, the regular write operations to the old |
| 5826 | * disk have to be duplicated to go to the new disk as well. |
| 5827 | * |
| 5828 | * Note that device->missing is handled by the caller, and that |
| 5829 | * the write to the old disk is already set up in the stripes |
| 5830 | * array. |
| 5831 | */ |
| 5832 | index_where_to_add = num_stripes; |
| 5833 | for (i = 0; i < num_stripes; i++) { |
| 5834 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5835 | /* write to new disk, too */ |
| 5836 | struct btrfs_bio_stripe *new = |
| 5837 | bbio->stripes + index_where_to_add; |
| 5838 | struct btrfs_bio_stripe *old = |
| 5839 | bbio->stripes + i; |
| 5840 | |
| 5841 | new->physical = old->physical; |
| 5842 | new->length = old->length; |
| 5843 | new->dev = dev_replace->tgtdev; |
| 5844 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5845 | index_where_to_add++; |
| 5846 | max_errors++; |
| 5847 | tgtdev_indexes++; |
| 5848 | } |
| 5849 | } |
| 5850 | num_stripes = index_where_to_add; |
| 5851 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5852 | int index_srcdev = 0; |
| 5853 | int found = 0; |
| 5854 | u64 physical_of_found = 0; |
| 5855 | |
| 5856 | /* |
| 5857 | * During the dev-replace procedure, the target drive can also |
| 5858 | * be used to read data in case it is needed to repair a corrupt |
| 5859 | * block elsewhere. This is possible if the requested area is |
| 5860 | * left of the left cursor. In this area, the target drive is a |
| 5861 | * full copy of the source drive. |
| 5862 | */ |
| 5863 | for (i = 0; i < num_stripes; i++) { |
| 5864 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5865 | /* |
| 5866 | * In case of DUP, in order to keep it simple, |
| 5867 | * only add the mirror with the lowest physical |
| 5868 | * address |
| 5869 | */ |
| 5870 | if (found && |
| 5871 | physical_of_found <= |
| 5872 | bbio->stripes[i].physical) |
| 5873 | continue; |
| 5874 | index_srcdev = i; |
| 5875 | found = 1; |
| 5876 | physical_of_found = bbio->stripes[i].physical; |
| 5877 | } |
| 5878 | } |
| 5879 | if (found) { |
| 5880 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5881 | bbio->stripes + num_stripes; |
| 5882 | |
| 5883 | tgtdev_stripe->physical = physical_of_found; |
| 5884 | tgtdev_stripe->length = |
| 5885 | bbio->stripes[index_srcdev].length; |
| 5886 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5887 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5888 | |
| 5889 | tgtdev_indexes++; |
| 5890 | num_stripes++; |
| 5891 | } |
| 5892 | } |
| 5893 | |
| 5894 | *num_stripes_ret = num_stripes; |
| 5895 | *max_errors_ret = max_errors; |
| 5896 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5897 | *bbio_ret = bbio; |
| 5898 | } |
| 5899 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5900 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5901 | { |
| 5902 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5903 | } |
| 5904 | |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5905 | /* |
| 5906 | * btrfs_get_io_geometry - calculates the geomery of a particular (address, len) |
| 5907 | * tuple. This information is used to calculate how big a |
| 5908 | * particular bio can get before it straddles a stripe. |
| 5909 | * |
| 5910 | * @fs_info - the filesystem |
| 5911 | * @logical - address that we want to figure out the geometry of |
| 5912 | * @len - the length of IO we are going to perform, starting at @logical |
| 5913 | * @op - type of operation - write or read |
| 5914 | * @io_geom - pointer used to return values |
| 5915 | * |
| 5916 | * Returns < 0 in case a chunk for the given logical address cannot be found, |
| 5917 | * usually shouldn't happen unless @logical is corrupted, 0 otherwise. |
| 5918 | */ |
| 5919 | 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] | 5920 | u64 logical, u64 len, struct btrfs_io_geometry *io_geom) |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5921 | { |
| 5922 | struct extent_map *em; |
| 5923 | struct map_lookup *map; |
| 5924 | u64 offset; |
| 5925 | u64 stripe_offset; |
| 5926 | u64 stripe_nr; |
| 5927 | u64 stripe_len; |
| 5928 | u64 raid56_full_stripe_start = (u64)-1; |
| 5929 | int data_stripes; |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5930 | int ret = 0; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5931 | |
| 5932 | ASSERT(op != BTRFS_MAP_DISCARD); |
| 5933 | |
| 5934 | em = btrfs_get_chunk_map(fs_info, logical, len); |
| 5935 | if (IS_ERR(em)) |
| 5936 | return PTR_ERR(em); |
| 5937 | |
| 5938 | map = em->map_lookup; |
| 5939 | /* Offset of this logical address in the chunk */ |
| 5940 | offset = logical - em->start; |
| 5941 | /* Len of a stripe in a chunk */ |
| 5942 | stripe_len = map->stripe_len; |
| 5943 | /* Stripe wher this block falls in */ |
| 5944 | stripe_nr = div64_u64(offset, stripe_len); |
| 5945 | /* Offset of stripe in the chunk */ |
| 5946 | stripe_offset = stripe_nr * stripe_len; |
| 5947 | if (offset < stripe_offset) { |
| 5948 | btrfs_crit(fs_info, |
| 5949 | "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu", |
| 5950 | stripe_offset, offset, em->start, logical, stripe_len); |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5951 | ret = -EINVAL; |
| 5952 | goto out; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5953 | } |
| 5954 | |
| 5955 | /* stripe_offset is the offset of this block in its stripe */ |
| 5956 | stripe_offset = offset - stripe_offset; |
| 5957 | data_stripes = nr_data_stripes(map); |
| 5958 | |
| 5959 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5960 | u64 max_len = stripe_len - stripe_offset; |
| 5961 | |
| 5962 | /* |
| 5963 | * In case of raid56, we need to know the stripe aligned start |
| 5964 | */ |
| 5965 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5966 | unsigned long full_stripe_len = stripe_len * data_stripes; |
| 5967 | raid56_full_stripe_start = offset; |
| 5968 | |
| 5969 | /* |
| 5970 | * Allow a write of a full stripe, but make sure we |
| 5971 | * don't allow straddling of stripes |
| 5972 | */ |
| 5973 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5974 | full_stripe_len); |
| 5975 | raid56_full_stripe_start *= full_stripe_len; |
| 5976 | |
| 5977 | /* |
| 5978 | * For writes to RAID[56], allow a full stripeset across |
| 5979 | * all disks. For other RAID types and for RAID[56] |
| 5980 | * reads, just allow a single stripe (on a single disk). |
| 5981 | */ |
| 5982 | if (op == BTRFS_MAP_WRITE) { |
| 5983 | max_len = stripe_len * data_stripes - |
| 5984 | (offset - raid56_full_stripe_start); |
| 5985 | } |
| 5986 | } |
| 5987 | len = min_t(u64, em->len - offset, max_len); |
| 5988 | } else { |
| 5989 | len = em->len - offset; |
| 5990 | } |
| 5991 | |
| 5992 | io_geom->len = len; |
| 5993 | io_geom->offset = offset; |
| 5994 | io_geom->stripe_len = stripe_len; |
| 5995 | io_geom->stripe_nr = stripe_nr; |
| 5996 | io_geom->stripe_offset = stripe_offset; |
| 5997 | io_geom->raid56_stripe_offset = raid56_full_stripe_start; |
| 5998 | |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5999 | out: |
| 6000 | /* once for us */ |
| 6001 | free_extent_map(em); |
| 6002 | return ret; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 6003 | } |
| 6004 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6005 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 6006 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6007 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6008 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6009 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6010 | { |
| 6011 | struct extent_map *em; |
| 6012 | struct map_lookup *map; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6013 | u64 stripe_offset; |
| 6014 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6015 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6016 | u32 stripe_index; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6017 | int data_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6018 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6019 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6020 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6021 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6022 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6023 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6024 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 6025 | int dev_replace_is_ongoing = 0; |
| 6026 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6027 | int patch_the_first_stripe_for_dev_replace = 0; |
| 6028 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6029 | u64 raid56_full_stripe_start = (u64)-1; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6030 | struct btrfs_io_geometry geom; |
| 6031 | |
| 6032 | ASSERT(bbio_ret); |
David Sterba | 75fb2e9 | 2018-08-03 00:36:29 +0200 | [diff] [blame] | 6033 | ASSERT(op != BTRFS_MAP_DISCARD); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6034 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6035 | ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom); |
| 6036 | if (ret < 0) |
| 6037 | return ret; |
| 6038 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6039 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Dan Carpenter | f113698 | 2019-08-09 17:07:39 +0300 | [diff] [blame] | 6040 | ASSERT(!IS_ERR(em)); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6041 | map = em->map_lookup; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6042 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6043 | *length = geom.len; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 6044 | stripe_len = geom.stripe_len; |
| 6045 | stripe_nr = geom.stripe_nr; |
| 6046 | stripe_offset = geom.stripe_offset; |
| 6047 | raid56_full_stripe_start = geom.raid56_stripe_offset; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6048 | data_stripes = nr_data_stripes(map); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6049 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6050 | down_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6051 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6052 | /* |
| 6053 | * Hold the semaphore for read during the whole operation, write is |
| 6054 | * requested at commit time but must wait. |
| 6055 | */ |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6056 | if (!dev_replace_is_ongoing) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6057 | up_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6058 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6059 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6060 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6061 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 6062 | dev_replace->srcdev->devid, |
| 6063 | &mirror_num, |
| 6064 | &physical_to_patch_in_first_stripe); |
| 6065 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6066 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 6067 | else |
| 6068 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6069 | } else if (mirror_num > map->num_stripes) { |
| 6070 | mirror_num = 0; |
| 6071 | } |
| 6072 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6073 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6074 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 6075 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6076 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6077 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6078 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6079 | mirror_num = 1; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 6080 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6081 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6082 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6083 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6084 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6085 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6086 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6087 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6088 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6089 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6090 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6091 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6092 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6093 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6094 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6095 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6096 | } else { |
| 6097 | mirror_num = 1; |
| 6098 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6099 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6100 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6101 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6102 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6103 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6104 | stripe_index *= map->sub_stripes; |
| 6105 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6106 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6107 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6108 | else if (mirror_num) |
| 6109 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6110 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6111 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6112 | stripe_index = find_live_mirror(fs_info, map, |
| 6113 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6114 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6115 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6116 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6117 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6118 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6119 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6120 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6121 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6122 | stripe_len * data_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6123 | |
| 6124 | /* RAID[56] write or recovery. Return all stripes */ |
| 6125 | num_stripes = map->num_stripes; |
| 6126 | max_errors = nr_parity_stripes(map); |
| 6127 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6128 | *length = map->stripe_len; |
| 6129 | stripe_index = 0; |
| 6130 | stripe_offset = 0; |
| 6131 | } else { |
| 6132 | /* |
| 6133 | * Mirror #0 or #1 means the original data block. |
| 6134 | * Mirror #2 is RAID5 parity block. |
| 6135 | * Mirror #3 is RAID6 Q block. |
| 6136 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6137 | stripe_nr = div_u64_rem(stripe_nr, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6138 | data_stripes, &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6139 | if (mirror_num > 1) |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6140 | stripe_index = data_stripes + mirror_num - 2; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6141 | |
| 6142 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6143 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 6144 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6145 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6146 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6147 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6148 | } else { |
| 6149 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6150 | * after this, stripe_nr is the number of stripes on this |
| 6151 | * device we have to walk to find the data, and stripe_index is |
| 6152 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6153 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6154 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6155 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6156 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6157 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6158 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6159 | btrfs_crit(fs_info, |
| 6160 | "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] | 6161 | stripe_index, map->num_stripes); |
| 6162 | ret = -EINVAL; |
| 6163 | goto out; |
| 6164 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6165 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6166 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6167 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6168 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6169 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6170 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6171 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6172 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6173 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6174 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6175 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6176 | if (!bbio) { |
| 6177 | ret = -ENOMEM; |
| 6178 | goto out; |
| 6179 | } |
Nikolay Borisov | 608769a | 2020-07-02 16:46:41 +0300 | [diff] [blame] | 6180 | |
| 6181 | for (i = 0; i < num_stripes; i++) { |
| 6182 | bbio->stripes[i].physical = map->stripes[stripe_index].physical + |
| 6183 | stripe_offset + stripe_nr * map->stripe_len; |
| 6184 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 6185 | stripe_index++; |
| 6186 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6187 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6188 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6189 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6190 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6191 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6192 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6193 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6194 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6195 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6196 | |
| 6197 | /* Fill in the logical address of each stripe */ |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6198 | tmp = stripe_nr * data_stripes; |
| 6199 | for (i = 0; i < data_stripes; i++) |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6200 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6201 | em->start + (tmp + i) * map->stripe_len; |
| 6202 | |
| 6203 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6204 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6205 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6206 | RAID6_Q_STRIPE; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6207 | |
Nikolay Borisov | 608769a | 2020-07-02 16:46:41 +0300 | [diff] [blame] | 6208 | sort_parity_stripes(bbio, num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6209 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6210 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6211 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6212 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6213 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6214 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6215 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6216 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6217 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6218 | } |
| 6219 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6220 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6221 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6222 | bbio->num_stripes = num_stripes; |
| 6223 | bbio->max_errors = max_errors; |
| 6224 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6225 | |
| 6226 | /* |
| 6227 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6228 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6229 | * available as a mirror |
| 6230 | */ |
| 6231 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6232 | WARN_ON(num_stripes > 1); |
| 6233 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6234 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6235 | bbio->mirror_num = map->num_stripes + 1; |
| 6236 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6237 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6238 | if (dev_replace_is_ongoing) { |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6239 | lockdep_assert_held(&dev_replace->rwsem); |
| 6240 | /* Unlock and let waiting writers proceed */ |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6241 | up_read(&dev_replace->rwsem); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6242 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6243 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6244 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6245 | } |
| 6246 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6247 | 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] | 6248 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6249 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6250 | { |
David Sterba | 75fb2e9 | 2018-08-03 00:36:29 +0200 | [diff] [blame] | 6251 | if (op == BTRFS_MAP_DISCARD) |
| 6252 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 6253 | length, bbio_ret); |
| 6254 | |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6255 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6256 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6257 | } |
| 6258 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6259 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6260 | 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] | 6261 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6262 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6263 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6264 | 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] | 6265 | } |
| 6266 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6267 | 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] | 6268 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6269 | bio->bi_private = bbio->private; |
| 6270 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6271 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6272 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6273 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6274 | } |
| 6275 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6276 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6277 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6278 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6279 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6280 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6281 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6282 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6283 | if (bio->bi_status == BLK_STS_IOERR || |
| 6284 | bio->bi_status == BLK_STS_TARGET) { |
Nikolay Borisov | c31efbd | 2020-07-03 11:14:27 +0300 | [diff] [blame] | 6285 | struct btrfs_device *dev = btrfs_io_bio(bio)->device; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6286 | |
Nikolay Borisov | 3eee86c | 2020-07-02 15:23:31 +0300 | [diff] [blame] | 6287 | ASSERT(dev->bdev); |
| 6288 | if (bio_op(bio) == REQ_OP_WRITE) |
| 6289 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6290 | BTRFS_DEV_STAT_WRITE_ERRS); |
Nikolay Borisov | 3eee86c | 2020-07-02 15:23:31 +0300 | [diff] [blame] | 6291 | else if (!(bio->bi_opf & REQ_RAHEAD)) |
| 6292 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6293 | BTRFS_DEV_STAT_READ_ERRS); |
Nikolay Borisov | 3eee86c | 2020-07-02 15:23:31 +0300 | [diff] [blame] | 6294 | if (bio->bi_opf & REQ_PREFLUSH) |
| 6295 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6296 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6297 | } |
| 6298 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6299 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6300 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6301 | is_orig_bio = 1; |
| 6302 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6303 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6304 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6305 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6306 | if (!is_orig_bio) { |
| 6307 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6308 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6309 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6310 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6311 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6312 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6313 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6314 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6315 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6316 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6317 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6318 | /* |
| 6319 | * this bio is actually up to date, we didn't |
| 6320 | * go over the max number of errors |
| 6321 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6322 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6323 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6324 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6325 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6326 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6327 | bio_put(bio); |
| 6328 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6329 | } |
| 6330 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6331 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
Nikolay Borisov | c31efbd | 2020-07-03 11:14:27 +0300 | [diff] [blame] | 6332 | u64 physical, struct btrfs_device *dev) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6333 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6334 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6335 | |
| 6336 | bio->bi_private = bbio; |
Nikolay Borisov | c31efbd | 2020-07-03 11:14:27 +0300 | [diff] [blame] | 6337 | btrfs_io_bio(bio)->device = dev; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6338 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6339 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6340 | btrfs_debug_in_rcu(fs_info, |
| 6341 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6342 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
David Sterba | 1db45a3 | 2019-11-28 15:31:46 +0100 | [diff] [blame] | 6343 | (unsigned long)dev->bdev->bd_dev, rcu_str_deref(dev->name), |
| 6344 | dev->devid, bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6345 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6346 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6347 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6348 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6349 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6350 | } |
| 6351 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6352 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6353 | { |
| 6354 | atomic_inc(&bbio->error); |
| 6355 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6356 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6357 | WARN_ON(bio != bbio->orig_bio); |
| 6358 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6359 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6360 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6361 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6362 | bio->bi_status = BLK_STS_IOERR; |
| 6363 | else |
| 6364 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6365 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6366 | } |
| 6367 | } |
| 6368 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6369 | 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] | 6370 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6371 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6372 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6373 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6374 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6375 | u64 length = 0; |
| 6376 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6377 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6378 | int dev_nr; |
| 6379 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6380 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6381 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6382 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6383 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6384 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6385 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6386 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6387 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6388 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6389 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6390 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6391 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6392 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6393 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6394 | bbio->orig_bio = first_bio; |
| 6395 | bbio->private = first_bio->bi_private; |
| 6396 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6397 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6398 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6399 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6400 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6401 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6402 | /* In this case, map_length has been set to the length of |
| 6403 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6404 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6405 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6406 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6407 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6408 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6409 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6410 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6411 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6412 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6413 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6414 | } |
| 6415 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6416 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6417 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6418 | "mapping failed logical %llu bio len %llu len %llu", |
| 6419 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6420 | BUG(); |
| 6421 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6422 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6423 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6424 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6425 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6426 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6427 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6428 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6429 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6430 | continue; |
| 6431 | } |
| 6432 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6433 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6434 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6435 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6436 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6437 | |
Nikolay Borisov | c31efbd | 2020-07-03 11:14:27 +0300 | [diff] [blame] | 6438 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, dev); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6439 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6440 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6441 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6442 | } |
| 6443 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6444 | /* |
| 6445 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 6446 | * return NULL. |
| 6447 | * |
| 6448 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 6449 | * only devid is used. |
| 6450 | * |
| 6451 | * If @seed is true, traverse through the seed devices. |
| 6452 | */ |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6453 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6454 | u64 devid, u8 *uuid, u8 *fsid, |
| 6455 | bool seed) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6456 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6457 | struct btrfs_device *device; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6458 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6459 | while (fs_devices) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6460 | if (!fsid || |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6461 | !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6462 | list_for_each_entry(device, &fs_devices->devices, |
| 6463 | dev_list) { |
| 6464 | if (device->devid == devid && |
| 6465 | (!uuid || memcmp(device->uuid, uuid, |
| 6466 | BTRFS_UUID_SIZE) == 0)) |
| 6467 | return device; |
| 6468 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6469 | } |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6470 | if (seed) |
| 6471 | fs_devices = fs_devices->seed; |
| 6472 | else |
| 6473 | return NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6474 | } |
| 6475 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6476 | } |
| 6477 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6478 | 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] | 6479 | u64 devid, u8 *dev_uuid) |
| 6480 | { |
| 6481 | struct btrfs_device *device; |
Josef Bacik | fccc000 | 2020-08-31 10:52:42 -0400 | [diff] [blame] | 6482 | unsigned int nofs_flag; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6483 | |
Josef Bacik | fccc000 | 2020-08-31 10:52:42 -0400 | [diff] [blame] | 6484 | /* |
| 6485 | * We call this under the chunk_mutex, so we want to use NOFS for this |
| 6486 | * allocation, however we don't want to change btrfs_alloc_device() to |
| 6487 | * always do NOFS because we use it in a lot of other GFP_KERNEL safe |
| 6488 | * places. |
| 6489 | */ |
| 6490 | nofs_flag = memalloc_nofs_save(); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6491 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
Josef Bacik | fccc000 | 2020-08-31 10:52:42 -0400 | [diff] [blame] | 6492 | memalloc_nofs_restore(nofs_flag); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6493 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6494 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6495 | |
| 6496 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6497 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6498 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6499 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6500 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6501 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6502 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6503 | return device; |
| 6504 | } |
| 6505 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6506 | /** |
| 6507 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6508 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6509 | * devid is provided (i.e. @devid != NULL). |
| 6510 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6511 | * is generated. |
| 6512 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6513 | * is generated. |
| 6514 | * |
| 6515 | * 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] | 6516 | * 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] | 6517 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6518 | */ |
| 6519 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6520 | const u64 *devid, |
| 6521 | const u8 *uuid) |
| 6522 | { |
| 6523 | struct btrfs_device *dev; |
| 6524 | u64 tmp; |
| 6525 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6526 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6527 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6528 | |
| 6529 | dev = __alloc_device(); |
| 6530 | if (IS_ERR(dev)) |
| 6531 | return dev; |
| 6532 | |
| 6533 | if (devid) |
| 6534 | tmp = *devid; |
| 6535 | else { |
| 6536 | int ret; |
| 6537 | |
| 6538 | ret = find_next_devid(fs_info, &tmp); |
| 6539 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6540 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6541 | return ERR_PTR(ret); |
| 6542 | } |
| 6543 | } |
| 6544 | dev->devid = tmp; |
| 6545 | |
| 6546 | if (uuid) |
| 6547 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6548 | else |
| 6549 | generate_random_uuid(dev->uuid); |
| 6550 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6551 | return dev; |
| 6552 | } |
| 6553 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6554 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6555 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6556 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6557 | if (error) |
| 6558 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6559 | devid, uuid); |
| 6560 | else |
| 6561 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6562 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6563 | } |
| 6564 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6565 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 6566 | { |
| 6567 | int index = btrfs_bg_flags_to_raid_index(type); |
| 6568 | int ncopies = btrfs_raid_array[index].ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6569 | const int nparity = btrfs_raid_array[index].nparity; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6570 | int data_stripes; |
| 6571 | |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6572 | if (nparity) |
| 6573 | data_stripes = num_stripes - nparity; |
| 6574 | else |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6575 | data_stripes = num_stripes / ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6576 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6577 | return div_u64(chunk_len, data_stripes); |
| 6578 | } |
| 6579 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6580 | 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] | 6581 | struct btrfs_chunk *chunk) |
| 6582 | { |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6583 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6584 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6585 | struct map_lookup *map; |
| 6586 | struct extent_map *em; |
| 6587 | u64 logical; |
| 6588 | u64 length; |
| 6589 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6590 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6591 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6592 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6593 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6594 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6595 | logical = key->offset; |
| 6596 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6597 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6598 | |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6599 | /* |
| 6600 | * Only need to verify chunk item if we're reading from sys chunk array, |
| 6601 | * as chunk item in tree block is already verified by tree-checker. |
| 6602 | */ |
| 6603 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { |
David Sterba | ddaf1d5 | 2019-03-20 16:40:48 +0100 | [diff] [blame] | 6604 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6605 | if (ret) |
| 6606 | return ret; |
| 6607 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6608 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6609 | read_lock(&map_tree->lock); |
| 6610 | em = lookup_extent_mapping(map_tree, logical, 1); |
| 6611 | read_unlock(&map_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6612 | |
| 6613 | /* already mapped? */ |
| 6614 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6615 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6616 | return 0; |
| 6617 | } else if (em) { |
| 6618 | free_extent_map(em); |
| 6619 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6620 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6621 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6622 | if (!em) |
| 6623 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6624 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6625 | if (!map) { |
| 6626 | free_extent_map(em); |
| 6627 | return -ENOMEM; |
| 6628 | } |
| 6629 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6630 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6631 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6632 | em->start = logical; |
| 6633 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6634 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6635 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6636 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6637 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6638 | map->num_stripes = num_stripes; |
| 6639 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6640 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6641 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6642 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6643 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6644 | map->verified_stripes = 0; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6645 | em->orig_block_len = calc_stripe_length(map->type, em->len, |
| 6646 | map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6647 | for (i = 0; i < num_stripes; i++) { |
| 6648 | map->stripes[i].physical = |
| 6649 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6650 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6651 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6652 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6653 | BTRFS_UUID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6654 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6655 | devid, uuid, NULL, true); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6656 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6657 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6658 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6659 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6660 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6661 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6662 | if (!map->stripes[i].dev) { |
| 6663 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6664 | add_missing_dev(fs_info->fs_devices, devid, |
| 6665 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6666 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6667 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6668 | btrfs_err(fs_info, |
| 6669 | "failed to init missing dev %llu: %ld", |
| 6670 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6671 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6672 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6673 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6674 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6675 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6676 | &(map->stripes[i].dev->dev_state)); |
| 6677 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6678 | } |
| 6679 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6680 | write_lock(&map_tree->lock); |
| 6681 | ret = add_extent_mapping(map_tree, em, 0); |
| 6682 | write_unlock(&map_tree->lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6683 | if (ret < 0) { |
| 6684 | btrfs_err(fs_info, |
| 6685 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6686 | em->start, em->len, ret); |
| 6687 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6688 | free_extent_map(em); |
| 6689 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6690 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6691 | } |
| 6692 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6693 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6694 | struct btrfs_dev_item *dev_item, |
| 6695 | struct btrfs_device *device) |
| 6696 | { |
| 6697 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6698 | |
| 6699 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6700 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6701 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6702 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6703 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6704 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6705 | device->type = btrfs_device_type(leaf, dev_item); |
| 6706 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6707 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6708 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6709 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6710 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6711 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6712 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6713 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6714 | } |
| 6715 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6716 | 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] | 6717 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6718 | { |
| 6719 | struct btrfs_fs_devices *fs_devices; |
| 6720 | int ret; |
| 6721 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6722 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6723 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6724 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6725 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6726 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6727 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6728 | return fs_devices; |
| 6729 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6730 | fs_devices = fs_devices->seed; |
| 6731 | } |
| 6732 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6733 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6734 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6735 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6736 | return ERR_PTR(-ENOENT); |
| 6737 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6738 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6739 | if (IS_ERR(fs_devices)) |
| 6740 | return fs_devices; |
| 6741 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 6742 | fs_devices->seeding = true; |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6743 | fs_devices->opened = 1; |
| 6744 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6745 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6746 | |
| 6747 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6748 | if (IS_ERR(fs_devices)) |
| 6749 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6750 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6751 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6752 | if (ret) { |
| 6753 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6754 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6755 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6756 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6757 | |
| 6758 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6759 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6760 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6761 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6762 | goto out; |
| 6763 | } |
| 6764 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6765 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6766 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6767 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6768 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6769 | } |
| 6770 | |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6771 | static int read_one_dev(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6772 | struct btrfs_dev_item *dev_item) |
| 6773 | { |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6774 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6775 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6776 | struct btrfs_device *device; |
| 6777 | u64 devid; |
| 6778 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6779 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6780 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6781 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6782 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6783 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6784 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6785 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6786 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6787 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6788 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6789 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6790 | if (IS_ERR(fs_devices)) |
| 6791 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6792 | } |
| 6793 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6794 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6795 | fs_uuid, true); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6796 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6797 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6798 | btrfs_report_missing_device(fs_info, devid, |
| 6799 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6800 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6801 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6802 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6803 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6804 | if (IS_ERR(device)) { |
| 6805 | btrfs_err(fs_info, |
| 6806 | "failed to add missing dev %llu: %ld", |
| 6807 | devid, PTR_ERR(device)); |
| 6808 | return PTR_ERR(device); |
| 6809 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6810 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6811 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6812 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6813 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6814 | btrfs_report_missing_device(fs_info, |
| 6815 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6816 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6817 | } |
| 6818 | btrfs_report_missing_device(fs_info, devid, |
| 6819 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6820 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6821 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6822 | if (!device->bdev && |
| 6823 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6824 | /* |
| 6825 | * this happens when a device that was properly setup |
| 6826 | * in the device info lists suddenly goes bad. |
| 6827 | * device->bdev is NULL, and so we have to set |
| 6828 | * device->missing to one here |
| 6829 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6830 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6831 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6832 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6833 | |
| 6834 | /* Move the device to its own fs_devices */ |
| 6835 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6836 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6837 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6838 | |
| 6839 | list_move(&device->dev_list, &fs_devices->devices); |
| 6840 | device->fs_devices->num_devices--; |
| 6841 | fs_devices->num_devices++; |
| 6842 | |
| 6843 | device->fs_devices->missing_devices--; |
| 6844 | fs_devices->missing_devices++; |
| 6845 | |
| 6846 | device->fs_devices = fs_devices; |
| 6847 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6848 | } |
| 6849 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6850 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6851 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6852 | if (device->generation != |
| 6853 | btrfs_device_generation(leaf, dev_item)) |
| 6854 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6855 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6856 | |
| 6857 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6858 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6859 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6860 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6861 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6862 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6863 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6864 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6865 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6866 | return ret; |
| 6867 | } |
| 6868 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6869 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6870 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6871 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6872 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6873 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6874 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6875 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6876 | u8 *array_ptr; |
| 6877 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6878 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6879 | u32 num_stripes; |
| 6880 | u32 array_size; |
| 6881 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6882 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6883 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6884 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6885 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6886 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6887 | /* |
| 6888 | * This will create extent buffer of nodesize, superblock size is |
| 6889 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6890 | * overallocate but we can keep it as-is, only the first page is used. |
| 6891 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6892 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6893 | if (IS_ERR(sb)) |
| 6894 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6895 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6896 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6897 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6898 | * 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] | 6899 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6900 | * pages up-to-date when the page is larger: extent does not cover the |
| 6901 | * whole page and consequently check_page_uptodate does not find all |
| 6902 | * the page's extents up-to-date (the hole beyond sb), |
| 6903 | * write_extent_buffer then triggers a WARN_ON. |
| 6904 | * |
| 6905 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6906 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6907 | * to silence the warning eg. on PowerPC 64. |
| 6908 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6909 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6910 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6911 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6912 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6913 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6914 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6915 | array_ptr = super_copy->sys_chunk_array; |
| 6916 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6917 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6918 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6919 | while (cur_offset < array_size) { |
| 6920 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6921 | len = sizeof(*disk_key); |
| 6922 | if (cur_offset + len > array_size) |
| 6923 | goto out_short_read; |
| 6924 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6925 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 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 | |
Johannes Thumshirn | 32ab3d1 | 2019-10-18 11:58:23 +0200 | [diff] [blame] | 6931 | if (key.type != BTRFS_CHUNK_ITEM_KEY) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6932 | btrfs_err(fs_info, |
| 6933 | "unexpected item type %u in sys_array at offset %u", |
| 6934 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6935 | ret = -EIO; |
| 6936 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6937 | } |
Johannes Thumshirn | 32ab3d1 | 2019-10-18 11:58:23 +0200 | [diff] [blame] | 6938 | |
| 6939 | chunk = (struct btrfs_chunk *)sb_array_offset; |
| 6940 | /* |
| 6941 | * At least one btrfs_chunk with one stripe must be present, |
| 6942 | * exact stripe count check comes afterwards |
| 6943 | */ |
| 6944 | len = btrfs_chunk_item_size(1); |
| 6945 | if (cur_offset + len > array_size) |
| 6946 | goto out_short_read; |
| 6947 | |
| 6948 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6949 | if (!num_stripes) { |
| 6950 | btrfs_err(fs_info, |
| 6951 | "invalid number of stripes %u in sys_array at offset %u", |
| 6952 | num_stripes, cur_offset); |
| 6953 | ret = -EIO; |
| 6954 | break; |
| 6955 | } |
| 6956 | |
| 6957 | type = btrfs_chunk_type(sb, chunk); |
| 6958 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
| 6959 | btrfs_err(fs_info, |
| 6960 | "invalid chunk type %llu in sys_array at offset %u", |
| 6961 | type, cur_offset); |
| 6962 | ret = -EIO; |
| 6963 | break; |
| 6964 | } |
| 6965 | |
| 6966 | len = btrfs_chunk_item_size(num_stripes); |
| 6967 | if (cur_offset + len > array_size) |
| 6968 | goto out_short_read; |
| 6969 | |
| 6970 | ret = read_one_chunk(&key, sb, chunk); |
| 6971 | if (ret) |
| 6972 | break; |
| 6973 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6974 | array_ptr += len; |
| 6975 | sb_array_offset += len; |
| 6976 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6977 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6978 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6979 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6980 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6981 | |
| 6982 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6983 | 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] | 6984 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6985 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6986 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6987 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6988 | } |
| 6989 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6990 | /* |
| 6991 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 6992 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6993 | * If the @failing_dev is specified, it's accounted as missing. |
| 6994 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6995 | * Return true if all chunks meet the minimal RW mount requirements. |
| 6996 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 6997 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6998 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 6999 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7000 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7001 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7002 | struct extent_map *em; |
| 7003 | u64 next_start = 0; |
| 7004 | bool ret = true; |
| 7005 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7006 | read_lock(&map_tree->lock); |
| 7007 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); |
| 7008 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7009 | /* No chunk at all? Return false anyway */ |
| 7010 | if (!em) { |
| 7011 | ret = false; |
| 7012 | goto out; |
| 7013 | } |
| 7014 | while (em) { |
| 7015 | struct map_lookup *map; |
| 7016 | int missing = 0; |
| 7017 | int max_tolerated; |
| 7018 | int i; |
| 7019 | |
| 7020 | map = em->map_lookup; |
| 7021 | max_tolerated = |
| 7022 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 7023 | map->type); |
| 7024 | for (i = 0; i < map->num_stripes; i++) { |
| 7025 | struct btrfs_device *dev = map->stripes[i].dev; |
| 7026 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 7027 | if (!dev || !dev->bdev || |
| 7028 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7029 | dev->last_flush_error) |
| 7030 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7031 | else if (failing_dev && failing_dev == dev) |
| 7032 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7033 | } |
| 7034 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 7035 | if (!failing_dev) |
| 7036 | btrfs_warn(fs_info, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 7037 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7038 | em->start, missing, max_tolerated); |
| 7039 | free_extent_map(em); |
| 7040 | ret = false; |
| 7041 | goto out; |
| 7042 | } |
| 7043 | next_start = extent_map_end(em); |
| 7044 | free_extent_map(em); |
| 7045 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7046 | read_lock(&map_tree->lock); |
| 7047 | em = lookup_extent_mapping(map_tree, next_start, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7048 | (u64)(-1) - next_start); |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7049 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 7050 | } |
| 7051 | out: |
| 7052 | return ret; |
| 7053 | } |
| 7054 | |
David Sterba | d85327b1 | 2020-07-08 22:55:14 +0200 | [diff] [blame] | 7055 | static void readahead_tree_node_children(struct extent_buffer *node) |
| 7056 | { |
| 7057 | int i; |
| 7058 | const int nr_items = btrfs_header_nritems(node); |
| 7059 | |
| 7060 | for (i = 0; i < nr_items; i++) { |
| 7061 | u64 start; |
| 7062 | |
| 7063 | start = btrfs_node_blockptr(node, i); |
| 7064 | readahead_tree_block(node->fs_info, start); |
| 7065 | } |
| 7066 | } |
| 7067 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7068 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7069 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 7070 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7071 | struct btrfs_path *path; |
| 7072 | struct extent_buffer *leaf; |
| 7073 | struct btrfs_key key; |
| 7074 | struct btrfs_key found_key; |
| 7075 | int ret; |
| 7076 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7077 | u64 total_dev = 0; |
David Sterba | d85327b1 | 2020-07-08 22:55:14 +0200 | [diff] [blame] | 7078 | u64 last_ra_node = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7079 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7080 | path = btrfs_alloc_path(); |
| 7081 | if (!path) |
| 7082 | return -ENOMEM; |
| 7083 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 7084 | /* |
| 7085 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 7086 | * otherwise we don't need it. |
| 7087 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7088 | mutex_lock(&uuid_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7089 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7090 | /* |
Boris Burkov | 48cfa61 | 2020-07-16 13:29:46 -0700 | [diff] [blame] | 7091 | * It is possible for mount and umount to race in such a way that |
| 7092 | * we execute this code path, but open_fs_devices failed to clear |
| 7093 | * total_rw_bytes. We certainly want it cleared before reading the |
| 7094 | * device items, so clear it here. |
| 7095 | */ |
| 7096 | fs_info->fs_devices->total_rw_bytes = 0; |
| 7097 | |
| 7098 | /* |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7099 | * Read all device items, and then all the chunk items. All |
| 7100 | * device items are found before any chunk item (their object id |
| 7101 | * is smaller than the lowest possible object id for a chunk |
| 7102 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7103 | */ |
| 7104 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7105 | key.offset = 0; |
| 7106 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7107 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7108 | if (ret < 0) |
| 7109 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7110 | while (1) { |
David Sterba | d85327b1 | 2020-07-08 22:55:14 +0200 | [diff] [blame] | 7111 | struct extent_buffer *node; |
| 7112 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7113 | leaf = path->nodes[0]; |
| 7114 | slot = path->slots[0]; |
| 7115 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7116 | ret = btrfs_next_leaf(root, path); |
| 7117 | if (ret == 0) |
| 7118 | continue; |
| 7119 | if (ret < 0) |
| 7120 | goto error; |
| 7121 | break; |
| 7122 | } |
David Sterba | d85327b1 | 2020-07-08 22:55:14 +0200 | [diff] [blame] | 7123 | /* |
| 7124 | * The nodes on level 1 are not locked but we don't need to do |
| 7125 | * that during mount time as nothing else can access the tree |
| 7126 | */ |
| 7127 | node = path->nodes[1]; |
| 7128 | if (node) { |
| 7129 | if (last_ra_node != node->start) { |
| 7130 | readahead_tree_node_children(node); |
| 7131 | last_ra_node = node->start; |
| 7132 | } |
| 7133 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7134 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7135 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7136 | struct btrfs_dev_item *dev_item; |
| 7137 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7138 | struct btrfs_dev_item); |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 7139 | ret = read_one_dev(leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7140 | if (ret) |
| 7141 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7142 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7143 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7144 | struct btrfs_chunk *chunk; |
| 7145 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
Josef Bacik | 01d01ca | 2020-07-17 15:12:28 -0400 | [diff] [blame] | 7146 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7147 | ret = read_one_chunk(&found_key, leaf, chunk); |
Josef Bacik | 01d01ca | 2020-07-17 15:12:28 -0400 | [diff] [blame] | 7148 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7149 | if (ret) |
| 7150 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7151 | } |
| 7152 | path->slots[0]++; |
| 7153 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7154 | |
| 7155 | /* |
| 7156 | * After loading chunk tree, we've got all device information, |
| 7157 | * do another round of validation checks. |
| 7158 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7159 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7160 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7161 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7162 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7163 | total_dev); |
| 7164 | ret = -EINVAL; |
| 7165 | goto error; |
| 7166 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7167 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7168 | fs_info->fs_devices->total_rw_bytes) { |
| 7169 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7170 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7171 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7172 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7173 | ret = -EINVAL; |
| 7174 | goto error; |
| 7175 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7176 | ret = 0; |
| 7177 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7178 | mutex_unlock(&uuid_mutex); |
| 7179 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7180 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7181 | return ret; |
| 7182 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7183 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7184 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7185 | { |
| 7186 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7187 | struct btrfs_device *device; |
| 7188 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7189 | while (fs_devices) { |
| 7190 | mutex_lock(&fs_devices->device_list_mutex); |
| 7191 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7192 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7193 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7194 | |
| 7195 | fs_devices = fs_devices->seed; |
| 7196 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7197 | } |
| 7198 | |
David Sterba | 1dc990d | 2019-08-21 20:05:32 +0200 | [diff] [blame] | 7199 | static u64 btrfs_dev_stats_value(const struct extent_buffer *eb, |
| 7200 | const struct btrfs_dev_stats_item *ptr, |
| 7201 | int index) |
| 7202 | { |
| 7203 | u64 val; |
| 7204 | |
| 7205 | read_extent_buffer(eb, &val, |
| 7206 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7207 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7208 | sizeof(val)); |
| 7209 | return val; |
| 7210 | } |
| 7211 | |
| 7212 | static void btrfs_set_dev_stats_value(struct extent_buffer *eb, |
| 7213 | struct btrfs_dev_stats_item *ptr, |
| 7214 | int index, u64 val) |
| 7215 | { |
| 7216 | write_extent_buffer(eb, &val, |
| 7217 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7218 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7219 | sizeof(val)); |
| 7220 | } |
| 7221 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7222 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7223 | { |
| 7224 | struct btrfs_key key; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7225 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7226 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7227 | struct extent_buffer *eb; |
| 7228 | int slot; |
| 7229 | int ret = 0; |
| 7230 | struct btrfs_device *device; |
| 7231 | struct btrfs_path *path = NULL; |
| 7232 | int i; |
| 7233 | |
| 7234 | path = btrfs_alloc_path(); |
Anand Jain | 3b80a984 | 2019-08-21 17:26:32 +0800 | [diff] [blame] | 7235 | if (!path) |
| 7236 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7237 | |
| 7238 | mutex_lock(&fs_devices->device_list_mutex); |
| 7239 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7240 | int item_size; |
| 7241 | struct btrfs_dev_stats_item *ptr; |
| 7242 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7243 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7244 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7245 | key.offset = device->devid; |
| 7246 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7247 | if (ret) { |
Anand Jain | ae4b9b4 | 2019-08-07 16:21:20 +0800 | [diff] [blame] | 7248 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7249 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7250 | device->dev_stats_valid = 1; |
| 7251 | btrfs_release_path(path); |
| 7252 | continue; |
| 7253 | } |
| 7254 | slot = path->slots[0]; |
| 7255 | eb = path->nodes[0]; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7256 | item_size = btrfs_item_size_nr(eb, slot); |
| 7257 | |
| 7258 | ptr = btrfs_item_ptr(eb, slot, |
| 7259 | struct btrfs_dev_stats_item); |
| 7260 | |
| 7261 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7262 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7263 | btrfs_dev_stat_set(device, i, |
| 7264 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7265 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7266 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7267 | } |
| 7268 | |
| 7269 | device->dev_stats_valid = 1; |
| 7270 | btrfs_dev_stat_print_on_load(device); |
| 7271 | btrfs_release_path(path); |
| 7272 | } |
| 7273 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7274 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7275 | btrfs_free_path(path); |
| 7276 | return ret < 0 ? ret : 0; |
| 7277 | } |
| 7278 | |
| 7279 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7280 | struct btrfs_device *device) |
| 7281 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7282 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7283 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7284 | struct btrfs_path *path; |
| 7285 | struct btrfs_key key; |
| 7286 | struct extent_buffer *eb; |
| 7287 | struct btrfs_dev_stats_item *ptr; |
| 7288 | int ret; |
| 7289 | int i; |
| 7290 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7291 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7292 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7293 | key.offset = device->devid; |
| 7294 | |
| 7295 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7296 | if (!path) |
| 7297 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7298 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7299 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7300 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7301 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7302 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7303 | goto out; |
| 7304 | } |
| 7305 | |
| 7306 | if (ret == 0 && |
| 7307 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7308 | /* need to delete old one and insert a new one */ |
| 7309 | ret = btrfs_del_item(trans, dev_root, path); |
| 7310 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7311 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7312 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7313 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7314 | goto out; |
| 7315 | } |
| 7316 | ret = 1; |
| 7317 | } |
| 7318 | |
| 7319 | if (ret == 1) { |
| 7320 | /* need to insert a new item */ |
| 7321 | btrfs_release_path(path); |
| 7322 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7323 | &key, sizeof(*ptr)); |
| 7324 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7325 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7326 | "insert dev_stats item for device %s failed %d", |
| 7327 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7328 | goto out; |
| 7329 | } |
| 7330 | } |
| 7331 | |
| 7332 | eb = path->nodes[0]; |
| 7333 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7334 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7335 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7336 | btrfs_dev_stat_read(device, i)); |
| 7337 | btrfs_mark_buffer_dirty(eb); |
| 7338 | |
| 7339 | out: |
| 7340 | btrfs_free_path(path); |
| 7341 | return ret; |
| 7342 | } |
| 7343 | |
| 7344 | /* |
| 7345 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7346 | */ |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7347 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7348 | { |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7349 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7350 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7351 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7352 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7353 | int ret = 0; |
| 7354 | |
| 7355 | mutex_lock(&fs_devices->device_list_mutex); |
| 7356 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7357 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7358 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7359 | continue; |
| 7360 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7361 | |
| 7362 | /* |
| 7363 | * There is a LOAD-LOAD control dependency between the value of |
| 7364 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7365 | * reading the in-memory counters. Such control dependencies |
| 7366 | * require explicit read memory barriers. |
| 7367 | * |
| 7368 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7369 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7370 | * barrier implied by atomic_xchg in |
| 7371 | * btrfs_dev_stats_read_and_reset |
| 7372 | */ |
| 7373 | smp_rmb(); |
| 7374 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7375 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7376 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7377 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7378 | } |
| 7379 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7380 | |
| 7381 | return ret; |
| 7382 | } |
| 7383 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7384 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7385 | { |
| 7386 | btrfs_dev_stat_inc(dev, index); |
| 7387 | btrfs_dev_stat_print_on_error(dev); |
| 7388 | } |
| 7389 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7390 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7391 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7392 | if (!dev->dev_stats_valid) |
| 7393 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7394 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7395 | "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] | 7396 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7397 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7398 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7399 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7400 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7401 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7402 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7403 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7404 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7405 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7406 | int i; |
| 7407 | |
| 7408 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7409 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7410 | break; |
| 7411 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7412 | return; /* all values == 0, suppress message */ |
| 7413 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7414 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7415 | "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] | 7416 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7417 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7418 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7419 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7420 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7421 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7422 | } |
| 7423 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7424 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7425 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7426 | { |
| 7427 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7428 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7429 | int i; |
| 7430 | |
| 7431 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7432 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
| 7433 | true); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7434 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7435 | |
| 7436 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7437 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7438 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7439 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7440 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7441 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7442 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7443 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7444 | if (stats->nr_items > i) |
| 7445 | stats->values[i] = |
| 7446 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7447 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7448 | btrfs_dev_stat_set(dev, i, 0); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7449 | } |
Anand Jain | a69976b | 2020-01-10 12:26:34 +0800 | [diff] [blame] | 7450 | btrfs_info(fs_info, "device stats zeroed by %s (%d)", |
| 7451 | current->comm, task_pid_nr(current)); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7452 | } else { |
| 7453 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7454 | if (stats->nr_items > i) |
| 7455 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7456 | } |
| 7457 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7458 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7459 | return 0; |
| 7460 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7461 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7462 | /* |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7463 | * Update the size and bytes used for each device where it changed. This is |
| 7464 | * delayed since we would otherwise get errors while writing out the |
| 7465 | * superblocks. |
| 7466 | * |
| 7467 | * Must be invoked during transaction commit. |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7468 | */ |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7469 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7470 | { |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7471 | struct btrfs_device *curr, *next; |
| 7472 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7473 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
| 7474 | |
| 7475 | if (list_empty(&trans->dev_update_list)) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7476 | return; |
| 7477 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7478 | /* |
| 7479 | * We don't need the device_list_mutex here. This list is owned by the |
| 7480 | * transaction and the transaction must complete before the device is |
| 7481 | * released. |
| 7482 | */ |
| 7483 | mutex_lock(&trans->fs_info->chunk_mutex); |
| 7484 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, |
| 7485 | post_commit_list) { |
| 7486 | list_del_init(&curr->post_commit_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7487 | curr->commit_total_bytes = curr->disk_total_bytes; |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7488 | curr->commit_bytes_used = curr->bytes_used; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7489 | } |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7490 | mutex_unlock(&trans->fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7491 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7492 | |
| 7493 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7494 | { |
| 7495 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7496 | while (fs_devices) { |
| 7497 | fs_devices->fs_info = fs_info; |
| 7498 | fs_devices = fs_devices->seed; |
| 7499 | } |
| 7500 | } |
| 7501 | |
| 7502 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7503 | { |
| 7504 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7505 | while (fs_devices) { |
| 7506 | fs_devices->fs_info = NULL; |
| 7507 | fs_devices = fs_devices->seed; |
| 7508 | } |
| 7509 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7510 | |
| 7511 | /* |
| 7512 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7513 | */ |
| 7514 | int btrfs_bg_type_to_factor(u64 flags) |
| 7515 | { |
David Sterba | 44b28ad | 2019-05-17 11:43:31 +0200 | [diff] [blame] | 7516 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 7517 | |
| 7518 | return btrfs_raid_array[index].ncopies; |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7519 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7520 | |
| 7521 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7522 | |
| 7523 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7524 | u64 chunk_offset, u64 devid, |
| 7525 | u64 physical_offset, u64 physical_len) |
| 7526 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7527 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7528 | struct extent_map *em; |
| 7529 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7530 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7531 | u64 stripe_len; |
| 7532 | bool found = false; |
| 7533 | int ret = 0; |
| 7534 | int i; |
| 7535 | |
| 7536 | read_lock(&em_tree->lock); |
| 7537 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7538 | read_unlock(&em_tree->lock); |
| 7539 | |
| 7540 | if (!em) { |
| 7541 | btrfs_err(fs_info, |
| 7542 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7543 | physical_offset, devid); |
| 7544 | ret = -EUCLEAN; |
| 7545 | goto out; |
| 7546 | } |
| 7547 | |
| 7548 | map = em->map_lookup; |
| 7549 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7550 | if (physical_len != stripe_len) { |
| 7551 | btrfs_err(fs_info, |
| 7552 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7553 | physical_offset, devid, em->start, physical_len, |
| 7554 | stripe_len); |
| 7555 | ret = -EUCLEAN; |
| 7556 | goto out; |
| 7557 | } |
| 7558 | |
| 7559 | for (i = 0; i < map->num_stripes; i++) { |
| 7560 | if (map->stripes[i].dev->devid == devid && |
| 7561 | map->stripes[i].physical == physical_offset) { |
| 7562 | found = true; |
| 7563 | if (map->verified_stripes >= map->num_stripes) { |
| 7564 | btrfs_err(fs_info, |
| 7565 | "too many dev extents for chunk %llu found", |
| 7566 | em->start); |
| 7567 | ret = -EUCLEAN; |
| 7568 | goto out; |
| 7569 | } |
| 7570 | map->verified_stripes++; |
| 7571 | break; |
| 7572 | } |
| 7573 | } |
| 7574 | if (!found) { |
| 7575 | btrfs_err(fs_info, |
| 7576 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7577 | physical_offset, devid); |
| 7578 | ret = -EUCLEAN; |
| 7579 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7580 | |
| 7581 | /* Make sure no dev extent is beyond device bondary */ |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7582 | dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7583 | if (!dev) { |
| 7584 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7585 | ret = -EUCLEAN; |
| 7586 | goto out; |
| 7587 | } |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7588 | |
| 7589 | /* It's possible this device is a dummy for seed device */ |
| 7590 | if (dev->disk_total_bytes == 0) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7591 | dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL, |
| 7592 | NULL, false); |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7593 | if (!dev) { |
| 7594 | btrfs_err(fs_info, "failed to find seed devid %llu", |
| 7595 | devid); |
| 7596 | ret = -EUCLEAN; |
| 7597 | goto out; |
| 7598 | } |
| 7599 | } |
| 7600 | |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7601 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7602 | btrfs_err(fs_info, |
| 7603 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7604 | devid, physical_offset, physical_len, |
| 7605 | dev->disk_total_bytes); |
| 7606 | ret = -EUCLEAN; |
| 7607 | goto out; |
| 7608 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7609 | out: |
| 7610 | free_extent_map(em); |
| 7611 | return ret; |
| 7612 | } |
| 7613 | |
| 7614 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7615 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7616 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7617 | struct extent_map *em; |
| 7618 | struct rb_node *node; |
| 7619 | int ret = 0; |
| 7620 | |
| 7621 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7622 | 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] | 7623 | em = rb_entry(node, struct extent_map, rb_node); |
| 7624 | if (em->map_lookup->num_stripes != |
| 7625 | em->map_lookup->verified_stripes) { |
| 7626 | btrfs_err(fs_info, |
| 7627 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7628 | em->start, em->map_lookup->verified_stripes, |
| 7629 | em->map_lookup->num_stripes); |
| 7630 | ret = -EUCLEAN; |
| 7631 | goto out; |
| 7632 | } |
| 7633 | } |
| 7634 | out: |
| 7635 | read_unlock(&em_tree->lock); |
| 7636 | return ret; |
| 7637 | } |
| 7638 | |
| 7639 | /* |
| 7640 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7641 | * later chunk allocation/free would cause unexpected behavior. |
| 7642 | * |
| 7643 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7644 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7645 | */ |
| 7646 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7647 | { |
| 7648 | struct btrfs_path *path; |
| 7649 | struct btrfs_root *root = fs_info->dev_root; |
| 7650 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7651 | u64 prev_devid = 0; |
| 7652 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7653 | int ret = 0; |
| 7654 | |
| 7655 | key.objectid = 1; |
| 7656 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7657 | key.offset = 0; |
| 7658 | |
| 7659 | path = btrfs_alloc_path(); |
| 7660 | if (!path) |
| 7661 | return -ENOMEM; |
| 7662 | |
| 7663 | path->reada = READA_FORWARD; |
| 7664 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7665 | if (ret < 0) |
| 7666 | goto out; |
| 7667 | |
| 7668 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7669 | ret = btrfs_next_item(root, path); |
| 7670 | if (ret < 0) |
| 7671 | goto out; |
| 7672 | /* No dev extents at all? Not good */ |
| 7673 | if (ret > 0) { |
| 7674 | ret = -EUCLEAN; |
| 7675 | goto out; |
| 7676 | } |
| 7677 | } |
| 7678 | while (1) { |
| 7679 | struct extent_buffer *leaf = path->nodes[0]; |
| 7680 | struct btrfs_dev_extent *dext; |
| 7681 | int slot = path->slots[0]; |
| 7682 | u64 chunk_offset; |
| 7683 | u64 physical_offset; |
| 7684 | u64 physical_len; |
| 7685 | u64 devid; |
| 7686 | |
| 7687 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7688 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7689 | break; |
| 7690 | devid = key.objectid; |
| 7691 | physical_offset = key.offset; |
| 7692 | |
| 7693 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7694 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7695 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7696 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7697 | /* Check if this dev extent overlaps with the previous one */ |
| 7698 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7699 | btrfs_err(fs_info, |
| 7700 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7701 | devid, physical_offset, prev_dev_ext_end); |
| 7702 | ret = -EUCLEAN; |
| 7703 | goto out; |
| 7704 | } |
| 7705 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7706 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7707 | physical_offset, physical_len); |
| 7708 | if (ret < 0) |
| 7709 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7710 | prev_devid = devid; |
| 7711 | prev_dev_ext_end = physical_offset + physical_len; |
| 7712 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7713 | ret = btrfs_next_item(root, path); |
| 7714 | if (ret < 0) |
| 7715 | goto out; |
| 7716 | if (ret > 0) { |
| 7717 | ret = 0; |
| 7718 | break; |
| 7719 | } |
| 7720 | } |
| 7721 | |
| 7722 | /* Ensure all chunks have corresponding dev extents */ |
| 7723 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7724 | out: |
| 7725 | btrfs_free_path(path); |
| 7726 | return ret; |
| 7727 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7728 | |
| 7729 | /* |
| 7730 | * Check whether the given block group or device is pinned by any inode being |
| 7731 | * used as a swapfile. |
| 7732 | */ |
| 7733 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7734 | { |
| 7735 | struct btrfs_swapfile_pin *sp; |
| 7736 | struct rb_node *node; |
| 7737 | |
| 7738 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7739 | node = fs_info->swapfile_pins.rb_node; |
| 7740 | while (node) { |
| 7741 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7742 | if (ptr < sp->ptr) |
| 7743 | node = node->rb_left; |
| 7744 | else if (ptr > sp->ptr) |
| 7745 | node = node->rb_right; |
| 7746 | else |
| 7747 | break; |
| 7748 | } |
| 7749 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7750 | return node != NULL; |
| 7751 | } |