David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
| 7 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9 | #include <linux/buffer_head.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 10 | #include <linux/blkdev.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 12 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 13 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 14 | #include <linux/semaphore.h> |
Andy Shevchenko | 8da4b8c | 2016-05-20 17:01:00 -0700 | [diff] [blame] | 15 | #include <linux/uuid.h> |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 16 | #include <linux/list_sort.h> |
David Sterba | 784352f | 2019-08-21 18:54:28 +0200 | [diff] [blame] | 17 | #include "misc.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 18 | #include "ctree.h" |
| 19 | #include "extent_map.h" |
| 20 | #include "disk-io.h" |
| 21 | #include "transaction.h" |
| 22 | #include "print-tree.h" |
| 23 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 24 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 25 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 26 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 27 | #include "rcu-string.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 28 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 29 | #include "sysfs.h" |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 30 | #include "tree-checker.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 31 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 32 | #include "block-group.h" |
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 | * |
| 249 | * does not protect: manipulation of the fs_devices::devices list! |
| 250 | * |
| 251 | * btrfs_device::name - renames (write side), read is RCU |
| 252 | * |
| 253 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 254 | * ------------------------------------------------ |
| 255 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 256 | * |
| 257 | * simple list traversal with read-only actions can be done with RCU protection |
| 258 | * |
| 259 | * may be used to exclude some operations from running concurrently without any |
| 260 | * modifications to the list (see write_all_supers) |
| 261 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 262 | * balance_mutex |
| 263 | * ------------- |
| 264 | * protects balance structures (status, state) and context accessed from |
| 265 | * several places (internally, ioctl) |
| 266 | * |
| 267 | * chunk_mutex |
| 268 | * ----------- |
| 269 | * protects chunks, adding or removing during allocation, trim or when a new |
Nikolay Borisov | 0b6f5d4 | 2019-05-09 18:11:11 +0300 | [diff] [blame] | 270 | * device is added/removed. Additionally it also protects post_commit_list of |
| 271 | * individual devices, since they can be added to the transaction's |
| 272 | * post_commit_list only with chunk_mutex held. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 273 | * |
| 274 | * cleaner_mutex |
| 275 | * ------------- |
| 276 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 277 | * cleaning together with relocation or delayed iputs |
| 278 | * |
| 279 | * |
| 280 | * Lock nesting |
| 281 | * ============ |
| 282 | * |
| 283 | * uuid_mutex |
| 284 | * volume_mutex |
| 285 | * device_list_mutex |
| 286 | * chunk_mutex |
| 287 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 288 | * |
| 289 | * |
| 290 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 291 | * ====================================== |
| 292 | * |
| 293 | * Maintains the exclusivity of the following operations that apply to the |
| 294 | * whole filesystem and cannot run in parallel. |
| 295 | * |
| 296 | * - Balance (*) |
| 297 | * - Device add |
| 298 | * - Device remove |
| 299 | * - Device replace (*) |
| 300 | * - Resize |
| 301 | * |
| 302 | * The device operations (as above) can be in one of the following states: |
| 303 | * |
| 304 | * - Running state |
| 305 | * - Paused state |
| 306 | * - Completed state |
| 307 | * |
| 308 | * Only device operations marked with (*) can go into the Paused state for the |
| 309 | * following reasons: |
| 310 | * |
| 311 | * - ioctl (only Balance can be Paused through ioctl) |
| 312 | * - filesystem remounted as read-only |
| 313 | * - filesystem unmounted and mounted as read-only |
| 314 | * - system power-cycle and filesystem mounted as read-only |
| 315 | * - filesystem or device errors leading to forced read-only |
| 316 | * |
| 317 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 318 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 319 | * A device operation in Paused or Running state can be canceled or resumed |
| 320 | * either by ioctl (Balance only) or when remounted as read-write. |
| 321 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 322 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 323 | */ |
| 324 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 325 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 326 | static LIST_HEAD(fs_uuids); |
David Sterba | 4143cb8 | 2019-10-01 19:57:37 +0200 | [diff] [blame] | 327 | struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 328 | { |
| 329 | return &fs_uuids; |
| 330 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 331 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 332 | /* |
| 333 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 334 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 335 | * @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] | 336 | * |
| 337 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 338 | * The returned struct is not linked onto any lists and can be destroyed with |
| 339 | * kfree() right away. |
| 340 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 341 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 342 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 343 | { |
| 344 | struct btrfs_fs_devices *fs_devs; |
| 345 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 346 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 347 | if (!fs_devs) |
| 348 | return ERR_PTR(-ENOMEM); |
| 349 | |
| 350 | mutex_init(&fs_devs->device_list_mutex); |
| 351 | |
| 352 | INIT_LIST_HEAD(&fs_devs->devices); |
| 353 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 354 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 355 | if (fsid) |
| 356 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 357 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 358 | if (metadata_fsid) |
| 359 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 360 | else if (fsid) |
| 361 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 362 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 363 | return fs_devs; |
| 364 | } |
| 365 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 366 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 367 | { |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 368 | WARN_ON(!list_empty(&device->post_commit_list)); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 369 | rcu_string_free(device->name); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 370 | extent_io_tree_release(&device->alloc_state); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 371 | bio_put(device->flush_bio); |
| 372 | kfree(device); |
| 373 | } |
| 374 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 375 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 376 | { |
| 377 | struct btrfs_device *device; |
| 378 | WARN_ON(fs_devices->opened); |
| 379 | while (!list_empty(&fs_devices->devices)) { |
| 380 | device = list_entry(fs_devices->devices.next, |
| 381 | struct btrfs_device, dev_list); |
| 382 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 383 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 384 | } |
| 385 | kfree(fs_devices); |
| 386 | } |
| 387 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 388 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 389 | { |
| 390 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 391 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 392 | while (!list_empty(&fs_uuids)) { |
| 393 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 394 | struct btrfs_fs_devices, fs_list); |
| 395 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 396 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 397 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 398 | } |
| 399 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 400 | /* |
| 401 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 402 | * 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] | 403 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 404 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 405 | static struct btrfs_device *__alloc_device(void) |
| 406 | { |
| 407 | struct btrfs_device *dev; |
| 408 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 409 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 410 | if (!dev) |
| 411 | return ERR_PTR(-ENOMEM); |
| 412 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 413 | /* |
| 414 | * Preallocate a bio that's always going to be used for flushing device |
| 415 | * barriers and matches the device lifespan |
| 416 | */ |
| 417 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 418 | if (!dev->flush_bio) { |
| 419 | kfree(dev); |
| 420 | return ERR_PTR(-ENOMEM); |
| 421 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 422 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 423 | INIT_LIST_HEAD(&dev->dev_list); |
| 424 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 425 | INIT_LIST_HEAD(&dev->post_commit_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 426 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 427 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 428 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 429 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 430 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 431 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 432 | extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 433 | |
| 434 | return dev; |
| 435 | } |
| 436 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 437 | static noinline struct btrfs_fs_devices *find_fsid( |
| 438 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 439 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 440 | struct btrfs_fs_devices *fs_devices; |
| 441 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 442 | ASSERT(fsid); |
| 443 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 444 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 445 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 446 | if (metadata_fsid) { |
| 447 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 448 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 449 | BTRFS_FSID_SIZE) == 0) |
| 450 | return fs_devices; |
| 451 | } else { |
| 452 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 453 | return fs_devices; |
| 454 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 455 | } |
| 456 | return NULL; |
| 457 | } |
| 458 | |
Su Yue | c6730a0 | 2020-01-10 14:11:33 +0200 | [diff] [blame] | 459 | static struct btrfs_fs_devices *find_fsid_with_metadata_uuid( |
| 460 | struct btrfs_super_block *disk_super) |
| 461 | { |
| 462 | |
| 463 | struct btrfs_fs_devices *fs_devices; |
| 464 | |
| 465 | /* |
| 466 | * Handle scanned device having completed its fsid change but |
| 467 | * belonging to a fs_devices that was created by first scanning |
| 468 | * a device which didn't have its fsid/metadata_uuid changed |
| 469 | * at all and the CHANGING_FSID_V2 flag set. |
| 470 | */ |
| 471 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 472 | if (fs_devices->fsid_change && |
| 473 | memcmp(disk_super->metadata_uuid, fs_devices->fsid, |
| 474 | BTRFS_FSID_SIZE) == 0 && |
| 475 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 476 | BTRFS_FSID_SIZE) == 0) { |
| 477 | return fs_devices; |
| 478 | } |
| 479 | } |
| 480 | /* |
| 481 | * Handle scanned device having completed its fsid change but |
| 482 | * belonging to a fs_devices that was created by a device that |
| 483 | * has an outdated pair of fsid/metadata_uuid and |
| 484 | * CHANGING_FSID_V2 flag set. |
| 485 | */ |
| 486 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 487 | if (fs_devices->fsid_change && |
| 488 | memcmp(fs_devices->metadata_uuid, |
| 489 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 490 | memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid, |
| 491 | BTRFS_FSID_SIZE) == 0) { |
| 492 | return fs_devices; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return find_fsid(disk_super->fsid, disk_super->metadata_uuid); |
| 497 | } |
| 498 | |
| 499 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 500 | static int |
| 501 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 502 | int flush, struct block_device **bdev, |
| 503 | struct buffer_head **bh) |
| 504 | { |
| 505 | int ret; |
| 506 | |
| 507 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 508 | |
| 509 | if (IS_ERR(*bdev)) { |
| 510 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 511 | goto error; |
| 512 | } |
| 513 | |
| 514 | if (flush) |
| 515 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 516 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 517 | if (ret) { |
| 518 | blkdev_put(*bdev, flags); |
| 519 | goto error; |
| 520 | } |
| 521 | invalidate_bdev(*bdev); |
| 522 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 523 | if (IS_ERR(*bh)) { |
| 524 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 525 | blkdev_put(*bdev, flags); |
| 526 | goto error; |
| 527 | } |
| 528 | |
| 529 | return 0; |
| 530 | |
| 531 | error: |
| 532 | *bdev = NULL; |
| 533 | *bh = NULL; |
| 534 | return ret; |
| 535 | } |
| 536 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 537 | static bool device_path_matched(const char *path, struct btrfs_device *device) |
| 538 | { |
| 539 | int found; |
| 540 | |
| 541 | rcu_read_lock(); |
| 542 | found = strcmp(rcu_str_deref(device->name), path); |
| 543 | rcu_read_unlock(); |
| 544 | |
| 545 | return found == 0; |
| 546 | } |
| 547 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 548 | /* |
| 549 | * Search and remove all stale (devices which are not mounted) devices. |
| 550 | * When both inputs are NULL, it will search and release all stale devices. |
| 551 | * path: Optional. When provided will it release all unmounted devices |
| 552 | * matching this path only. |
| 553 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 554 | * devices. |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 555 | * Return: 0 for success or if @path is NULL. |
| 556 | * -EBUSY if @path is a mounted device. |
| 557 | * -ENOENT if @path does not match any device in the list. |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 558 | */ |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 559 | static int btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 560 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 561 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 562 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 563 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 564 | int ret = 0; |
| 565 | |
| 566 | if (path) |
| 567 | ret = -ENOENT; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 568 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 569 | 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] | 570 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 571 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 572 | list_for_each_entry_safe(device, tmp_device, |
| 573 | &fs_devices->devices, dev_list) { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 574 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 575 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 576 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 577 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 578 | if (path && !device_path_matched(path, device)) |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 579 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 580 | if (fs_devices->opened) { |
| 581 | /* for an already deleted device return 0 */ |
| 582 | if (path && ret != 0) |
| 583 | ret = -EBUSY; |
| 584 | break; |
| 585 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 586 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 587 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 588 | fs_devices->num_devices--; |
| 589 | list_del(&device->dev_list); |
| 590 | btrfs_free_device(device); |
| 591 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 592 | ret = 0; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 593 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 594 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 595 | } |
| 596 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 597 | |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 598 | if (fs_devices->num_devices == 0) { |
| 599 | btrfs_sysfs_remove_fsid(fs_devices); |
| 600 | list_del(&fs_devices->fs_list); |
| 601 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 602 | } |
| 603 | } |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 604 | |
| 605 | return ret; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 606 | } |
| 607 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 608 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 609 | struct btrfs_device *device, fmode_t flags, |
| 610 | void *holder) |
| 611 | { |
| 612 | struct request_queue *q; |
| 613 | struct block_device *bdev; |
| 614 | struct buffer_head *bh; |
| 615 | struct btrfs_super_block *disk_super; |
| 616 | u64 devid; |
| 617 | int ret; |
| 618 | |
| 619 | if (device->bdev) |
| 620 | return -EINVAL; |
| 621 | if (!device->name) |
| 622 | return -EINVAL; |
| 623 | |
| 624 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 625 | &bdev, &bh); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | |
| 629 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 630 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 631 | if (devid != device->devid) |
| 632 | goto error_brelse; |
| 633 | |
| 634 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 635 | goto error_brelse; |
| 636 | |
| 637 | device->generation = btrfs_super_generation(disk_super); |
| 638 | |
| 639 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 640 | if (btrfs_super_incompat_flags(disk_super) & |
| 641 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 642 | pr_err( |
| 643 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| 644 | goto error_brelse; |
| 645 | } |
| 646 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 647 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 648 | fs_devices->seeding = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 649 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 650 | if (bdev_read_only(bdev)) |
| 651 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 652 | else |
| 653 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 657 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 658 | fs_devices->rotating = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 659 | |
| 660 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 661 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 662 | device->mode = flags; |
| 663 | |
| 664 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 665 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 666 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 667 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 668 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 669 | } |
| 670 | brelse(bh); |
| 671 | |
| 672 | return 0; |
| 673 | |
| 674 | error_brelse: |
| 675 | brelse(bh); |
| 676 | blkdev_put(bdev, flags); |
| 677 | |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 681 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 682 | * 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] | 683 | * being created with a disk that has already completed its fsid change. Such |
| 684 | * disk can belong to an fs which has its FSID changed or to one which doesn't. |
| 685 | * Handle both cases here. |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 686 | */ |
| 687 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 688 | struct btrfs_super_block *disk_super) |
| 689 | { |
| 690 | struct btrfs_fs_devices *fs_devices; |
| 691 | |
| 692 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 693 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 694 | BTRFS_FSID_SIZE) != 0 && |
| 695 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 696 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 697 | return fs_devices; |
| 698 | } |
| 699 | } |
| 700 | |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 701 | return find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 702 | } |
| 703 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 704 | |
| 705 | static struct btrfs_fs_devices *find_fsid_changed( |
| 706 | struct btrfs_super_block *disk_super) |
| 707 | { |
| 708 | struct btrfs_fs_devices *fs_devices; |
| 709 | |
| 710 | /* |
| 711 | * Handles the case where scanned device is part of an fs that had |
| 712 | * multiple successful changes of FSID but curently device didn't |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 713 | * observe it. Meaning our fsid will be different than theirs. We need |
| 714 | * to handle two subcases : |
| 715 | * 1 - The fs still continues to have different METADATA/FSID uuids. |
| 716 | * 2 - The fs is switched back to its original FSID (METADATA/FSID |
| 717 | * are equal). |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 718 | */ |
| 719 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 720 | /* Changed UUIDs */ |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 721 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 722 | BTRFS_FSID_SIZE) != 0 && |
| 723 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 724 | BTRFS_FSID_SIZE) == 0 && |
| 725 | memcmp(fs_devices->fsid, disk_super->fsid, |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 726 | BTRFS_FSID_SIZE) != 0) |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 727 | return fs_devices; |
Nikolay Borisov | 0584071 | 2020-01-10 14:11:34 +0200 | [diff] [blame] | 728 | |
| 729 | /* Unchanged UUIDs */ |
| 730 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 731 | BTRFS_FSID_SIZE) == 0 && |
| 732 | memcmp(fs_devices->fsid, disk_super->metadata_uuid, |
| 733 | BTRFS_FSID_SIZE) == 0) |
| 734 | return fs_devices; |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | return NULL; |
| 738 | } |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 739 | |
| 740 | static struct btrfs_fs_devices *find_fsid_reverted_metadata( |
| 741 | struct btrfs_super_block *disk_super) |
| 742 | { |
| 743 | struct btrfs_fs_devices *fs_devices; |
| 744 | |
| 745 | /* |
| 746 | * Handle the case where the scanned device is part of an fs whose last |
| 747 | * metadata UUID change reverted it to the original FSID. At the same |
| 748 | * time * fs_devices was first created by another constitutent device |
| 749 | * which didn't fully observe the operation. This results in an |
| 750 | * btrfs_fs_devices created with metadata/fsid different AND |
| 751 | * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the |
| 752 | * fs_devices equal to the FSID of the disk. |
| 753 | */ |
| 754 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 755 | if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 756 | BTRFS_FSID_SIZE) != 0 && |
| 757 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 758 | BTRFS_FSID_SIZE) == 0 && |
| 759 | fs_devices->fsid_change) |
| 760 | return fs_devices; |
| 761 | } |
| 762 | |
| 763 | return NULL; |
| 764 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 765 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 766 | * Add new device to list of registered devices |
| 767 | * |
| 768 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 769 | * device pointer which was just added or updated when successful |
| 770 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 771 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 772 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 773 | struct btrfs_super_block *disk_super, |
| 774 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 775 | { |
| 776 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 777 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 778 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 779 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 780 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 781 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 782 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 783 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 784 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 785 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 786 | if (fsid_change_in_progress) { |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 787 | if (!has_metadata_uuid) |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 788 | fs_devices = find_fsid_inprogress(disk_super); |
Su Yue | c0d81c7 | 2020-01-10 14:11:32 +0200 | [diff] [blame] | 789 | else |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 790 | fs_devices = find_fsid_changed(disk_super); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 791 | } else if (has_metadata_uuid) { |
Su Yue | c6730a0 | 2020-01-10 14:11:33 +0200 | [diff] [blame] | 792 | fs_devices = find_fsid_with_metadata_uuid(disk_super); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 793 | } else { |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 794 | fs_devices = find_fsid_reverted_metadata(disk_super); |
| 795 | if (!fs_devices) |
| 796 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 799 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 800 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 801 | if (has_metadata_uuid) |
| 802 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 803 | disk_super->metadata_uuid); |
| 804 | else |
| 805 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 806 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 807 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 808 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 809 | |
Al Viro | 92900e5 | 2019-01-27 04:58:00 +0000 | [diff] [blame] | 810 | fs_devices->fsid_change = fsid_change_in_progress; |
| 811 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 812 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 813 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 814 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 815 | device = NULL; |
| 816 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 817 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 818 | device = btrfs_find_device(fs_devices, devid, |
| 819 | disk_super->dev_item.uuid, NULL, false); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 820 | |
| 821 | /* |
| 822 | * If this disk has been pulled into an fs devices created by |
| 823 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 824 | * metadata_uuid/fsid values of the fs_devices. |
| 825 | */ |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 826 | if (fs_devices->fsid_change && |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 827 | found_transid > fs_devices->latest_generation) { |
| 828 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 829 | BTRFS_FSID_SIZE); |
Nikolay Borisov | 1362089 | 2020-01-10 14:11:35 +0200 | [diff] [blame] | 830 | |
| 831 | if (has_metadata_uuid) |
| 832 | memcpy(fs_devices->metadata_uuid, |
| 833 | disk_super->metadata_uuid, |
| 834 | BTRFS_FSID_SIZE); |
| 835 | else |
| 836 | memcpy(fs_devices->metadata_uuid, |
| 837 | disk_super->fsid, BTRFS_FSID_SIZE); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 838 | |
| 839 | fs_devices->fsid_change = false; |
| 840 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 841 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 842 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 843 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 844 | if (fs_devices->opened) { |
| 845 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 846 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 847 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 848 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 849 | device = btrfs_alloc_device(NULL, &devid, |
| 850 | disk_super->dev_item.uuid); |
| 851 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 852 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 853 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 854 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 855 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 856 | |
| 857 | name = rcu_string_strdup(path, GFP_NOFS); |
| 858 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 859 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 860 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 861 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 862 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 863 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 864 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 865 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 866 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 867 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 868 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 869 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 870 | |
| 871 | if (disk_super->label[0]) |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 872 | pr_info( |
| 873 | "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n", |
| 874 | disk_super->label, devid, found_transid, path, |
| 875 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 876 | else |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 877 | pr_info( |
| 878 | "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n", |
| 879 | disk_super->fsid, devid, found_transid, path, |
| 880 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 881 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 882 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 883 | /* |
| 884 | * When FS is already mounted. |
| 885 | * 1. If you are here and if the device->name is NULL that |
| 886 | * means this device was missing at time of FS mount. |
| 887 | * 2. If you are here and if the device->name is different |
| 888 | * from 'path' that means either |
| 889 | * a. The same device disappeared and reappeared with |
| 890 | * different name. or |
| 891 | * b. The missing-disk-which-was-replaced, has |
| 892 | * reappeared now. |
| 893 | * |
| 894 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 895 | * and unintentional. |
| 896 | * |
| 897 | * Further in case of 1 and 2a above, the disk at 'path' |
| 898 | * would have missed some transaction when it was away and |
| 899 | * in case of 2a the stale bdev has to be updated as well. |
| 900 | * 2b must not be allowed at all time. |
| 901 | */ |
| 902 | |
| 903 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 904 | * For now, we do allow update to btrfs_fs_device through the |
| 905 | * btrfs dev scan cli after FS has been mounted. We're still |
| 906 | * tracking a problem where systems fail mount by subvolume id |
| 907 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 908 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 909 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 910 | /* |
| 911 | * That is if the FS is _not_ mounted and if you |
| 912 | * are here, that means there is more than one |
| 913 | * disk with same uuid and devid.We keep the one |
| 914 | * with larger generation number or the last-in if |
| 915 | * generation are equal. |
| 916 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 917 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 918 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 919 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 920 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 921 | /* |
| 922 | * We are going to replace the device path for a given devid, |
| 923 | * make sure it's the same device if the device is mounted |
| 924 | */ |
| 925 | if (device->bdev) { |
| 926 | struct block_device *path_bdev; |
| 927 | |
| 928 | path_bdev = lookup_bdev(path); |
| 929 | if (IS_ERR(path_bdev)) { |
| 930 | mutex_unlock(&fs_devices->device_list_mutex); |
| 931 | return ERR_CAST(path_bdev); |
| 932 | } |
| 933 | |
| 934 | if (device->bdev != path_bdev) { |
| 935 | bdput(path_bdev); |
| 936 | mutex_unlock(&fs_devices->device_list_mutex); |
| 937 | btrfs_warn_in_rcu(device->fs_info, |
| 938 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 939 | disk_super->fsid, devid, |
| 940 | rcu_str_deref(device->name), path); |
| 941 | return ERR_PTR(-EEXIST); |
| 942 | } |
| 943 | bdput(path_bdev); |
| 944 | btrfs_info_in_rcu(device->fs_info, |
| 945 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 946 | disk_super->fsid, devid, |
| 947 | rcu_str_deref(device->name), path); |
| 948 | } |
| 949 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 950 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 951 | if (!name) { |
| 952 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 953 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 954 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 955 | rcu_string_free(device->name); |
| 956 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 957 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 958 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 959 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 960 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 961 | } |
| 962 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 963 | /* |
| 964 | * Unmount does not free the btrfs_device struct but would zero |
| 965 | * generation along with most of the other members. So just update |
| 966 | * it back. We need it to pick the disk with largest generation |
| 967 | * (as above). |
| 968 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 969 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 970 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 971 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 972 | fs_devices->latest_generation); |
| 973 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 974 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 975 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 976 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 977 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 978 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 979 | } |
| 980 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 981 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 982 | { |
| 983 | struct btrfs_fs_devices *fs_devices; |
| 984 | struct btrfs_device *device; |
| 985 | struct btrfs_device *orig_dev; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 986 | int ret = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 987 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 988 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 989 | if (IS_ERR(fs_devices)) |
| 990 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 991 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 992 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 993 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 994 | |
| 995 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 996 | struct rcu_string *name; |
| 997 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 998 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 999 | orig_dev->uuid); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1000 | if (IS_ERR(device)) { |
| 1001 | ret = PTR_ERR(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1002 | goto error; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1003 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1004 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 1005 | /* |
| 1006 | * This is ok to do without rcu read locked because we hold the |
| 1007 | * uuid mutex so nothing we touch in here is going to disappear. |
| 1008 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1009 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 1010 | name = rcu_string_strdup(orig_dev->name->str, |
| 1011 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1012 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1013 | btrfs_free_device(device); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1014 | ret = -ENOMEM; |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 1015 | goto error; |
| 1016 | } |
| 1017 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 1018 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1019 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1020 | list_add(&device->dev_list, &fs_devices->devices); |
| 1021 | device->fs_devices = fs_devices; |
| 1022 | fs_devices->num_devices++; |
| 1023 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1024 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1025 | return fs_devices; |
| 1026 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1027 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1028 | free_fs_devices(fs_devices); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 1029 | return ERR_PTR(ret); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1030 | } |
| 1031 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1032 | /* |
| 1033 | * After we have read the system tree and know devids belonging to |
| 1034 | * this filesystem, remove the device which does not belong there. |
| 1035 | */ |
| 1036 | 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] | 1037 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1038 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1039 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1040 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1041 | mutex_lock(&uuid_mutex); |
| 1042 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1043 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1044 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1045 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1046 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1047 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1048 | &device->dev_state) && |
| 1049 | (!latest_dev || |
| 1050 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1051 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1052 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1053 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1054 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1055 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1056 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1057 | /* |
| 1058 | * In the first step, keep the device which has |
| 1059 | * the correct fsid and the devid that is used |
| 1060 | * for the dev_replace procedure. |
| 1061 | * In the second step, the dev_replace state is |
| 1062 | * read from the device tree and it is known |
| 1063 | * whether the procedure is really active or |
| 1064 | * not, which means whether this device is |
| 1065 | * used or whether it should be removed. |
| 1066 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1067 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1068 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1069 | continue; |
| 1070 | } |
| 1071 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1072 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1073 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1074 | device->bdev = NULL; |
| 1075 | fs_devices->open_devices--; |
| 1076 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1077 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1078 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1079 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1080 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1081 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1082 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1083 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1084 | list_del_init(&device->dev_list); |
| 1085 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1086 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1087 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1088 | |
| 1089 | if (fs_devices->seed) { |
| 1090 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1091 | goto again; |
| 1092 | } |
| 1093 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1094 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1095 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1096 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1097 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1098 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1099 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1100 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1101 | if (!device->bdev) |
| 1102 | return; |
| 1103 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1104 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1105 | sync_blockdev(device->bdev); |
| 1106 | invalidate_bdev(device->bdev); |
| 1107 | } |
| 1108 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1109 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1112 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1113 | { |
| 1114 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1115 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1116 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1117 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1118 | list_del_init(&device->dev_alloc_list); |
| 1119 | fs_devices->rw_devices--; |
| 1120 | } |
| 1121 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1122 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1123 | fs_devices->missing_devices--; |
| 1124 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1125 | btrfs_close_bdev(device); |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1126 | if (device->bdev) { |
Johannes Thumshirn | 3fff397 | 2019-11-26 09:40:05 +0100 | [diff] [blame] | 1127 | fs_devices->open_devices--; |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1128 | device->bdev = NULL; |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1129 | } |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1130 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1131 | |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1132 | device->fs_info = NULL; |
| 1133 | atomic_set(&device->dev_stats_ccnt, 0); |
| 1134 | extent_io_tree_release(&device->alloc_state); |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1135 | |
Johannes Thumshirn | 321f69f | 2019-12-04 14:36:39 +0100 | [diff] [blame] | 1136 | /* Verify the device is back in a pristine state */ |
| 1137 | ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state)); |
| 1138 | ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
| 1139 | ASSERT(list_empty(&device->dev_alloc_list)); |
| 1140 | ASSERT(list_empty(&device->post_commit_list)); |
| 1141 | ASSERT(atomic_read(&device->reada_in_flight) == 0); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1144 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1145 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1146 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1147 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1148 | if (--fs_devices->opened > 0) |
| 1149 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1150 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1151 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1152 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1153 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1154 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1155 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1156 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1157 | WARN_ON(fs_devices->open_devices); |
| 1158 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1159 | fs_devices->opened = 0; |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 1160 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1161 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1165 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1166 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1167 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1168 | int ret; |
| 1169 | |
| 1170 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1171 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1172 | if (!fs_devices->opened) { |
| 1173 | seed_devices = fs_devices->seed; |
| 1174 | fs_devices->seed = NULL; |
| 1175 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1176 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1177 | |
| 1178 | while (seed_devices) { |
| 1179 | fs_devices = seed_devices; |
| 1180 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1181 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1182 | free_fs_devices(fs_devices); |
| 1183 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1184 | return ret; |
| 1185 | } |
| 1186 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1187 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1188 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1189 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1190 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1191 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1192 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1193 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1194 | flags |= FMODE_EXCL; |
| 1195 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1196 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1197 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1198 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1199 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1200 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1201 | if (!latest_dev || |
| 1202 | device->generation > latest_dev->generation) |
| 1203 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1204 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1205 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1206 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1207 | goto out; |
| 1208 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1209 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1210 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1211 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1212 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1213 | return ret; |
| 1214 | } |
| 1215 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1216 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1217 | { |
| 1218 | struct btrfs_device *dev1, *dev2; |
| 1219 | |
| 1220 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1221 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1222 | |
| 1223 | if (dev1->devid < dev2->devid) |
| 1224 | return -1; |
| 1225 | else if (dev1->devid > dev2->devid) |
| 1226 | return 1; |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1230 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1231 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1232 | { |
| 1233 | int ret; |
| 1234 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1235 | lockdep_assert_held(&uuid_mutex); |
| 1236 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1237 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1238 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1239 | fs_devices->opened++; |
| 1240 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1241 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1242 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1243 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1244 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1245 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1246 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1247 | return ret; |
| 1248 | } |
| 1249 | |
Nikolay Borisov | f6d9abb | 2020-02-14 00:24:29 +0900 | [diff] [blame] | 1250 | void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1251 | { |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1252 | put_page(page); |
| 1253 | } |
| 1254 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1255 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1256 | struct page **page, |
| 1257 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1258 | { |
| 1259 | void *p; |
| 1260 | pgoff_t index; |
| 1261 | |
| 1262 | /* make sure our super fits in the device */ |
| 1263 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1264 | return 1; |
| 1265 | |
| 1266 | /* make sure our super fits in the page */ |
| 1267 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1268 | return 1; |
| 1269 | |
| 1270 | /* make sure our super doesn't straddle pages on disk */ |
| 1271 | index = bytenr >> PAGE_SHIFT; |
| 1272 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1273 | return 1; |
| 1274 | |
| 1275 | /* pull in the page with our super */ |
| 1276 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1277 | index, GFP_KERNEL); |
| 1278 | |
Johannes Thumshirn | c514c9b | 2020-02-14 00:24:30 +0900 | [diff] [blame] | 1279 | if (IS_ERR(*page)) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1280 | return 1; |
| 1281 | |
Johannes Thumshirn | c514c9b | 2020-02-14 00:24:30 +0900 | [diff] [blame] | 1282 | p = page_address(*page); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1283 | |
| 1284 | /* align our pointer to the offset of the super block */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 1285 | *disk_super = p + offset_in_page(bytenr); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1286 | |
| 1287 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1288 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1289 | btrfs_release_disk_super(*page); |
| 1290 | return 1; |
| 1291 | } |
| 1292 | |
| 1293 | if ((*disk_super)->label[0] && |
| 1294 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1295 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
Anand Jain | 228a73a | 2019-01-04 13:31:54 +0800 | [diff] [blame] | 1300 | int btrfs_forget_devices(const char *path) |
| 1301 | { |
| 1302 | int ret; |
| 1303 | |
| 1304 | mutex_lock(&uuid_mutex); |
| 1305 | ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| 1306 | mutex_unlock(&uuid_mutex); |
| 1307 | |
| 1308 | return ret; |
| 1309 | } |
| 1310 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1311 | /* |
| 1312 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1313 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1314 | * is read via pagecache |
| 1315 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1316 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1317 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1318 | { |
| 1319 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1320 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1321 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1322 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1323 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1324 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1325 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1326 | lockdep_assert_held(&uuid_mutex); |
| 1327 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1328 | /* |
| 1329 | * we would like to check all the supers, but that would make |
| 1330 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1331 | * So, we need to add a special mount option to scan for |
| 1332 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1333 | */ |
| 1334 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1335 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1336 | |
| 1337 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1338 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1339 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1340 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1341 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1342 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1343 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1344 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1345 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1346 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1347 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1348 | if (new_device_added) |
| 1349 | btrfs_free_stale_devices(path, device); |
| 1350 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1351 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1352 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1353 | |
| 1354 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1355 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1356 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1357 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1358 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1359 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1360 | /* |
| 1361 | * Try to find a chunk that intersects [start, start + len] range and when one |
| 1362 | * such is found, record the end of it in *start |
| 1363 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1364 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| 1365 | u64 len) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1366 | { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1367 | u64 physical_start, physical_end; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1368 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1369 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1370 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1371 | if (!find_first_extent_bit(&device->alloc_state, *start, |
| 1372 | &physical_start, &physical_end, |
| 1373 | CHUNK_ALLOCATED, NULL)) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1374 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1375 | if (in_range(physical_start, *start, len) || |
| 1376 | in_range(*start, physical_start, |
| 1377 | physical_end - physical_start)) { |
| 1378 | *start = physical_end + 1; |
| 1379 | return true; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1380 | } |
| 1381 | } |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1382 | return false; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1386 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1387 | * find_free_dev_extent_start - find free space in the specified device |
| 1388 | * @device: the device which we search the free space in |
| 1389 | * @num_bytes: the size of the free space that we need |
| 1390 | * @search_start: the position from which to begin the search |
| 1391 | * @start: store the start of the free space. |
| 1392 | * @len: the size of the free space. that we find, or the size |
| 1393 | * 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] | 1394 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1395 | * this uses a pretty simple search, the expectation is that it is |
| 1396 | * called very infrequently and that a given device has a small number |
| 1397 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1398 | * |
| 1399 | * @start is used to store the start of the free space if we find. But if we |
| 1400 | * don't find suitable free space, it will be used to store the start position |
| 1401 | * of the max free space. |
| 1402 | * |
| 1403 | * @len is used to store the size of the free space that we find. |
| 1404 | * But if we don't find suitable free space, it is used to store the size of |
| 1405 | * the max free space. |
Qu Wenruo | 135da97 | 2019-07-19 14:51:42 +0800 | [diff] [blame] | 1406 | * |
| 1407 | * NOTE: This function will search *commit* root of device tree, and does extra |
| 1408 | * check to ensure dev extents are not double allocated. |
| 1409 | * This makes the function safe to allocate dev extents but may not report |
| 1410 | * correct usable device space, as device extent freed in current transaction |
| 1411 | * is not reported as avaiable. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1412 | */ |
Qu Wenruo | 9e3246a | 2019-07-19 14:51:41 +0800 | [diff] [blame] | 1413 | static int find_free_dev_extent_start(struct btrfs_device *device, |
| 1414 | u64 num_bytes, u64 search_start, u64 *start, |
| 1415 | u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1416 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1417 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1418 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1419 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1420 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1421 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1422 | u64 hole_size; |
| 1423 | u64 max_hole_start; |
| 1424 | u64 max_hole_size; |
| 1425 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1426 | u64 search_end = device->total_bytes; |
| 1427 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1428 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1429 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1430 | |
| 1431 | /* |
| 1432 | * We don't want to overwrite the superblock on the drive nor any area |
| 1433 | * used by the boot loader (grub for example), so we make sure to start |
| 1434 | * at an offset of at least 1MB. |
| 1435 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1436 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1437 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1438 | path = btrfs_alloc_path(); |
| 1439 | if (!path) |
| 1440 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1441 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1442 | max_hole_start = search_start; |
| 1443 | max_hole_size = 0; |
| 1444 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1445 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1446 | if (search_start >= search_end || |
| 1447 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1448 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1449 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1452 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1453 | path->search_commit_root = 1; |
| 1454 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1455 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1456 | key.objectid = device->devid; |
| 1457 | key.offset = search_start; |
| 1458 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1459 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1460 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1461 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1462 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1463 | if (ret > 0) { |
| 1464 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1465 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1466 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1467 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1468 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1469 | while (1) { |
| 1470 | l = path->nodes[0]; |
| 1471 | slot = path->slots[0]; |
| 1472 | if (slot >= btrfs_header_nritems(l)) { |
| 1473 | ret = btrfs_next_leaf(root, path); |
| 1474 | if (ret == 0) |
| 1475 | continue; |
| 1476 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1477 | goto out; |
| 1478 | |
| 1479 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1480 | } |
| 1481 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1482 | |
| 1483 | if (key.objectid < device->devid) |
| 1484 | goto next; |
| 1485 | |
| 1486 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1487 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1488 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1489 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1490 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1491 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1492 | if (key.offset > search_start) { |
| 1493 | hole_size = key.offset - search_start; |
| 1494 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1495 | /* |
| 1496 | * Have to check before we set max_hole_start, otherwise |
| 1497 | * we could end up sending back this offset anyway. |
| 1498 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1499 | if (contains_pending_extent(device, &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1500 | hole_size)) { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1501 | if (key.offset >= search_start) |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1502 | hole_size = key.offset - search_start; |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1503 | else |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1504 | hole_size = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1505 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1506 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1507 | if (hole_size > max_hole_size) { |
| 1508 | max_hole_start = search_start; |
| 1509 | max_hole_size = hole_size; |
| 1510 | } |
| 1511 | |
| 1512 | /* |
| 1513 | * If this free space is greater than which we need, |
| 1514 | * it must be the max free space that we have found |
| 1515 | * until now, so max_hole_start must point to the start |
| 1516 | * of this free space and the length of this free space |
| 1517 | * is stored in max_hole_size. Thus, we return |
| 1518 | * max_hole_start and max_hole_size and go back to the |
| 1519 | * caller. |
| 1520 | */ |
| 1521 | if (hole_size >= num_bytes) { |
| 1522 | ret = 0; |
| 1523 | goto out; |
| 1524 | } |
| 1525 | } |
| 1526 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1527 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1528 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1529 | dev_extent); |
| 1530 | if (extent_end > search_start) |
| 1531 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1532 | next: |
| 1533 | path->slots[0]++; |
| 1534 | cond_resched(); |
| 1535 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1536 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1537 | /* |
| 1538 | * At this point, search_start should be the end of |
| 1539 | * allocated dev extents, and when shrinking the device, |
| 1540 | * search_end may be smaller than search_start. |
| 1541 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1542 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1543 | hole_size = search_end - search_start; |
| 1544 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1545 | if (contains_pending_extent(device, &search_start, hole_size)) { |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1546 | btrfs_release_path(path); |
| 1547 | goto again; |
| 1548 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1549 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1550 | if (hole_size > max_hole_size) { |
| 1551 | max_hole_start = search_start; |
| 1552 | max_hole_size = hole_size; |
| 1553 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1554 | } |
| 1555 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1556 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1557 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1558 | ret = -ENOSPC; |
| 1559 | else |
| 1560 | ret = 0; |
| 1561 | |
| 1562 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1563 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1564 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1565 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1566 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1567 | return ret; |
| 1568 | } |
| 1569 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1570 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1571 | u64 *start, u64 *len) |
| 1572 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1573 | /* FIXME use last free of some kind */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1574 | return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1577 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1578 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1579 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1580 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1581 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1582 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1583 | int ret; |
| 1584 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1585 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1586 | struct btrfs_key found_key; |
| 1587 | struct extent_buffer *leaf = NULL; |
| 1588 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1589 | |
| 1590 | path = btrfs_alloc_path(); |
| 1591 | if (!path) |
| 1592 | return -ENOMEM; |
| 1593 | |
| 1594 | key.objectid = device->devid; |
| 1595 | key.offset = start; |
| 1596 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1597 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1598 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1599 | if (ret > 0) { |
| 1600 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1601 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1602 | if (ret) |
| 1603 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1604 | leaf = path->nodes[0]; |
| 1605 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1606 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1607 | struct btrfs_dev_extent); |
| 1608 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1609 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1610 | key = found_key; |
| 1611 | btrfs_release_path(path); |
| 1612 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1613 | } else if (ret == 0) { |
| 1614 | leaf = path->nodes[0]; |
| 1615 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1616 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1617 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1618 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1619 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1620 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1621 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1622 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1623 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1624 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1625 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1626 | btrfs_handle_fs_error(fs_info, ret, |
| 1627 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1628 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1629 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1630 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1631 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1632 | btrfs_free_path(path); |
| 1633 | return ret; |
| 1634 | } |
| 1635 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1636 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1637 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1638 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1639 | { |
| 1640 | int ret; |
| 1641 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1642 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1643 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1644 | struct btrfs_dev_extent *extent; |
| 1645 | struct extent_buffer *leaf; |
| 1646 | struct btrfs_key key; |
| 1647 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1648 | 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] | 1649 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1650 | path = btrfs_alloc_path(); |
| 1651 | if (!path) |
| 1652 | return -ENOMEM; |
| 1653 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1654 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1655 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1656 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1657 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1658 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1659 | if (ret) |
| 1660 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1661 | |
| 1662 | leaf = path->nodes[0]; |
| 1663 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1664 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1665 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1666 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1667 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1668 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1669 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1670 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1671 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1672 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1673 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1674 | btrfs_free_path(path); |
| 1675 | return ret; |
| 1676 | } |
| 1677 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1678 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1679 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1680 | struct extent_map_tree *em_tree; |
| 1681 | struct extent_map *em; |
| 1682 | struct rb_node *n; |
| 1683 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1684 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 1685 | em_tree = &fs_info->mapping_tree; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1686 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1687 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1688 | if (n) { |
| 1689 | em = rb_entry(n, struct extent_map, rb_node); |
| 1690 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1691 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1692 | read_unlock(&em_tree->lock); |
| 1693 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1694 | return ret; |
| 1695 | } |
| 1696 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1697 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1698 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1699 | { |
| 1700 | int ret; |
| 1701 | struct btrfs_key key; |
| 1702 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1703 | struct btrfs_path *path; |
| 1704 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1705 | path = btrfs_alloc_path(); |
| 1706 | if (!path) |
| 1707 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1708 | |
| 1709 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1710 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1711 | key.offset = (u64)-1; |
| 1712 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1713 | 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] | 1714 | if (ret < 0) |
| 1715 | goto error; |
| 1716 | |
Anand Jain | a06dee4d | 2019-08-27 15:40:44 +0800 | [diff] [blame] | 1717 | if (ret == 0) { |
| 1718 | /* Corruption */ |
| 1719 | btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); |
| 1720 | ret = -EUCLEAN; |
| 1721 | goto error; |
| 1722 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1723 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1724 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1725 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1726 | BTRFS_DEV_ITEM_KEY); |
| 1727 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1728 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1729 | } else { |
| 1730 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1731 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1732 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1733 | } |
| 1734 | ret = 0; |
| 1735 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1736 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1737 | return ret; |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * the device information is stored in the chunk root |
| 1742 | * the btrfs_device struct should be fully filled in |
| 1743 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1744 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1745 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1746 | { |
| 1747 | int ret; |
| 1748 | struct btrfs_path *path; |
| 1749 | struct btrfs_dev_item *dev_item; |
| 1750 | struct extent_buffer *leaf; |
| 1751 | struct btrfs_key key; |
| 1752 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1753 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1754 | path = btrfs_alloc_path(); |
| 1755 | if (!path) |
| 1756 | return -ENOMEM; |
| 1757 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1758 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1759 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1760 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1761 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1762 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1763 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1764 | if (ret) |
| 1765 | goto out; |
| 1766 | |
| 1767 | leaf = path->nodes[0]; |
| 1768 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1769 | |
| 1770 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1771 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1772 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1773 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1774 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1775 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1776 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1777 | btrfs_device_get_disk_total_bytes(device)); |
| 1778 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1779 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1780 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1781 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1782 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1783 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1784 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1785 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1786 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1787 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1788 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1789 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1790 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1791 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1792 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1793 | out: |
| 1794 | btrfs_free_path(path); |
| 1795 | return ret; |
| 1796 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1797 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1798 | /* |
| 1799 | * Function to update ctime/mtime for a given device path. |
| 1800 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1801 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1802 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1803 | { |
| 1804 | struct file *filp; |
| 1805 | |
| 1806 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1807 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1808 | return; |
| 1809 | file_update_time(filp); |
| 1810 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1811 | } |
| 1812 | |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1813 | static int btrfs_rm_dev_item(struct btrfs_device *device) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1814 | { |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1815 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1816 | int ret; |
| 1817 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1818 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1819 | struct btrfs_trans_handle *trans; |
| 1820 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1821 | path = btrfs_alloc_path(); |
| 1822 | if (!path) |
| 1823 | return -ENOMEM; |
| 1824 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1825 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1826 | if (IS_ERR(trans)) { |
| 1827 | btrfs_free_path(path); |
| 1828 | return PTR_ERR(trans); |
| 1829 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1830 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1831 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1832 | key.offset = device->devid; |
| 1833 | |
| 1834 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1835 | if (ret) { |
| 1836 | if (ret > 0) |
| 1837 | ret = -ENOENT; |
| 1838 | btrfs_abort_transaction(trans, ret); |
| 1839 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1840 | goto out; |
| 1841 | } |
| 1842 | |
| 1843 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1844 | if (ret) { |
| 1845 | btrfs_abort_transaction(trans, ret); |
| 1846 | btrfs_end_transaction(trans); |
| 1847 | } |
| 1848 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1849 | out: |
| 1850 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1851 | if (!ret) |
| 1852 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1853 | return ret; |
| 1854 | } |
| 1855 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1856 | /* |
| 1857 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 1858 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 1859 | * replace. |
| 1860 | */ |
| 1861 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 1862 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1863 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1864 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1865 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1866 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1867 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1868 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1869 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1870 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1871 | all_avail = fs_info->avail_data_alloc_bits | |
| 1872 | fs_info->avail_system_alloc_bits | |
| 1873 | fs_info->avail_metadata_alloc_bits; |
| 1874 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1875 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1876 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 1877 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1878 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1879 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1880 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 1881 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1882 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1883 | if (ret) |
| 1884 | return ret; |
| 1885 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1889 | } |
| 1890 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1891 | static struct btrfs_device * btrfs_find_next_active_device( |
| 1892 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1893 | { |
| 1894 | struct btrfs_device *next_device; |
| 1895 | |
| 1896 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 1897 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1898 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 1899 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1900 | return next_device; |
| 1901 | } |
| 1902 | |
| 1903 | return NULL; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 1908 | * and replace it with the provided or the next active device, in the context |
| 1909 | * where this function called, there should be always be another device (or |
| 1910 | * this_dev) which is active. |
| 1911 | */ |
David Sterba | b105e92 | 2019-10-01 19:57:35 +0200 | [diff] [blame] | 1912 | void __cold btrfs_assign_next_active_device(struct btrfs_device *device, |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1913 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1914 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1915 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1916 | struct btrfs_device *next_device; |
| 1917 | |
| 1918 | if (this_dev) |
| 1919 | next_device = this_dev; |
| 1920 | else |
| 1921 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 1922 | device); |
| 1923 | ASSERT(next_device); |
| 1924 | |
| 1925 | if (fs_info->sb->s_bdev && |
| 1926 | (fs_info->sb->s_bdev == device->bdev)) |
| 1927 | fs_info->sb->s_bdev = next_device->bdev; |
| 1928 | |
| 1929 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 1930 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1931 | } |
| 1932 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1933 | /* |
| 1934 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 1935 | * currently replaced. |
| 1936 | */ |
| 1937 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 1938 | { |
| 1939 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 1940 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1941 | down_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1942 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 1943 | ASSERT(num_devices > 1); |
| 1944 | num_devices--; |
| 1945 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1946 | up_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1947 | |
| 1948 | return num_devices; |
| 1949 | } |
| 1950 | |
Johannes Thumshirn | 6fbceb9 | 2020-02-14 00:24:31 +0900 | [diff] [blame^] | 1951 | static void btrfs_scratch_superblocks(struct block_device *bdev, |
| 1952 | const char *device_path) |
| 1953 | { |
| 1954 | struct buffer_head *bh; |
| 1955 | struct btrfs_super_block *disk_super; |
| 1956 | int copy_num; |
| 1957 | |
| 1958 | if (!bdev) |
| 1959 | return; |
| 1960 | |
| 1961 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) { |
| 1962 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 1963 | continue; |
| 1964 | |
| 1965 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1966 | |
| 1967 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1968 | set_buffer_dirty(bh); |
| 1969 | sync_dirty_buffer(bh); |
| 1970 | brelse(bh); |
| 1971 | } |
| 1972 | |
| 1973 | /* Notify udev that device has changed */ |
| 1974 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 1975 | |
| 1976 | /* Update ctime/mtime for device path for libblkid */ |
| 1977 | update_dev_time(device_path); |
| 1978 | } |
| 1979 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1980 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 1981 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1982 | { |
| 1983 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1984 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1985 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1986 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1987 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1988 | |
| 1989 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1990 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1991 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1992 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1993 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1994 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1995 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1996 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 1997 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 1998 | |
| 1999 | if (IS_ERR(device)) { |
| 2000 | if (PTR_ERR(device) == -ENOENT && |
| 2001 | strcmp(device_path, "missing") == 0) |
| 2002 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 2003 | else |
| 2004 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2005 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2006 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2007 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 2008 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 2009 | btrfs_warn_in_rcu(fs_info, |
| 2010 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 2011 | rcu_str_deref(device->name), device->devid); |
| 2012 | ret = -ETXTBSY; |
| 2013 | goto out; |
| 2014 | } |
| 2015 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2016 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2017 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2018 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2019 | } |
| 2020 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2021 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 2022 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 2023 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2024 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2025 | } |
| 2026 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2027 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2028 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2029 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2030 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2031 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2032 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2033 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2034 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2035 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2036 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2037 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2038 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2039 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2040 | /* |
| 2041 | * TODO: the superblock still includes this device in its num_devices |
| 2042 | * counter although write_all_supers() is not locked out. This |
| 2043 | * could give a filesystem state which requires a degraded mount. |
| 2044 | */ |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 2045 | ret = btrfs_rm_dev_item(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2046 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2047 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2048 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2049 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
David Sterba | 163e97e | 2019-03-20 16:32:55 +0100 | [diff] [blame] | 2050 | btrfs_scrub_cancel_dev(device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2051 | |
| 2052 | /* |
| 2053 | * the device list mutex makes sure that we don't change |
| 2054 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2055 | * the device supers. Whoever is writing all supers, should |
| 2056 | * lock the device list mutex before getting the number of |
| 2057 | * devices in the super block (super_copy). Conversely, |
| 2058 | * whoever updates the number of devices in the super block |
| 2059 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2060 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2061 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2062 | /* |
| 2063 | * In normal cases the cur_devices == fs_devices. But in case |
| 2064 | * of deleting a seed device, the cur_devices should point to |
| 2065 | * its own fs_devices listed under the fs_devices->seed. |
| 2066 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2067 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2068 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2069 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2070 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2071 | cur_devices->num_devices--; |
| 2072 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2073 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2074 | if (cur_devices != fs_devices) |
| 2075 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2076 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2077 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2078 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2079 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2080 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2081 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2082 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2083 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2084 | /* remove sysfs entry */ |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2085 | btrfs_sysfs_remove_devices_dir(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2086 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2087 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2088 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2089 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2090 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2091 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2092 | /* |
| 2093 | * at this point, the device is zero sized and detached from |
| 2094 | * the devices list. All that's left is to zero out the old |
| 2095 | * supers and free the device. |
| 2096 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2097 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2098 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 2099 | |
| 2100 | btrfs_close_bdev(device); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2101 | synchronize_rcu(); |
| 2102 | btrfs_free_device(device); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2103 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2104 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2105 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2106 | if (fs_devices->seed == cur_devices) { |
| 2107 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2108 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2109 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2110 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2111 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2112 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2113 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2114 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2115 | } |
| 2116 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2117 | out: |
| 2118 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2119 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2120 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2121 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2122 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2123 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2124 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2125 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2126 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2127 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2128 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2129 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2130 | } |
| 2131 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2132 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2133 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2134 | struct btrfs_fs_devices *fs_devices; |
| 2135 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2136 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2137 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2138 | /* |
| 2139 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2140 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2141 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2142 | * srcdev will have its correct fs_devices in both the cases. |
| 2143 | */ |
| 2144 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2145 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2146 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2147 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2148 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2149 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2150 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2151 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2152 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2153 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2154 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2155 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2156 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2157 | } |
| 2158 | |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2159 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2160 | { |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2161 | struct btrfs_fs_info *fs_info = srcdev->fs_info; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2162 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2163 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2164 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2165 | /* zero out the old super if it is writable */ |
| 2166 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2167 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2168 | |
| 2169 | btrfs_close_bdev(srcdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2170 | synchronize_rcu(); |
| 2171 | btrfs_free_device(srcdev); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2172 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2173 | /* if this is no devs we rather delete the fs_devices */ |
| 2174 | if (!fs_devices->num_devices) { |
| 2175 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2176 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2177 | /* |
| 2178 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2179 | * seed. In case of a seed device being replaced, the replace |
| 2180 | * target added to the sprout FS, so there will be no more |
| 2181 | * device left under the seed FS. |
| 2182 | */ |
| 2183 | ASSERT(fs_devices->seeding); |
| 2184 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2185 | tmp_fs_devices = fs_info->fs_devices; |
| 2186 | while (tmp_fs_devices) { |
| 2187 | if (tmp_fs_devices->seed == fs_devices) { |
| 2188 | tmp_fs_devices->seed = fs_devices->seed; |
| 2189 | break; |
| 2190 | } |
| 2191 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2192 | } |
| 2193 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2194 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2195 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2196 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2197 | } |
| 2198 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2199 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2200 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2201 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2202 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2203 | mutex_lock(&fs_devices->device_list_mutex); |
| 2204 | |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2205 | btrfs_sysfs_remove_devices_dir(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2206 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2207 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2208 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2209 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2210 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2211 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2212 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2213 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2214 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2215 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2216 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2217 | |
| 2218 | /* |
| 2219 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2220 | * may lead to a call to btrfs_show_devname() which will try |
| 2221 | * to hold device_list_mutex. And here this device |
| 2222 | * is already out of device list, so we don't have to hold |
| 2223 | * the device_list_mutex lock. |
| 2224 | */ |
| 2225 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2226 | |
| 2227 | btrfs_close_bdev(tgtdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2228 | synchronize_rcu(); |
| 2229 | btrfs_free_device(tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2230 | } |
| 2231 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2232 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2233 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2234 | { |
| 2235 | int ret = 0; |
| 2236 | struct btrfs_super_block *disk_super; |
| 2237 | u64 devid; |
| 2238 | u8 *dev_uuid; |
| 2239 | struct block_device *bdev; |
| 2240 | struct buffer_head *bh; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2241 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2242 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2243 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2244 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2245 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2246 | return ERR_PTR(ret); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2247 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2248 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2249 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2250 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2251 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2252 | disk_super->metadata_uuid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2253 | else |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2254 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2255 | disk_super->fsid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2256 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2257 | brelse(bh); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2258 | if (!device) |
| 2259 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2260 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2261 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2262 | } |
| 2263 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2264 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2265 | * Lookup a device given by device id, or the path if the id is 0. |
| 2266 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2267 | struct btrfs_device *btrfs_find_device_by_devspec( |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2268 | struct btrfs_fs_info *fs_info, u64 devid, |
| 2269 | const char *device_path) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2270 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2271 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2272 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2273 | if (devid) { |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2274 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2275 | NULL, true); |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2276 | if (!device) |
| 2277 | return ERR_PTR(-ENOENT); |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2278 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2279 | } |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2280 | |
| 2281 | if (!device_path || !device_path[0]) |
| 2282 | return ERR_PTR(-EINVAL); |
| 2283 | |
| 2284 | if (strcmp(device_path, "missing") == 0) { |
| 2285 | /* Find first missing device */ |
| 2286 | list_for_each_entry(device, &fs_info->fs_devices->devices, |
| 2287 | dev_list) { |
| 2288 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2289 | &device->dev_state) && !device->bdev) |
| 2290 | return device; |
| 2291 | } |
| 2292 | return ERR_PTR(-ENOENT); |
| 2293 | } |
| 2294 | |
| 2295 | return btrfs_find_device_by_path(fs_info, device_path); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2296 | } |
| 2297 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2298 | /* |
| 2299 | * does all the dirty work required for changing file system's UUID. |
| 2300 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2301 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2302 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2303 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2304 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2305 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2306 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2307 | struct btrfs_device *device; |
| 2308 | u64 super_flags; |
| 2309 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2310 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2311 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2312 | return -EINVAL; |
| 2313 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2314 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2315 | if (IS_ERR(seed_devices)) |
| 2316 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2317 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2318 | old_devices = clone_fs_devices(fs_devices); |
| 2319 | if (IS_ERR(old_devices)) { |
| 2320 | kfree(seed_devices); |
| 2321 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2322 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2323 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2324 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2325 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2326 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2327 | seed_devices->opened = 1; |
| 2328 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2329 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2330 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2331 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2332 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2333 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2334 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2335 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2336 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2337 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2338 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2339 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2340 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2341 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 2342 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2343 | fs_devices->num_devices = 0; |
| 2344 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2345 | fs_devices->missing_devices = 0; |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2346 | fs_devices->rotating = false; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2347 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2348 | |
| 2349 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2350 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2351 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2352 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2353 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2354 | super_flags = btrfs_super_flags(disk_super) & |
| 2355 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2356 | btrfs_set_super_flags(disk_super, super_flags); |
| 2357 | |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2362 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2363 | */ |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2364 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2365 | { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2366 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2367 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2368 | struct btrfs_path *path; |
| 2369 | struct extent_buffer *leaf; |
| 2370 | struct btrfs_dev_item *dev_item; |
| 2371 | struct btrfs_device *device; |
| 2372 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2373 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2374 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2375 | u64 devid; |
| 2376 | int ret; |
| 2377 | |
| 2378 | path = btrfs_alloc_path(); |
| 2379 | if (!path) |
| 2380 | return -ENOMEM; |
| 2381 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2382 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2383 | key.offset = 0; |
| 2384 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2385 | |
| 2386 | while (1) { |
| 2387 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2388 | if (ret < 0) |
| 2389 | goto error; |
| 2390 | |
| 2391 | leaf = path->nodes[0]; |
| 2392 | next_slot: |
| 2393 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2394 | ret = btrfs_next_leaf(root, path); |
| 2395 | if (ret > 0) |
| 2396 | break; |
| 2397 | if (ret < 0) |
| 2398 | goto error; |
| 2399 | leaf = path->nodes[0]; |
| 2400 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2401 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2402 | continue; |
| 2403 | } |
| 2404 | |
| 2405 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2406 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2407 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2408 | break; |
| 2409 | |
| 2410 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2411 | struct btrfs_dev_item); |
| 2412 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2413 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2414 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2415 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2416 | BTRFS_FSID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2417 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2418 | fs_uuid, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2419 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2420 | |
| 2421 | if (device->fs_devices->seeding) { |
| 2422 | btrfs_set_device_generation(leaf, dev_item, |
| 2423 | device->generation); |
| 2424 | btrfs_mark_buffer_dirty(leaf); |
| 2425 | } |
| 2426 | |
| 2427 | path->slots[0]++; |
| 2428 | goto next_slot; |
| 2429 | } |
| 2430 | ret = 0; |
| 2431 | error: |
| 2432 | btrfs_free_path(path); |
| 2433 | return ret; |
| 2434 | } |
| 2435 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2436 | 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] | 2437 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2438 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2439 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2440 | struct btrfs_trans_handle *trans; |
| 2441 | struct btrfs_device *device; |
| 2442 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2443 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2444 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2445 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2446 | u64 orig_super_total_bytes; |
| 2447 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2448 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2449 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2450 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2451 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2452 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2453 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2454 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2455 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2456 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2457 | if (IS_ERR(bdev)) |
| 2458 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2459 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2460 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2461 | seeding_dev = 1; |
| 2462 | down_write(&sb->s_umount); |
| 2463 | mutex_lock(&uuid_mutex); |
| 2464 | } |
| 2465 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2466 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2467 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2468 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2469 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2470 | if (device->bdev == bdev) { |
| 2471 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2472 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2473 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2474 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2475 | } |
| 2476 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2477 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2478 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2479 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2480 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2481 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2482 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2483 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2484 | } |
| 2485 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2486 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2487 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2488 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2489 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2490 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2491 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2492 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2493 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2494 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2495 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2496 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2499 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2500 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2501 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2502 | device->io_width = fs_info->sectorsize; |
| 2503 | device->io_align = fs_info->sectorsize; |
| 2504 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2505 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2506 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2507 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2508 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2509 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2510 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2511 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2512 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2513 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2514 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2515 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2516 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2517 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2518 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2519 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2520 | if (ret) { |
| 2521 | btrfs_abort_transaction(trans, ret); |
| 2522 | goto error_trans; |
| 2523 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2524 | } |
| 2525 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2526 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2527 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2528 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2529 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2530 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2531 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2532 | fs_devices->num_devices++; |
| 2533 | fs_devices->open_devices++; |
| 2534 | fs_devices->rw_devices++; |
| 2535 | fs_devices->total_devices++; |
| 2536 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2537 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2538 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2539 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2540 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2541 | fs_devices->rotating = true; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2542 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2543 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2544 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2545 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2546 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2547 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2548 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2549 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2550 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2551 | |
| 2552 | /* add sysfs device entry */ |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2553 | btrfs_sysfs_add_devices_dir(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2554 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2555 | /* |
| 2556 | * we've got more storage, clear any full flags on the space |
| 2557 | * infos |
| 2558 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2559 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2560 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2561 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2562 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2563 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2564 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2565 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 2566 | ret = init_first_rw_device(trans); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2567 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2568 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2569 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2570 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2571 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2572 | } |
| 2573 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2574 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2575 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2576 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2577 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | if (seeding_dev) { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2581 | ret = btrfs_finish_sprout(trans); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2582 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2583 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2584 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2585 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2586 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 2587 | btrfs_sysfs_update_sprout_fsid(fs_devices, |
| 2588 | fs_info->fs_devices->fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2589 | } |
| 2590 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2591 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2592 | |
| 2593 | if (seeding_dev) { |
| 2594 | mutex_unlock(&uuid_mutex); |
| 2595 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2596 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2597 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2598 | if (ret) /* transaction commit */ |
| 2599 | return ret; |
| 2600 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2601 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2602 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2603 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2604 | "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] | 2605 | trans = btrfs_attach_transaction(root); |
| 2606 | if (IS_ERR(trans)) { |
| 2607 | if (PTR_ERR(trans) == -ENOENT) |
| 2608 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2609 | ret = PTR_ERR(trans); |
| 2610 | trans = NULL; |
| 2611 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2612 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2613 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2614 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2615 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2616 | /* Update ctime/mtime for libblkid */ |
| 2617 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2618 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2619 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2620 | error_sysfs: |
Anand Jain | f3cd2c5 | 2020-02-12 17:28:13 +0800 | [diff] [blame] | 2621 | btrfs_sysfs_remove_devices_dir(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2622 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2623 | mutex_lock(&fs_info->chunk_mutex); |
| 2624 | list_del_rcu(&device->dev_list); |
| 2625 | list_del(&device->dev_alloc_list); |
| 2626 | fs_info->fs_devices->num_devices--; |
| 2627 | fs_info->fs_devices->open_devices--; |
| 2628 | fs_info->fs_devices->rw_devices--; |
| 2629 | fs_info->fs_devices->total_devices--; |
| 2630 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2631 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2632 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2633 | orig_super_total_bytes); |
| 2634 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2635 | orig_super_num_devices); |
| 2636 | mutex_unlock(&fs_info->chunk_mutex); |
| 2637 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2638 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2639 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2640 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2641 | if (trans) |
| 2642 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2643 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2644 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2645 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2646 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2647 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2648 | mutex_unlock(&uuid_mutex); |
| 2649 | up_write(&sb->s_umount); |
| 2650 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2651 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2652 | } |
| 2653 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2654 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2655 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2656 | { |
| 2657 | int ret; |
| 2658 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2659 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2660 | struct btrfs_dev_item *dev_item; |
| 2661 | struct extent_buffer *leaf; |
| 2662 | struct btrfs_key key; |
| 2663 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2664 | path = btrfs_alloc_path(); |
| 2665 | if (!path) |
| 2666 | return -ENOMEM; |
| 2667 | |
| 2668 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2669 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2670 | key.offset = device->devid; |
| 2671 | |
| 2672 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2673 | if (ret < 0) |
| 2674 | goto out; |
| 2675 | |
| 2676 | if (ret > 0) { |
| 2677 | ret = -ENOENT; |
| 2678 | goto out; |
| 2679 | } |
| 2680 | |
| 2681 | leaf = path->nodes[0]; |
| 2682 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2683 | |
| 2684 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2685 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2686 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2687 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2688 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2689 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2690 | btrfs_device_get_disk_total_bytes(device)); |
| 2691 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2692 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2693 | btrfs_mark_buffer_dirty(leaf); |
| 2694 | |
| 2695 | out: |
| 2696 | btrfs_free_path(path); |
| 2697 | return ret; |
| 2698 | } |
| 2699 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2700 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2701 | struct btrfs_device *device, u64 new_size) |
| 2702 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2703 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2704 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2705 | u64 old_total; |
| 2706 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2707 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2708 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2709 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2710 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2711 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2712 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2713 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2714 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2715 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2716 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2717 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2718 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2719 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2720 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2721 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2722 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2723 | btrfs_set_super_total_bytes(super_copy, |
| 2724 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2725 | device->fs_devices->total_rw_bytes += diff; |
| 2726 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2727 | btrfs_device_set_total_bytes(device, new_size); |
| 2728 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2729 | btrfs_clear_space_info_full(device->fs_info); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 2730 | if (list_empty(&device->post_commit_list)) |
| 2731 | list_add_tail(&device->post_commit_list, |
| 2732 | &trans->transaction->dev_update_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2733 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2734 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2735 | return btrfs_update_device(trans, device); |
| 2736 | } |
| 2737 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2738 | 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] | 2739 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2740 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2741 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2742 | int ret; |
| 2743 | struct btrfs_path *path; |
| 2744 | struct btrfs_key key; |
| 2745 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2746 | path = btrfs_alloc_path(); |
| 2747 | if (!path) |
| 2748 | return -ENOMEM; |
| 2749 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2750 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2751 | key.offset = chunk_offset; |
| 2752 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2753 | |
| 2754 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2755 | if (ret < 0) |
| 2756 | goto out; |
| 2757 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2758 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2759 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2760 | ret = -ENOENT; |
| 2761 | goto out; |
| 2762 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2763 | |
| 2764 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2765 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2766 | btrfs_handle_fs_error(fs_info, ret, |
| 2767 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2768 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2769 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2770 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2771 | } |
| 2772 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2773 | 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] | 2774 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2775 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2776 | struct btrfs_disk_key *disk_key; |
| 2777 | struct btrfs_chunk *chunk; |
| 2778 | u8 *ptr; |
| 2779 | int ret = 0; |
| 2780 | u32 num_stripes; |
| 2781 | u32 array_size; |
| 2782 | u32 len = 0; |
| 2783 | u32 cur; |
| 2784 | struct btrfs_key key; |
| 2785 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2786 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2787 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2788 | |
| 2789 | ptr = super_copy->sys_chunk_array; |
| 2790 | cur = 0; |
| 2791 | |
| 2792 | while (cur < array_size) { |
| 2793 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2794 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2795 | |
| 2796 | len = sizeof(*disk_key); |
| 2797 | |
| 2798 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2799 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2800 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2801 | len += btrfs_chunk_item_size(num_stripes); |
| 2802 | } else { |
| 2803 | ret = -EIO; |
| 2804 | break; |
| 2805 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2806 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2807 | key.offset == chunk_offset) { |
| 2808 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2809 | array_size -= len; |
| 2810 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2811 | } else { |
| 2812 | ptr += len; |
| 2813 | cur += len; |
| 2814 | } |
| 2815 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2816 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2817 | return ret; |
| 2818 | } |
| 2819 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2820 | /* |
| 2821 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2822 | * @logical: Logical block offset in bytes. |
| 2823 | * @length: Length of extent in bytes. |
| 2824 | * |
| 2825 | * Return: Chunk mapping or ERR_PTR. |
| 2826 | */ |
| 2827 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2828 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2829 | { |
| 2830 | struct extent_map_tree *em_tree; |
| 2831 | struct extent_map *em; |
| 2832 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 2833 | em_tree = &fs_info->mapping_tree; |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2834 | read_lock(&em_tree->lock); |
| 2835 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2836 | read_unlock(&em_tree->lock); |
| 2837 | |
| 2838 | if (!em) { |
| 2839 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2840 | logical, length); |
| 2841 | return ERR_PTR(-EINVAL); |
| 2842 | } |
| 2843 | |
| 2844 | if (em->start > logical || em->start + em->len < logical) { |
| 2845 | btrfs_crit(fs_info, |
| 2846 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2847 | logical, length, em->start, em->start + em->len); |
| 2848 | free_extent_map(em); |
| 2849 | return ERR_PTR(-EINVAL); |
| 2850 | } |
| 2851 | |
| 2852 | /* callers are responsible for dropping em's ref. */ |
| 2853 | return em; |
| 2854 | } |
| 2855 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2856 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2857 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2858 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2859 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2860 | struct map_lookup *map; |
| 2861 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2862 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2863 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2864 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2865 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2866 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2867 | /* |
| 2868 | * 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] | 2869 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2870 | * do anything we still error out. |
| 2871 | */ |
| 2872 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2873 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2874 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2875 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2876 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2877 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2878 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2879 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2880 | /* |
| 2881 | * Take the device list mutex to prevent races with the final phase of |
| 2882 | * a device replace operation that replaces the device object associated |
| 2883 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2884 | */ |
| 2885 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2886 | for (i = 0; i < map->num_stripes; i++) { |
| 2887 | struct btrfs_device *device = map->stripes[i].dev; |
| 2888 | ret = btrfs_free_dev_extent(trans, device, |
| 2889 | map->stripes[i].physical, |
| 2890 | &dev_extent_len); |
| 2891 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2892 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2893 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2894 | goto out; |
| 2895 | } |
| 2896 | |
| 2897 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2898 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2899 | btrfs_device_set_bytes_used(device, |
| 2900 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2901 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2902 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2903 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2904 | } |
| 2905 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 2906 | ret = btrfs_update_device(trans, device); |
| 2907 | if (ret) { |
| 2908 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2909 | btrfs_abort_transaction(trans, ret); |
| 2910 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2911 | } |
| 2912 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2913 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2914 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2915 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2916 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2917 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2918 | goto out; |
| 2919 | } |
| 2920 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2921 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2922 | |
| 2923 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2924 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2925 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2926 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2927 | goto out; |
| 2928 | } |
| 2929 | } |
| 2930 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 2931 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2932 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2933 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2934 | goto out; |
| 2935 | } |
| 2936 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2937 | out: |
| 2938 | /* once for us */ |
| 2939 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2940 | return ret; |
| 2941 | } |
| 2942 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2943 | 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] | 2944 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2945 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2946 | struct btrfs_trans_handle *trans; |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2947 | struct btrfs_block_group *block_group; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2948 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2949 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2950 | /* |
| 2951 | * Prevent races with automatic removal of unused block groups. |
| 2952 | * After we relocate and before we remove the chunk with offset |
| 2953 | * chunk_offset, automatic removal of the block group can kick in, |
| 2954 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2955 | * |
| 2956 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2957 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2958 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2959 | * we release the path used to search the chunk/dev tree and before |
| 2960 | * the current task acquires this mutex and calls us. |
| 2961 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2962 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2963 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2964 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2965 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2966 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2967 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2968 | if (ret) |
| 2969 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2970 | |
Dennis Zhou | b0643e5 | 2019-12-13 16:22:14 -0800 | [diff] [blame] | 2971 | block_group = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2972 | if (!block_group) |
| 2973 | return -ENOENT; |
| 2974 | btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); |
| 2975 | btrfs_put_block_group(block_group); |
| 2976 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2977 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 2978 | chunk_offset); |
| 2979 | if (IS_ERR(trans)) { |
| 2980 | ret = PTR_ERR(trans); |
| 2981 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 2982 | return ret; |
| 2983 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 2984 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2985 | /* |
| 2986 | * step two, delete the device extents and the |
| 2987 | * chunk tree entries |
| 2988 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2989 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2990 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2991 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2992 | } |
| 2993 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2994 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2995 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2996 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2997 | struct btrfs_path *path; |
| 2998 | struct extent_buffer *leaf; |
| 2999 | struct btrfs_chunk *chunk; |
| 3000 | struct btrfs_key key; |
| 3001 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3002 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3003 | bool retried = false; |
| 3004 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3005 | int ret; |
| 3006 | |
| 3007 | path = btrfs_alloc_path(); |
| 3008 | if (!path) |
| 3009 | return -ENOMEM; |
| 3010 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3011 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3012 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3013 | key.offset = (u64)-1; |
| 3014 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3015 | |
| 3016 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3017 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3018 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3019 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3020 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3021 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3022 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3023 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3024 | |
| 3025 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3026 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3027 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3028 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3029 | if (ret < 0) |
| 3030 | goto error; |
| 3031 | if (ret > 0) |
| 3032 | break; |
| 3033 | |
| 3034 | leaf = path->nodes[0]; |
| 3035 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3036 | |
| 3037 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3038 | struct btrfs_chunk); |
| 3039 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3040 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3041 | |
| 3042 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3043 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3044 | if (ret == -ENOSPC) |
| 3045 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3046 | else |
| 3047 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3048 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3049 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3050 | |
| 3051 | if (found_key.offset == 0) |
| 3052 | break; |
| 3053 | key.offset = found_key.offset - 1; |
| 3054 | } |
| 3055 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3056 | if (failed && !retried) { |
| 3057 | failed = 0; |
| 3058 | retried = true; |
| 3059 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3060 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3061 | ret = -ENOSPC; |
| 3062 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3063 | error: |
| 3064 | btrfs_free_path(path); |
| 3065 | return ret; |
| 3066 | } |
| 3067 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3068 | /* |
| 3069 | * return 1 : allocate a data chunk successfully, |
| 3070 | * return <0: errors during allocating a data chunk, |
| 3071 | * return 0 : no need to allocate a data chunk. |
| 3072 | */ |
| 3073 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3074 | u64 chunk_offset) |
| 3075 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3076 | struct btrfs_block_group *cache; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3077 | u64 bytes_used; |
| 3078 | u64 chunk_type; |
| 3079 | |
| 3080 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3081 | ASSERT(cache); |
| 3082 | chunk_type = cache->flags; |
| 3083 | btrfs_put_block_group(cache); |
| 3084 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3085 | if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA)) |
| 3086 | return 0; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3087 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3088 | spin_lock(&fs_info->data_sinfo->lock); |
| 3089 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3090 | spin_unlock(&fs_info->data_sinfo->lock); |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3091 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3092 | if (!bytes_used) { |
| 3093 | struct btrfs_trans_handle *trans; |
| 3094 | int ret; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3095 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3096 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3097 | if (IS_ERR(trans)) |
| 3098 | return PTR_ERR(trans); |
| 3099 | |
| 3100 | ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA); |
| 3101 | btrfs_end_transaction(trans); |
| 3102 | if (ret < 0) |
| 3103 | return ret; |
| 3104 | return 1; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3105 | } |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3106 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3107 | return 0; |
| 3108 | } |
| 3109 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3110 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3111 | struct btrfs_balance_control *bctl) |
| 3112 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3113 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3114 | struct btrfs_trans_handle *trans; |
| 3115 | struct btrfs_balance_item *item; |
| 3116 | struct btrfs_disk_balance_args disk_bargs; |
| 3117 | struct btrfs_path *path; |
| 3118 | struct extent_buffer *leaf; |
| 3119 | struct btrfs_key key; |
| 3120 | int ret, err; |
| 3121 | |
| 3122 | path = btrfs_alloc_path(); |
| 3123 | if (!path) |
| 3124 | return -ENOMEM; |
| 3125 | |
| 3126 | trans = btrfs_start_transaction(root, 0); |
| 3127 | if (IS_ERR(trans)) { |
| 3128 | btrfs_free_path(path); |
| 3129 | return PTR_ERR(trans); |
| 3130 | } |
| 3131 | |
| 3132 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3133 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3134 | key.offset = 0; |
| 3135 | |
| 3136 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3137 | sizeof(*item)); |
| 3138 | if (ret) |
| 3139 | goto out; |
| 3140 | |
| 3141 | leaf = path->nodes[0]; |
| 3142 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3143 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3144 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3145 | |
| 3146 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3147 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3148 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3149 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3150 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3151 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3152 | |
| 3153 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3154 | |
| 3155 | btrfs_mark_buffer_dirty(leaf); |
| 3156 | out: |
| 3157 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3158 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3159 | if (err && !ret) |
| 3160 | ret = err; |
| 3161 | return ret; |
| 3162 | } |
| 3163 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3164 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3165 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3166 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3167 | struct btrfs_trans_handle *trans; |
| 3168 | struct btrfs_path *path; |
| 3169 | struct btrfs_key key; |
| 3170 | int ret, err; |
| 3171 | |
| 3172 | path = btrfs_alloc_path(); |
| 3173 | if (!path) |
| 3174 | return -ENOMEM; |
| 3175 | |
| 3176 | trans = btrfs_start_transaction(root, 0); |
| 3177 | if (IS_ERR(trans)) { |
| 3178 | btrfs_free_path(path); |
| 3179 | return PTR_ERR(trans); |
| 3180 | } |
| 3181 | |
| 3182 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3183 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3184 | key.offset = 0; |
| 3185 | |
| 3186 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3187 | if (ret < 0) |
| 3188 | goto out; |
| 3189 | if (ret > 0) { |
| 3190 | ret = -ENOENT; |
| 3191 | goto out; |
| 3192 | } |
| 3193 | |
| 3194 | ret = btrfs_del_item(trans, root, path); |
| 3195 | out: |
| 3196 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3197 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3198 | if (err && !ret) |
| 3199 | ret = err; |
| 3200 | return ret; |
| 3201 | } |
| 3202 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3203 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3204 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3205 | * resume after balance was interrupted. |
| 3206 | */ |
| 3207 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3208 | { |
| 3209 | /* |
| 3210 | * Turn on soft mode for chunk types that were being converted. |
| 3211 | */ |
| 3212 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3213 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3214 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3215 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3216 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3217 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3218 | |
| 3219 | /* |
| 3220 | * Turn on usage filter if is not already used. The idea is |
| 3221 | * that chunks that we have already balanced should be |
| 3222 | * reasonably full. Don't do it for chunks that are being |
| 3223 | * converted - that will keep us from relocating unconverted |
| 3224 | * (albeit full) chunks. |
| 3225 | */ |
| 3226 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3227 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3228 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3229 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3230 | bctl->data.usage = 90; |
| 3231 | } |
| 3232 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3233 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3234 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3235 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3236 | bctl->sys.usage = 90; |
| 3237 | } |
| 3238 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3239 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3240 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3241 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3242 | bctl->meta.usage = 90; |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3247 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3248 | */ |
| 3249 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3250 | { |
| 3251 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3252 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3253 | |
| 3254 | BUG_ON(!fs_info->balance_ctl); |
| 3255 | |
| 3256 | spin_lock(&fs_info->balance_lock); |
| 3257 | fs_info->balance_ctl = NULL; |
| 3258 | spin_unlock(&fs_info->balance_lock); |
| 3259 | |
| 3260 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3261 | ret = del_balance_item(fs_info); |
| 3262 | if (ret) |
| 3263 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3264 | } |
| 3265 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3266 | /* |
| 3267 | * Balance filters. Return 1 if chunk should be filtered out |
| 3268 | * (should not be balanced). |
| 3269 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3270 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3271 | struct btrfs_balance_args *bargs) |
| 3272 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3273 | chunk_type = chunk_to_extended(chunk_type) & |
| 3274 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3275 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3276 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3277 | return 0; |
| 3278 | |
| 3279 | return 1; |
| 3280 | } |
| 3281 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3282 | 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] | 3283 | struct btrfs_balance_args *bargs) |
| 3284 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3285 | struct btrfs_block_group *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3286 | u64 chunk_used; |
| 3287 | u64 user_thresh_min; |
| 3288 | u64 user_thresh_max; |
| 3289 | int ret = 1; |
| 3290 | |
| 3291 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3292 | chunk_used = cache->used; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3293 | |
| 3294 | if (bargs->usage_min == 0) |
| 3295 | user_thresh_min = 0; |
| 3296 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3297 | user_thresh_min = div_factor_fine(cache->length, |
| 3298 | bargs->usage_min); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3299 | |
| 3300 | if (bargs->usage_max == 0) |
| 3301 | user_thresh_max = 1; |
| 3302 | else if (bargs->usage_max > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3303 | user_thresh_max = cache->length; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3304 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3305 | user_thresh_max = div_factor_fine(cache->length, |
| 3306 | bargs->usage_max); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3307 | |
| 3308 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3309 | ret = 0; |
| 3310 | |
| 3311 | btrfs_put_block_group(cache); |
| 3312 | return ret; |
| 3313 | } |
| 3314 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3315 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3316 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3317 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3318 | struct btrfs_block_group *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3319 | u64 chunk_used, user_thresh; |
| 3320 | int ret = 1; |
| 3321 | |
| 3322 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3323 | chunk_used = cache->used; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3324 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3325 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3326 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3327 | else if (bargs->usage > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3328 | user_thresh = cache->length; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3329 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3330 | user_thresh = div_factor_fine(cache->length, bargs->usage); |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3331 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3332 | if (chunk_used < user_thresh) |
| 3333 | ret = 0; |
| 3334 | |
| 3335 | btrfs_put_block_group(cache); |
| 3336 | return ret; |
| 3337 | } |
| 3338 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3339 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3340 | struct btrfs_chunk *chunk, |
| 3341 | struct btrfs_balance_args *bargs) |
| 3342 | { |
| 3343 | struct btrfs_stripe *stripe; |
| 3344 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3345 | int i; |
| 3346 | |
| 3347 | for (i = 0; i < num_stripes; i++) { |
| 3348 | stripe = btrfs_stripe_nr(chunk, i); |
| 3349 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3350 | return 0; |
| 3351 | } |
| 3352 | |
| 3353 | return 1; |
| 3354 | } |
| 3355 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3356 | static u64 calc_data_stripes(u64 type, int num_stripes) |
| 3357 | { |
| 3358 | const int index = btrfs_bg_flags_to_raid_index(type); |
| 3359 | const int ncopies = btrfs_raid_array[index].ncopies; |
| 3360 | const int nparity = btrfs_raid_array[index].nparity; |
| 3361 | |
| 3362 | if (nparity) |
| 3363 | return num_stripes - nparity; |
| 3364 | else |
| 3365 | return num_stripes / ncopies; |
| 3366 | } |
| 3367 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3368 | /* [pstart, pend) */ |
| 3369 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3370 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3371 | struct btrfs_balance_args *bargs) |
| 3372 | { |
| 3373 | struct btrfs_stripe *stripe; |
| 3374 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3375 | u64 stripe_offset; |
| 3376 | u64 stripe_length; |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3377 | u64 type; |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3378 | int factor; |
| 3379 | int i; |
| 3380 | |
| 3381 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3382 | return 0; |
| 3383 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3384 | type = btrfs_chunk_type(leaf, chunk); |
| 3385 | factor = calc_data_stripes(type, num_stripes); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3386 | |
| 3387 | for (i = 0; i < num_stripes; i++) { |
| 3388 | stripe = btrfs_stripe_nr(chunk, i); |
| 3389 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3390 | continue; |
| 3391 | |
| 3392 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3393 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3394 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3395 | |
| 3396 | if (stripe_offset < bargs->pend && |
| 3397 | stripe_offset + stripe_length > bargs->pstart) |
| 3398 | return 0; |
| 3399 | } |
| 3400 | |
| 3401 | return 1; |
| 3402 | } |
| 3403 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3404 | /* [vstart, vend) */ |
| 3405 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3406 | struct btrfs_chunk *chunk, |
| 3407 | u64 chunk_offset, |
| 3408 | struct btrfs_balance_args *bargs) |
| 3409 | { |
| 3410 | if (chunk_offset < bargs->vend && |
| 3411 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3412 | /* at least part of the chunk is inside this vrange */ |
| 3413 | return 0; |
| 3414 | |
| 3415 | return 1; |
| 3416 | } |
| 3417 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3418 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3419 | struct btrfs_chunk *chunk, |
| 3420 | struct btrfs_balance_args *bargs) |
| 3421 | { |
| 3422 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3423 | |
| 3424 | if (bargs->stripes_min <= num_stripes |
| 3425 | && num_stripes <= bargs->stripes_max) |
| 3426 | return 0; |
| 3427 | |
| 3428 | return 1; |
| 3429 | } |
| 3430 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3431 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3432 | struct btrfs_balance_args *bargs) |
| 3433 | { |
| 3434 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3435 | return 0; |
| 3436 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3437 | chunk_type = chunk_to_extended(chunk_type) & |
| 3438 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3439 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3440 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3441 | return 1; |
| 3442 | |
| 3443 | return 0; |
| 3444 | } |
| 3445 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3446 | static int should_balance_chunk(struct extent_buffer *leaf, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3447 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3448 | { |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3449 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3450 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3451 | struct btrfs_balance_args *bargs = NULL; |
| 3452 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3453 | |
| 3454 | /* type filter */ |
| 3455 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3456 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3457 | return 0; |
| 3458 | } |
| 3459 | |
| 3460 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3461 | bargs = &bctl->data; |
| 3462 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3463 | bargs = &bctl->sys; |
| 3464 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3465 | bargs = &bctl->meta; |
| 3466 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3467 | /* profiles filter */ |
| 3468 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3469 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3470 | return 0; |
| 3471 | } |
| 3472 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3473 | /* usage filter */ |
| 3474 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3475 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3476 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3477 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3478 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3479 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3480 | } |
| 3481 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3482 | /* devid filter */ |
| 3483 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3484 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3485 | return 0; |
| 3486 | } |
| 3487 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3488 | /* drange filter, makes sense only with devid filter */ |
| 3489 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3490 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3491 | return 0; |
| 3492 | } |
| 3493 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3494 | /* vrange filter */ |
| 3495 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3496 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3500 | /* stripes filter */ |
| 3501 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3502 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3503 | return 0; |
| 3504 | } |
| 3505 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3506 | /* soft profile changing mode */ |
| 3507 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3508 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3509 | return 0; |
| 3510 | } |
| 3511 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3512 | /* |
| 3513 | * limited by count, must be the last filter |
| 3514 | */ |
| 3515 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3516 | if (bargs->limit == 0) |
| 3517 | return 0; |
| 3518 | else |
| 3519 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3520 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3521 | /* |
| 3522 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3523 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3524 | * about the count of all chunks that satisfy the filters. |
| 3525 | */ |
| 3526 | if (bargs->limit_max == 0) |
| 3527 | return 0; |
| 3528 | else |
| 3529 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3530 | } |
| 3531 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3532 | return 1; |
| 3533 | } |
| 3534 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3535 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3536 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3537 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3538 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3539 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3540 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3541 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3542 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3543 | struct btrfs_key found_key; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3544 | struct extent_buffer *leaf; |
| 3545 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3546 | int ret; |
| 3547 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3548 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3549 | /* 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] | 3550 | u64 limit_data = bctl->data.limit; |
| 3551 | u64 limit_meta = bctl->meta.limit; |
| 3552 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3553 | u32 count_data = 0; |
| 3554 | u32 count_meta = 0; |
| 3555 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3556 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3557 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3558 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3559 | if (!path) { |
| 3560 | ret = -ENOMEM; |
| 3561 | goto error; |
| 3562 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3563 | |
| 3564 | /* zero out stat counters */ |
| 3565 | spin_lock(&fs_info->balance_lock); |
| 3566 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3567 | spin_unlock(&fs_info->balance_lock); |
| 3568 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3569 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3570 | /* |
| 3571 | * The single value limit and min/max limits use the same bytes |
| 3572 | * in the |
| 3573 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3574 | bctl->data.limit = limit_data; |
| 3575 | bctl->meta.limit = limit_meta; |
| 3576 | bctl->sys.limit = limit_sys; |
| 3577 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3578 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3579 | key.offset = (u64)-1; |
| 3580 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3581 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3582 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3583 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3584 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3585 | ret = -ECANCELED; |
| 3586 | goto error; |
| 3587 | } |
| 3588 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3589 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3590 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3591 | if (ret < 0) { |
| 3592 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3593 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3594 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3595 | |
| 3596 | /* |
| 3597 | * this shouldn't happen, it means the last relocate |
| 3598 | * failed |
| 3599 | */ |
| 3600 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3601 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3602 | |
| 3603 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3604 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3605 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3606 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3607 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3608 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3609 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3610 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3611 | leaf = path->nodes[0]; |
| 3612 | slot = path->slots[0]; |
| 3613 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3614 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3615 | if (found_key.objectid != key.objectid) { |
| 3616 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3617 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3618 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3619 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3620 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3621 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3622 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3623 | if (!counting) { |
| 3624 | spin_lock(&fs_info->balance_lock); |
| 3625 | bctl->stat.considered++; |
| 3626 | spin_unlock(&fs_info->balance_lock); |
| 3627 | } |
| 3628 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3629 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3630 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3631 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3632 | if (!ret) { |
| 3633 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3634 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3635 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3636 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3637 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3638 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3639 | spin_lock(&fs_info->balance_lock); |
| 3640 | bctl->stat.expected++; |
| 3641 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3642 | |
| 3643 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3644 | count_data++; |
| 3645 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3646 | count_sys++; |
| 3647 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3648 | count_meta++; |
| 3649 | |
| 3650 | goto loop; |
| 3651 | } |
| 3652 | |
| 3653 | /* |
| 3654 | * Apply limit_min filter, no need to check if the LIMITS |
| 3655 | * filter is used, limit_min is 0 by default |
| 3656 | */ |
| 3657 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3658 | count_data < bctl->data.limit_min) |
| 3659 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3660 | count_meta < bctl->meta.limit_min) |
| 3661 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3662 | count_sys < bctl->sys.limit_min)) { |
| 3663 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3664 | goto loop; |
| 3665 | } |
| 3666 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3667 | if (!chunk_reserved) { |
| 3668 | /* |
| 3669 | * We may be relocating the only data chunk we have, |
| 3670 | * which could potentially end up with losing data's |
| 3671 | * raid profile, so lets allocate an empty one in |
| 3672 | * advance. |
| 3673 | */ |
| 3674 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3675 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3676 | if (ret < 0) { |
| 3677 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3678 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3679 | } else if (ret == 1) { |
| 3680 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3681 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3682 | } |
| 3683 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3684 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3685 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3686 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3687 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3688 | } else if (ret == -ETXTBSY) { |
| 3689 | btrfs_info(fs_info, |
| 3690 | "skipping relocation of block group %llu due to active swapfile", |
| 3691 | found_key.offset); |
| 3692 | ret = 0; |
| 3693 | } else if (ret) { |
| 3694 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3695 | } else { |
| 3696 | spin_lock(&fs_info->balance_lock); |
| 3697 | bctl->stat.completed++; |
| 3698 | spin_unlock(&fs_info->balance_lock); |
| 3699 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3700 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3701 | if (found_key.offset == 0) |
| 3702 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3703 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3704 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3705 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3706 | if (counting) { |
| 3707 | btrfs_release_path(path); |
| 3708 | counting = false; |
| 3709 | goto again; |
| 3710 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3711 | error: |
| 3712 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3713 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3714 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3715 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3716 | if (!ret) |
| 3717 | ret = -ENOSPC; |
| 3718 | } |
| 3719 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3720 | return ret; |
| 3721 | } |
| 3722 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3723 | /** |
| 3724 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3725 | * @flags: profile to validate |
| 3726 | * @extended: if true @flags is treated as an extended profile |
| 3727 | */ |
| 3728 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3729 | { |
| 3730 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3731 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3732 | |
| 3733 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3734 | |
| 3735 | /* 1) check that all other bits are zeroed */ |
| 3736 | if (flags & ~mask) |
| 3737 | return 0; |
| 3738 | |
| 3739 | /* 2) see if profile is reduced */ |
| 3740 | if (flags == 0) |
| 3741 | return !extended; /* "0" is valid for usual profiles */ |
| 3742 | |
David Sterba | c149916 | 2019-10-01 19:44:42 +0200 | [diff] [blame] | 3743 | return has_single_bit_set(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3744 | } |
| 3745 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3746 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3747 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3748 | /* cancel requested || normal exit path */ |
| 3749 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3750 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3751 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3752 | } |
| 3753 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3754 | /* Non-zero return value signifies invalidity */ |
| 3755 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3756 | u64 allowed) |
| 3757 | { |
| 3758 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3759 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3760 | (bctl_arg->target & ~allowed))); |
| 3761 | } |
| 3762 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3763 | /* |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3764 | * Fill @buf with textual description of balance filter flags @bargs, up to |
| 3765 | * @size_buf including the terminating null. The output may be trimmed if it |
| 3766 | * does not fit into the provided buffer. |
| 3767 | */ |
| 3768 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, |
| 3769 | u32 size_buf) |
| 3770 | { |
| 3771 | int ret; |
| 3772 | u32 size_bp = size_buf; |
| 3773 | char *bp = buf; |
| 3774 | u64 flags = bargs->flags; |
| 3775 | char tmp_buf[128] = {'\0'}; |
| 3776 | |
| 3777 | if (!flags) |
| 3778 | return; |
| 3779 | |
| 3780 | #define CHECK_APPEND_NOARG(a) \ |
| 3781 | do { \ |
| 3782 | ret = snprintf(bp, size_bp, (a)); \ |
| 3783 | if (ret < 0 || ret >= size_bp) \ |
| 3784 | goto out_overflow; \ |
| 3785 | size_bp -= ret; \ |
| 3786 | bp += ret; \ |
| 3787 | } while (0) |
| 3788 | |
| 3789 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3790 | do { \ |
| 3791 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3792 | if (ret < 0 || ret >= size_bp) \ |
| 3793 | goto out_overflow; \ |
| 3794 | size_bp -= ret; \ |
| 3795 | bp += ret; \ |
| 3796 | } while (0) |
| 3797 | |
| 3798 | #define CHECK_APPEND_2ARG(a, v1, v2) \ |
| 3799 | do { \ |
| 3800 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ |
| 3801 | if (ret < 0 || ret >= size_bp) \ |
| 3802 | goto out_overflow; \ |
| 3803 | size_bp -= ret; \ |
| 3804 | bp += ret; \ |
| 3805 | } while (0) |
| 3806 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3807 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3808 | CHECK_APPEND_1ARG("convert=%s,", |
| 3809 | btrfs_bg_type_to_raid_name(bargs->target)); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3810 | |
| 3811 | if (flags & BTRFS_BALANCE_ARGS_SOFT) |
| 3812 | CHECK_APPEND_NOARG("soft,"); |
| 3813 | |
| 3814 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { |
| 3815 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, |
| 3816 | sizeof(tmp_buf)); |
| 3817 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); |
| 3818 | } |
| 3819 | |
| 3820 | if (flags & BTRFS_BALANCE_ARGS_USAGE) |
| 3821 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); |
| 3822 | |
| 3823 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) |
| 3824 | CHECK_APPEND_2ARG("usage=%u..%u,", |
| 3825 | bargs->usage_min, bargs->usage_max); |
| 3826 | |
| 3827 | if (flags & BTRFS_BALANCE_ARGS_DEVID) |
| 3828 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); |
| 3829 | |
| 3830 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) |
| 3831 | CHECK_APPEND_2ARG("drange=%llu..%llu,", |
| 3832 | bargs->pstart, bargs->pend); |
| 3833 | |
| 3834 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) |
| 3835 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", |
| 3836 | bargs->vstart, bargs->vend); |
| 3837 | |
| 3838 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) |
| 3839 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); |
| 3840 | |
| 3841 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) |
| 3842 | CHECK_APPEND_2ARG("limit=%u..%u,", |
| 3843 | bargs->limit_min, bargs->limit_max); |
| 3844 | |
| 3845 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) |
| 3846 | CHECK_APPEND_2ARG("stripes=%u..%u,", |
| 3847 | bargs->stripes_min, bargs->stripes_max); |
| 3848 | |
| 3849 | #undef CHECK_APPEND_2ARG |
| 3850 | #undef CHECK_APPEND_1ARG |
| 3851 | #undef CHECK_APPEND_NOARG |
| 3852 | |
| 3853 | out_overflow: |
| 3854 | |
| 3855 | if (size_bp < size_buf) |
| 3856 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ |
| 3857 | else |
| 3858 | buf[0] = '\0'; |
| 3859 | } |
| 3860 | |
| 3861 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) |
| 3862 | { |
| 3863 | u32 size_buf = 1024; |
| 3864 | char tmp_buf[192] = {'\0'}; |
| 3865 | char *buf; |
| 3866 | char *bp; |
| 3867 | u32 size_bp = size_buf; |
| 3868 | int ret; |
| 3869 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3870 | |
| 3871 | buf = kzalloc(size_buf, GFP_KERNEL); |
| 3872 | if (!buf) |
| 3873 | return; |
| 3874 | |
| 3875 | bp = buf; |
| 3876 | |
| 3877 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3878 | do { \ |
| 3879 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3880 | if (ret < 0 || ret >= size_bp) \ |
| 3881 | goto out_overflow; \ |
| 3882 | size_bp -= ret; \ |
| 3883 | bp += ret; \ |
| 3884 | } while (0) |
| 3885 | |
| 3886 | if (bctl->flags & BTRFS_BALANCE_FORCE) |
| 3887 | CHECK_APPEND_1ARG("%s", "-f "); |
| 3888 | |
| 3889 | if (bctl->flags & BTRFS_BALANCE_DATA) { |
| 3890 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); |
| 3891 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); |
| 3892 | } |
| 3893 | |
| 3894 | if (bctl->flags & BTRFS_BALANCE_METADATA) { |
| 3895 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); |
| 3896 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); |
| 3897 | } |
| 3898 | |
| 3899 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { |
| 3900 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); |
| 3901 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); |
| 3902 | } |
| 3903 | |
| 3904 | #undef CHECK_APPEND_1ARG |
| 3905 | |
| 3906 | out_overflow: |
| 3907 | |
| 3908 | if (size_bp < size_buf) |
| 3909 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ |
| 3910 | btrfs_info(fs_info, "balance: %s %s", |
| 3911 | (bctl->flags & BTRFS_BALANCE_RESUME) ? |
| 3912 | "resume" : "start", buf); |
| 3913 | |
| 3914 | kfree(buf); |
| 3915 | } |
| 3916 | |
| 3917 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3918 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3919 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3920 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 3921 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3922 | struct btrfs_ioctl_balance_args *bargs) |
| 3923 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3924 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3925 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3926 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3927 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3928 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3929 | unsigned seq; |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3930 | bool reducing_redundancy; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 3931 | int i; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3932 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3933 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3934 | atomic_read(&fs_info->balance_pause_req) || |
| 3935 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3936 | ret = -EINVAL; |
| 3937 | goto out; |
| 3938 | } |
| 3939 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3940 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3941 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3942 | mixed = 1; |
| 3943 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3944 | /* |
| 3945 | * In case of mixed groups both data and meta should be picked, |
| 3946 | * and identical options should be given for both of them. |
| 3947 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3948 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3949 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3950 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3951 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3952 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3953 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3954 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3955 | ret = -EINVAL; |
| 3956 | goto out; |
| 3957 | } |
| 3958 | } |
| 3959 | |
Josef Bacik | b35cf1f | 2020-01-10 11:11:24 -0500 | [diff] [blame] | 3960 | /* |
| 3961 | * rw_devices will not change at the moment, device add/delete/replace |
| 3962 | * are excluded by EXCL_OP |
| 3963 | */ |
| 3964 | num_devices = fs_info->fs_devices->rw_devices; |
Qu Wenruo | fab2735 | 2019-09-25 10:13:27 +0800 | [diff] [blame] | 3965 | |
| 3966 | /* |
| 3967 | * SINGLE profile on-disk has no profile bit, but in-memory we have a |
| 3968 | * special bit for it, to make it easier to distinguish. Thus we need |
| 3969 | * to set it manually, or balance would refuse the profile. |
| 3970 | */ |
| 3971 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 3972 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
| 3973 | if (num_devices >= btrfs_raid_array[i].devs_min) |
| 3974 | allowed |= btrfs_raid_array[i].bg_flag; |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 3975 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3976 | if (validate_convert_profile(&bctl->data, allowed)) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3977 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3978 | "balance: invalid convert data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3979 | btrfs_bg_type_to_raid_name(bctl->data.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3980 | ret = -EINVAL; |
| 3981 | goto out; |
| 3982 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3983 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3984 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3985 | "balance: invalid convert metadata profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3986 | btrfs_bg_type_to_raid_name(bctl->meta.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3987 | ret = -EINVAL; |
| 3988 | goto out; |
| 3989 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3990 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3991 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3992 | "balance: invalid convert system profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3993 | btrfs_bg_type_to_raid_name(bctl->sys.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3994 | ret = -EINVAL; |
| 3995 | goto out; |
| 3996 | } |
| 3997 | |
David Sterba | 6079e12 | 2019-05-17 11:43:29 +0200 | [diff] [blame] | 3998 | /* |
| 3999 | * Allow to reduce metadata or system integrity only if force set for |
| 4000 | * profiles with redundancy (copies, parity) |
| 4001 | */ |
| 4002 | allowed = 0; |
| 4003 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { |
| 4004 | if (btrfs_raid_array[i].ncopies >= 2 || |
| 4005 | btrfs_raid_array[i].tolerated_failures >= 1) |
| 4006 | allowed |= btrfs_raid_array[i].bg_flag; |
| 4007 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4008 | do { |
| 4009 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 4010 | |
| 4011 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4012 | (fs_info->avail_system_alloc_bits & allowed) && |
| 4013 | !(bctl->sys.target & allowed)) || |
| 4014 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 4015 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4016 | !(bctl->meta.target & allowed))) |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4017 | reducing_redundancy = true; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4018 | else |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4019 | reducing_redundancy = false; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4020 | |
| 4021 | /* if we're not converting, the target field is uninitialized */ |
| 4022 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4023 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 4024 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 4025 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4026 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4027 | |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4028 | if (reducing_redundancy) { |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4029 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 4030 | btrfs_info(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4031 | "balance: force reducing metadata redundancy"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4032 | } else { |
| 4033 | btrfs_err(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 4034 | "balance: reduces metadata redundancy, use --force if you want this"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 4035 | ret = -EINVAL; |
| 4036 | goto out; |
| 4037 | } |
| 4038 | } |
| 4039 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 4040 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 4041 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4042 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4043 | "balance: metadata profile %s has lower redundancy than data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 4044 | btrfs_bg_type_to_raid_name(meta_target), |
| 4045 | btrfs_bg_type_to_raid_name(data_target)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 4048 | if (fs_info->send_in_progress) { |
| 4049 | btrfs_warn_rl(fs_info, |
| 4050 | "cannot run balance while send operations are in progress (%d in progress)", |
| 4051 | fs_info->send_in_progress); |
| 4052 | ret = -EAGAIN; |
| 4053 | goto out; |
| 4054 | } |
| 4055 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4056 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4057 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4058 | goto out; |
| 4059 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4060 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 4061 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4062 | BUG_ON(fs_info->balance_ctl); |
| 4063 | spin_lock(&fs_info->balance_lock); |
| 4064 | fs_info->balance_ctl = bctl; |
| 4065 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4066 | } else { |
| 4067 | BUG_ON(ret != -EEXIST); |
| 4068 | spin_lock(&fs_info->balance_lock); |
| 4069 | update_balance_args(bctl); |
| 4070 | spin_unlock(&fs_info->balance_lock); |
| 4071 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4072 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4073 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 4074 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4075 | describe_balance_start_or_resume(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4076 | mutex_unlock(&fs_info->balance_mutex); |
| 4077 | |
| 4078 | ret = __btrfs_balance(fs_info); |
| 4079 | |
| 4080 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4081 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) |
| 4082 | btrfs_info(fs_info, "balance: paused"); |
| 4083 | else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req)) |
| 4084 | btrfs_info(fs_info, "balance: canceled"); |
| 4085 | else |
| 4086 | btrfs_info(fs_info, "balance: ended with status: %d", ret); |
| 4087 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4088 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4089 | |
| 4090 | if (bargs) { |
| 4091 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4092 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4093 | } |
| 4094 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4095 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4096 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4097 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4098 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4099 | } |
| 4100 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4101 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4102 | |
| 4103 | return ret; |
| 4104 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4105 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4106 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4107 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4108 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4109 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4110 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4111 | return ret; |
| 4112 | } |
| 4113 | |
| 4114 | static int balance_kthread(void *data) |
| 4115 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4116 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4117 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4118 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4119 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4120 | if (fs_info->balance_ctl) |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4121 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4122 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4123 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4124 | return ret; |
| 4125 | } |
| 4126 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4127 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4128 | { |
| 4129 | struct task_struct *tsk; |
| 4130 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4131 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4132 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4133 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4134 | return 0; |
| 4135 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4136 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4137 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4138 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4139 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4140 | return 0; |
| 4141 | } |
| 4142 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4143 | /* |
| 4144 | * A ro->rw remount sequence should continue with the paused balance |
| 4145 | * regardless of who pauses it, system or the user as of now, so set |
| 4146 | * the resume flag. |
| 4147 | */ |
| 4148 | spin_lock(&fs_info->balance_lock); |
| 4149 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4150 | spin_unlock(&fs_info->balance_lock); |
| 4151 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4152 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4153 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4154 | } |
| 4155 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4156 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4157 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4158 | struct btrfs_balance_control *bctl; |
| 4159 | struct btrfs_balance_item *item; |
| 4160 | struct btrfs_disk_balance_args disk_bargs; |
| 4161 | struct btrfs_path *path; |
| 4162 | struct extent_buffer *leaf; |
| 4163 | struct btrfs_key key; |
| 4164 | int ret; |
| 4165 | |
| 4166 | path = btrfs_alloc_path(); |
| 4167 | if (!path) |
| 4168 | return -ENOMEM; |
| 4169 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4170 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4171 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4172 | key.offset = 0; |
| 4173 | |
| 4174 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4175 | if (ret < 0) |
| 4176 | goto out; |
| 4177 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4178 | ret = 0; |
| 4179 | goto out; |
| 4180 | } |
| 4181 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4182 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4183 | if (!bctl) { |
| 4184 | ret = -ENOMEM; |
| 4185 | goto out; |
| 4186 | } |
| 4187 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4188 | leaf = path->nodes[0]; |
| 4189 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4190 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4191 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4192 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4193 | |
| 4194 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4195 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4196 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4197 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4198 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4199 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4200 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4201 | /* |
| 4202 | * This should never happen, as the paused balance state is recovered |
| 4203 | * during mount without any chance of other exclusive ops to collide. |
| 4204 | * |
| 4205 | * This gives the exclusive op status to balance and keeps in paused |
| 4206 | * state until user intervention (cancel or umount). If the ownership |
| 4207 | * cannot be assigned, show a message but do not fail. The balance |
| 4208 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4209 | * set up. |
| 4210 | */ |
| 4211 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4212 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4213 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4214 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4215 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4216 | BUG_ON(fs_info->balance_ctl); |
| 4217 | spin_lock(&fs_info->balance_lock); |
| 4218 | fs_info->balance_ctl = bctl; |
| 4219 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4220 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4221 | out: |
| 4222 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4223 | return ret; |
| 4224 | } |
| 4225 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4226 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4227 | { |
| 4228 | int ret = 0; |
| 4229 | |
| 4230 | mutex_lock(&fs_info->balance_mutex); |
| 4231 | if (!fs_info->balance_ctl) { |
| 4232 | mutex_unlock(&fs_info->balance_mutex); |
| 4233 | return -ENOTCONN; |
| 4234 | } |
| 4235 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4236 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4237 | atomic_inc(&fs_info->balance_pause_req); |
| 4238 | mutex_unlock(&fs_info->balance_mutex); |
| 4239 | |
| 4240 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4241 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4242 | |
| 4243 | mutex_lock(&fs_info->balance_mutex); |
| 4244 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4245 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4246 | atomic_dec(&fs_info->balance_pause_req); |
| 4247 | } else { |
| 4248 | ret = -ENOTCONN; |
| 4249 | } |
| 4250 | |
| 4251 | mutex_unlock(&fs_info->balance_mutex); |
| 4252 | return ret; |
| 4253 | } |
| 4254 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4255 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4256 | { |
| 4257 | mutex_lock(&fs_info->balance_mutex); |
| 4258 | if (!fs_info->balance_ctl) { |
| 4259 | mutex_unlock(&fs_info->balance_mutex); |
| 4260 | return -ENOTCONN; |
| 4261 | } |
| 4262 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4263 | /* |
| 4264 | * A paused balance with the item stored on disk can be resumed at |
| 4265 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4266 | * and we must not allow cancelling as it deletes the item. |
| 4267 | */ |
| 4268 | if (sb_rdonly(fs_info->sb)) { |
| 4269 | mutex_unlock(&fs_info->balance_mutex); |
| 4270 | return -EROFS; |
| 4271 | } |
| 4272 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4273 | atomic_inc(&fs_info->balance_cancel_req); |
| 4274 | /* |
| 4275 | * if we are running just wait and return, balance item is |
| 4276 | * deleted in btrfs_balance in this case |
| 4277 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4278 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4279 | mutex_unlock(&fs_info->balance_mutex); |
| 4280 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4281 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4282 | mutex_lock(&fs_info->balance_mutex); |
| 4283 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4284 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4285 | /* |
| 4286 | * Lock released to allow other waiters to continue, we'll |
| 4287 | * reexamine the status again. |
| 4288 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4289 | mutex_lock(&fs_info->balance_mutex); |
| 4290 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4291 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4292 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4293 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4294 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4295 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4296 | } |
| 4297 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4298 | BUG_ON(fs_info->balance_ctl || |
| 4299 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4300 | atomic_dec(&fs_info->balance_cancel_req); |
| 4301 | mutex_unlock(&fs_info->balance_mutex); |
| 4302 | return 0; |
| 4303 | } |
| 4304 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4305 | static int btrfs_uuid_scan_kthread(void *data) |
| 4306 | { |
| 4307 | struct btrfs_fs_info *fs_info = data; |
| 4308 | struct btrfs_root *root = fs_info->tree_root; |
| 4309 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4310 | struct btrfs_path *path = NULL; |
| 4311 | int ret = 0; |
| 4312 | struct extent_buffer *eb; |
| 4313 | int slot; |
| 4314 | struct btrfs_root_item root_item; |
| 4315 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4316 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4317 | |
| 4318 | path = btrfs_alloc_path(); |
| 4319 | if (!path) { |
| 4320 | ret = -ENOMEM; |
| 4321 | goto out; |
| 4322 | } |
| 4323 | |
| 4324 | key.objectid = 0; |
| 4325 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4326 | key.offset = 0; |
| 4327 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4328 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4329 | ret = btrfs_search_forward(root, &key, path, |
| 4330 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4331 | if (ret) { |
| 4332 | if (ret > 0) |
| 4333 | ret = 0; |
| 4334 | break; |
| 4335 | } |
| 4336 | |
| 4337 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4338 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4339 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4340 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4341 | goto skip; |
| 4342 | |
| 4343 | eb = path->nodes[0]; |
| 4344 | slot = path->slots[0]; |
| 4345 | item_size = btrfs_item_size_nr(eb, slot); |
| 4346 | if (item_size < sizeof(root_item)) |
| 4347 | goto skip; |
| 4348 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4349 | read_extent_buffer(eb, &root_item, |
| 4350 | btrfs_item_ptr_offset(eb, slot), |
| 4351 | (int)sizeof(root_item)); |
| 4352 | if (btrfs_root_refs(&root_item) == 0) |
| 4353 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4354 | |
| 4355 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4356 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4357 | if (trans) |
| 4358 | goto update_tree; |
| 4359 | |
| 4360 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4361 | /* |
| 4362 | * 1 - subvol uuid item |
| 4363 | * 1 - received_subvol uuid item |
| 4364 | */ |
| 4365 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4366 | if (IS_ERR(trans)) { |
| 4367 | ret = PTR_ERR(trans); |
| 4368 | break; |
| 4369 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4370 | continue; |
| 4371 | } else { |
| 4372 | goto skip; |
| 4373 | } |
| 4374 | update_tree: |
| 4375 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4376 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4377 | BTRFS_UUID_KEY_SUBVOL, |
| 4378 | key.objectid); |
| 4379 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4380 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4381 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4382 | break; |
| 4383 | } |
| 4384 | } |
| 4385 | |
| 4386 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4387 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4388 | root_item.received_uuid, |
| 4389 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4390 | key.objectid); |
| 4391 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4392 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4393 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4394 | break; |
| 4395 | } |
| 4396 | } |
| 4397 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4398 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4399 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4400 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4401 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4402 | if (ret) |
| 4403 | break; |
| 4404 | } |
| 4405 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4406 | btrfs_release_path(path); |
| 4407 | if (key.offset < (u64)-1) { |
| 4408 | key.offset++; |
| 4409 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4410 | key.offset = 0; |
| 4411 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4412 | } else if (key.objectid < (u64)-1) { |
| 4413 | key.offset = 0; |
| 4414 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4415 | key.objectid++; |
| 4416 | } else { |
| 4417 | break; |
| 4418 | } |
| 4419 | cond_resched(); |
| 4420 | } |
| 4421 | |
| 4422 | out: |
| 4423 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4424 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4425 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4426 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4427 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4428 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4429 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4430 | up(&fs_info->uuid_tree_rescan_sem); |
| 4431 | return 0; |
| 4432 | } |
| 4433 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4434 | /* |
| 4435 | * Callback for btrfs_uuid_tree_iterate(). |
| 4436 | * returns: |
| 4437 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4438 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4439 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4440 | */ |
| 4441 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4442 | u8 *uuid, u8 type, u64 subid) |
| 4443 | { |
| 4444 | struct btrfs_key key; |
| 4445 | int ret = 0; |
| 4446 | struct btrfs_root *subvol_root; |
| 4447 | |
| 4448 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4449 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4450 | goto out; |
| 4451 | |
| 4452 | key.objectid = subid; |
| 4453 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4454 | key.offset = (u64)-1; |
Josef Bacik | 3619c94 | 2020-01-24 09:32:24 -0500 | [diff] [blame] | 4455 | subvol_root = btrfs_get_fs_root(fs_info, &key, true); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4456 | if (IS_ERR(subvol_root)) { |
| 4457 | ret = PTR_ERR(subvol_root); |
| 4458 | if (ret == -ENOENT) |
| 4459 | ret = 1; |
| 4460 | goto out; |
| 4461 | } |
| 4462 | |
| 4463 | switch (type) { |
| 4464 | case BTRFS_UUID_KEY_SUBVOL: |
| 4465 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4466 | ret = 1; |
| 4467 | break; |
| 4468 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4469 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4470 | BTRFS_UUID_SIZE)) |
| 4471 | ret = 1; |
| 4472 | break; |
| 4473 | } |
Josef Bacik | 0024652 | 2020-01-24 09:33:01 -0500 | [diff] [blame] | 4474 | btrfs_put_root(subvol_root); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4475 | out: |
| 4476 | return ret; |
| 4477 | } |
| 4478 | |
| 4479 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4480 | { |
| 4481 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4482 | int ret; |
| 4483 | |
| 4484 | /* |
| 4485 | * 1st step is to iterate through the existing UUID tree and |
| 4486 | * to delete all entries that contain outdated data. |
| 4487 | * 2nd step is to add all missing entries to the UUID tree. |
| 4488 | */ |
| 4489 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4490 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4491 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4492 | up(&fs_info->uuid_tree_rescan_sem); |
| 4493 | return ret; |
| 4494 | } |
| 4495 | return btrfs_uuid_scan_kthread(data); |
| 4496 | } |
| 4497 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4498 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4499 | { |
| 4500 | struct btrfs_trans_handle *trans; |
| 4501 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4502 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4503 | struct task_struct *task; |
| 4504 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4505 | |
| 4506 | /* |
| 4507 | * 1 - root node |
| 4508 | * 1 - root item |
| 4509 | */ |
| 4510 | trans = btrfs_start_transaction(tree_root, 2); |
| 4511 | if (IS_ERR(trans)) |
| 4512 | return PTR_ERR(trans); |
| 4513 | |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 4514 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4515 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4516 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4517 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4518 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4519 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | fs_info->uuid_root = uuid_root; |
| 4523 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4524 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4525 | if (ret) |
| 4526 | return ret; |
| 4527 | |
| 4528 | down(&fs_info->uuid_tree_rescan_sem); |
| 4529 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4530 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4531 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4532 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4533 | up(&fs_info->uuid_tree_rescan_sem); |
| 4534 | return PTR_ERR(task); |
| 4535 | } |
| 4536 | |
| 4537 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4538 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4539 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4540 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4541 | { |
| 4542 | struct task_struct *task; |
| 4543 | |
| 4544 | down(&fs_info->uuid_tree_rescan_sem); |
| 4545 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4546 | if (IS_ERR(task)) { |
| 4547 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4548 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4549 | up(&fs_info->uuid_tree_rescan_sem); |
| 4550 | return PTR_ERR(task); |
| 4551 | } |
| 4552 | |
| 4553 | return 0; |
| 4554 | } |
| 4555 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4556 | /* |
| 4557 | * shrinking a device means finding all of the device extents past |
| 4558 | * the new size, and then following the back refs to the chunks. |
| 4559 | * The chunk relocation code actually frees the device extent |
| 4560 | */ |
| 4561 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4562 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4563 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4564 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4565 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4566 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4567 | struct btrfs_path *path; |
| 4568 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4569 | u64 chunk_offset; |
| 4570 | int ret; |
| 4571 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4572 | int failed = 0; |
| 4573 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4574 | struct extent_buffer *l; |
| 4575 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4576 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4577 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4578 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4579 | u64 diff; |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4580 | u64 start; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4581 | |
| 4582 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4583 | start = new_size; |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4584 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4585 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4586 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4587 | return -EINVAL; |
| 4588 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4589 | path = btrfs_alloc_path(); |
| 4590 | if (!path) |
| 4591 | return -ENOMEM; |
| 4592 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4593 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4594 | |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4595 | trans = btrfs_start_transaction(root, 0); |
| 4596 | if (IS_ERR(trans)) { |
| 4597 | btrfs_free_path(path); |
| 4598 | return PTR_ERR(trans); |
| 4599 | } |
| 4600 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4601 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4602 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4603 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4604 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4605 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4606 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4607 | } |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4608 | |
| 4609 | /* |
| 4610 | * Once the device's size has been set to the new size, ensure all |
| 4611 | * in-memory chunks are synced to disk so that the loop below sees them |
| 4612 | * and relocates them accordingly. |
| 4613 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 4614 | if (contains_pending_extent(device, &start, diff)) { |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4615 | mutex_unlock(&fs_info->chunk_mutex); |
| 4616 | ret = btrfs_commit_transaction(trans); |
| 4617 | if (ret) |
| 4618 | goto done; |
| 4619 | } else { |
| 4620 | mutex_unlock(&fs_info->chunk_mutex); |
| 4621 | btrfs_end_transaction(trans); |
| 4622 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4623 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4624 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4625 | key.objectid = device->devid; |
| 4626 | key.offset = (u64)-1; |
| 4627 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4628 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4629 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4630 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4631 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4632 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4633 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4634 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4635 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4636 | |
| 4637 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4638 | if (ret) |
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 | if (ret < 0) |
| 4641 | goto done; |
| 4642 | if (ret) { |
| 4643 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4644 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4645 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | l = path->nodes[0]; |
| 4649 | slot = path->slots[0]; |
| 4650 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4651 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4652 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4653 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4654 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4655 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4656 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4657 | |
| 4658 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4659 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4660 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4661 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4662 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4663 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4664 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4665 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4666 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4667 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4668 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4669 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4670 | /* |
| 4671 | * We may be relocating the only data chunk we have, |
| 4672 | * which could potentially end up with losing data's |
| 4673 | * raid profile, so lets allocate an empty one in |
| 4674 | * advance. |
| 4675 | */ |
| 4676 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4677 | if (ret < 0) { |
| 4678 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4679 | goto done; |
| 4680 | } |
| 4681 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4682 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4683 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4684 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4685 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4686 | } else if (ret) { |
| 4687 | if (ret == -ETXTBSY) { |
| 4688 | btrfs_warn(fs_info, |
| 4689 | "could not shrink block group %llu due to active swapfile", |
| 4690 | chunk_offset); |
| 4691 | } |
| 4692 | goto done; |
| 4693 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4694 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4695 | |
| 4696 | if (failed && !retried) { |
| 4697 | failed = 0; |
| 4698 | retried = true; |
| 4699 | goto again; |
| 4700 | } else if (failed && retried) { |
| 4701 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4702 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4703 | } |
| 4704 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4705 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4706 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4707 | if (IS_ERR(trans)) { |
| 4708 | ret = PTR_ERR(trans); |
| 4709 | goto done; |
| 4710 | } |
| 4711 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4712 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4713 | btrfs_device_set_disk_total_bytes(device, new_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4714 | if (list_empty(&device->post_commit_list)) |
| 4715 | list_add_tail(&device->post_commit_list, |
| 4716 | &trans->transaction->dev_update_list); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4717 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4718 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4719 | btrfs_set_super_total_bytes(super_copy, |
| 4720 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4721 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4722 | |
| 4723 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4724 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4725 | if (ret < 0) { |
| 4726 | btrfs_abort_transaction(trans, ret); |
| 4727 | btrfs_end_transaction(trans); |
| 4728 | } else { |
| 4729 | ret = btrfs_commit_transaction(trans); |
| 4730 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4731 | done: |
| 4732 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4733 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4734 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4735 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4736 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4737 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4738 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4739 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4740 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4741 | return ret; |
| 4742 | } |
| 4743 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4744 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4745 | struct btrfs_key *key, |
| 4746 | struct btrfs_chunk *chunk, int item_size) |
| 4747 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4748 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4749 | struct btrfs_disk_key disk_key; |
| 4750 | u32 array_size; |
| 4751 | u8 *ptr; |
| 4752 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4753 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4754 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4755 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4756 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4757 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4758 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4759 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4760 | |
| 4761 | ptr = super_copy->sys_chunk_array + array_size; |
| 4762 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4763 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4764 | ptr += sizeof(disk_key); |
| 4765 | memcpy(ptr, chunk, item_size); |
| 4766 | item_size += sizeof(disk_key); |
| 4767 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4768 | mutex_unlock(&fs_info->chunk_mutex); |
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 | return 0; |
| 4771 | } |
| 4772 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4773 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4774 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4775 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4776 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4777 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4778 | const struct btrfs_device_info *di_a = a; |
| 4779 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4780 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4781 | if (di_a->max_avail > di_b->max_avail) |
| 4782 | return -1; |
| 4783 | if (di_a->max_avail < di_b->max_avail) |
| 4784 | return 1; |
| 4785 | if (di_a->total_avail > di_b->total_avail) |
| 4786 | return -1; |
| 4787 | if (di_a->total_avail < di_b->total_avail) |
| 4788 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4789 | return 0; |
| 4790 | } |
| 4791 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4792 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4793 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4794 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4795 | return; |
| 4796 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4797 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4798 | } |
| 4799 | |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 4800 | static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4801 | { |
| 4802 | if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4))) |
| 4803 | return; |
| 4804 | |
| 4805 | btrfs_set_fs_incompat(info, RAID1C34); |
| 4806 | } |
| 4807 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4808 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4809 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4810 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4811 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4812 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4813 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4814 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4815 | struct extent_map_tree *em_tree; |
| 4816 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4817 | struct btrfs_device_info *devices_info = NULL; |
| 4818 | u64 total_avail; |
| 4819 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4820 | int data_stripes; /* number of stripes that count for |
| 4821 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4822 | int sub_stripes; /* sub_stripes info for map */ |
| 4823 | int dev_stripes; /* stripes per dev */ |
| 4824 | int devs_max; /* max devs to use */ |
| 4825 | int devs_min; /* min devs needed */ |
| 4826 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4827 | int ncopies; /* how many copies to data has */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4828 | int nparity; /* number of stripes worth of bytes to |
| 4829 | store parity information */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4830 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4831 | u64 max_stripe_size; |
| 4832 | u64 max_chunk_size; |
| 4833 | u64 stripe_size; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4834 | u64 chunk_size; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4835 | int ndevs; |
| 4836 | int i; |
| 4837 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4838 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4839 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4840 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4841 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4842 | if (list_empty(&fs_devices->alloc_list)) { |
| 4843 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4844 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4845 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4846 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4847 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4848 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4849 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4850 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4851 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4852 | devs_max = btrfs_raid_array[index].devs_max; |
David Sterba | e3ecdb3 | 2019-05-17 11:43:38 +0200 | [diff] [blame] | 4853 | if (!devs_max) |
| 4854 | devs_max = BTRFS_MAX_DEVS(info); |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4855 | devs_min = btrfs_raid_array[index].devs_min; |
| 4856 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4857 | ncopies = btrfs_raid_array[index].ncopies; |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4858 | nparity = btrfs_raid_array[index].nparity; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4859 | |
| 4860 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4861 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4862 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4863 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4864 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4865 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4866 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4867 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4868 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4869 | max_chunk_size = max_stripe_size; |
| 4870 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4871 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4872 | max_chunk_size = 2 * max_stripe_size; |
Qu Wenruo | c17add7 | 2019-08-28 10:33:12 +0800 | [diff] [blame] | 4873 | devs_max = min_t(int, devs_max, BTRFS_MAX_DEVS_SYS_CHUNK); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4874 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4875 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4876 | type); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4877 | BUG(); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4878 | } |
| 4879 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4880 | /* We don't want a chunk larger than 10% of writable space */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4881 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4882 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4883 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4884 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4885 | GFP_NOFS); |
| 4886 | if (!devices_info) |
| 4887 | return -ENOMEM; |
| 4888 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4889 | /* |
| 4890 | * in the first pass through the devices list, we gather information |
| 4891 | * about the available holes on each device. |
| 4892 | */ |
| 4893 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4894 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4895 | u64 max_avail; |
| 4896 | u64 dev_offset; |
| 4897 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4898 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4899 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4900 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4901 | continue; |
| 4902 | } |
| 4903 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 4904 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 4905 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4906 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4907 | continue; |
| 4908 | |
| 4909 | if (device->total_bytes > device->bytes_used) |
| 4910 | total_avail = device->total_bytes - device->bytes_used; |
| 4911 | else |
| 4912 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4913 | |
| 4914 | /* If there is no space on this device, skip it. */ |
| 4915 | if (total_avail == 0) |
| 4916 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4917 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 4918 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4919 | max_stripe_size * dev_stripes, |
| 4920 | &dev_offset, &max_avail); |
| 4921 | if (ret && ret != -ENOSPC) |
| 4922 | goto error; |
| 4923 | |
| 4924 | if (ret == 0) |
| 4925 | max_avail = max_stripe_size * dev_stripes; |
| 4926 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4927 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 4928 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4929 | btrfs_debug(info, |
| 4930 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 4931 | __func__, device->devid, max_avail, |
| 4932 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4933 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4934 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4935 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4936 | if (ndevs == fs_devices->rw_devices) { |
| 4937 | WARN(1, "%s: found more than %llu devices\n", |
| 4938 | __func__, fs_devices->rw_devices); |
| 4939 | break; |
| 4940 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4941 | devices_info[ndevs].dev_offset = dev_offset; |
| 4942 | devices_info[ndevs].max_avail = max_avail; |
| 4943 | devices_info[ndevs].total_avail = total_avail; |
| 4944 | devices_info[ndevs].dev = device; |
| 4945 | ++ndevs; |
| 4946 | } |
| 4947 | |
| 4948 | /* |
| 4949 | * now sort the devices by hole size / available space |
| 4950 | */ |
| 4951 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4952 | btrfs_cmp_device_info, NULL); |
| 4953 | |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 4954 | /* |
| 4955 | * Round down to number of usable stripes, devs_increment can be any |
| 4956 | * number so we can't use round_down() |
| 4957 | */ |
| 4958 | ndevs -= ndevs % devs_increment; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4959 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4960 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4961 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4962 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 4963 | btrfs_debug(info, |
| 4964 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4965 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4966 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4967 | goto error; |
| 4968 | } |
| 4969 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 4970 | ndevs = min(ndevs, devs_max); |
| 4971 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4972 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4973 | * The primary goal is to maximize the number of stripes, so use as |
| 4974 | * many devices as possible, even if the stripes are not maximum sized. |
| 4975 | * |
| 4976 | * The DUP profile stores more than one stripe per device, the |
| 4977 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4978 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4979 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4980 | num_stripes = ndevs * dev_stripes; |
| 4981 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4982 | /* |
| 4983 | * this will have to be fixed for RAID1 and RAID10 over |
| 4984 | * more drives |
| 4985 | */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4986 | data_stripes = (num_stripes - nparity) / ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4987 | |
| 4988 | /* |
| 4989 | * Use the number of data stripes to figure out how big this chunk |
| 4990 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4991 | * and compare that answer with the max chunk size. If it's higher, |
| 4992 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4993 | */ |
| 4994 | if (stripe_size * data_stripes > max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4995 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4996 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 4997 | * then use it, unless it ends up being even bigger than the |
| 4998 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4999 | */ |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 5000 | stripe_size = min(round_up(div_u64(max_chunk_size, |
| 5001 | data_stripes), SZ_16M), |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 5002 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 5003 | } |
| 5004 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 5005 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5006 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5007 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5008 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 5009 | if (!map) { |
| 5010 | ret = -ENOMEM; |
| 5011 | goto error; |
| 5012 | } |
| 5013 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 5014 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 5015 | for (i = 0; i < ndevs; ++i) { |
| 5016 | for (j = 0; j < dev_stripes; ++j) { |
| 5017 | int s = i * dev_stripes + j; |
| 5018 | map->stripes[s].dev = devices_info[i].dev; |
| 5019 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 5020 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 5021 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5022 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 5023 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 5024 | map->io_align = BTRFS_STRIPE_LEN; |
| 5025 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5026 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5027 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5028 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5029 | chunk_size = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5030 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5031 | trace_btrfs_chunk_alloc(info, map, start, chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5032 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5033 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5034 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5035 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5036 | ret = -ENOMEM; |
| 5037 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5038 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5039 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5040 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5041 | em->start = start; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5042 | em->len = chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5043 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5044 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5045 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5046 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5047 | em_tree = &info->mapping_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5048 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5049 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5050 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5051 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5052 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 5053 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5054 | } |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 5055 | write_unlock(&em_tree->lock); |
| 5056 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 5057 | ret = btrfs_make_block_group(trans, 0, type, start, chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5058 | if (ret) |
| 5059 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5060 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 5061 | for (i = 0; i < map->num_stripes; i++) { |
| 5062 | struct btrfs_device *dev = map->stripes[i].dev; |
| 5063 | |
| 5064 | btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size); |
| 5065 | if (list_empty(&dev->post_commit_list)) |
| 5066 | list_add_tail(&dev->post_commit_list, |
| 5067 | &trans->transaction->dev_update_list); |
| 5068 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 5069 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 5070 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 5071 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5072 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5073 | check_raid56_incompat_flag(info, type); |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 5074 | check_raid1c34_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5075 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5076 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5077 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5078 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5079 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5080 | write_lock(&em_tree->lock); |
| 5081 | remove_extent_mapping(em_tree, em); |
| 5082 | write_unlock(&em_tree->lock); |
| 5083 | |
| 5084 | /* One for our allocation */ |
| 5085 | free_extent_map(em); |
| 5086 | /* One for the tree reference */ |
| 5087 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5088 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5089 | kfree(devices_info); |
| 5090 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5091 | } |
| 5092 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5093 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5094 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5095 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5096 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5097 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5098 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5099 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5100 | struct btrfs_device *device; |
| 5101 | struct btrfs_chunk *chunk; |
| 5102 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5103 | struct extent_map *em; |
| 5104 | struct map_lookup *map; |
| 5105 | size_t item_size; |
| 5106 | u64 dev_offset; |
| 5107 | u64 stripe_size; |
| 5108 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5109 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5110 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5111 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5112 | if (IS_ERR(em)) |
| 5113 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5114 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5115 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5116 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5117 | stripe_size = em->orig_block_len; |
| 5118 | |
| 5119 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5120 | if (!chunk) { |
| 5121 | ret = -ENOMEM; |
| 5122 | goto out; |
| 5123 | } |
| 5124 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5125 | /* |
| 5126 | * Take the device list mutex to prevent races with the final phase of |
| 5127 | * a device replace operation that replaces the device object associated |
| 5128 | * with the map's stripes, because the device object's id can change |
| 5129 | * at any time during that final phase of the device replace operation |
| 5130 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5131 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5132 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5133 | for (i = 0; i < map->num_stripes; i++) { |
| 5134 | device = map->stripes[i].dev; |
| 5135 | dev_offset = map->stripes[i].physical; |
| 5136 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5137 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5138 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5139 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5140 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5141 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5142 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5143 | break; |
| 5144 | } |
| 5145 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5146 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5147 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5148 | } |
| 5149 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5150 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5151 | for (i = 0; i < map->num_stripes; i++) { |
| 5152 | device = map->stripes[i].dev; |
| 5153 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5154 | |
| 5155 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5156 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5157 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5158 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5159 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5160 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5161 | |
| 5162 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5163 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5164 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5165 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5166 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5167 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5168 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5169 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5170 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5171 | |
| 5172 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5173 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5174 | key.offset = chunk_offset; |
| 5175 | |
| 5176 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5177 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5178 | /* |
| 5179 | * TODO: Cleanup of inserted chunk root in case of |
| 5180 | * failure. |
| 5181 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5182 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5183 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5184 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5185 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5186 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5187 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5188 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5192 | * Chunk allocation falls into two parts. The first part does work |
| 5193 | * that makes the new allocated chunk usable, but does not do any operation |
| 5194 | * that modifies the chunk tree. The second part does the work that |
| 5195 | * requires modifying the chunk tree. This division is important for the |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5196 | * bootstrap process of adding storage to a seed btrfs. |
| 5197 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5198 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5199 | { |
| 5200 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5201 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5202 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5203 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5204 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5205 | } |
| 5206 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5207 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5208 | { |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5209 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5210 | u64 chunk_offset; |
| 5211 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5212 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5213 | int ret; |
| 5214 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5215 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5216 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5217 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5218 | if (ret) |
| 5219 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5220 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5221 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5222 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5223 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5224 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5225 | } |
| 5226 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5227 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5228 | { |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5229 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5230 | |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5231 | return btrfs_raid_array[index].tolerated_failures; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5232 | } |
| 5233 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5234 | 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] | 5235 | { |
| 5236 | struct extent_map *em; |
| 5237 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5238 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5239 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5240 | int i; |
| 5241 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5242 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5243 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5244 | return 1; |
| 5245 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5246 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5247 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5248 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5249 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5250 | miss_ndevs++; |
| 5251 | continue; |
| 5252 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5253 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5254 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5255 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5256 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5257 | } |
| 5258 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5259 | |
| 5260 | /* |
| 5261 | * If the number of missing devices is larger than max errors, |
| 5262 | * we can not write the data into that chunk successfully, so |
| 5263 | * set it readonly. |
| 5264 | */ |
| 5265 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5266 | readonly = 1; |
| 5267 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5268 | free_extent_map(em); |
| 5269 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5270 | } |
| 5271 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5272 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5273 | { |
| 5274 | struct extent_map *em; |
| 5275 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5276 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5277 | write_lock(&tree->lock); |
| 5278 | em = lookup_extent_mapping(tree, 0, (u64)-1); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5279 | if (em) |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5280 | remove_extent_mapping(tree, em); |
| 5281 | write_unlock(&tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5282 | if (!em) |
| 5283 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5284 | /* once for us */ |
| 5285 | free_extent_map(em); |
| 5286 | /* once for the tree */ |
| 5287 | free_extent_map(em); |
| 5288 | } |
| 5289 | } |
| 5290 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5291 | 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] | 5292 | { |
| 5293 | struct extent_map *em; |
| 5294 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5295 | int ret; |
| 5296 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5297 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5298 | if (IS_ERR(em)) |
| 5299 | /* |
| 5300 | * We could return errors for these cases, but that could get |
| 5301 | * ugly and we'd probably do the same thing which is just not do |
| 5302 | * anything else and exit, so return 1 so the callers don't try |
| 5303 | * to use other copies. |
| 5304 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5305 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5306 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5307 | map = em->map_lookup; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5308 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK)) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5309 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5310 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5311 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5312 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5313 | ret = 2; |
| 5314 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5315 | /* |
| 5316 | * There could be two corrupted data stripes, we need |
| 5317 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5318 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5319 | * Fail a stripe at a time on every retry except the |
| 5320 | * stripe under reconstruction. |
| 5321 | */ |
| 5322 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5323 | else |
| 5324 | ret = 1; |
| 5325 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5326 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5327 | down_read(&fs_info->dev_replace.rwsem); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5328 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5329 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5330 | ret++; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5331 | up_read(&fs_info->dev_replace.rwsem); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5332 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5333 | return ret; |
| 5334 | } |
| 5335 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5336 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5337 | u64 logical) |
| 5338 | { |
| 5339 | struct extent_map *em; |
| 5340 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5341 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5342 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5343 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5344 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5345 | if (!WARN_ON(IS_ERR(em))) { |
| 5346 | map = em->map_lookup; |
| 5347 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5348 | len = map->stripe_len * nr_data_stripes(map); |
| 5349 | free_extent_map(em); |
| 5350 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5351 | return len; |
| 5352 | } |
| 5353 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5354 | 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] | 5355 | { |
| 5356 | struct extent_map *em; |
| 5357 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5358 | int ret = 0; |
| 5359 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5360 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5361 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5362 | if(!WARN_ON(IS_ERR(em))) { |
| 5363 | map = em->map_lookup; |
| 5364 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5365 | ret = 1; |
| 5366 | free_extent_map(em); |
| 5367 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5368 | return ret; |
| 5369 | } |
| 5370 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5371 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5372 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5373 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5374 | { |
| 5375 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5376 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5377 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5378 | int tolerance; |
| 5379 | struct btrfs_device *srcdev; |
| 5380 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5381 | ASSERT((map->type & |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5382 | (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10))); |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5383 | |
| 5384 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5385 | num_stripes = map->sub_stripes; |
| 5386 | else |
| 5387 | num_stripes = map->num_stripes; |
| 5388 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5389 | preferred_mirror = first + current->pid % num_stripes; |
| 5390 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5391 | if (dev_replace_is_ongoing && |
| 5392 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5393 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5394 | srcdev = fs_info->dev_replace.srcdev; |
| 5395 | else |
| 5396 | srcdev = NULL; |
| 5397 | |
| 5398 | /* |
| 5399 | * try to avoid the drive that is the source drive for a |
| 5400 | * dev-replace procedure, only choose it if no other non-missing |
| 5401 | * mirror is available |
| 5402 | */ |
| 5403 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5404 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5405 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5406 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5407 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5408 | if (map->stripes[i].dev->bdev && |
| 5409 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5410 | return i; |
| 5411 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5412 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5413 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5414 | /* we couldn't find one that doesn't fail. Just return something |
| 5415 | * and the io error handling code will clean up eventually |
| 5416 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5417 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5418 | } |
| 5419 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5420 | static inline int parity_smaller(u64 a, u64 b) |
| 5421 | { |
| 5422 | return a > b; |
| 5423 | } |
| 5424 | |
| 5425 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5426 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5427 | { |
| 5428 | struct btrfs_bio_stripe s; |
| 5429 | int i; |
| 5430 | u64 l; |
| 5431 | int again = 1; |
| 5432 | |
| 5433 | while (again) { |
| 5434 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5435 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5436 | if (parity_smaller(bbio->raid_map[i], |
| 5437 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5438 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5439 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5440 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5441 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5442 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5443 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5444 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5445 | again = 1; |
| 5446 | } |
| 5447 | } |
| 5448 | } |
| 5449 | } |
| 5450 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5451 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5452 | { |
| 5453 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5454 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5455 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5456 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5457 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5458 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5459 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5460 | /* |
| 5461 | * plus the raid_map, which includes both the tgt dev |
| 5462 | * and the stripes |
| 5463 | */ |
| 5464 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5465 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5466 | |
| 5467 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5468 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5469 | |
| 5470 | return bbio; |
| 5471 | } |
| 5472 | |
| 5473 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5474 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5475 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5476 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5480 | { |
| 5481 | if (!bbio) |
| 5482 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5483 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5484 | kfree(bbio); |
| 5485 | } |
| 5486 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5487 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5488 | /* |
| 5489 | * Please note that, discard won't be sent to target device of device |
| 5490 | * replace. |
| 5491 | */ |
| 5492 | 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] | 5493 | u64 logical, u64 *length_ret, |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5494 | struct btrfs_bio **bbio_ret) |
| 5495 | { |
| 5496 | struct extent_map *em; |
| 5497 | struct map_lookup *map; |
| 5498 | struct btrfs_bio *bbio; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5499 | u64 length = *length_ret; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5500 | u64 offset; |
| 5501 | u64 stripe_nr; |
| 5502 | u64 stripe_nr_end; |
| 5503 | u64 stripe_end_offset; |
| 5504 | u64 stripe_cnt; |
| 5505 | u64 stripe_len; |
| 5506 | u64 stripe_offset; |
| 5507 | u64 num_stripes; |
| 5508 | u32 stripe_index; |
| 5509 | u32 factor = 0; |
| 5510 | u32 sub_stripes = 0; |
| 5511 | u64 stripes_per_dev = 0; |
| 5512 | u32 remaining_stripes = 0; |
| 5513 | u32 last_stripe = 0; |
| 5514 | int ret = 0; |
| 5515 | int i; |
| 5516 | |
| 5517 | /* discard always return a bbio */ |
| 5518 | ASSERT(bbio_ret); |
| 5519 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5520 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5521 | if (IS_ERR(em)) |
| 5522 | return PTR_ERR(em); |
| 5523 | |
| 5524 | map = em->map_lookup; |
| 5525 | /* we don't discard raid56 yet */ |
| 5526 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5527 | ret = -EOPNOTSUPP; |
| 5528 | goto out; |
| 5529 | } |
| 5530 | |
| 5531 | offset = logical - em->start; |
Qu Wenruo | 2d97461 | 2019-10-23 21:57:26 +0800 | [diff] [blame] | 5532 | length = min_t(u64, em->start + em->len - logical, length); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5533 | *length_ret = length; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5534 | |
| 5535 | stripe_len = map->stripe_len; |
| 5536 | /* |
| 5537 | * stripe_nr counts the total number of stripes we have to stride |
| 5538 | * to get to this block |
| 5539 | */ |
| 5540 | stripe_nr = div64_u64(offset, stripe_len); |
| 5541 | |
| 5542 | /* stripe_offset is the offset of this block in its stripe */ |
| 5543 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5544 | |
| 5545 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5546 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5547 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5548 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5549 | (offset + length); |
| 5550 | /* |
| 5551 | * after this, stripe_nr is the number of stripes on this |
| 5552 | * device we have to walk to find the data, and stripe_index is |
| 5553 | * the number of our device in the stripe array |
| 5554 | */ |
| 5555 | num_stripes = 1; |
| 5556 | stripe_index = 0; |
| 5557 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5558 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5559 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5560 | sub_stripes = 1; |
| 5561 | else |
| 5562 | sub_stripes = map->sub_stripes; |
| 5563 | |
| 5564 | factor = map->num_stripes / sub_stripes; |
| 5565 | num_stripes = min_t(u64, map->num_stripes, |
| 5566 | sub_stripes * stripe_cnt); |
| 5567 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5568 | stripe_index *= sub_stripes; |
| 5569 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5570 | &remaining_stripes); |
| 5571 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5572 | last_stripe *= sub_stripes; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5573 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5574 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5575 | num_stripes = map->num_stripes; |
| 5576 | } else { |
| 5577 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5578 | &stripe_index); |
| 5579 | } |
| 5580 | |
| 5581 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5582 | if (!bbio) { |
| 5583 | ret = -ENOMEM; |
| 5584 | goto out; |
| 5585 | } |
| 5586 | |
| 5587 | for (i = 0; i < num_stripes; i++) { |
| 5588 | bbio->stripes[i].physical = |
| 5589 | map->stripes[stripe_index].physical + |
| 5590 | stripe_offset + stripe_nr * map->stripe_len; |
| 5591 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5592 | |
| 5593 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5594 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5595 | bbio->stripes[i].length = stripes_per_dev * |
| 5596 | map->stripe_len; |
| 5597 | |
| 5598 | if (i / sub_stripes < remaining_stripes) |
| 5599 | bbio->stripes[i].length += |
| 5600 | map->stripe_len; |
| 5601 | |
| 5602 | /* |
| 5603 | * Special for the first stripe and |
| 5604 | * the last stripe: |
| 5605 | * |
| 5606 | * |-------|...|-------| |
| 5607 | * |----------| |
| 5608 | * off end_off |
| 5609 | */ |
| 5610 | if (i < sub_stripes) |
| 5611 | bbio->stripes[i].length -= |
| 5612 | stripe_offset; |
| 5613 | |
| 5614 | if (stripe_index >= last_stripe && |
| 5615 | stripe_index <= (last_stripe + |
| 5616 | sub_stripes - 1)) |
| 5617 | bbio->stripes[i].length -= |
| 5618 | stripe_end_offset; |
| 5619 | |
| 5620 | if (i == sub_stripes - 1) |
| 5621 | stripe_offset = 0; |
| 5622 | } else { |
| 5623 | bbio->stripes[i].length = length; |
| 5624 | } |
| 5625 | |
| 5626 | stripe_index++; |
| 5627 | if (stripe_index == map->num_stripes) { |
| 5628 | stripe_index = 0; |
| 5629 | stripe_nr++; |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | *bbio_ret = bbio; |
| 5634 | bbio->map_type = map->type; |
| 5635 | bbio->num_stripes = num_stripes; |
| 5636 | out: |
| 5637 | free_extent_map(em); |
| 5638 | return ret; |
| 5639 | } |
| 5640 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5641 | /* |
| 5642 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5643 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5644 | * left cursor can also be read from the target drive. |
| 5645 | * |
| 5646 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5647 | * array of stripes. |
| 5648 | * For READ, it also needs to be supported using the same mirror number. |
| 5649 | * |
| 5650 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5651 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5652 | * case. |
| 5653 | */ |
| 5654 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5655 | u64 logical, u64 length, |
| 5656 | u64 srcdev_devid, int *mirror_num, |
| 5657 | u64 *physical) |
| 5658 | { |
| 5659 | struct btrfs_bio *bbio = NULL; |
| 5660 | int num_stripes; |
| 5661 | int index_srcdev = 0; |
| 5662 | int found = 0; |
| 5663 | u64 physical_of_found = 0; |
| 5664 | int i; |
| 5665 | int ret = 0; |
| 5666 | |
| 5667 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5668 | logical, &length, &bbio, 0, 0); |
| 5669 | if (ret) { |
| 5670 | ASSERT(bbio == NULL); |
| 5671 | return ret; |
| 5672 | } |
| 5673 | |
| 5674 | num_stripes = bbio->num_stripes; |
| 5675 | if (*mirror_num > num_stripes) { |
| 5676 | /* |
| 5677 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5678 | * that means that the requested area is not left of the left |
| 5679 | * cursor |
| 5680 | */ |
| 5681 | btrfs_put_bbio(bbio); |
| 5682 | return -EIO; |
| 5683 | } |
| 5684 | |
| 5685 | /* |
| 5686 | * process the rest of the function using the mirror_num of the source |
| 5687 | * drive. Therefore look it up first. At the end, patch the device |
| 5688 | * pointer to the one of the target drive. |
| 5689 | */ |
| 5690 | for (i = 0; i < num_stripes; i++) { |
| 5691 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5692 | continue; |
| 5693 | |
| 5694 | /* |
| 5695 | * In case of DUP, in order to keep it simple, only add the |
| 5696 | * mirror with the lowest physical address |
| 5697 | */ |
| 5698 | if (found && |
| 5699 | physical_of_found <= bbio->stripes[i].physical) |
| 5700 | continue; |
| 5701 | |
| 5702 | index_srcdev = i; |
| 5703 | found = 1; |
| 5704 | physical_of_found = bbio->stripes[i].physical; |
| 5705 | } |
| 5706 | |
| 5707 | btrfs_put_bbio(bbio); |
| 5708 | |
| 5709 | ASSERT(found); |
| 5710 | if (!found) |
| 5711 | return -EIO; |
| 5712 | |
| 5713 | *mirror_num = index_srcdev + 1; |
| 5714 | *physical = physical_of_found; |
| 5715 | return ret; |
| 5716 | } |
| 5717 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5718 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5719 | struct btrfs_bio **bbio_ret, |
| 5720 | struct btrfs_dev_replace *dev_replace, |
| 5721 | int *num_stripes_ret, int *max_errors_ret) |
| 5722 | { |
| 5723 | struct btrfs_bio *bbio = *bbio_ret; |
| 5724 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5725 | int tgtdev_indexes = 0; |
| 5726 | int num_stripes = *num_stripes_ret; |
| 5727 | int max_errors = *max_errors_ret; |
| 5728 | int i; |
| 5729 | |
| 5730 | if (op == BTRFS_MAP_WRITE) { |
| 5731 | int index_where_to_add; |
| 5732 | |
| 5733 | /* |
| 5734 | * duplicate the write operations while the dev replace |
| 5735 | * procedure is running. Since the copying of the old disk to |
| 5736 | * the new disk takes place at run time while the filesystem is |
| 5737 | * mounted writable, the regular write operations to the old |
| 5738 | * disk have to be duplicated to go to the new disk as well. |
| 5739 | * |
| 5740 | * Note that device->missing is handled by the caller, and that |
| 5741 | * the write to the old disk is already set up in the stripes |
| 5742 | * array. |
| 5743 | */ |
| 5744 | index_where_to_add = num_stripes; |
| 5745 | for (i = 0; i < num_stripes; i++) { |
| 5746 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5747 | /* write to new disk, too */ |
| 5748 | struct btrfs_bio_stripe *new = |
| 5749 | bbio->stripes + index_where_to_add; |
| 5750 | struct btrfs_bio_stripe *old = |
| 5751 | bbio->stripes + i; |
| 5752 | |
| 5753 | new->physical = old->physical; |
| 5754 | new->length = old->length; |
| 5755 | new->dev = dev_replace->tgtdev; |
| 5756 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5757 | index_where_to_add++; |
| 5758 | max_errors++; |
| 5759 | tgtdev_indexes++; |
| 5760 | } |
| 5761 | } |
| 5762 | num_stripes = index_where_to_add; |
| 5763 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5764 | int index_srcdev = 0; |
| 5765 | int found = 0; |
| 5766 | u64 physical_of_found = 0; |
| 5767 | |
| 5768 | /* |
| 5769 | * During the dev-replace procedure, the target drive can also |
| 5770 | * be used to read data in case it is needed to repair a corrupt |
| 5771 | * block elsewhere. This is possible if the requested area is |
| 5772 | * left of the left cursor. In this area, the target drive is a |
| 5773 | * full copy of the source drive. |
| 5774 | */ |
| 5775 | for (i = 0; i < num_stripes; i++) { |
| 5776 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5777 | /* |
| 5778 | * In case of DUP, in order to keep it simple, |
| 5779 | * only add the mirror with the lowest physical |
| 5780 | * address |
| 5781 | */ |
| 5782 | if (found && |
| 5783 | physical_of_found <= |
| 5784 | bbio->stripes[i].physical) |
| 5785 | continue; |
| 5786 | index_srcdev = i; |
| 5787 | found = 1; |
| 5788 | physical_of_found = bbio->stripes[i].physical; |
| 5789 | } |
| 5790 | } |
| 5791 | if (found) { |
| 5792 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5793 | bbio->stripes + num_stripes; |
| 5794 | |
| 5795 | tgtdev_stripe->physical = physical_of_found; |
| 5796 | tgtdev_stripe->length = |
| 5797 | bbio->stripes[index_srcdev].length; |
| 5798 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5799 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5800 | |
| 5801 | tgtdev_indexes++; |
| 5802 | num_stripes++; |
| 5803 | } |
| 5804 | } |
| 5805 | |
| 5806 | *num_stripes_ret = num_stripes; |
| 5807 | *max_errors_ret = max_errors; |
| 5808 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5809 | *bbio_ret = bbio; |
| 5810 | } |
| 5811 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5812 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5813 | { |
| 5814 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5815 | } |
| 5816 | |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5817 | /* |
| 5818 | * btrfs_get_io_geometry - calculates the geomery of a particular (address, len) |
| 5819 | * tuple. This information is used to calculate how big a |
| 5820 | * particular bio can get before it straddles a stripe. |
| 5821 | * |
| 5822 | * @fs_info - the filesystem |
| 5823 | * @logical - address that we want to figure out the geometry of |
| 5824 | * @len - the length of IO we are going to perform, starting at @logical |
| 5825 | * @op - type of operation - write or read |
| 5826 | * @io_geom - pointer used to return values |
| 5827 | * |
| 5828 | * Returns < 0 in case a chunk for the given logical address cannot be found, |
| 5829 | * usually shouldn't happen unless @logical is corrupted, 0 otherwise. |
| 5830 | */ |
| 5831 | 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] | 5832 | u64 logical, u64 len, struct btrfs_io_geometry *io_geom) |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5833 | { |
| 5834 | struct extent_map *em; |
| 5835 | struct map_lookup *map; |
| 5836 | u64 offset; |
| 5837 | u64 stripe_offset; |
| 5838 | u64 stripe_nr; |
| 5839 | u64 stripe_len; |
| 5840 | u64 raid56_full_stripe_start = (u64)-1; |
| 5841 | int data_stripes; |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5842 | int ret = 0; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5843 | |
| 5844 | ASSERT(op != BTRFS_MAP_DISCARD); |
| 5845 | |
| 5846 | em = btrfs_get_chunk_map(fs_info, logical, len); |
| 5847 | if (IS_ERR(em)) |
| 5848 | return PTR_ERR(em); |
| 5849 | |
| 5850 | map = em->map_lookup; |
| 5851 | /* Offset of this logical address in the chunk */ |
| 5852 | offset = logical - em->start; |
| 5853 | /* Len of a stripe in a chunk */ |
| 5854 | stripe_len = map->stripe_len; |
| 5855 | /* Stripe wher this block falls in */ |
| 5856 | stripe_nr = div64_u64(offset, stripe_len); |
| 5857 | /* Offset of stripe in the chunk */ |
| 5858 | stripe_offset = stripe_nr * stripe_len; |
| 5859 | if (offset < stripe_offset) { |
| 5860 | btrfs_crit(fs_info, |
| 5861 | "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu", |
| 5862 | stripe_offset, offset, em->start, logical, stripe_len); |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5863 | ret = -EINVAL; |
| 5864 | goto out; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | /* stripe_offset is the offset of this block in its stripe */ |
| 5868 | stripe_offset = offset - stripe_offset; |
| 5869 | data_stripes = nr_data_stripes(map); |
| 5870 | |
| 5871 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5872 | u64 max_len = stripe_len - stripe_offset; |
| 5873 | |
| 5874 | /* |
| 5875 | * In case of raid56, we need to know the stripe aligned start |
| 5876 | */ |
| 5877 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5878 | unsigned long full_stripe_len = stripe_len * data_stripes; |
| 5879 | raid56_full_stripe_start = offset; |
| 5880 | |
| 5881 | /* |
| 5882 | * Allow a write of a full stripe, but make sure we |
| 5883 | * don't allow straddling of stripes |
| 5884 | */ |
| 5885 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5886 | full_stripe_len); |
| 5887 | raid56_full_stripe_start *= full_stripe_len; |
| 5888 | |
| 5889 | /* |
| 5890 | * For writes to RAID[56], allow a full stripeset across |
| 5891 | * all disks. For other RAID types and for RAID[56] |
| 5892 | * reads, just allow a single stripe (on a single disk). |
| 5893 | */ |
| 5894 | if (op == BTRFS_MAP_WRITE) { |
| 5895 | max_len = stripe_len * data_stripes - |
| 5896 | (offset - raid56_full_stripe_start); |
| 5897 | } |
| 5898 | } |
| 5899 | len = min_t(u64, em->len - offset, max_len); |
| 5900 | } else { |
| 5901 | len = em->len - offset; |
| 5902 | } |
| 5903 | |
| 5904 | io_geom->len = len; |
| 5905 | io_geom->offset = offset; |
| 5906 | io_geom->stripe_len = stripe_len; |
| 5907 | io_geom->stripe_nr = stripe_nr; |
| 5908 | io_geom->stripe_offset = stripe_offset; |
| 5909 | io_geom->raid56_stripe_offset = raid56_full_stripe_start; |
| 5910 | |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5911 | out: |
| 5912 | /* once for us */ |
| 5913 | free_extent_map(em); |
| 5914 | return ret; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5915 | } |
| 5916 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5917 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 5918 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5919 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5920 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5921 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5922 | { |
| 5923 | struct extent_map *em; |
| 5924 | struct map_lookup *map; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5925 | u64 stripe_offset; |
| 5926 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5927 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5928 | u32 stripe_index; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5929 | int data_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5930 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5931 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5932 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5933 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5934 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5935 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5936 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5937 | int dev_replace_is_ongoing = 0; |
| 5938 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5939 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5940 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5941 | u64 raid56_full_stripe_start = (u64)-1; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5942 | struct btrfs_io_geometry geom; |
| 5943 | |
| 5944 | ASSERT(bbio_ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5945 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5946 | if (op == BTRFS_MAP_DISCARD) |
| 5947 | return __btrfs_map_block_for_discard(fs_info, logical, |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5948 | length, bbio_ret); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5949 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5950 | ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom); |
| 5951 | if (ret < 0) |
| 5952 | return ret; |
| 5953 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5954 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Dan Carpenter | f113698 | 2019-08-09 17:07:39 +0300 | [diff] [blame] | 5955 | ASSERT(!IS_ERR(em)); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5956 | map = em->map_lookup; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5957 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5958 | *length = geom.len; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5959 | stripe_len = geom.stripe_len; |
| 5960 | stripe_nr = geom.stripe_nr; |
| 5961 | stripe_offset = geom.stripe_offset; |
| 5962 | raid56_full_stripe_start = geom.raid56_stripe_offset; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5963 | data_stripes = nr_data_stripes(map); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5964 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5965 | down_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5966 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 5967 | /* |
| 5968 | * Hold the semaphore for read during the whole operation, write is |
| 5969 | * requested at commit time but must wait. |
| 5970 | */ |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5971 | if (!dev_replace_is_ongoing) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5972 | up_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5973 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5974 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5975 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5976 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 5977 | dev_replace->srcdev->devid, |
| 5978 | &mirror_num, |
| 5979 | &physical_to_patch_in_first_stripe); |
| 5980 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5981 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5982 | else |
| 5983 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5984 | } else if (mirror_num > map->num_stripes) { |
| 5985 | mirror_num = 0; |
| 5986 | } |
| 5987 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5988 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5989 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5990 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5991 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5992 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5993 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5994 | mirror_num = 1; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5995 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5996 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5997 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5998 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5999 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6000 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6001 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6002 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6003 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6004 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6005 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6006 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6007 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6008 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6009 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6010 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6011 | } else { |
| 6012 | mirror_num = 1; |
| 6013 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 6014 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6015 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6016 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6017 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6018 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6019 | stripe_index *= map->sub_stripes; |
| 6020 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6021 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6022 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6023 | else if (mirror_num) |
| 6024 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6025 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6026 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6027 | stripe_index = find_live_mirror(fs_info, map, |
| 6028 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 6029 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 6030 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6031 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6032 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6033 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6034 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6035 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6036 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6037 | stripe_len * data_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6038 | |
| 6039 | /* RAID[56] write or recovery. Return all stripes */ |
| 6040 | num_stripes = map->num_stripes; |
| 6041 | max_errors = nr_parity_stripes(map); |
| 6042 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6043 | *length = map->stripe_len; |
| 6044 | stripe_index = 0; |
| 6045 | stripe_offset = 0; |
| 6046 | } else { |
| 6047 | /* |
| 6048 | * Mirror #0 or #1 means the original data block. |
| 6049 | * Mirror #2 is RAID5 parity block. |
| 6050 | * Mirror #3 is RAID6 Q block. |
| 6051 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6052 | stripe_nr = div_u64_rem(stripe_nr, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6053 | data_stripes, &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6054 | if (mirror_num > 1) |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6055 | stripe_index = data_stripes + mirror_num - 2; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6056 | |
| 6057 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6058 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 6059 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 6060 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 6061 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6062 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6063 | } else { |
| 6064 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6065 | * after this, stripe_nr is the number of stripes on this |
| 6066 | * device we have to walk to find the data, and stripe_index is |
| 6067 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6068 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6069 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 6070 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6071 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6072 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 6073 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6074 | btrfs_crit(fs_info, |
| 6075 | "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] | 6076 | stripe_index, map->num_stripes); |
| 6077 | ret = -EINVAL; |
| 6078 | goto out; |
| 6079 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6080 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6081 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6082 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6083 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6084 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6085 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6086 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6087 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6088 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6089 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6090 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6091 | if (!bbio) { |
| 6092 | ret = -ENOMEM; |
| 6093 | goto out; |
| 6094 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6095 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6096 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6097 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6098 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6099 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6100 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6101 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6102 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6103 | |
| 6104 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 6105 | sizeof(struct btrfs_bio_stripe) * |
| 6106 | num_alloc_stripes + |
| 6107 | sizeof(int) * tgtdev_indexes); |
| 6108 | |
| 6109 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6110 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6111 | |
| 6112 | /* Fill in the logical address of each stripe */ |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6113 | tmp = stripe_nr * data_stripes; |
| 6114 | for (i = 0; i < data_stripes; i++) |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6115 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6116 | em->start + (tmp + i) * map->stripe_len; |
| 6117 | |
| 6118 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6119 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6120 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6121 | RAID6_Q_STRIPE; |
| 6122 | } |
| 6123 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 6124 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6125 | for (i = 0; i < num_stripes; i++) { |
| 6126 | bbio->stripes[i].physical = |
| 6127 | map->stripes[stripe_index].physical + |
| 6128 | stripe_offset + |
| 6129 | stripe_nr * map->stripe_len; |
| 6130 | bbio->stripes[i].dev = |
| 6131 | map->stripes[stripe_index].dev; |
| 6132 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6133 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6134 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6135 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6136 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6137 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6138 | if (bbio->raid_map) |
| 6139 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 6140 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6141 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6142 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6143 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6144 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6145 | } |
| 6146 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6147 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6148 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6149 | bbio->num_stripes = num_stripes; |
| 6150 | bbio->max_errors = max_errors; |
| 6151 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6152 | |
| 6153 | /* |
| 6154 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6155 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6156 | * available as a mirror |
| 6157 | */ |
| 6158 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6159 | WARN_ON(num_stripes > 1); |
| 6160 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6161 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6162 | bbio->mirror_num = map->num_stripes + 1; |
| 6163 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6164 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6165 | if (dev_replace_is_ongoing) { |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6166 | lockdep_assert_held(&dev_replace->rwsem); |
| 6167 | /* Unlock and let waiting writers proceed */ |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6168 | up_read(&dev_replace->rwsem); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6169 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6170 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6171 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6172 | } |
| 6173 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6174 | 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] | 6175 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6176 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6177 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6178 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6179 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6180 | } |
| 6181 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6182 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6183 | 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] | 6184 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6185 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6186 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6187 | 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] | 6188 | } |
| 6189 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6190 | 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] | 6191 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6192 | bio->bi_private = bbio->private; |
| 6193 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6194 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6195 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6196 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6197 | } |
| 6198 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6199 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6200 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6201 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6202 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6203 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6204 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6205 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6206 | if (bio->bi_status == BLK_STS_IOERR || |
| 6207 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6208 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6209 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6210 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6211 | |
| 6212 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6213 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6214 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6215 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6216 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6217 | BTRFS_DEV_STAT_WRITE_ERRS); |
David Sterba | 0cc068e | 2019-03-07 15:40:50 +0100 | [diff] [blame] | 6218 | else if (!(bio->bi_opf & REQ_RAHEAD)) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6219 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6220 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6221 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6222 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6223 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6224 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6225 | } |
| 6226 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6227 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6228 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6229 | is_orig_bio = 1; |
| 6230 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6231 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6232 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6233 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6234 | if (!is_orig_bio) { |
| 6235 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6236 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6237 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6238 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6239 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6240 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6241 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6242 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6243 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6244 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6245 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6246 | /* |
| 6247 | * this bio is actually up to date, we didn't |
| 6248 | * go over the max number of errors |
| 6249 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6250 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6251 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6252 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6253 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6254 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6255 | bio_put(bio); |
| 6256 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6257 | } |
| 6258 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6259 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6260 | u64 physical, int dev_nr) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6261 | { |
| 6262 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6263 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6264 | |
| 6265 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6266 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6267 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6268 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6269 | btrfs_debug_in_rcu(fs_info, |
| 6270 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6271 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
| 6272 | (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid, |
| 6273 | bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6274 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6275 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6276 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6277 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6278 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6279 | } |
| 6280 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6281 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6282 | { |
| 6283 | atomic_inc(&bbio->error); |
| 6284 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6285 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6286 | WARN_ON(bio != bbio->orig_bio); |
| 6287 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6288 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6289 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6290 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6291 | bio->bi_status = BLK_STS_IOERR; |
| 6292 | else |
| 6293 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6294 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6295 | } |
| 6296 | } |
| 6297 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6298 | 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] | 6299 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6300 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6301 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6302 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6303 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6304 | u64 length = 0; |
| 6305 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6306 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6307 | int dev_nr; |
| 6308 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6309 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6310 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6311 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6312 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6313 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6314 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6315 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6316 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6317 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6318 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6319 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6320 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6321 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6322 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6323 | bbio->orig_bio = first_bio; |
| 6324 | bbio->private = first_bio->bi_private; |
| 6325 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6326 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6327 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6328 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6329 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6330 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6331 | /* In this case, map_length has been set to the length of |
| 6332 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6333 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6334 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6335 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6336 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6337 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6338 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6339 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6340 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6341 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6342 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6343 | } |
| 6344 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6345 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6346 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6347 | "mapping failed logical %llu bio len %llu len %llu", |
| 6348 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6349 | BUG(); |
| 6350 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6351 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6352 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6353 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6354 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6355 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6356 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6357 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6358 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6359 | continue; |
| 6360 | } |
| 6361 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6362 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6363 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6364 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6365 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6366 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6367 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6368 | dev_nr); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6369 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6370 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6371 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6372 | } |
| 6373 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6374 | /* |
| 6375 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 6376 | * return NULL. |
| 6377 | * |
| 6378 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 6379 | * only devid is used. |
| 6380 | * |
| 6381 | * If @seed is true, traverse through the seed devices. |
| 6382 | */ |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6383 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6384 | u64 devid, u8 *uuid, u8 *fsid, |
| 6385 | bool seed) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6386 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6387 | struct btrfs_device *device; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6388 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6389 | while (fs_devices) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6390 | if (!fsid || |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6391 | !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6392 | list_for_each_entry(device, &fs_devices->devices, |
| 6393 | dev_list) { |
| 6394 | if (device->devid == devid && |
| 6395 | (!uuid || memcmp(device->uuid, uuid, |
| 6396 | BTRFS_UUID_SIZE) == 0)) |
| 6397 | return device; |
| 6398 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6399 | } |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6400 | if (seed) |
| 6401 | fs_devices = fs_devices->seed; |
| 6402 | else |
| 6403 | return NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6404 | } |
| 6405 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6406 | } |
| 6407 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6408 | 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] | 6409 | u64 devid, u8 *dev_uuid) |
| 6410 | { |
| 6411 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6412 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6413 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6414 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6415 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6416 | |
| 6417 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6418 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6419 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6420 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6421 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6422 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6423 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6424 | return device; |
| 6425 | } |
| 6426 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6427 | /** |
| 6428 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6429 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6430 | * devid is provided (i.e. @devid != NULL). |
| 6431 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6432 | * is generated. |
| 6433 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6434 | * is generated. |
| 6435 | * |
| 6436 | * 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] | 6437 | * 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] | 6438 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6439 | */ |
| 6440 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6441 | const u64 *devid, |
| 6442 | const u8 *uuid) |
| 6443 | { |
| 6444 | struct btrfs_device *dev; |
| 6445 | u64 tmp; |
| 6446 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6447 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6448 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6449 | |
| 6450 | dev = __alloc_device(); |
| 6451 | if (IS_ERR(dev)) |
| 6452 | return dev; |
| 6453 | |
| 6454 | if (devid) |
| 6455 | tmp = *devid; |
| 6456 | else { |
| 6457 | int ret; |
| 6458 | |
| 6459 | ret = find_next_devid(fs_info, &tmp); |
| 6460 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6461 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6462 | return ERR_PTR(ret); |
| 6463 | } |
| 6464 | } |
| 6465 | dev->devid = tmp; |
| 6466 | |
| 6467 | if (uuid) |
| 6468 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6469 | else |
| 6470 | generate_random_uuid(dev->uuid); |
| 6471 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6472 | return dev; |
| 6473 | } |
| 6474 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6475 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6476 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6477 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6478 | if (error) |
| 6479 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6480 | devid, uuid); |
| 6481 | else |
| 6482 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6483 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6484 | } |
| 6485 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6486 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 6487 | { |
| 6488 | int index = btrfs_bg_flags_to_raid_index(type); |
| 6489 | int ncopies = btrfs_raid_array[index].ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6490 | const int nparity = btrfs_raid_array[index].nparity; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6491 | int data_stripes; |
| 6492 | |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6493 | if (nparity) |
| 6494 | data_stripes = num_stripes - nparity; |
| 6495 | else |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6496 | data_stripes = num_stripes / ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6497 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6498 | return div_u64(chunk_len, data_stripes); |
| 6499 | } |
| 6500 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6501 | 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] | 6502 | struct btrfs_chunk *chunk) |
| 6503 | { |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6504 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6505 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6506 | struct map_lookup *map; |
| 6507 | struct extent_map *em; |
| 6508 | u64 logical; |
| 6509 | u64 length; |
| 6510 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6511 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6512 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6513 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6514 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6515 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6516 | logical = key->offset; |
| 6517 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6518 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6519 | |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6520 | /* |
| 6521 | * Only need to verify chunk item if we're reading from sys chunk array, |
| 6522 | * as chunk item in tree block is already verified by tree-checker. |
| 6523 | */ |
| 6524 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { |
David Sterba | ddaf1d5 | 2019-03-20 16:40:48 +0100 | [diff] [blame] | 6525 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6526 | if (ret) |
| 6527 | return ret; |
| 6528 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6529 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6530 | read_lock(&map_tree->lock); |
| 6531 | em = lookup_extent_mapping(map_tree, logical, 1); |
| 6532 | read_unlock(&map_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6533 | |
| 6534 | /* already mapped? */ |
| 6535 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6536 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6537 | return 0; |
| 6538 | } else if (em) { |
| 6539 | free_extent_map(em); |
| 6540 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6541 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6542 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6543 | if (!em) |
| 6544 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6545 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6546 | if (!map) { |
| 6547 | free_extent_map(em); |
| 6548 | return -ENOMEM; |
| 6549 | } |
| 6550 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6551 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6552 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6553 | em->start = logical; |
| 6554 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6555 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6556 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6557 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6558 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6559 | map->num_stripes = num_stripes; |
| 6560 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6561 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6562 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6563 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6564 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6565 | map->verified_stripes = 0; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6566 | em->orig_block_len = calc_stripe_length(map->type, em->len, |
| 6567 | map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6568 | for (i = 0; i < num_stripes; i++) { |
| 6569 | map->stripes[i].physical = |
| 6570 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6571 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6572 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6573 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6574 | BTRFS_UUID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6575 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6576 | devid, uuid, NULL, true); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6577 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6578 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6579 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6580 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6581 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6582 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6583 | if (!map->stripes[i].dev) { |
| 6584 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6585 | add_missing_dev(fs_info->fs_devices, devid, |
| 6586 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6587 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6588 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6589 | btrfs_err(fs_info, |
| 6590 | "failed to init missing dev %llu: %ld", |
| 6591 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6592 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6593 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6594 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6595 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6596 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6597 | &(map->stripes[i].dev->dev_state)); |
| 6598 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6599 | } |
| 6600 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6601 | write_lock(&map_tree->lock); |
| 6602 | ret = add_extent_mapping(map_tree, em, 0); |
| 6603 | write_unlock(&map_tree->lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6604 | if (ret < 0) { |
| 6605 | btrfs_err(fs_info, |
| 6606 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6607 | em->start, em->len, ret); |
| 6608 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6609 | free_extent_map(em); |
| 6610 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6611 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6612 | } |
| 6613 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6614 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6615 | struct btrfs_dev_item *dev_item, |
| 6616 | struct btrfs_device *device) |
| 6617 | { |
| 6618 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6619 | |
| 6620 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6621 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6622 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6623 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6624 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6625 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6626 | device->type = btrfs_device_type(leaf, dev_item); |
| 6627 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6628 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6629 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6630 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6631 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6632 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6633 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6634 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6635 | } |
| 6636 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6637 | 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] | 6638 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6639 | { |
| 6640 | struct btrfs_fs_devices *fs_devices; |
| 6641 | int ret; |
| 6642 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6643 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6644 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6645 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6646 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6647 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6648 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6649 | return fs_devices; |
| 6650 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6651 | fs_devices = fs_devices->seed; |
| 6652 | } |
| 6653 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6654 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6655 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6656 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6657 | return ERR_PTR(-ENOENT); |
| 6658 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6659 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6660 | if (IS_ERR(fs_devices)) |
| 6661 | return fs_devices; |
| 6662 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 6663 | fs_devices->seeding = true; |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6664 | fs_devices->opened = 1; |
| 6665 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6666 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6667 | |
| 6668 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6669 | if (IS_ERR(fs_devices)) |
| 6670 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6671 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6672 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6673 | if (ret) { |
| 6674 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6675 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6676 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6677 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6678 | |
| 6679 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6680 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6681 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6682 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6683 | goto out; |
| 6684 | } |
| 6685 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6686 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6687 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6688 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6689 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6690 | } |
| 6691 | |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6692 | static int read_one_dev(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6693 | struct btrfs_dev_item *dev_item) |
| 6694 | { |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6695 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6696 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6697 | struct btrfs_device *device; |
| 6698 | u64 devid; |
| 6699 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6700 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6701 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6702 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6703 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6704 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6705 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6706 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6707 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6708 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6709 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6710 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6711 | if (IS_ERR(fs_devices)) |
| 6712 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6713 | } |
| 6714 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6715 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6716 | fs_uuid, true); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6717 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6718 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6719 | btrfs_report_missing_device(fs_info, devid, |
| 6720 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6721 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6722 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6723 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6724 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6725 | if (IS_ERR(device)) { |
| 6726 | btrfs_err(fs_info, |
| 6727 | "failed to add missing dev %llu: %ld", |
| 6728 | devid, PTR_ERR(device)); |
| 6729 | return PTR_ERR(device); |
| 6730 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6731 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6732 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6733 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6734 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6735 | btrfs_report_missing_device(fs_info, |
| 6736 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6737 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6738 | } |
| 6739 | btrfs_report_missing_device(fs_info, devid, |
| 6740 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6741 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6742 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6743 | if (!device->bdev && |
| 6744 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6745 | /* |
| 6746 | * this happens when a device that was properly setup |
| 6747 | * in the device info lists suddenly goes bad. |
| 6748 | * device->bdev is NULL, and so we have to set |
| 6749 | * device->missing to one here |
| 6750 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6751 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6752 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6753 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6754 | |
| 6755 | /* Move the device to its own fs_devices */ |
| 6756 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6757 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6758 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6759 | |
| 6760 | list_move(&device->dev_list, &fs_devices->devices); |
| 6761 | device->fs_devices->num_devices--; |
| 6762 | fs_devices->num_devices++; |
| 6763 | |
| 6764 | device->fs_devices->missing_devices--; |
| 6765 | fs_devices->missing_devices++; |
| 6766 | |
| 6767 | device->fs_devices = fs_devices; |
| 6768 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6769 | } |
| 6770 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6771 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6772 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6773 | if (device->generation != |
| 6774 | btrfs_device_generation(leaf, dev_item)) |
| 6775 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6776 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6777 | |
| 6778 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6779 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6780 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6781 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6782 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6783 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6784 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6785 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6786 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6787 | return ret; |
| 6788 | } |
| 6789 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6790 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6791 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6792 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6793 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6794 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6795 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6796 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6797 | u8 *array_ptr; |
| 6798 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6799 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6800 | u32 num_stripes; |
| 6801 | u32 array_size; |
| 6802 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6803 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6804 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6805 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6806 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6807 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6808 | /* |
| 6809 | * This will create extent buffer of nodesize, superblock size is |
| 6810 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6811 | * overallocate but we can keep it as-is, only the first page is used. |
| 6812 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6813 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6814 | if (IS_ERR(sb)) |
| 6815 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6816 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6817 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6818 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6819 | * 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] | 6820 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6821 | * pages up-to-date when the page is larger: extent does not cover the |
| 6822 | * whole page and consequently check_page_uptodate does not find all |
| 6823 | * the page's extents up-to-date (the hole beyond sb), |
| 6824 | * write_extent_buffer then triggers a WARN_ON. |
| 6825 | * |
| 6826 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6827 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6828 | * to silence the warning eg. on PowerPC 64. |
| 6829 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6830 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6831 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6832 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6833 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6834 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6835 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6836 | array_ptr = super_copy->sys_chunk_array; |
| 6837 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6838 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6839 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6840 | while (cur_offset < array_size) { |
| 6841 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6842 | len = sizeof(*disk_key); |
| 6843 | if (cur_offset + len > array_size) |
| 6844 | goto out_short_read; |
| 6845 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6846 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6847 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6848 | array_ptr += len; |
| 6849 | sb_array_offset += len; |
| 6850 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6851 | |
Johannes Thumshirn | 32ab3d1 | 2019-10-18 11:58:23 +0200 | [diff] [blame] | 6852 | if (key.type != BTRFS_CHUNK_ITEM_KEY) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6853 | btrfs_err(fs_info, |
| 6854 | "unexpected item type %u in sys_array at offset %u", |
| 6855 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6856 | ret = -EIO; |
| 6857 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6858 | } |
Johannes Thumshirn | 32ab3d1 | 2019-10-18 11:58:23 +0200 | [diff] [blame] | 6859 | |
| 6860 | chunk = (struct btrfs_chunk *)sb_array_offset; |
| 6861 | /* |
| 6862 | * At least one btrfs_chunk with one stripe must be present, |
| 6863 | * exact stripe count check comes afterwards |
| 6864 | */ |
| 6865 | len = btrfs_chunk_item_size(1); |
| 6866 | if (cur_offset + len > array_size) |
| 6867 | goto out_short_read; |
| 6868 | |
| 6869 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6870 | if (!num_stripes) { |
| 6871 | btrfs_err(fs_info, |
| 6872 | "invalid number of stripes %u in sys_array at offset %u", |
| 6873 | num_stripes, cur_offset); |
| 6874 | ret = -EIO; |
| 6875 | break; |
| 6876 | } |
| 6877 | |
| 6878 | type = btrfs_chunk_type(sb, chunk); |
| 6879 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
| 6880 | btrfs_err(fs_info, |
| 6881 | "invalid chunk type %llu in sys_array at offset %u", |
| 6882 | type, cur_offset); |
| 6883 | ret = -EIO; |
| 6884 | break; |
| 6885 | } |
| 6886 | |
| 6887 | len = btrfs_chunk_item_size(num_stripes); |
| 6888 | if (cur_offset + len > array_size) |
| 6889 | goto out_short_read; |
| 6890 | |
| 6891 | ret = read_one_chunk(&key, sb, chunk); |
| 6892 | if (ret) |
| 6893 | break; |
| 6894 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6895 | array_ptr += len; |
| 6896 | sb_array_offset += len; |
| 6897 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6898 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6899 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6900 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6901 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6902 | |
| 6903 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6904 | 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] | 6905 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6906 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6907 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6908 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6909 | } |
| 6910 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6911 | /* |
| 6912 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 6913 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6914 | * If the @failing_dev is specified, it's accounted as missing. |
| 6915 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6916 | * Return true if all chunks meet the minimal RW mount requirements. |
| 6917 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 6918 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6919 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 6920 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6921 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6922 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6923 | struct extent_map *em; |
| 6924 | u64 next_start = 0; |
| 6925 | bool ret = true; |
| 6926 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6927 | read_lock(&map_tree->lock); |
| 6928 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); |
| 6929 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6930 | /* No chunk at all? Return false anyway */ |
| 6931 | if (!em) { |
| 6932 | ret = false; |
| 6933 | goto out; |
| 6934 | } |
| 6935 | while (em) { |
| 6936 | struct map_lookup *map; |
| 6937 | int missing = 0; |
| 6938 | int max_tolerated; |
| 6939 | int i; |
| 6940 | |
| 6941 | map = em->map_lookup; |
| 6942 | max_tolerated = |
| 6943 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 6944 | map->type); |
| 6945 | for (i = 0; i < map->num_stripes; i++) { |
| 6946 | struct btrfs_device *dev = map->stripes[i].dev; |
| 6947 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6948 | if (!dev || !dev->bdev || |
| 6949 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6950 | dev->last_flush_error) |
| 6951 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6952 | else if (failing_dev && failing_dev == dev) |
| 6953 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6954 | } |
| 6955 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6956 | if (!failing_dev) |
| 6957 | btrfs_warn(fs_info, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 6958 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6959 | em->start, missing, max_tolerated); |
| 6960 | free_extent_map(em); |
| 6961 | ret = false; |
| 6962 | goto out; |
| 6963 | } |
| 6964 | next_start = extent_map_end(em); |
| 6965 | free_extent_map(em); |
| 6966 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6967 | read_lock(&map_tree->lock); |
| 6968 | em = lookup_extent_mapping(map_tree, next_start, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6969 | (u64)(-1) - next_start); |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6970 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6971 | } |
| 6972 | out: |
| 6973 | return ret; |
| 6974 | } |
| 6975 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6976 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6977 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6978 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6979 | struct btrfs_path *path; |
| 6980 | struct extent_buffer *leaf; |
| 6981 | struct btrfs_key key; |
| 6982 | struct btrfs_key found_key; |
| 6983 | int ret; |
| 6984 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6985 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6986 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6987 | path = btrfs_alloc_path(); |
| 6988 | if (!path) |
| 6989 | return -ENOMEM; |
| 6990 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 6991 | /* |
| 6992 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 6993 | * otherwise we don't need it. |
| 6994 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6995 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 6996 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6997 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6998 | /* |
| 6999 | * Read all device items, and then all the chunk items. All |
| 7000 | * device items are found before any chunk item (their object id |
| 7001 | * is smaller than the lowest possible object id for a chunk |
| 7002 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7003 | */ |
| 7004 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7005 | key.offset = 0; |
| 7006 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7007 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7008 | if (ret < 0) |
| 7009 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7010 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7011 | leaf = path->nodes[0]; |
| 7012 | slot = path->slots[0]; |
| 7013 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7014 | ret = btrfs_next_leaf(root, path); |
| 7015 | if (ret == 0) |
| 7016 | continue; |
| 7017 | if (ret < 0) |
| 7018 | goto error; |
| 7019 | break; |
| 7020 | } |
| 7021 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7022 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7023 | struct btrfs_dev_item *dev_item; |
| 7024 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7025 | struct btrfs_dev_item); |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 7026 | ret = read_one_dev(leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7027 | if (ret) |
| 7028 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7029 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7030 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7031 | struct btrfs_chunk *chunk; |
| 7032 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7033 | ret = read_one_chunk(&found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7034 | if (ret) |
| 7035 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7036 | } |
| 7037 | path->slots[0]++; |
| 7038 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7039 | |
| 7040 | /* |
| 7041 | * After loading chunk tree, we've got all device information, |
| 7042 | * do another round of validation checks. |
| 7043 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7044 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7045 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7046 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7047 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7048 | total_dev); |
| 7049 | ret = -EINVAL; |
| 7050 | goto error; |
| 7051 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7052 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7053 | fs_info->fs_devices->total_rw_bytes) { |
| 7054 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7055 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7056 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7057 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7058 | ret = -EINVAL; |
| 7059 | goto error; |
| 7060 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7061 | ret = 0; |
| 7062 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7063 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7064 | mutex_unlock(&uuid_mutex); |
| 7065 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7066 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7067 | return ret; |
| 7068 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7069 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7070 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7071 | { |
| 7072 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7073 | struct btrfs_device *device; |
| 7074 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7075 | while (fs_devices) { |
| 7076 | mutex_lock(&fs_devices->device_list_mutex); |
| 7077 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7078 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7079 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7080 | |
| 7081 | fs_devices = fs_devices->seed; |
| 7082 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
David Sterba | 1dc990d | 2019-08-21 20:05:32 +0200 | [diff] [blame] | 7085 | static u64 btrfs_dev_stats_value(const struct extent_buffer *eb, |
| 7086 | const struct btrfs_dev_stats_item *ptr, |
| 7087 | int index) |
| 7088 | { |
| 7089 | u64 val; |
| 7090 | |
| 7091 | read_extent_buffer(eb, &val, |
| 7092 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7093 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7094 | sizeof(val)); |
| 7095 | return val; |
| 7096 | } |
| 7097 | |
| 7098 | static void btrfs_set_dev_stats_value(struct extent_buffer *eb, |
| 7099 | struct btrfs_dev_stats_item *ptr, |
| 7100 | int index, u64 val) |
| 7101 | { |
| 7102 | write_extent_buffer(eb, &val, |
| 7103 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7104 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7105 | sizeof(val)); |
| 7106 | } |
| 7107 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7108 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7109 | { |
| 7110 | struct btrfs_key key; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7111 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7112 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7113 | struct extent_buffer *eb; |
| 7114 | int slot; |
| 7115 | int ret = 0; |
| 7116 | struct btrfs_device *device; |
| 7117 | struct btrfs_path *path = NULL; |
| 7118 | int i; |
| 7119 | |
| 7120 | path = btrfs_alloc_path(); |
Anand Jain | 3b80a984 | 2019-08-21 17:26:32 +0800 | [diff] [blame] | 7121 | if (!path) |
| 7122 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7123 | |
| 7124 | mutex_lock(&fs_devices->device_list_mutex); |
| 7125 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7126 | int item_size; |
| 7127 | struct btrfs_dev_stats_item *ptr; |
| 7128 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7129 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7130 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7131 | key.offset = device->devid; |
| 7132 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7133 | if (ret) { |
Anand Jain | ae4b9b4 | 2019-08-07 16:21:20 +0800 | [diff] [blame] | 7134 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7135 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7136 | device->dev_stats_valid = 1; |
| 7137 | btrfs_release_path(path); |
| 7138 | continue; |
| 7139 | } |
| 7140 | slot = path->slots[0]; |
| 7141 | eb = path->nodes[0]; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7142 | item_size = btrfs_item_size_nr(eb, slot); |
| 7143 | |
| 7144 | ptr = btrfs_item_ptr(eb, slot, |
| 7145 | struct btrfs_dev_stats_item); |
| 7146 | |
| 7147 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7148 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7149 | btrfs_dev_stat_set(device, i, |
| 7150 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7151 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7152 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7153 | } |
| 7154 | |
| 7155 | device->dev_stats_valid = 1; |
| 7156 | btrfs_dev_stat_print_on_load(device); |
| 7157 | btrfs_release_path(path); |
| 7158 | } |
| 7159 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7160 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7161 | btrfs_free_path(path); |
| 7162 | return ret < 0 ? ret : 0; |
| 7163 | } |
| 7164 | |
| 7165 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7166 | struct btrfs_device *device) |
| 7167 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7168 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7169 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7170 | struct btrfs_path *path; |
| 7171 | struct btrfs_key key; |
| 7172 | struct extent_buffer *eb; |
| 7173 | struct btrfs_dev_stats_item *ptr; |
| 7174 | int ret; |
| 7175 | int i; |
| 7176 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7177 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7178 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7179 | key.offset = device->devid; |
| 7180 | |
| 7181 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7182 | if (!path) |
| 7183 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7184 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7185 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7186 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7187 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7188 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7189 | goto out; |
| 7190 | } |
| 7191 | |
| 7192 | if (ret == 0 && |
| 7193 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7194 | /* need to delete old one and insert a new one */ |
| 7195 | ret = btrfs_del_item(trans, dev_root, path); |
| 7196 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7197 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7198 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7199 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7200 | goto out; |
| 7201 | } |
| 7202 | ret = 1; |
| 7203 | } |
| 7204 | |
| 7205 | if (ret == 1) { |
| 7206 | /* need to insert a new item */ |
| 7207 | btrfs_release_path(path); |
| 7208 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7209 | &key, sizeof(*ptr)); |
| 7210 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7211 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7212 | "insert dev_stats item for device %s failed %d", |
| 7213 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7214 | goto out; |
| 7215 | } |
| 7216 | } |
| 7217 | |
| 7218 | eb = path->nodes[0]; |
| 7219 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7220 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7221 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7222 | btrfs_dev_stat_read(device, i)); |
| 7223 | btrfs_mark_buffer_dirty(eb); |
| 7224 | |
| 7225 | out: |
| 7226 | btrfs_free_path(path); |
| 7227 | return ret; |
| 7228 | } |
| 7229 | |
| 7230 | /* |
| 7231 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7232 | */ |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7233 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7234 | { |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7235 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7236 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7237 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7238 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7239 | int ret = 0; |
| 7240 | |
| 7241 | mutex_lock(&fs_devices->device_list_mutex); |
| 7242 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7243 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7244 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7245 | continue; |
| 7246 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7247 | |
| 7248 | /* |
| 7249 | * There is a LOAD-LOAD control dependency between the value of |
| 7250 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7251 | * reading the in-memory counters. Such control dependencies |
| 7252 | * require explicit read memory barriers. |
| 7253 | * |
| 7254 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7255 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7256 | * barrier implied by atomic_xchg in |
| 7257 | * btrfs_dev_stats_read_and_reset |
| 7258 | */ |
| 7259 | smp_rmb(); |
| 7260 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7261 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7262 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7263 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7264 | } |
| 7265 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7266 | |
| 7267 | return ret; |
| 7268 | } |
| 7269 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7270 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7271 | { |
| 7272 | btrfs_dev_stat_inc(dev, index); |
| 7273 | btrfs_dev_stat_print_on_error(dev); |
| 7274 | } |
| 7275 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7276 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7277 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7278 | if (!dev->dev_stats_valid) |
| 7279 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7280 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7281 | "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] | 7282 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7283 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7284 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7285 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7286 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7287 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7288 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7289 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7290 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7291 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7292 | int i; |
| 7293 | |
| 7294 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7295 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7296 | break; |
| 7297 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7298 | return; /* all values == 0, suppress message */ |
| 7299 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7300 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7301 | "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] | 7302 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7303 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7304 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7305 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7306 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7307 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7308 | } |
| 7309 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7310 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7311 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7312 | { |
| 7313 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7314 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7315 | int i; |
| 7316 | |
| 7317 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7318 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
| 7319 | true); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7320 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7321 | |
| 7322 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7323 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7324 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7325 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7326 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7327 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7328 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7329 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7330 | if (stats->nr_items > i) |
| 7331 | stats->values[i] = |
| 7332 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7333 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7334 | btrfs_dev_stat_set(dev, i, 0); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7335 | } |
Anand Jain | a69976b | 2020-01-10 12:26:34 +0800 | [diff] [blame] | 7336 | btrfs_info(fs_info, "device stats zeroed by %s (%d)", |
| 7337 | current->comm, task_pid_nr(current)); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7338 | } else { |
| 7339 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7340 | if (stats->nr_items > i) |
| 7341 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7342 | } |
| 7343 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7344 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7345 | return 0; |
| 7346 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7347 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7348 | /* |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7349 | * Update the size and bytes used for each device where it changed. This is |
| 7350 | * delayed since we would otherwise get errors while writing out the |
| 7351 | * superblocks. |
| 7352 | * |
| 7353 | * Must be invoked during transaction commit. |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7354 | */ |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7355 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7356 | { |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7357 | struct btrfs_device *curr, *next; |
| 7358 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7359 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
| 7360 | |
| 7361 | if (list_empty(&trans->dev_update_list)) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7362 | return; |
| 7363 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7364 | /* |
| 7365 | * We don't need the device_list_mutex here. This list is owned by the |
| 7366 | * transaction and the transaction must complete before the device is |
| 7367 | * released. |
| 7368 | */ |
| 7369 | mutex_lock(&trans->fs_info->chunk_mutex); |
| 7370 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, |
| 7371 | post_commit_list) { |
| 7372 | list_del_init(&curr->post_commit_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7373 | curr->commit_total_bytes = curr->disk_total_bytes; |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7374 | curr->commit_bytes_used = curr->bytes_used; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7375 | } |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7376 | mutex_unlock(&trans->fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7377 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7378 | |
| 7379 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7380 | { |
| 7381 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7382 | while (fs_devices) { |
| 7383 | fs_devices->fs_info = fs_info; |
| 7384 | fs_devices = fs_devices->seed; |
| 7385 | } |
| 7386 | } |
| 7387 | |
| 7388 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7389 | { |
| 7390 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7391 | while (fs_devices) { |
| 7392 | fs_devices->fs_info = NULL; |
| 7393 | fs_devices = fs_devices->seed; |
| 7394 | } |
| 7395 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7396 | |
| 7397 | /* |
| 7398 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7399 | */ |
| 7400 | int btrfs_bg_type_to_factor(u64 flags) |
| 7401 | { |
David Sterba | 44b28ad | 2019-05-17 11:43:31 +0200 | [diff] [blame] | 7402 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 7403 | |
| 7404 | return btrfs_raid_array[index].ncopies; |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7405 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7406 | |
| 7407 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7408 | |
| 7409 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7410 | u64 chunk_offset, u64 devid, |
| 7411 | u64 physical_offset, u64 physical_len) |
| 7412 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7413 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7414 | struct extent_map *em; |
| 7415 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7416 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7417 | u64 stripe_len; |
| 7418 | bool found = false; |
| 7419 | int ret = 0; |
| 7420 | int i; |
| 7421 | |
| 7422 | read_lock(&em_tree->lock); |
| 7423 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7424 | read_unlock(&em_tree->lock); |
| 7425 | |
| 7426 | if (!em) { |
| 7427 | btrfs_err(fs_info, |
| 7428 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7429 | physical_offset, devid); |
| 7430 | ret = -EUCLEAN; |
| 7431 | goto out; |
| 7432 | } |
| 7433 | |
| 7434 | map = em->map_lookup; |
| 7435 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7436 | if (physical_len != stripe_len) { |
| 7437 | btrfs_err(fs_info, |
| 7438 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7439 | physical_offset, devid, em->start, physical_len, |
| 7440 | stripe_len); |
| 7441 | ret = -EUCLEAN; |
| 7442 | goto out; |
| 7443 | } |
| 7444 | |
| 7445 | for (i = 0; i < map->num_stripes; i++) { |
| 7446 | if (map->stripes[i].dev->devid == devid && |
| 7447 | map->stripes[i].physical == physical_offset) { |
| 7448 | found = true; |
| 7449 | if (map->verified_stripes >= map->num_stripes) { |
| 7450 | btrfs_err(fs_info, |
| 7451 | "too many dev extents for chunk %llu found", |
| 7452 | em->start); |
| 7453 | ret = -EUCLEAN; |
| 7454 | goto out; |
| 7455 | } |
| 7456 | map->verified_stripes++; |
| 7457 | break; |
| 7458 | } |
| 7459 | } |
| 7460 | if (!found) { |
| 7461 | btrfs_err(fs_info, |
| 7462 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7463 | physical_offset, devid); |
| 7464 | ret = -EUCLEAN; |
| 7465 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7466 | |
| 7467 | /* Make sure no dev extent is beyond device bondary */ |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7468 | dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7469 | if (!dev) { |
| 7470 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7471 | ret = -EUCLEAN; |
| 7472 | goto out; |
| 7473 | } |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7474 | |
| 7475 | /* It's possible this device is a dummy for seed device */ |
| 7476 | if (dev->disk_total_bytes == 0) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7477 | dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL, |
| 7478 | NULL, false); |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7479 | if (!dev) { |
| 7480 | btrfs_err(fs_info, "failed to find seed devid %llu", |
| 7481 | devid); |
| 7482 | ret = -EUCLEAN; |
| 7483 | goto out; |
| 7484 | } |
| 7485 | } |
| 7486 | |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7487 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7488 | btrfs_err(fs_info, |
| 7489 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7490 | devid, physical_offset, physical_len, |
| 7491 | dev->disk_total_bytes); |
| 7492 | ret = -EUCLEAN; |
| 7493 | goto out; |
| 7494 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7495 | out: |
| 7496 | free_extent_map(em); |
| 7497 | return ret; |
| 7498 | } |
| 7499 | |
| 7500 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7501 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7502 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7503 | struct extent_map *em; |
| 7504 | struct rb_node *node; |
| 7505 | int ret = 0; |
| 7506 | |
| 7507 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7508 | 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] | 7509 | em = rb_entry(node, struct extent_map, rb_node); |
| 7510 | if (em->map_lookup->num_stripes != |
| 7511 | em->map_lookup->verified_stripes) { |
| 7512 | btrfs_err(fs_info, |
| 7513 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7514 | em->start, em->map_lookup->verified_stripes, |
| 7515 | em->map_lookup->num_stripes); |
| 7516 | ret = -EUCLEAN; |
| 7517 | goto out; |
| 7518 | } |
| 7519 | } |
| 7520 | out: |
| 7521 | read_unlock(&em_tree->lock); |
| 7522 | return ret; |
| 7523 | } |
| 7524 | |
| 7525 | /* |
| 7526 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7527 | * later chunk allocation/free would cause unexpected behavior. |
| 7528 | * |
| 7529 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7530 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7531 | */ |
| 7532 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7533 | { |
| 7534 | struct btrfs_path *path; |
| 7535 | struct btrfs_root *root = fs_info->dev_root; |
| 7536 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7537 | u64 prev_devid = 0; |
| 7538 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7539 | int ret = 0; |
| 7540 | |
| 7541 | key.objectid = 1; |
| 7542 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7543 | key.offset = 0; |
| 7544 | |
| 7545 | path = btrfs_alloc_path(); |
| 7546 | if (!path) |
| 7547 | return -ENOMEM; |
| 7548 | |
| 7549 | path->reada = READA_FORWARD; |
| 7550 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7551 | if (ret < 0) |
| 7552 | goto out; |
| 7553 | |
| 7554 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7555 | ret = btrfs_next_item(root, path); |
| 7556 | if (ret < 0) |
| 7557 | goto out; |
| 7558 | /* No dev extents at all? Not good */ |
| 7559 | if (ret > 0) { |
| 7560 | ret = -EUCLEAN; |
| 7561 | goto out; |
| 7562 | } |
| 7563 | } |
| 7564 | while (1) { |
| 7565 | struct extent_buffer *leaf = path->nodes[0]; |
| 7566 | struct btrfs_dev_extent *dext; |
| 7567 | int slot = path->slots[0]; |
| 7568 | u64 chunk_offset; |
| 7569 | u64 physical_offset; |
| 7570 | u64 physical_len; |
| 7571 | u64 devid; |
| 7572 | |
| 7573 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7574 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7575 | break; |
| 7576 | devid = key.objectid; |
| 7577 | physical_offset = key.offset; |
| 7578 | |
| 7579 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7580 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7581 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7582 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7583 | /* Check if this dev extent overlaps with the previous one */ |
| 7584 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7585 | btrfs_err(fs_info, |
| 7586 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7587 | devid, physical_offset, prev_dev_ext_end); |
| 7588 | ret = -EUCLEAN; |
| 7589 | goto out; |
| 7590 | } |
| 7591 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7592 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7593 | physical_offset, physical_len); |
| 7594 | if (ret < 0) |
| 7595 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7596 | prev_devid = devid; |
| 7597 | prev_dev_ext_end = physical_offset + physical_len; |
| 7598 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7599 | ret = btrfs_next_item(root, path); |
| 7600 | if (ret < 0) |
| 7601 | goto out; |
| 7602 | if (ret > 0) { |
| 7603 | ret = 0; |
| 7604 | break; |
| 7605 | } |
| 7606 | } |
| 7607 | |
| 7608 | /* Ensure all chunks have corresponding dev extents */ |
| 7609 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7610 | out: |
| 7611 | btrfs_free_path(path); |
| 7612 | return ret; |
| 7613 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7614 | |
| 7615 | /* |
| 7616 | * Check whether the given block group or device is pinned by any inode being |
| 7617 | * used as a swapfile. |
| 7618 | */ |
| 7619 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7620 | { |
| 7621 | struct btrfs_swapfile_pin *sp; |
| 7622 | struct rb_node *node; |
| 7623 | |
| 7624 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7625 | node = fs_info->swapfile_pins.rb_node; |
| 7626 | while (node) { |
| 7627 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7628 | if (ptr < sp->ptr) |
| 7629 | node = node->rb_left; |
| 7630 | else if (ptr > sp->ptr) |
| 7631 | node = node->rb_right; |
| 7632 | else |
| 7633 | break; |
| 7634 | } |
| 7635 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7636 | return node != NULL; |
| 7637 | } |