David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007 Oracle. All rights reserved. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
| 7 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 9 | #include <linux/buffer_head.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 10 | #include <linux/blkdev.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 11 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 12 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 13 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 14 | #include <linux/semaphore.h> |
Andy Shevchenko | 8da4b8c | 2016-05-20 17:01:00 -0700 | [diff] [blame] | 15 | #include <linux/uuid.h> |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 16 | #include <linux/list_sort.h> |
David Sterba | 784352f | 2019-08-21 18:54:28 +0200 | [diff] [blame] | 17 | #include "misc.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 18 | #include "ctree.h" |
| 19 | #include "extent_map.h" |
| 20 | #include "disk-io.h" |
| 21 | #include "transaction.h" |
| 22 | #include "print-tree.h" |
| 23 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 24 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 25 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 26 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 27 | #include "rcu-string.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 28 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 29 | #include "sysfs.h" |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 30 | #include "tree-checker.h" |
Josef Bacik | 8719aaa | 2019-06-18 16:09:16 -0400 | [diff] [blame] | 31 | #include "space-info.h" |
Josef Bacik | aac0023 | 2019-06-20 15:37:44 -0400 | [diff] [blame] | 32 | #include "block-group.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 33 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 34 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 35 | [BTRFS_RAID_RAID10] = { |
| 36 | .sub_stripes = 2, |
| 37 | .dev_stripes = 1, |
| 38 | .devs_max = 0, /* 0 == as many as possible */ |
| 39 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 40 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 41 | .devs_increment = 2, |
| 42 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 43 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 44 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 45 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 46 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 47 | }, |
| 48 | [BTRFS_RAID_RAID1] = { |
| 49 | .sub_stripes = 1, |
| 50 | .dev_stripes = 1, |
| 51 | .devs_max = 2, |
| 52 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 53 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 54 | .devs_increment = 2, |
| 55 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 56 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 57 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 58 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 59 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 60 | }, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 61 | [BTRFS_RAID_RAID1C3] = { |
| 62 | .sub_stripes = 1, |
| 63 | .dev_stripes = 1, |
David Sterba | cf93e15 | 2019-11-27 16:10:54 +0100 | [diff] [blame] | 64 | .devs_max = 3, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 65 | .devs_min = 3, |
| 66 | .tolerated_failures = 2, |
| 67 | .devs_increment = 3, |
| 68 | .ncopies = 3, |
David Sterba | db26a02 | 2019-11-25 15:34:48 +0100 | [diff] [blame] | 69 | .nparity = 0, |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 70 | .raid_name = "raid1c3", |
| 71 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3, |
| 72 | .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET, |
| 73 | }, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 74 | [BTRFS_RAID_RAID1C4] = { |
| 75 | .sub_stripes = 1, |
| 76 | .dev_stripes = 1, |
David Sterba | cf93e15 | 2019-11-27 16:10:54 +0100 | [diff] [blame] | 77 | .devs_max = 4, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 78 | .devs_min = 4, |
| 79 | .tolerated_failures = 3, |
| 80 | .devs_increment = 4, |
| 81 | .ncopies = 4, |
David Sterba | db26a02 | 2019-11-25 15:34:48 +0100 | [diff] [blame] | 82 | .nparity = 0, |
David Sterba | 8d6fac0 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 83 | .raid_name = "raid1c4", |
| 84 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4, |
| 85 | .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET, |
| 86 | }, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 87 | [BTRFS_RAID_DUP] = { |
| 88 | .sub_stripes = 1, |
| 89 | .dev_stripes = 2, |
| 90 | .devs_max = 1, |
| 91 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 92 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 93 | .devs_increment = 1, |
| 94 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 95 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 96 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 97 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 98 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 99 | }, |
| 100 | [BTRFS_RAID_RAID0] = { |
| 101 | .sub_stripes = 1, |
| 102 | .dev_stripes = 1, |
| 103 | .devs_max = 0, |
| 104 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 105 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 106 | .devs_increment = 1, |
| 107 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 108 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 109 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 110 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 111 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 112 | }, |
| 113 | [BTRFS_RAID_SINGLE] = { |
| 114 | .sub_stripes = 1, |
| 115 | .dev_stripes = 1, |
| 116 | .devs_max = 1, |
| 117 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 118 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 119 | .devs_increment = 1, |
| 120 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 121 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 122 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 123 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 124 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 125 | }, |
| 126 | [BTRFS_RAID_RAID5] = { |
| 127 | .sub_stripes = 1, |
| 128 | .dev_stripes = 1, |
| 129 | .devs_max = 0, |
| 130 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 131 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 132 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 133 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 134 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 135 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 136 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 137 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 138 | }, |
| 139 | [BTRFS_RAID_RAID6] = { |
| 140 | .sub_stripes = 1, |
| 141 | .dev_stripes = 1, |
| 142 | .devs_max = 0, |
| 143 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 144 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 145 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 146 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 147 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 148 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 149 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 150 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 151 | }, |
| 152 | }; |
| 153 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 154 | const char *btrfs_bg_type_to_raid_name(u64 flags) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 155 | { |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 156 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 157 | |
| 158 | if (index >= BTRFS_NR_RAID_TYPES) |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 159 | return NULL; |
| 160 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 161 | return btrfs_raid_array[index].raid_name; |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Anand Jain | f89e09c | 2018-11-20 16:12:55 +0800 | [diff] [blame] | 164 | /* |
| 165 | * Fill @buf with textual description of @bg_flags, no more than @size_buf |
| 166 | * bytes including terminating null byte. |
| 167 | */ |
| 168 | void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf) |
| 169 | { |
| 170 | int i; |
| 171 | int ret; |
| 172 | char *bp = buf; |
| 173 | u64 flags = bg_flags; |
| 174 | u32 size_bp = size_buf; |
| 175 | |
| 176 | if (!flags) { |
| 177 | strcpy(bp, "NONE"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | #define DESCRIBE_FLAG(flag, desc) \ |
| 182 | do { \ |
| 183 | if (flags & (flag)) { \ |
| 184 | ret = snprintf(bp, size_bp, "%s|", (desc)); \ |
| 185 | if (ret < 0 || ret >= size_bp) \ |
| 186 | goto out_overflow; \ |
| 187 | size_bp -= ret; \ |
| 188 | bp += ret; \ |
| 189 | flags &= ~(flag); \ |
| 190 | } \ |
| 191 | } while (0) |
| 192 | |
| 193 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data"); |
| 194 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system"); |
| 195 | DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata"); |
| 196 | |
| 197 | DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single"); |
| 198 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 199 | DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag, |
| 200 | btrfs_raid_array[i].raid_name); |
| 201 | #undef DESCRIBE_FLAG |
| 202 | |
| 203 | if (flags) { |
| 204 | ret = snprintf(bp, size_bp, "0x%llx|", flags); |
| 205 | size_bp -= ret; |
| 206 | } |
| 207 | |
| 208 | if (size_bp < size_buf) |
| 209 | buf[size_buf - size_bp - 1] = '\0'; /* remove last | */ |
| 210 | |
| 211 | /* |
| 212 | * The text is trimmed, it's up to the caller to provide sufficiently |
| 213 | * large buffer |
| 214 | */ |
| 215 | out_overflow:; |
| 216 | } |
| 217 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 218 | static int init_first_rw_device(struct btrfs_trans_handle *trans); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 219 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 220 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 221 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 222 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 223 | enum btrfs_map_op op, |
| 224 | u64 logical, u64 *length, |
| 225 | struct btrfs_bio **bbio_ret, |
| 226 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 227 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 228 | /* |
| 229 | * Device locking |
| 230 | * ============== |
| 231 | * |
| 232 | * There are several mutexes that protect manipulation of devices and low-level |
| 233 | * structures like chunks but not block groups, extents or files |
| 234 | * |
| 235 | * uuid_mutex (global lock) |
| 236 | * ------------------------ |
| 237 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 238 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 239 | * device) or requested by the device= mount option |
| 240 | * |
| 241 | * the mutex can be very coarse and can cover long-running operations |
| 242 | * |
| 243 | * protects: updates to fs_devices counters like missing devices, rw devices, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 244 | * seeding, structure cloning, opening/closing devices at mount/umount time |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 245 | * |
| 246 | * global::fs_devs - add, remove, updates to the global list |
| 247 | * |
| 248 | * does not protect: manipulation of the fs_devices::devices list! |
| 249 | * |
| 250 | * btrfs_device::name - renames (write side), read is RCU |
| 251 | * |
| 252 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 253 | * ------------------------------------------------ |
| 254 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 255 | * |
| 256 | * simple list traversal with read-only actions can be done with RCU protection |
| 257 | * |
| 258 | * may be used to exclude some operations from running concurrently without any |
| 259 | * modifications to the list (see write_all_supers) |
| 260 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 261 | * balance_mutex |
| 262 | * ------------- |
| 263 | * protects balance structures (status, state) and context accessed from |
| 264 | * several places (internally, ioctl) |
| 265 | * |
| 266 | * chunk_mutex |
| 267 | * ----------- |
| 268 | * protects chunks, adding or removing during allocation, trim or when a new |
Nikolay Borisov | 0b6f5d4 | 2019-05-09 18:11:11 +0300 | [diff] [blame] | 269 | * device is added/removed. Additionally it also protects post_commit_list of |
| 270 | * individual devices, since they can be added to the transaction's |
| 271 | * post_commit_list only with chunk_mutex held. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 272 | * |
| 273 | * cleaner_mutex |
| 274 | * ------------- |
| 275 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 276 | * cleaning together with relocation or delayed iputs |
| 277 | * |
| 278 | * |
| 279 | * Lock nesting |
| 280 | * ============ |
| 281 | * |
| 282 | * uuid_mutex |
| 283 | * volume_mutex |
| 284 | * device_list_mutex |
| 285 | * chunk_mutex |
| 286 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 287 | * |
| 288 | * |
| 289 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 290 | * ====================================== |
| 291 | * |
| 292 | * Maintains the exclusivity of the following operations that apply to the |
| 293 | * whole filesystem and cannot run in parallel. |
| 294 | * |
| 295 | * - Balance (*) |
| 296 | * - Device add |
| 297 | * - Device remove |
| 298 | * - Device replace (*) |
| 299 | * - Resize |
| 300 | * |
| 301 | * The device operations (as above) can be in one of the following states: |
| 302 | * |
| 303 | * - Running state |
| 304 | * - Paused state |
| 305 | * - Completed state |
| 306 | * |
| 307 | * Only device operations marked with (*) can go into the Paused state for the |
| 308 | * following reasons: |
| 309 | * |
| 310 | * - ioctl (only Balance can be Paused through ioctl) |
| 311 | * - filesystem remounted as read-only |
| 312 | * - filesystem unmounted and mounted as read-only |
| 313 | * - system power-cycle and filesystem mounted as read-only |
| 314 | * - filesystem or device errors leading to forced read-only |
| 315 | * |
| 316 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 317 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 318 | * A device operation in Paused or Running state can be canceled or resumed |
| 319 | * either by ioctl (Balance only) or when remounted as read-write. |
| 320 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 321 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 322 | */ |
| 323 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 324 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 325 | static LIST_HEAD(fs_uuids); |
David Sterba | 4143cb8 | 2019-10-01 19:57:37 +0200 | [diff] [blame] | 326 | struct list_head * __attribute_const__ btrfs_get_fs_uuids(void) |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 327 | { |
| 328 | return &fs_uuids; |
| 329 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 330 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 331 | /* |
| 332 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 333 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 334 | * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 335 | * |
| 336 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 337 | * The returned struct is not linked onto any lists and can be destroyed with |
| 338 | * kfree() right away. |
| 339 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 340 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 341 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 342 | { |
| 343 | struct btrfs_fs_devices *fs_devs; |
| 344 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 345 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 346 | if (!fs_devs) |
| 347 | return ERR_PTR(-ENOMEM); |
| 348 | |
| 349 | mutex_init(&fs_devs->device_list_mutex); |
| 350 | |
| 351 | INIT_LIST_HEAD(&fs_devs->devices); |
| 352 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 353 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 354 | if (fsid) |
| 355 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 356 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 357 | if (metadata_fsid) |
| 358 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 359 | else if (fsid) |
| 360 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 361 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 362 | return fs_devs; |
| 363 | } |
| 364 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 365 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 366 | { |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 367 | WARN_ON(!list_empty(&device->post_commit_list)); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 368 | rcu_string_free(device->name); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 369 | extent_io_tree_release(&device->alloc_state); |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 370 | bio_put(device->flush_bio); |
| 371 | kfree(device); |
| 372 | } |
| 373 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 374 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 375 | { |
| 376 | struct btrfs_device *device; |
| 377 | WARN_ON(fs_devices->opened); |
| 378 | while (!list_empty(&fs_devices->devices)) { |
| 379 | device = list_entry(fs_devices->devices.next, |
| 380 | struct btrfs_device, dev_list); |
| 381 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 382 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 383 | } |
| 384 | kfree(fs_devices); |
| 385 | } |
| 386 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 387 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 388 | { |
| 389 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 390 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 391 | while (!list_empty(&fs_uuids)) { |
| 392 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 393 | struct btrfs_fs_devices, fs_list); |
| 394 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 395 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 396 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 397 | } |
| 398 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 399 | /* |
| 400 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 401 | * Returned struct is not linked onto any lists and must be destroyed using |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 402 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 403 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 404 | static struct btrfs_device *__alloc_device(void) |
| 405 | { |
| 406 | struct btrfs_device *dev; |
| 407 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 408 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 409 | if (!dev) |
| 410 | return ERR_PTR(-ENOMEM); |
| 411 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 412 | /* |
| 413 | * Preallocate a bio that's always going to be used for flushing device |
| 414 | * barriers and matches the device lifespan |
| 415 | */ |
| 416 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 417 | if (!dev->flush_bio) { |
| 418 | kfree(dev); |
| 419 | return ERR_PTR(-ENOMEM); |
| 420 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 421 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 422 | INIT_LIST_HEAD(&dev->dev_list); |
| 423 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 424 | INIT_LIST_HEAD(&dev->post_commit_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 425 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 426 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 427 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 428 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 429 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 430 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 431 | extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 432 | |
| 433 | return dev; |
| 434 | } |
| 435 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 436 | static noinline struct btrfs_fs_devices *find_fsid( |
| 437 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 438 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 439 | struct btrfs_fs_devices *fs_devices; |
| 440 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 441 | ASSERT(fsid); |
| 442 | |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 443 | if (metadata_fsid) { |
| 444 | /* |
| 445 | * Handle scanned device having completed its fsid change but |
| 446 | * belonging to a fs_devices that was created by first scanning |
| 447 | * a device which didn't have its fsid/metadata_uuid changed |
| 448 | * at all and the CHANGING_FSID_V2 flag set. |
| 449 | */ |
| 450 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 451 | if (fs_devices->fsid_change && |
| 452 | memcmp(metadata_fsid, fs_devices->fsid, |
| 453 | BTRFS_FSID_SIZE) == 0 && |
| 454 | memcmp(fs_devices->fsid, fs_devices->metadata_uuid, |
| 455 | BTRFS_FSID_SIZE) == 0) { |
| 456 | return fs_devices; |
| 457 | } |
| 458 | } |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 459 | /* |
| 460 | * Handle scanned device having completed its fsid change but |
| 461 | * belonging to a fs_devices that was created by a device that |
| 462 | * has an outdated pair of fsid/metadata_uuid and |
| 463 | * CHANGING_FSID_V2 flag set. |
| 464 | */ |
| 465 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 466 | if (fs_devices->fsid_change && |
| 467 | memcmp(fs_devices->metadata_uuid, |
| 468 | fs_devices->fsid, BTRFS_FSID_SIZE) != 0 && |
| 469 | memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 470 | BTRFS_FSID_SIZE) == 0) { |
| 471 | return fs_devices; |
| 472 | } |
| 473 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* Handle non-split brain cases */ |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 477 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 478 | if (metadata_fsid) { |
| 479 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 480 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 481 | BTRFS_FSID_SIZE) == 0) |
| 482 | return fs_devices; |
| 483 | } else { |
| 484 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 485 | return fs_devices; |
| 486 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 487 | } |
| 488 | return NULL; |
| 489 | } |
| 490 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 491 | static int |
| 492 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 493 | int flush, struct block_device **bdev, |
| 494 | struct buffer_head **bh) |
| 495 | { |
| 496 | int ret; |
| 497 | |
| 498 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 499 | |
| 500 | if (IS_ERR(*bdev)) { |
| 501 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 502 | goto error; |
| 503 | } |
| 504 | |
| 505 | if (flush) |
| 506 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 507 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 508 | if (ret) { |
| 509 | blkdev_put(*bdev, flags); |
| 510 | goto error; |
| 511 | } |
| 512 | invalidate_bdev(*bdev); |
| 513 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 514 | if (IS_ERR(*bh)) { |
| 515 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 516 | blkdev_put(*bdev, flags); |
| 517 | goto error; |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | |
| 522 | error: |
| 523 | *bdev = NULL; |
| 524 | *bh = NULL; |
| 525 | return ret; |
| 526 | } |
| 527 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 528 | static bool device_path_matched(const char *path, struct btrfs_device *device) |
| 529 | { |
| 530 | int found; |
| 531 | |
| 532 | rcu_read_lock(); |
| 533 | found = strcmp(rcu_str_deref(device->name), path); |
| 534 | rcu_read_unlock(); |
| 535 | |
| 536 | return found == 0; |
| 537 | } |
| 538 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 539 | /* |
| 540 | * Search and remove all stale (devices which are not mounted) devices. |
| 541 | * When both inputs are NULL, it will search and release all stale devices. |
| 542 | * path: Optional. When provided will it release all unmounted devices |
| 543 | * matching this path only. |
| 544 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 545 | * devices. |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 546 | * Return: 0 for success or if @path is NULL. |
| 547 | * -EBUSY if @path is a mounted device. |
| 548 | * -ENOENT if @path does not match any device in the list. |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 549 | */ |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 550 | static int btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 551 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 552 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 553 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 554 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 555 | int ret = 0; |
| 556 | |
| 557 | if (path) |
| 558 | ret = -ENOENT; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 559 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 560 | 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] | 561 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 562 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 563 | list_for_each_entry_safe(device, tmp_device, |
| 564 | &fs_devices->devices, dev_list) { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 565 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 566 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 567 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 568 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 569 | if (path && !device_path_matched(path, device)) |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 570 | continue; |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 571 | if (fs_devices->opened) { |
| 572 | /* for an already deleted device return 0 */ |
| 573 | if (path && ret != 0) |
| 574 | ret = -EBUSY; |
| 575 | break; |
| 576 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 577 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 578 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 579 | fs_devices->num_devices--; |
| 580 | list_del(&device->dev_list); |
| 581 | btrfs_free_device(device); |
| 582 | |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 583 | ret = 0; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 584 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 585 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 586 | } |
| 587 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 588 | |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 589 | if (fs_devices->num_devices == 0) { |
| 590 | btrfs_sysfs_remove_fsid(fs_devices); |
| 591 | list_del(&fs_devices->fs_list); |
| 592 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 593 | } |
| 594 | } |
Anand Jain | 70bc708 | 2019-01-04 13:31:53 +0800 | [diff] [blame] | 595 | |
| 596 | return ret; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 597 | } |
| 598 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 599 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 600 | struct btrfs_device *device, fmode_t flags, |
| 601 | void *holder) |
| 602 | { |
| 603 | struct request_queue *q; |
| 604 | struct block_device *bdev; |
| 605 | struct buffer_head *bh; |
| 606 | struct btrfs_super_block *disk_super; |
| 607 | u64 devid; |
| 608 | int ret; |
| 609 | |
| 610 | if (device->bdev) |
| 611 | return -EINVAL; |
| 612 | if (!device->name) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 616 | &bdev, &bh); |
| 617 | if (ret) |
| 618 | return ret; |
| 619 | |
| 620 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 621 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 622 | if (devid != device->devid) |
| 623 | goto error_brelse; |
| 624 | |
| 625 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 626 | goto error_brelse; |
| 627 | |
| 628 | device->generation = btrfs_super_generation(disk_super); |
| 629 | |
| 630 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 631 | if (btrfs_super_incompat_flags(disk_super) & |
| 632 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 633 | pr_err( |
| 634 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| 635 | goto error_brelse; |
| 636 | } |
| 637 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 638 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 639 | fs_devices->seeding = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 640 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 641 | if (bdev_read_only(bdev)) |
| 642 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 643 | else |
| 644 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 648 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 649 | fs_devices->rotating = true; |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 650 | |
| 651 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 652 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 653 | device->mode = flags; |
| 654 | |
| 655 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 656 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 657 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 658 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 659 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 660 | } |
| 661 | brelse(bh); |
| 662 | |
| 663 | return 0; |
| 664 | |
| 665 | error_brelse: |
| 666 | brelse(bh); |
| 667 | blkdev_put(bdev, flags); |
| 668 | |
| 669 | return -EINVAL; |
| 670 | } |
| 671 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 672 | /* |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 673 | * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices |
| 674 | * being created with a disk that has already completed its fsid change. |
| 675 | */ |
| 676 | static struct btrfs_fs_devices *find_fsid_inprogress( |
| 677 | struct btrfs_super_block *disk_super) |
| 678 | { |
| 679 | struct btrfs_fs_devices *fs_devices; |
| 680 | |
| 681 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 682 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 683 | BTRFS_FSID_SIZE) != 0 && |
| 684 | memcmp(fs_devices->metadata_uuid, disk_super->fsid, |
| 685 | BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) { |
| 686 | return fs_devices; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return NULL; |
| 691 | } |
| 692 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 693 | |
| 694 | static struct btrfs_fs_devices *find_fsid_changed( |
| 695 | struct btrfs_super_block *disk_super) |
| 696 | { |
| 697 | struct btrfs_fs_devices *fs_devices; |
| 698 | |
| 699 | /* |
| 700 | * Handles the case where scanned device is part of an fs that had |
| 701 | * multiple successful changes of FSID but curently device didn't |
| 702 | * observe it. Meaning our fsid will be different than theirs. |
| 703 | */ |
| 704 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
| 705 | if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid, |
| 706 | BTRFS_FSID_SIZE) != 0 && |
| 707 | memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid, |
| 708 | BTRFS_FSID_SIZE) == 0 && |
| 709 | memcmp(fs_devices->fsid, disk_super->fsid, |
| 710 | BTRFS_FSID_SIZE) != 0) { |
| 711 | return fs_devices; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | return NULL; |
| 716 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 717 | /* |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 718 | * Add new device to list of registered devices |
| 719 | * |
| 720 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 721 | * device pointer which was just added or updated when successful |
| 722 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 723 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 724 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 725 | struct btrfs_super_block *disk_super, |
| 726 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 727 | { |
| 728 | struct btrfs_device *device; |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 729 | struct btrfs_fs_devices *fs_devices = NULL; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 730 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 731 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 732 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 733 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 734 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 735 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 736 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 737 | |
Nikolay Borisov | cc5de4e | 2018-10-30 16:43:28 +0200 | [diff] [blame] | 738 | if (fsid_change_in_progress) { |
| 739 | if (!has_metadata_uuid) { |
| 740 | /* |
| 741 | * When we have an image which has CHANGING_FSID_V2 set |
| 742 | * it might belong to either a filesystem which has |
| 743 | * disks with completed fsid change or it might belong |
| 744 | * to fs with no UUID changes in effect, handle both. |
| 745 | */ |
| 746 | fs_devices = find_fsid_inprogress(disk_super); |
| 747 | if (!fs_devices) |
| 748 | fs_devices = find_fsid(disk_super->fsid, NULL); |
| 749 | } else { |
| 750 | fs_devices = find_fsid_changed(disk_super); |
| 751 | } |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 752 | } else if (has_metadata_uuid) { |
| 753 | fs_devices = find_fsid(disk_super->fsid, |
| 754 | disk_super->metadata_uuid); |
| 755 | } else { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 756 | fs_devices = find_fsid(disk_super->fsid, NULL); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 759 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 760 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 761 | if (has_metadata_uuid) |
| 762 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 763 | disk_super->metadata_uuid); |
| 764 | else |
| 765 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 766 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 767 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 768 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 769 | |
Al Viro | 92900e5 | 2019-01-27 04:58:00 +0000 | [diff] [blame] | 770 | fs_devices->fsid_change = fsid_change_in_progress; |
| 771 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 772 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 773 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 774 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 775 | device = NULL; |
| 776 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 777 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 778 | device = btrfs_find_device(fs_devices, devid, |
| 779 | disk_super->dev_item.uuid, NULL, false); |
Nikolay Borisov | 7a62d0f | 2018-10-30 16:43:27 +0200 | [diff] [blame] | 780 | |
| 781 | /* |
| 782 | * If this disk has been pulled into an fs devices created by |
| 783 | * a device which had the CHANGING_FSID_V2 flag then replace the |
| 784 | * metadata_uuid/fsid values of the fs_devices. |
| 785 | */ |
| 786 | if (has_metadata_uuid && fs_devices->fsid_change && |
| 787 | found_transid > fs_devices->latest_generation) { |
| 788 | memcpy(fs_devices->fsid, disk_super->fsid, |
| 789 | BTRFS_FSID_SIZE); |
| 790 | memcpy(fs_devices->metadata_uuid, |
| 791 | disk_super->metadata_uuid, BTRFS_FSID_SIZE); |
| 792 | |
| 793 | fs_devices->fsid_change = false; |
| 794 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 795 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 796 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 797 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 798 | if (fs_devices->opened) { |
| 799 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 800 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 801 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 802 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 803 | device = btrfs_alloc_device(NULL, &devid, |
| 804 | disk_super->dev_item.uuid); |
| 805 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 806 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 807 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 808 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 809 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 810 | |
| 811 | name = rcu_string_strdup(path, GFP_NOFS); |
| 812 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 813 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 814 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 815 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 816 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 817 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 818 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 819 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 820 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 821 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 822 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 823 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 824 | |
| 825 | if (disk_super->label[0]) |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 826 | pr_info( |
| 827 | "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n", |
| 828 | disk_super->label, devid, found_transid, path, |
| 829 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 830 | else |
Anand Jain | aa6c0df | 2019-10-02 18:30:48 +0800 | [diff] [blame] | 831 | pr_info( |
| 832 | "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n", |
| 833 | disk_super->fsid, devid, found_transid, path, |
| 834 | current->comm, task_pid_nr(current)); |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 835 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 836 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 837 | /* |
| 838 | * When FS is already mounted. |
| 839 | * 1. If you are here and if the device->name is NULL that |
| 840 | * means this device was missing at time of FS mount. |
| 841 | * 2. If you are here and if the device->name is different |
| 842 | * from 'path' that means either |
| 843 | * a. The same device disappeared and reappeared with |
| 844 | * different name. or |
| 845 | * b. The missing-disk-which-was-replaced, has |
| 846 | * reappeared now. |
| 847 | * |
| 848 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 849 | * and unintentional. |
| 850 | * |
| 851 | * Further in case of 1 and 2a above, the disk at 'path' |
| 852 | * would have missed some transaction when it was away and |
| 853 | * in case of 2a the stale bdev has to be updated as well. |
| 854 | * 2b must not be allowed at all time. |
| 855 | */ |
| 856 | |
| 857 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 858 | * For now, we do allow update to btrfs_fs_device through the |
| 859 | * btrfs dev scan cli after FS has been mounted. We're still |
| 860 | * tracking a problem where systems fail mount by subvolume id |
| 861 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 862 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 863 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 864 | /* |
| 865 | * That is if the FS is _not_ mounted and if you |
| 866 | * are here, that means there is more than one |
| 867 | * disk with same uuid and devid.We keep the one |
| 868 | * with larger generation number or the last-in if |
| 869 | * generation are equal. |
| 870 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 871 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 872 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 873 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 874 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 875 | /* |
| 876 | * We are going to replace the device path for a given devid, |
| 877 | * make sure it's the same device if the device is mounted |
| 878 | */ |
| 879 | if (device->bdev) { |
| 880 | struct block_device *path_bdev; |
| 881 | |
| 882 | path_bdev = lookup_bdev(path); |
| 883 | if (IS_ERR(path_bdev)) { |
| 884 | mutex_unlock(&fs_devices->device_list_mutex); |
| 885 | return ERR_CAST(path_bdev); |
| 886 | } |
| 887 | |
| 888 | if (device->bdev != path_bdev) { |
| 889 | bdput(path_bdev); |
| 890 | mutex_unlock(&fs_devices->device_list_mutex); |
| 891 | btrfs_warn_in_rcu(device->fs_info, |
| 892 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 893 | disk_super->fsid, devid, |
| 894 | rcu_str_deref(device->name), path); |
| 895 | return ERR_PTR(-EEXIST); |
| 896 | } |
| 897 | bdput(path_bdev); |
| 898 | btrfs_info_in_rcu(device->fs_info, |
| 899 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 900 | disk_super->fsid, devid, |
| 901 | rcu_str_deref(device->name), path); |
| 902 | } |
| 903 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 904 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 905 | if (!name) { |
| 906 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 907 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 908 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 909 | rcu_string_free(device->name); |
| 910 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 911 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 912 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 913 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 914 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 915 | } |
| 916 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 917 | /* |
| 918 | * Unmount does not free the btrfs_device struct but would zero |
| 919 | * generation along with most of the other members. So just update |
| 920 | * it back. We need it to pick the disk with largest generation |
| 921 | * (as above). |
| 922 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 923 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 924 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame] | 925 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 926 | fs_devices->latest_generation); |
| 927 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 928 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 929 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 930 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 931 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 932 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 933 | } |
| 934 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 935 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 936 | { |
| 937 | struct btrfs_fs_devices *fs_devices; |
| 938 | struct btrfs_device *device; |
| 939 | struct btrfs_device *orig_dev; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 940 | int ret = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 941 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 942 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 943 | if (IS_ERR(fs_devices)) |
| 944 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 945 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 946 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 947 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 948 | |
| 949 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 950 | struct rcu_string *name; |
| 951 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 952 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 953 | orig_dev->uuid); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 954 | if (IS_ERR(device)) { |
| 955 | ret = PTR_ERR(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 956 | goto error; |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 957 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 958 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 959 | /* |
| 960 | * This is ok to do without rcu read locked because we hold the |
| 961 | * uuid mutex so nothing we touch in here is going to disappear. |
| 962 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 963 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 964 | name = rcu_string_strdup(orig_dev->name->str, |
| 965 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 966 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 967 | btrfs_free_device(device); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 968 | ret = -ENOMEM; |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 969 | goto error; |
| 970 | } |
| 971 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 972 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 973 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 974 | list_add(&device->dev_list, &fs_devices->devices); |
| 975 | device->fs_devices = fs_devices; |
| 976 | fs_devices->num_devices++; |
| 977 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 978 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 979 | return fs_devices; |
| 980 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 981 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 982 | free_fs_devices(fs_devices); |
Anand Jain | d2979aa | 2019-08-27 15:40:45 +0800 | [diff] [blame] | 983 | return ERR_PTR(ret); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 984 | } |
| 985 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 986 | /* |
| 987 | * After we have read the system tree and know devids belonging to |
| 988 | * this filesystem, remove the device which does not belong there. |
| 989 | */ |
| 990 | 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] | 991 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 992 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 993 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 994 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 995 | mutex_lock(&uuid_mutex); |
| 996 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 997 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 998 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 999 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1000 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1001 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1002 | &device->dev_state) && |
| 1003 | (!latest_dev || |
| 1004 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1005 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1006 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1007 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1008 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1009 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1010 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1011 | /* |
| 1012 | * In the first step, keep the device which has |
| 1013 | * the correct fsid and the devid that is used |
| 1014 | * for the dev_replace procedure. |
| 1015 | * In the second step, the dev_replace state is |
| 1016 | * read from the device tree and it is known |
| 1017 | * whether the procedure is really active or |
| 1018 | * not, which means whether this device is |
| 1019 | * used or whether it should be removed. |
| 1020 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1021 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1022 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1023 | continue; |
| 1024 | } |
| 1025 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1026 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1027 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1028 | device->bdev = NULL; |
| 1029 | fs_devices->open_devices--; |
| 1030 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1031 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1032 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1033 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1034 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1035 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1036 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1037 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1038 | list_del_init(&device->dev_list); |
| 1039 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1040 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1041 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1042 | |
| 1043 | if (fs_devices->seed) { |
| 1044 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1045 | goto again; |
| 1046 | } |
| 1047 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1048 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1049 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1050 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1051 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1052 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1053 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1054 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1055 | if (!device->bdev) |
| 1056 | return; |
| 1057 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1058 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1059 | sync_blockdev(device->bdev); |
| 1060 | invalidate_bdev(device->bdev); |
| 1061 | } |
| 1062 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1063 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1066 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1067 | { |
| 1068 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 1069 | struct btrfs_device *new_device; |
| 1070 | struct rcu_string *name; |
| 1071 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1072 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1073 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1074 | list_del_init(&device->dev_alloc_list); |
| 1075 | fs_devices->rw_devices--; |
| 1076 | } |
| 1077 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1078 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1079 | fs_devices->missing_devices--; |
| 1080 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1081 | btrfs_close_bdev(device); |
Johannes Thumshirn | 3fff397 | 2019-11-26 09:40:05 +0100 | [diff] [blame^] | 1082 | if (device->bdev) |
| 1083 | fs_devices->open_devices--; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1084 | |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1085 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 1086 | device->uuid); |
| 1087 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 1088 | |
| 1089 | /* Safe because we are under uuid_mutex */ |
| 1090 | if (device->name) { |
| 1091 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 1092 | BUG_ON(!name); /* -ENOMEM */ |
| 1093 | rcu_assign_pointer(new_device->name, name); |
| 1094 | } |
| 1095 | |
| 1096 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 1097 | new_device->fs_devices = device->fs_devices; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1098 | |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 1099 | synchronize_rcu(); |
| 1100 | btrfs_free_device(device); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1103 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1104 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1105 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1106 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1107 | if (--fs_devices->opened > 0) |
| 1108 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1109 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1110 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1111 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1112 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1113 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1114 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1115 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1116 | WARN_ON(fs_devices->open_devices); |
| 1117 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1118 | fs_devices->opened = 0; |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 1119 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1120 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1124 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1125 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1126 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1127 | int ret; |
| 1128 | |
| 1129 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1130 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1131 | if (!fs_devices->opened) { |
| 1132 | seed_devices = fs_devices->seed; |
| 1133 | fs_devices->seed = NULL; |
| 1134 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1135 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1136 | |
| 1137 | while (seed_devices) { |
| 1138 | fs_devices = seed_devices; |
| 1139 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1140 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1141 | free_fs_devices(fs_devices); |
| 1142 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1143 | return ret; |
| 1144 | } |
| 1145 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1146 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1147 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1148 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1149 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1150 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1151 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1152 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1153 | flags |= FMODE_EXCL; |
| 1154 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1155 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1156 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1157 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1158 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1159 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1160 | if (!latest_dev || |
| 1161 | device->generation > latest_dev->generation) |
| 1162 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1163 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1164 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1165 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1166 | goto out; |
| 1167 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1168 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1169 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1170 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1171 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1172 | return ret; |
| 1173 | } |
| 1174 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1175 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1176 | { |
| 1177 | struct btrfs_device *dev1, *dev2; |
| 1178 | |
| 1179 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1180 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1181 | |
| 1182 | if (dev1->devid < dev2->devid) |
| 1183 | return -1; |
| 1184 | else if (dev1->devid > dev2->devid) |
| 1185 | return 1; |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1189 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1190 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1191 | { |
| 1192 | int ret; |
| 1193 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1194 | lockdep_assert_held(&uuid_mutex); |
| 1195 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1196 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1197 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1198 | fs_devices->opened++; |
| 1199 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1200 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1201 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1202 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1203 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1204 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1205 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1206 | return ret; |
| 1207 | } |
| 1208 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1209 | static void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1210 | { |
| 1211 | kunmap(page); |
| 1212 | put_page(page); |
| 1213 | } |
| 1214 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1215 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1216 | struct page **page, |
| 1217 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1218 | { |
| 1219 | void *p; |
| 1220 | pgoff_t index; |
| 1221 | |
| 1222 | /* make sure our super fits in the device */ |
| 1223 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1224 | return 1; |
| 1225 | |
| 1226 | /* make sure our super fits in the page */ |
| 1227 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1228 | return 1; |
| 1229 | |
| 1230 | /* make sure our super doesn't straddle pages on disk */ |
| 1231 | index = bytenr >> PAGE_SHIFT; |
| 1232 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1233 | return 1; |
| 1234 | |
| 1235 | /* pull in the page with our super */ |
| 1236 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1237 | index, GFP_KERNEL); |
| 1238 | |
| 1239 | if (IS_ERR_OR_NULL(*page)) |
| 1240 | return 1; |
| 1241 | |
| 1242 | p = kmap(*page); |
| 1243 | |
| 1244 | /* align our pointer to the offset of the super block */ |
Johannes Thumshirn | 7073017 | 2018-12-05 15:23:03 +0100 | [diff] [blame] | 1245 | *disk_super = p + offset_in_page(bytenr); |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1246 | |
| 1247 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1248 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1249 | btrfs_release_disk_super(*page); |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
| 1253 | if ((*disk_super)->label[0] && |
| 1254 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1255 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1256 | |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
Anand Jain | 228a73a | 2019-01-04 13:31:54 +0800 | [diff] [blame] | 1260 | int btrfs_forget_devices(const char *path) |
| 1261 | { |
| 1262 | int ret; |
| 1263 | |
| 1264 | mutex_lock(&uuid_mutex); |
| 1265 | ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL); |
| 1266 | mutex_unlock(&uuid_mutex); |
| 1267 | |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1271 | /* |
| 1272 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1273 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1274 | * is read via pagecache |
| 1275 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1276 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1277 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1278 | { |
| 1279 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1280 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1281 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1282 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1283 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1284 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1285 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1286 | lockdep_assert_held(&uuid_mutex); |
| 1287 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1288 | /* |
| 1289 | * we would like to check all the supers, but that would make |
| 1290 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1291 | * So, we need to add a special mount option to scan for |
| 1292 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1293 | */ |
| 1294 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1295 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1296 | |
| 1297 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1298 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1299 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1300 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1301 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1302 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1303 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1304 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1305 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1306 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1307 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1308 | if (new_device_added) |
| 1309 | btrfs_free_stale_devices(path, device); |
| 1310 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1311 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1312 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1313 | |
| 1314 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1315 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1316 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1317 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1318 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1319 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1320 | /* |
| 1321 | * Try to find a chunk that intersects [start, start + len] range and when one |
| 1322 | * such is found, record the end of it in *start |
| 1323 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1324 | static bool contains_pending_extent(struct btrfs_device *device, u64 *start, |
| 1325 | u64 len) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1326 | { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1327 | u64 physical_start, physical_end; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1328 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1329 | lockdep_assert_held(&device->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1330 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1331 | if (!find_first_extent_bit(&device->alloc_state, *start, |
| 1332 | &physical_start, &physical_end, |
| 1333 | CHUNK_ALLOCATED, NULL)) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1334 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1335 | if (in_range(physical_start, *start, len) || |
| 1336 | in_range(*start, physical_start, |
| 1337 | physical_end - physical_start)) { |
| 1338 | *start = physical_end + 1; |
| 1339 | return true; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1340 | } |
| 1341 | } |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1342 | return false; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1346 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1347 | * find_free_dev_extent_start - find free space in the specified device |
| 1348 | * @device: the device which we search the free space in |
| 1349 | * @num_bytes: the size of the free space that we need |
| 1350 | * @search_start: the position from which to begin the search |
| 1351 | * @start: store the start of the free space. |
| 1352 | * @len: the size of the free space. that we find, or the size |
| 1353 | * 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] | 1354 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1355 | * this uses a pretty simple search, the expectation is that it is |
| 1356 | * called very infrequently and that a given device has a small number |
| 1357 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1358 | * |
| 1359 | * @start is used to store the start of the free space if we find. But if we |
| 1360 | * don't find suitable free space, it will be used to store the start position |
| 1361 | * of the max free space. |
| 1362 | * |
| 1363 | * @len is used to store the size of the free space that we find. |
| 1364 | * But if we don't find suitable free space, it is used to store the size of |
| 1365 | * the max free space. |
Qu Wenruo | 135da97 | 2019-07-19 14:51:42 +0800 | [diff] [blame] | 1366 | * |
| 1367 | * NOTE: This function will search *commit* root of device tree, and does extra |
| 1368 | * check to ensure dev extents are not double allocated. |
| 1369 | * This makes the function safe to allocate dev extents but may not report |
| 1370 | * correct usable device space, as device extent freed in current transaction |
| 1371 | * is not reported as avaiable. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1372 | */ |
Qu Wenruo | 9e3246a | 2019-07-19 14:51:41 +0800 | [diff] [blame] | 1373 | static int find_free_dev_extent_start(struct btrfs_device *device, |
| 1374 | u64 num_bytes, u64 search_start, u64 *start, |
| 1375 | u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1376 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1377 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1378 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1379 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1380 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1381 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1382 | u64 hole_size; |
| 1383 | u64 max_hole_start; |
| 1384 | u64 max_hole_size; |
| 1385 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1386 | u64 search_end = device->total_bytes; |
| 1387 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1388 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1389 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1390 | |
| 1391 | /* |
| 1392 | * We don't want to overwrite the superblock on the drive nor any area |
| 1393 | * used by the boot loader (grub for example), so we make sure to start |
| 1394 | * at an offset of at least 1MB. |
| 1395 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1396 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1397 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1398 | path = btrfs_alloc_path(); |
| 1399 | if (!path) |
| 1400 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1401 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1402 | max_hole_start = search_start; |
| 1403 | max_hole_size = 0; |
| 1404 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1405 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1406 | if (search_start >= search_end || |
| 1407 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1408 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1409 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1412 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1413 | path->search_commit_root = 1; |
| 1414 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1415 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1416 | key.objectid = device->devid; |
| 1417 | key.offset = search_start; |
| 1418 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1419 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1420 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1421 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1422 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1423 | if (ret > 0) { |
| 1424 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1425 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1426 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1427 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1428 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1429 | while (1) { |
| 1430 | l = path->nodes[0]; |
| 1431 | slot = path->slots[0]; |
| 1432 | if (slot >= btrfs_header_nritems(l)) { |
| 1433 | ret = btrfs_next_leaf(root, path); |
| 1434 | if (ret == 0) |
| 1435 | continue; |
| 1436 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1437 | goto out; |
| 1438 | |
| 1439 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1440 | } |
| 1441 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1442 | |
| 1443 | if (key.objectid < device->devid) |
| 1444 | goto next; |
| 1445 | |
| 1446 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1447 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1448 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1449 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1450 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1451 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1452 | if (key.offset > search_start) { |
| 1453 | hole_size = key.offset - search_start; |
| 1454 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1455 | /* |
| 1456 | * Have to check before we set max_hole_start, otherwise |
| 1457 | * we could end up sending back this offset anyway. |
| 1458 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1459 | if (contains_pending_extent(device, &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1460 | hole_size)) { |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1461 | if (key.offset >= search_start) |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1462 | hole_size = key.offset - search_start; |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1463 | else |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1464 | hole_size = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1465 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1466 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1467 | if (hole_size > max_hole_size) { |
| 1468 | max_hole_start = search_start; |
| 1469 | max_hole_size = hole_size; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * If this free space is greater than which we need, |
| 1474 | * it must be the max free space that we have found |
| 1475 | * until now, so max_hole_start must point to the start |
| 1476 | * of this free space and the length of this free space |
| 1477 | * is stored in max_hole_size. Thus, we return |
| 1478 | * max_hole_start and max_hole_size and go back to the |
| 1479 | * caller. |
| 1480 | */ |
| 1481 | if (hole_size >= num_bytes) { |
| 1482 | ret = 0; |
| 1483 | goto out; |
| 1484 | } |
| 1485 | } |
| 1486 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1487 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1488 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1489 | dev_extent); |
| 1490 | if (extent_end > search_start) |
| 1491 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1492 | next: |
| 1493 | path->slots[0]++; |
| 1494 | cond_resched(); |
| 1495 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1496 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1497 | /* |
| 1498 | * At this point, search_start should be the end of |
| 1499 | * allocated dev extents, and when shrinking the device, |
| 1500 | * search_end may be smaller than search_start. |
| 1501 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1502 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1503 | hole_size = search_end - search_start; |
| 1504 | |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 1505 | if (contains_pending_extent(device, &search_start, hole_size)) { |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1506 | btrfs_release_path(path); |
| 1507 | goto again; |
| 1508 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1509 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1510 | if (hole_size > max_hole_size) { |
| 1511 | max_hole_start = search_start; |
| 1512 | max_hole_size = hole_size; |
| 1513 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1516 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1517 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1518 | ret = -ENOSPC; |
| 1519 | else |
| 1520 | ret = 0; |
| 1521 | |
| 1522 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1523 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1524 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1525 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1526 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1527 | return ret; |
| 1528 | } |
| 1529 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1530 | int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1531 | u64 *start, u64 *len) |
| 1532 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1533 | /* FIXME use last free of some kind */ |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 1534 | return find_free_dev_extent_start(device, num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1535 | } |
| 1536 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1537 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1538 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1539 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1540 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1541 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1542 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1543 | int ret; |
| 1544 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1545 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1546 | struct btrfs_key found_key; |
| 1547 | struct extent_buffer *leaf = NULL; |
| 1548 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1549 | |
| 1550 | path = btrfs_alloc_path(); |
| 1551 | if (!path) |
| 1552 | return -ENOMEM; |
| 1553 | |
| 1554 | key.objectid = device->devid; |
| 1555 | key.offset = start; |
| 1556 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1557 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1558 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1559 | if (ret > 0) { |
| 1560 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1561 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1562 | if (ret) |
| 1563 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1564 | leaf = path->nodes[0]; |
| 1565 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1566 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1567 | struct btrfs_dev_extent); |
| 1568 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1569 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1570 | key = found_key; |
| 1571 | btrfs_release_path(path); |
| 1572 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1573 | } else if (ret == 0) { |
| 1574 | leaf = path->nodes[0]; |
| 1575 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1576 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1577 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1578 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1579 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1580 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1581 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1582 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1583 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1584 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1585 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1586 | btrfs_handle_fs_error(fs_info, ret, |
| 1587 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1588 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1589 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1590 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1591 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1592 | btrfs_free_path(path); |
| 1593 | return ret; |
| 1594 | } |
| 1595 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1596 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1597 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1598 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1599 | { |
| 1600 | int ret; |
| 1601 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1602 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1603 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1604 | struct btrfs_dev_extent *extent; |
| 1605 | struct extent_buffer *leaf; |
| 1606 | struct btrfs_key key; |
| 1607 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1608 | 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] | 1609 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1610 | path = btrfs_alloc_path(); |
| 1611 | if (!path) |
| 1612 | return -ENOMEM; |
| 1613 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1614 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1615 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1616 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1617 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1618 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1619 | if (ret) |
| 1620 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1621 | |
| 1622 | leaf = path->nodes[0]; |
| 1623 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1624 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1625 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1626 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1627 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1628 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1629 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1630 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1631 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1632 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1633 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1634 | btrfs_free_path(path); |
| 1635 | return ret; |
| 1636 | } |
| 1637 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1638 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1639 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1640 | struct extent_map_tree *em_tree; |
| 1641 | struct extent_map *em; |
| 1642 | struct rb_node *n; |
| 1643 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1644 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 1645 | em_tree = &fs_info->mapping_tree; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1646 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1647 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1648 | if (n) { |
| 1649 | em = rb_entry(n, struct extent_map, rb_node); |
| 1650 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1651 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1652 | read_unlock(&em_tree->lock); |
| 1653 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1654 | return ret; |
| 1655 | } |
| 1656 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1657 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1658 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1659 | { |
| 1660 | int ret; |
| 1661 | struct btrfs_key key; |
| 1662 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1663 | struct btrfs_path *path; |
| 1664 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1665 | path = btrfs_alloc_path(); |
| 1666 | if (!path) |
| 1667 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1668 | |
| 1669 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1670 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1671 | key.offset = (u64)-1; |
| 1672 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1673 | 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] | 1674 | if (ret < 0) |
| 1675 | goto error; |
| 1676 | |
Anand Jain | a06dee4d | 2019-08-27 15:40:44 +0800 | [diff] [blame] | 1677 | if (ret == 0) { |
| 1678 | /* Corruption */ |
| 1679 | btrfs_err(fs_info, "corrupted chunk tree devid -1 matched"); |
| 1680 | ret = -EUCLEAN; |
| 1681 | goto error; |
| 1682 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1683 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1684 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1685 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1686 | BTRFS_DEV_ITEM_KEY); |
| 1687 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1688 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1689 | } else { |
| 1690 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1691 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1692 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1693 | } |
| 1694 | ret = 0; |
| 1695 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1696 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1697 | return ret; |
| 1698 | } |
| 1699 | |
| 1700 | /* |
| 1701 | * the device information is stored in the chunk root |
| 1702 | * the btrfs_device struct should be fully filled in |
| 1703 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1704 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1705 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1706 | { |
| 1707 | int ret; |
| 1708 | struct btrfs_path *path; |
| 1709 | struct btrfs_dev_item *dev_item; |
| 1710 | struct extent_buffer *leaf; |
| 1711 | struct btrfs_key key; |
| 1712 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1713 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1714 | path = btrfs_alloc_path(); |
| 1715 | if (!path) |
| 1716 | return -ENOMEM; |
| 1717 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1718 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1719 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1720 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1721 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1722 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1723 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1724 | if (ret) |
| 1725 | goto out; |
| 1726 | |
| 1727 | leaf = path->nodes[0]; |
| 1728 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1729 | |
| 1730 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1731 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1732 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1733 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1734 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1735 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1736 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1737 | btrfs_device_get_disk_total_bytes(device)); |
| 1738 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1739 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1740 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1741 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1742 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1743 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1744 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1745 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1746 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1747 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1748 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1749 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1750 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1751 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1752 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1753 | out: |
| 1754 | btrfs_free_path(path); |
| 1755 | return ret; |
| 1756 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1757 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1758 | /* |
| 1759 | * Function to update ctime/mtime for a given device path. |
| 1760 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1761 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1762 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1763 | { |
| 1764 | struct file *filp; |
| 1765 | |
| 1766 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1767 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1768 | return; |
| 1769 | file_update_time(filp); |
| 1770 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1771 | } |
| 1772 | |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1773 | static int btrfs_rm_dev_item(struct btrfs_device *device) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1774 | { |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1775 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1776 | int ret; |
| 1777 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1778 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1779 | struct btrfs_trans_handle *trans; |
| 1780 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1781 | path = btrfs_alloc_path(); |
| 1782 | if (!path) |
| 1783 | return -ENOMEM; |
| 1784 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1785 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1786 | if (IS_ERR(trans)) { |
| 1787 | btrfs_free_path(path); |
| 1788 | return PTR_ERR(trans); |
| 1789 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1790 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1791 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1792 | key.offset = device->devid; |
| 1793 | |
| 1794 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1795 | if (ret) { |
| 1796 | if (ret > 0) |
| 1797 | ret = -ENOENT; |
| 1798 | btrfs_abort_transaction(trans, ret); |
| 1799 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1800 | goto out; |
| 1801 | } |
| 1802 | |
| 1803 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1804 | if (ret) { |
| 1805 | btrfs_abort_transaction(trans, ret); |
| 1806 | btrfs_end_transaction(trans); |
| 1807 | } |
| 1808 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1809 | out: |
| 1810 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1811 | if (!ret) |
| 1812 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1813 | return ret; |
| 1814 | } |
| 1815 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1816 | /* |
| 1817 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 1818 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 1819 | * replace. |
| 1820 | */ |
| 1821 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 1822 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1823 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1824 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1825 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1826 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1827 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1828 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1829 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1830 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1831 | all_avail = fs_info->avail_data_alloc_bits | |
| 1832 | fs_info->avail_system_alloc_bits | |
| 1833 | fs_info->avail_metadata_alloc_bits; |
| 1834 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1835 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1836 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 1837 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1838 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1839 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1840 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 1841 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1842 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1843 | if (ret) |
| 1844 | return ret; |
| 1845 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1849 | } |
| 1850 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1851 | static struct btrfs_device * btrfs_find_next_active_device( |
| 1852 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1853 | { |
| 1854 | struct btrfs_device *next_device; |
| 1855 | |
| 1856 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 1857 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1858 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 1859 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1860 | return next_device; |
| 1861 | } |
| 1862 | |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | |
| 1866 | /* |
| 1867 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 1868 | * and replace it with the provided or the next active device, in the context |
| 1869 | * where this function called, there should be always be another device (or |
| 1870 | * this_dev) which is active. |
| 1871 | */ |
David Sterba | b105e92 | 2019-10-01 19:57:35 +0200 | [diff] [blame] | 1872 | void __cold btrfs_assign_next_active_device(struct btrfs_device *device, |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1873 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1874 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1875 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1876 | struct btrfs_device *next_device; |
| 1877 | |
| 1878 | if (this_dev) |
| 1879 | next_device = this_dev; |
| 1880 | else |
| 1881 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 1882 | device); |
| 1883 | ASSERT(next_device); |
| 1884 | |
| 1885 | if (fs_info->sb->s_bdev && |
| 1886 | (fs_info->sb->s_bdev == device->bdev)) |
| 1887 | fs_info->sb->s_bdev = next_device->bdev; |
| 1888 | |
| 1889 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 1890 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1891 | } |
| 1892 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1893 | /* |
| 1894 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 1895 | * currently replaced. |
| 1896 | */ |
| 1897 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 1898 | { |
| 1899 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 1900 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1901 | down_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1902 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 1903 | ASSERT(num_devices > 1); |
| 1904 | num_devices--; |
| 1905 | } |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 1906 | up_read(&fs_info->dev_replace.rwsem); |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1907 | |
| 1908 | return num_devices; |
| 1909 | } |
| 1910 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1911 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 1912 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1913 | { |
| 1914 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1915 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1916 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1917 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1918 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1919 | |
| 1920 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1921 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1922 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1923 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1924 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1925 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1926 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1927 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 1928 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 1929 | |
| 1930 | if (IS_ERR(device)) { |
| 1931 | if (PTR_ERR(device) == -ENOENT && |
| 1932 | strcmp(device_path, "missing") == 0) |
| 1933 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 1934 | else |
| 1935 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1936 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 1937 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1938 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 1939 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 1940 | btrfs_warn_in_rcu(fs_info, |
| 1941 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 1942 | rcu_str_deref(device->name), device->devid); |
| 1943 | ret = -ETXTBSY; |
| 1944 | goto out; |
| 1945 | } |
| 1946 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1947 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1948 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1949 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1950 | } |
| 1951 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1952 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 1953 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1954 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1955 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1956 | } |
| 1957 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1958 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 1959 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1960 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1961 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 1962 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1963 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1964 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1965 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1966 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1967 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1968 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1969 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1970 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1971 | /* |
| 1972 | * TODO: the superblock still includes this device in its num_devices |
| 1973 | * counter although write_all_supers() is not locked out. This |
| 1974 | * could give a filesystem state which requires a degraded mount. |
| 1975 | */ |
David Sterba | f331a95 | 2019-03-20 16:31:53 +0100 | [diff] [blame] | 1976 | ret = btrfs_rm_dev_item(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1977 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1978 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1979 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1980 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
David Sterba | 163e97e | 2019-03-20 16:32:55 +0100 | [diff] [blame] | 1981 | btrfs_scrub_cancel_dev(device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1982 | |
| 1983 | /* |
| 1984 | * the device list mutex makes sure that we don't change |
| 1985 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1986 | * the device supers. Whoever is writing all supers, should |
| 1987 | * lock the device list mutex before getting the number of |
| 1988 | * devices in the super block (super_copy). Conversely, |
| 1989 | * whoever updates the number of devices in the super block |
| 1990 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1991 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1992 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 1993 | /* |
| 1994 | * In normal cases the cur_devices == fs_devices. But in case |
| 1995 | * of deleting a seed device, the cur_devices should point to |
| 1996 | * its own fs_devices listed under the fs_devices->seed. |
| 1997 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1998 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1999 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2000 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2001 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2002 | cur_devices->num_devices--; |
| 2003 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2004 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2005 | if (cur_devices != fs_devices) |
| 2006 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2007 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2008 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2009 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2010 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2011 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2012 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2013 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2014 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2015 | /* remove sysfs entry */ |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2016 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2017 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2018 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2019 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2020 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2021 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2022 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2023 | /* |
| 2024 | * at this point, the device is zero sized and detached from |
| 2025 | * the devices list. All that's left is to zero out the old |
| 2026 | * supers and free the device. |
| 2027 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2028 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2029 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 2030 | |
| 2031 | btrfs_close_bdev(device); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2032 | synchronize_rcu(); |
| 2033 | btrfs_free_device(device); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2034 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2035 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2036 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2037 | if (fs_devices->seed == cur_devices) { |
| 2038 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2039 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2040 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2041 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2042 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2043 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2044 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2045 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2046 | } |
| 2047 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2048 | out: |
| 2049 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2050 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2051 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2052 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2053 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2054 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2055 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2056 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2057 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2058 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2059 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2060 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2061 | } |
| 2062 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2063 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2064 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2065 | struct btrfs_fs_devices *fs_devices; |
| 2066 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2067 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2068 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2069 | /* |
| 2070 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2071 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2072 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2073 | * srcdev will have its correct fs_devices in both the cases. |
| 2074 | */ |
| 2075 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2076 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2077 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2078 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2079 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2080 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2081 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2082 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2083 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2084 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2085 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2086 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2087 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2088 | } |
| 2089 | |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2090 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev) |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2091 | { |
David Sterba | 65237ee | 2019-03-20 16:34:54 +0100 | [diff] [blame] | 2092 | struct btrfs_fs_info *fs_info = srcdev->fs_info; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2093 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2094 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2095 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2096 | /* zero out the old super if it is writable */ |
| 2097 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2098 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2099 | |
| 2100 | btrfs_close_bdev(srcdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2101 | synchronize_rcu(); |
| 2102 | btrfs_free_device(srcdev); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2103 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2104 | /* if this is no devs we rather delete the fs_devices */ |
| 2105 | if (!fs_devices->num_devices) { |
| 2106 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2107 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2108 | /* |
| 2109 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2110 | * seed. In case of a seed device being replaced, the replace |
| 2111 | * target added to the sprout FS, so there will be no more |
| 2112 | * device left under the seed FS. |
| 2113 | */ |
| 2114 | ASSERT(fs_devices->seeding); |
| 2115 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2116 | tmp_fs_devices = fs_info->fs_devices; |
| 2117 | while (tmp_fs_devices) { |
| 2118 | if (tmp_fs_devices->seed == fs_devices) { |
| 2119 | tmp_fs_devices->seed = fs_devices->seed; |
| 2120 | break; |
| 2121 | } |
| 2122 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2123 | } |
| 2124 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2125 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2126 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2127 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2128 | } |
| 2129 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2130 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2131 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2132 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2133 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2134 | mutex_lock(&fs_devices->device_list_mutex); |
| 2135 | |
| 2136 | btrfs_sysfs_rm_device_link(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2137 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2138 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2139 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2140 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2141 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2142 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2143 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2144 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2145 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2146 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2147 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2148 | |
| 2149 | /* |
| 2150 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2151 | * may lead to a call to btrfs_show_devname() which will try |
| 2152 | * to hold device_list_mutex. And here this device |
| 2153 | * is already out of device list, so we don't have to hold |
| 2154 | * the device_list_mutex lock. |
| 2155 | */ |
| 2156 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2157 | |
| 2158 | btrfs_close_bdev(tgtdev); |
Nikolay Borisov | 8e75fd8 | 2019-03-27 14:24:11 +0200 | [diff] [blame] | 2159 | synchronize_rcu(); |
| 2160 | btrfs_free_device(tgtdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2163 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2164 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2165 | { |
| 2166 | int ret = 0; |
| 2167 | struct btrfs_super_block *disk_super; |
| 2168 | u64 devid; |
| 2169 | u8 *dev_uuid; |
| 2170 | struct block_device *bdev; |
| 2171 | struct buffer_head *bh; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2172 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2173 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2174 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2175 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2176 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2177 | return ERR_PTR(ret); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2178 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2179 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2180 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2181 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2182 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2183 | disk_super->metadata_uuid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2184 | else |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2185 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2186 | disk_super->fsid, true); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2187 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2188 | brelse(bh); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2189 | if (!device) |
| 2190 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2191 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2192 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2193 | } |
| 2194 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2195 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2196 | * Lookup a device given by device id, or the path if the id is 0. |
| 2197 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2198 | struct btrfs_device *btrfs_find_device_by_devspec( |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2199 | struct btrfs_fs_info *fs_info, u64 devid, |
| 2200 | const char *device_path) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2201 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2202 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2203 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2204 | if (devid) { |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2205 | device = btrfs_find_device(fs_info->fs_devices, devid, NULL, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2206 | NULL, true); |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2207 | if (!device) |
| 2208 | return ERR_PTR(-ENOENT); |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2209 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2210 | } |
Anand Jain | 6e927ce | 2019-01-17 23:32:29 +0800 | [diff] [blame] | 2211 | |
| 2212 | if (!device_path || !device_path[0]) |
| 2213 | return ERR_PTR(-EINVAL); |
| 2214 | |
| 2215 | if (strcmp(device_path, "missing") == 0) { |
| 2216 | /* Find first missing device */ |
| 2217 | list_for_each_entry(device, &fs_info->fs_devices->devices, |
| 2218 | dev_list) { |
| 2219 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2220 | &device->dev_state) && !device->bdev) |
| 2221 | return device; |
| 2222 | } |
| 2223 | return ERR_PTR(-ENOENT); |
| 2224 | } |
| 2225 | |
| 2226 | return btrfs_find_device_by_path(fs_info, device_path); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2227 | } |
| 2228 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2229 | /* |
| 2230 | * does all the dirty work required for changing file system's UUID. |
| 2231 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2232 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2233 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2234 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2235 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2236 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2237 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2238 | struct btrfs_device *device; |
| 2239 | u64 super_flags; |
| 2240 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2241 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2242 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2243 | return -EINVAL; |
| 2244 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2245 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2246 | if (IS_ERR(seed_devices)) |
| 2247 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2248 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2249 | old_devices = clone_fs_devices(fs_devices); |
| 2250 | if (IS_ERR(old_devices)) { |
| 2251 | kfree(seed_devices); |
| 2252 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2253 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2254 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2255 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2256 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2257 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2258 | seed_devices->opened = 1; |
| 2259 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2260 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2261 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2262 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2263 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2264 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2265 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2266 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2267 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2268 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2269 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2270 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2271 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2272 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 2273 | fs_devices->seeding = false; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2274 | fs_devices->num_devices = 0; |
| 2275 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2276 | fs_devices->missing_devices = 0; |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2277 | fs_devices->rotating = false; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2278 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2279 | |
| 2280 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2281 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2282 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2283 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2284 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2285 | super_flags = btrfs_super_flags(disk_super) & |
| 2286 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2287 | btrfs_set_super_flags(disk_super, super_flags); |
| 2288 | |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
| 2292 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2293 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2294 | */ |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2295 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2296 | { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2297 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2298 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2299 | struct btrfs_path *path; |
| 2300 | struct extent_buffer *leaf; |
| 2301 | struct btrfs_dev_item *dev_item; |
| 2302 | struct btrfs_device *device; |
| 2303 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2304 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2305 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2306 | u64 devid; |
| 2307 | int ret; |
| 2308 | |
| 2309 | path = btrfs_alloc_path(); |
| 2310 | if (!path) |
| 2311 | return -ENOMEM; |
| 2312 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2313 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2314 | key.offset = 0; |
| 2315 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2316 | |
| 2317 | while (1) { |
| 2318 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2319 | if (ret < 0) |
| 2320 | goto error; |
| 2321 | |
| 2322 | leaf = path->nodes[0]; |
| 2323 | next_slot: |
| 2324 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2325 | ret = btrfs_next_leaf(root, path); |
| 2326 | if (ret > 0) |
| 2327 | break; |
| 2328 | if (ret < 0) |
| 2329 | goto error; |
| 2330 | leaf = path->nodes[0]; |
| 2331 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2332 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2333 | continue; |
| 2334 | } |
| 2335 | |
| 2336 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2337 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2338 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2339 | break; |
| 2340 | |
| 2341 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2342 | struct btrfs_dev_item); |
| 2343 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2344 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2345 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2346 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2347 | BTRFS_FSID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 2348 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 2349 | fs_uuid, true); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2350 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2351 | |
| 2352 | if (device->fs_devices->seeding) { |
| 2353 | btrfs_set_device_generation(leaf, dev_item, |
| 2354 | device->generation); |
| 2355 | btrfs_mark_buffer_dirty(leaf); |
| 2356 | } |
| 2357 | |
| 2358 | path->slots[0]++; |
| 2359 | goto next_slot; |
| 2360 | } |
| 2361 | ret = 0; |
| 2362 | error: |
| 2363 | btrfs_free_path(path); |
| 2364 | return ret; |
| 2365 | } |
| 2366 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2367 | 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] | 2368 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2369 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2370 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2371 | struct btrfs_trans_handle *trans; |
| 2372 | struct btrfs_device *device; |
| 2373 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2374 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2375 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2376 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2377 | u64 orig_super_total_bytes; |
| 2378 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2379 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2380 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2381 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2382 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2383 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2384 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2385 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2386 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2387 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2388 | if (IS_ERR(bdev)) |
| 2389 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2390 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2391 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2392 | seeding_dev = 1; |
| 2393 | down_write(&sb->s_umount); |
| 2394 | mutex_lock(&uuid_mutex); |
| 2395 | } |
| 2396 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2397 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2398 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2399 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2400 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2401 | if (device->bdev == bdev) { |
| 2402 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2403 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2404 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2405 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2406 | } |
| 2407 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2408 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2409 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2410 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2411 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2412 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2413 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2414 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2415 | } |
| 2416 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2417 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2418 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2419 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2420 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2421 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2422 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2423 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2424 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2425 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2426 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2427 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2430 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2431 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2432 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2433 | device->io_width = fs_info->sectorsize; |
| 2434 | device->io_align = fs_info->sectorsize; |
| 2435 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2436 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2437 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2438 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2439 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2440 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2441 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2442 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2443 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2444 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2445 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2446 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2447 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2448 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2449 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2450 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2451 | if (ret) { |
| 2452 | btrfs_abort_transaction(trans, ret); |
| 2453 | goto error_trans; |
| 2454 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2455 | } |
| 2456 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2457 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2458 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2459 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2460 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2461 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2462 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2463 | fs_devices->num_devices++; |
| 2464 | fs_devices->open_devices++; |
| 2465 | fs_devices->rw_devices++; |
| 2466 | fs_devices->total_devices++; |
| 2467 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2468 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2469 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2470 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2471 | if (!blk_queue_nonrot(q)) |
Johannes Thumshirn | 7f0432d | 2019-11-13 11:27:28 +0100 | [diff] [blame] | 2472 | fs_devices->rotating = true; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2473 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2474 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2475 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2476 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2477 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2478 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2479 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2480 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2481 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2482 | |
| 2483 | /* add sysfs device entry */ |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2484 | btrfs_sysfs_add_device_link(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2485 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2486 | /* |
| 2487 | * we've got more storage, clear any full flags on the space |
| 2488 | * infos |
| 2489 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2490 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2491 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2492 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2493 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2494 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2495 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2496 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 2497 | ret = init_first_rw_device(trans); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2498 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2499 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2500 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2501 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2502 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2503 | } |
| 2504 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2505 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2506 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2507 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2508 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | if (seeding_dev) { |
David Sterba | 5c466629 | 2019-03-20 16:36:39 +0100 | [diff] [blame] | 2512 | ret = btrfs_finish_sprout(trans); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2513 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2514 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2515 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2516 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2517 | |
David Sterba | f93c399 | 2019-08-01 18:50:16 +0200 | [diff] [blame] | 2518 | btrfs_sysfs_update_sprout_fsid(fs_devices, |
| 2519 | fs_info->fs_devices->fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2520 | } |
| 2521 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2522 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2523 | |
| 2524 | if (seeding_dev) { |
| 2525 | mutex_unlock(&uuid_mutex); |
| 2526 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2527 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2528 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2529 | if (ret) /* transaction commit */ |
| 2530 | return ret; |
| 2531 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2532 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2533 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2534 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2535 | "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] | 2536 | trans = btrfs_attach_transaction(root); |
| 2537 | if (IS_ERR(trans)) { |
| 2538 | if (PTR_ERR(trans) == -ENOENT) |
| 2539 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2540 | ret = PTR_ERR(trans); |
| 2541 | trans = NULL; |
| 2542 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2543 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2544 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2545 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2546 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2547 | /* Update ctime/mtime for libblkid */ |
| 2548 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2549 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2550 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2551 | error_sysfs: |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2552 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2553 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2554 | mutex_lock(&fs_info->chunk_mutex); |
| 2555 | list_del_rcu(&device->dev_list); |
| 2556 | list_del(&device->dev_alloc_list); |
| 2557 | fs_info->fs_devices->num_devices--; |
| 2558 | fs_info->fs_devices->open_devices--; |
| 2559 | fs_info->fs_devices->rw_devices--; |
| 2560 | fs_info->fs_devices->total_devices--; |
| 2561 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2562 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2563 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2564 | orig_super_total_bytes); |
| 2565 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2566 | orig_super_num_devices); |
| 2567 | mutex_unlock(&fs_info->chunk_mutex); |
| 2568 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2569 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2570 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2571 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2572 | if (trans) |
| 2573 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2574 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2575 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2576 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2577 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2578 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2579 | mutex_unlock(&uuid_mutex); |
| 2580 | up_write(&sb->s_umount); |
| 2581 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2582 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2583 | } |
| 2584 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2585 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2586 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2587 | { |
| 2588 | int ret; |
| 2589 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2590 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2591 | struct btrfs_dev_item *dev_item; |
| 2592 | struct extent_buffer *leaf; |
| 2593 | struct btrfs_key key; |
| 2594 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2595 | path = btrfs_alloc_path(); |
| 2596 | if (!path) |
| 2597 | return -ENOMEM; |
| 2598 | |
| 2599 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2600 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2601 | key.offset = device->devid; |
| 2602 | |
| 2603 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2604 | if (ret < 0) |
| 2605 | goto out; |
| 2606 | |
| 2607 | if (ret > 0) { |
| 2608 | ret = -ENOENT; |
| 2609 | goto out; |
| 2610 | } |
| 2611 | |
| 2612 | leaf = path->nodes[0]; |
| 2613 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2614 | |
| 2615 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2616 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2617 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2618 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2619 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2620 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2621 | btrfs_device_get_disk_total_bytes(device)); |
| 2622 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2623 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2624 | btrfs_mark_buffer_dirty(leaf); |
| 2625 | |
| 2626 | out: |
| 2627 | btrfs_free_path(path); |
| 2628 | return ret; |
| 2629 | } |
| 2630 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2631 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2632 | struct btrfs_device *device, u64 new_size) |
| 2633 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2634 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2635 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2636 | u64 old_total; |
| 2637 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2638 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2639 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2640 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2641 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2642 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2643 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2644 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2645 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2646 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2647 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2648 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2649 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2650 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2651 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2652 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2653 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2654 | btrfs_set_super_total_bytes(super_copy, |
| 2655 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2656 | device->fs_devices->total_rw_bytes += diff; |
| 2657 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2658 | btrfs_device_set_total_bytes(device, new_size); |
| 2659 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2660 | btrfs_clear_space_info_full(device->fs_info); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 2661 | if (list_empty(&device->post_commit_list)) |
| 2662 | list_add_tail(&device->post_commit_list, |
| 2663 | &trans->transaction->dev_update_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2664 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2665 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2666 | return btrfs_update_device(trans, device); |
| 2667 | } |
| 2668 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2669 | 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] | 2670 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2671 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2672 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2673 | int ret; |
| 2674 | struct btrfs_path *path; |
| 2675 | struct btrfs_key key; |
| 2676 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2677 | path = btrfs_alloc_path(); |
| 2678 | if (!path) |
| 2679 | return -ENOMEM; |
| 2680 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2681 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2682 | key.offset = chunk_offset; |
| 2683 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2684 | |
| 2685 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2686 | if (ret < 0) |
| 2687 | goto out; |
| 2688 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2689 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2690 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2691 | ret = -ENOENT; |
| 2692 | goto out; |
| 2693 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2694 | |
| 2695 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2696 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2697 | btrfs_handle_fs_error(fs_info, ret, |
| 2698 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2699 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2700 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2701 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2702 | } |
| 2703 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2704 | 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] | 2705 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2706 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2707 | struct btrfs_disk_key *disk_key; |
| 2708 | struct btrfs_chunk *chunk; |
| 2709 | u8 *ptr; |
| 2710 | int ret = 0; |
| 2711 | u32 num_stripes; |
| 2712 | u32 array_size; |
| 2713 | u32 len = 0; |
| 2714 | u32 cur; |
| 2715 | struct btrfs_key key; |
| 2716 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2717 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2718 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2719 | |
| 2720 | ptr = super_copy->sys_chunk_array; |
| 2721 | cur = 0; |
| 2722 | |
| 2723 | while (cur < array_size) { |
| 2724 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2725 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2726 | |
| 2727 | len = sizeof(*disk_key); |
| 2728 | |
| 2729 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2730 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2731 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2732 | len += btrfs_chunk_item_size(num_stripes); |
| 2733 | } else { |
| 2734 | ret = -EIO; |
| 2735 | break; |
| 2736 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2737 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2738 | key.offset == chunk_offset) { |
| 2739 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2740 | array_size -= len; |
| 2741 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2742 | } else { |
| 2743 | ptr += len; |
| 2744 | cur += len; |
| 2745 | } |
| 2746 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2747 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2748 | return ret; |
| 2749 | } |
| 2750 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2751 | /* |
| 2752 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2753 | * @logical: Logical block offset in bytes. |
| 2754 | * @length: Length of extent in bytes. |
| 2755 | * |
| 2756 | * Return: Chunk mapping or ERR_PTR. |
| 2757 | */ |
| 2758 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2759 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2760 | { |
| 2761 | struct extent_map_tree *em_tree; |
| 2762 | struct extent_map *em; |
| 2763 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 2764 | em_tree = &fs_info->mapping_tree; |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2765 | read_lock(&em_tree->lock); |
| 2766 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2767 | read_unlock(&em_tree->lock); |
| 2768 | |
| 2769 | if (!em) { |
| 2770 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2771 | logical, length); |
| 2772 | return ERR_PTR(-EINVAL); |
| 2773 | } |
| 2774 | |
| 2775 | if (em->start > logical || em->start + em->len < logical) { |
| 2776 | btrfs_crit(fs_info, |
| 2777 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2778 | logical, length, em->start, em->start + em->len); |
| 2779 | free_extent_map(em); |
| 2780 | return ERR_PTR(-EINVAL); |
| 2781 | } |
| 2782 | |
| 2783 | /* callers are responsible for dropping em's ref. */ |
| 2784 | return em; |
| 2785 | } |
| 2786 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2787 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2788 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2789 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2790 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2791 | struct map_lookup *map; |
| 2792 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2793 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2794 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2795 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2796 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2797 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2798 | /* |
| 2799 | * 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] | 2800 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2801 | * do anything we still error out. |
| 2802 | */ |
| 2803 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2804 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2805 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2806 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2807 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2808 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2809 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2810 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2811 | /* |
| 2812 | * Take the device list mutex to prevent races with the final phase of |
| 2813 | * a device replace operation that replaces the device object associated |
| 2814 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2815 | */ |
| 2816 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2817 | for (i = 0; i < map->num_stripes; i++) { |
| 2818 | struct btrfs_device *device = map->stripes[i].dev; |
| 2819 | ret = btrfs_free_dev_extent(trans, device, |
| 2820 | map->stripes[i].physical, |
| 2821 | &dev_extent_len); |
| 2822 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2823 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2824 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2825 | goto out; |
| 2826 | } |
| 2827 | |
| 2828 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2829 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2830 | btrfs_device_set_bytes_used(device, |
| 2831 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2832 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2833 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2834 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2835 | } |
| 2836 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 2837 | ret = btrfs_update_device(trans, device); |
| 2838 | if (ret) { |
| 2839 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2840 | btrfs_abort_transaction(trans, ret); |
| 2841 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2842 | } |
| 2843 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2844 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2845 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2846 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2847 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2848 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2849 | goto out; |
| 2850 | } |
| 2851 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2852 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2853 | |
| 2854 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2855 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2856 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2857 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2858 | goto out; |
| 2859 | } |
| 2860 | } |
| 2861 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 2862 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2863 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2864 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2865 | goto out; |
| 2866 | } |
| 2867 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2868 | out: |
| 2869 | /* once for us */ |
| 2870 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2871 | return ret; |
| 2872 | } |
| 2873 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2874 | 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] | 2875 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2876 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2877 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2878 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2879 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2880 | /* |
| 2881 | * Prevent races with automatic removal of unused block groups. |
| 2882 | * After we relocate and before we remove the chunk with offset |
| 2883 | * chunk_offset, automatic removal of the block group can kick in, |
| 2884 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2885 | * |
| 2886 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2887 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2888 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2889 | * we release the path used to search the chunk/dev tree and before |
| 2890 | * the current task acquires this mutex and calls us. |
| 2891 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2892 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2893 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2894 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2895 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2896 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2897 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2898 | if (ret) |
| 2899 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2900 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2901 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 2902 | chunk_offset); |
| 2903 | if (IS_ERR(trans)) { |
| 2904 | ret = PTR_ERR(trans); |
| 2905 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 2906 | return ret; |
| 2907 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 2908 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2909 | /* |
| 2910 | * step two, delete the device extents and the |
| 2911 | * chunk tree entries |
| 2912 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2913 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2914 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2915 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2916 | } |
| 2917 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2918 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2919 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2920 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2921 | struct btrfs_path *path; |
| 2922 | struct extent_buffer *leaf; |
| 2923 | struct btrfs_chunk *chunk; |
| 2924 | struct btrfs_key key; |
| 2925 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2926 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2927 | bool retried = false; |
| 2928 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2929 | int ret; |
| 2930 | |
| 2931 | path = btrfs_alloc_path(); |
| 2932 | if (!path) |
| 2933 | return -ENOMEM; |
| 2934 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2935 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2936 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2937 | key.offset = (u64)-1; |
| 2938 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2939 | |
| 2940 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2941 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2942 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2943 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2944 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2945 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2946 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2947 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2948 | |
| 2949 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2950 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2951 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2952 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2953 | if (ret < 0) |
| 2954 | goto error; |
| 2955 | if (ret > 0) |
| 2956 | break; |
| 2957 | |
| 2958 | leaf = path->nodes[0]; |
| 2959 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2960 | |
| 2961 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2962 | struct btrfs_chunk); |
| 2963 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2964 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2965 | |
| 2966 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2967 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2968 | if (ret == -ENOSPC) |
| 2969 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2970 | else |
| 2971 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2972 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2973 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2974 | |
| 2975 | if (found_key.offset == 0) |
| 2976 | break; |
| 2977 | key.offset = found_key.offset - 1; |
| 2978 | } |
| 2979 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2980 | if (failed && !retried) { |
| 2981 | failed = 0; |
| 2982 | retried = true; |
| 2983 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2984 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2985 | ret = -ENOSPC; |
| 2986 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2987 | error: |
| 2988 | btrfs_free_path(path); |
| 2989 | return ret; |
| 2990 | } |
| 2991 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 2992 | /* |
| 2993 | * return 1 : allocate a data chunk successfully, |
| 2994 | * return <0: errors during allocating a data chunk, |
| 2995 | * return 0 : no need to allocate a data chunk. |
| 2996 | */ |
| 2997 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 2998 | u64 chunk_offset) |
| 2999 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3000 | struct btrfs_block_group *cache; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3001 | u64 bytes_used; |
| 3002 | u64 chunk_type; |
| 3003 | |
| 3004 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3005 | ASSERT(cache); |
| 3006 | chunk_type = cache->flags; |
| 3007 | btrfs_put_block_group(cache); |
| 3008 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3009 | if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA)) |
| 3010 | return 0; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3011 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3012 | spin_lock(&fs_info->data_sinfo->lock); |
| 3013 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3014 | spin_unlock(&fs_info->data_sinfo->lock); |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3015 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3016 | if (!bytes_used) { |
| 3017 | struct btrfs_trans_handle *trans; |
| 3018 | int ret; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3019 | |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3020 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3021 | if (IS_ERR(trans)) |
| 3022 | return PTR_ERR(trans); |
| 3023 | |
| 3024 | ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA); |
| 3025 | btrfs_end_transaction(trans); |
| 3026 | if (ret < 0) |
| 3027 | return ret; |
| 3028 | return 1; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3029 | } |
Johannes Thumshirn | 5ae2169 | 2019-10-18 11:58:22 +0200 | [diff] [blame] | 3030 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3031 | return 0; |
| 3032 | } |
| 3033 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3034 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3035 | struct btrfs_balance_control *bctl) |
| 3036 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3037 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3038 | struct btrfs_trans_handle *trans; |
| 3039 | struct btrfs_balance_item *item; |
| 3040 | struct btrfs_disk_balance_args disk_bargs; |
| 3041 | struct btrfs_path *path; |
| 3042 | struct extent_buffer *leaf; |
| 3043 | struct btrfs_key key; |
| 3044 | int ret, err; |
| 3045 | |
| 3046 | path = btrfs_alloc_path(); |
| 3047 | if (!path) |
| 3048 | return -ENOMEM; |
| 3049 | |
| 3050 | trans = btrfs_start_transaction(root, 0); |
| 3051 | if (IS_ERR(trans)) { |
| 3052 | btrfs_free_path(path); |
| 3053 | return PTR_ERR(trans); |
| 3054 | } |
| 3055 | |
| 3056 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3057 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3058 | key.offset = 0; |
| 3059 | |
| 3060 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3061 | sizeof(*item)); |
| 3062 | if (ret) |
| 3063 | goto out; |
| 3064 | |
| 3065 | leaf = path->nodes[0]; |
| 3066 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3067 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3068 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3069 | |
| 3070 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3071 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3072 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3073 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3074 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3075 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3076 | |
| 3077 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3078 | |
| 3079 | btrfs_mark_buffer_dirty(leaf); |
| 3080 | out: |
| 3081 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3082 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3083 | if (err && !ret) |
| 3084 | ret = err; |
| 3085 | return ret; |
| 3086 | } |
| 3087 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3088 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3089 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3090 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3091 | struct btrfs_trans_handle *trans; |
| 3092 | struct btrfs_path *path; |
| 3093 | struct btrfs_key key; |
| 3094 | int ret, err; |
| 3095 | |
| 3096 | path = btrfs_alloc_path(); |
| 3097 | if (!path) |
| 3098 | return -ENOMEM; |
| 3099 | |
| 3100 | trans = btrfs_start_transaction(root, 0); |
| 3101 | if (IS_ERR(trans)) { |
| 3102 | btrfs_free_path(path); |
| 3103 | return PTR_ERR(trans); |
| 3104 | } |
| 3105 | |
| 3106 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3107 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3108 | key.offset = 0; |
| 3109 | |
| 3110 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3111 | if (ret < 0) |
| 3112 | goto out; |
| 3113 | if (ret > 0) { |
| 3114 | ret = -ENOENT; |
| 3115 | goto out; |
| 3116 | } |
| 3117 | |
| 3118 | ret = btrfs_del_item(trans, root, path); |
| 3119 | out: |
| 3120 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3121 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3122 | if (err && !ret) |
| 3123 | ret = err; |
| 3124 | return ret; |
| 3125 | } |
| 3126 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3127 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3128 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3129 | * resume after balance was interrupted. |
| 3130 | */ |
| 3131 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3132 | { |
| 3133 | /* |
| 3134 | * Turn on soft mode for chunk types that were being converted. |
| 3135 | */ |
| 3136 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3137 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3138 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3139 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3140 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3141 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3142 | |
| 3143 | /* |
| 3144 | * Turn on usage filter if is not already used. The idea is |
| 3145 | * that chunks that we have already balanced should be |
| 3146 | * reasonably full. Don't do it for chunks that are being |
| 3147 | * converted - that will keep us from relocating unconverted |
| 3148 | * (albeit full) chunks. |
| 3149 | */ |
| 3150 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3151 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3152 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3153 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3154 | bctl->data.usage = 90; |
| 3155 | } |
| 3156 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3157 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3158 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3159 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3160 | bctl->sys.usage = 90; |
| 3161 | } |
| 3162 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3163 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3164 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3165 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3166 | bctl->meta.usage = 90; |
| 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3171 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3172 | */ |
| 3173 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3174 | { |
| 3175 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3176 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3177 | |
| 3178 | BUG_ON(!fs_info->balance_ctl); |
| 3179 | |
| 3180 | spin_lock(&fs_info->balance_lock); |
| 3181 | fs_info->balance_ctl = NULL; |
| 3182 | spin_unlock(&fs_info->balance_lock); |
| 3183 | |
| 3184 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3185 | ret = del_balance_item(fs_info); |
| 3186 | if (ret) |
| 3187 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3188 | } |
| 3189 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3190 | /* |
| 3191 | * Balance filters. Return 1 if chunk should be filtered out |
| 3192 | * (should not be balanced). |
| 3193 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3194 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3195 | struct btrfs_balance_args *bargs) |
| 3196 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3197 | chunk_type = chunk_to_extended(chunk_type) & |
| 3198 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3199 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3200 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3201 | return 0; |
| 3202 | |
| 3203 | return 1; |
| 3204 | } |
| 3205 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3206 | 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] | 3207 | struct btrfs_balance_args *bargs) |
| 3208 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3209 | struct btrfs_block_group *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3210 | u64 chunk_used; |
| 3211 | u64 user_thresh_min; |
| 3212 | u64 user_thresh_max; |
| 3213 | int ret = 1; |
| 3214 | |
| 3215 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3216 | chunk_used = cache->used; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3217 | |
| 3218 | if (bargs->usage_min == 0) |
| 3219 | user_thresh_min = 0; |
| 3220 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3221 | user_thresh_min = div_factor_fine(cache->length, |
| 3222 | bargs->usage_min); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3223 | |
| 3224 | if (bargs->usage_max == 0) |
| 3225 | user_thresh_max = 1; |
| 3226 | else if (bargs->usage_max > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3227 | user_thresh_max = cache->length; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3228 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3229 | user_thresh_max = div_factor_fine(cache->length, |
| 3230 | bargs->usage_max); |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3231 | |
| 3232 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3233 | ret = 0; |
| 3234 | |
| 3235 | btrfs_put_block_group(cache); |
| 3236 | return ret; |
| 3237 | } |
| 3238 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3239 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3240 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3241 | { |
David Sterba | 32da5386 | 2019-10-29 19:20:18 +0100 | [diff] [blame] | 3242 | struct btrfs_block_group *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3243 | u64 chunk_used, user_thresh; |
| 3244 | int ret = 1; |
| 3245 | |
| 3246 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
David Sterba | bf38be6 | 2019-10-23 18:48:11 +0200 | [diff] [blame] | 3247 | chunk_used = cache->used; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3248 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3249 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3250 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3251 | else if (bargs->usage > 100) |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3252 | user_thresh = cache->length; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3253 | else |
David Sterba | b3470b5 | 2019-10-23 18:48:22 +0200 | [diff] [blame] | 3254 | user_thresh = div_factor_fine(cache->length, bargs->usage); |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3255 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3256 | if (chunk_used < user_thresh) |
| 3257 | ret = 0; |
| 3258 | |
| 3259 | btrfs_put_block_group(cache); |
| 3260 | return ret; |
| 3261 | } |
| 3262 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3263 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3264 | struct btrfs_chunk *chunk, |
| 3265 | struct btrfs_balance_args *bargs) |
| 3266 | { |
| 3267 | struct btrfs_stripe *stripe; |
| 3268 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3269 | int i; |
| 3270 | |
| 3271 | for (i = 0; i < num_stripes; i++) { |
| 3272 | stripe = btrfs_stripe_nr(chunk, i); |
| 3273 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3274 | return 0; |
| 3275 | } |
| 3276 | |
| 3277 | return 1; |
| 3278 | } |
| 3279 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3280 | static u64 calc_data_stripes(u64 type, int num_stripes) |
| 3281 | { |
| 3282 | const int index = btrfs_bg_flags_to_raid_index(type); |
| 3283 | const int ncopies = btrfs_raid_array[index].ncopies; |
| 3284 | const int nparity = btrfs_raid_array[index].nparity; |
| 3285 | |
| 3286 | if (nparity) |
| 3287 | return num_stripes - nparity; |
| 3288 | else |
| 3289 | return num_stripes / ncopies; |
| 3290 | } |
| 3291 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3292 | /* [pstart, pend) */ |
| 3293 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3294 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3295 | struct btrfs_balance_args *bargs) |
| 3296 | { |
| 3297 | struct btrfs_stripe *stripe; |
| 3298 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3299 | u64 stripe_offset; |
| 3300 | u64 stripe_length; |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3301 | u64 type; |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3302 | int factor; |
| 3303 | int i; |
| 3304 | |
| 3305 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3306 | return 0; |
| 3307 | |
David Sterba | 946c925 | 2019-05-17 11:43:34 +0200 | [diff] [blame] | 3308 | type = btrfs_chunk_type(leaf, chunk); |
| 3309 | factor = calc_data_stripes(type, num_stripes); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3310 | |
| 3311 | for (i = 0; i < num_stripes; i++) { |
| 3312 | stripe = btrfs_stripe_nr(chunk, i); |
| 3313 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3314 | continue; |
| 3315 | |
| 3316 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3317 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3318 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3319 | |
| 3320 | if (stripe_offset < bargs->pend && |
| 3321 | stripe_offset + stripe_length > bargs->pstart) |
| 3322 | return 0; |
| 3323 | } |
| 3324 | |
| 3325 | return 1; |
| 3326 | } |
| 3327 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3328 | /* [vstart, vend) */ |
| 3329 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3330 | struct btrfs_chunk *chunk, |
| 3331 | u64 chunk_offset, |
| 3332 | struct btrfs_balance_args *bargs) |
| 3333 | { |
| 3334 | if (chunk_offset < bargs->vend && |
| 3335 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3336 | /* at least part of the chunk is inside this vrange */ |
| 3337 | return 0; |
| 3338 | |
| 3339 | return 1; |
| 3340 | } |
| 3341 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3342 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3343 | struct btrfs_chunk *chunk, |
| 3344 | struct btrfs_balance_args *bargs) |
| 3345 | { |
| 3346 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3347 | |
| 3348 | if (bargs->stripes_min <= num_stripes |
| 3349 | && num_stripes <= bargs->stripes_max) |
| 3350 | return 0; |
| 3351 | |
| 3352 | return 1; |
| 3353 | } |
| 3354 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3355 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3356 | struct btrfs_balance_args *bargs) |
| 3357 | { |
| 3358 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3359 | return 0; |
| 3360 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3361 | chunk_type = chunk_to_extended(chunk_type) & |
| 3362 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3363 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3364 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3365 | return 1; |
| 3366 | |
| 3367 | return 0; |
| 3368 | } |
| 3369 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3370 | static int should_balance_chunk(struct extent_buffer *leaf, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3371 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3372 | { |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3373 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3374 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3375 | struct btrfs_balance_args *bargs = NULL; |
| 3376 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3377 | |
| 3378 | /* type filter */ |
| 3379 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3380 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3381 | return 0; |
| 3382 | } |
| 3383 | |
| 3384 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3385 | bargs = &bctl->data; |
| 3386 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3387 | bargs = &bctl->sys; |
| 3388 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3389 | bargs = &bctl->meta; |
| 3390 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3391 | /* profiles filter */ |
| 3392 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3393 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3394 | return 0; |
| 3395 | } |
| 3396 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3397 | /* usage filter */ |
| 3398 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3399 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3400 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3401 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3402 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3403 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3404 | } |
| 3405 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3406 | /* devid filter */ |
| 3407 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3408 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3409 | return 0; |
| 3410 | } |
| 3411 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3412 | /* drange filter, makes sense only with devid filter */ |
| 3413 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3414 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3415 | return 0; |
| 3416 | } |
| 3417 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3418 | /* vrange filter */ |
| 3419 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3420 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3421 | return 0; |
| 3422 | } |
| 3423 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3424 | /* stripes filter */ |
| 3425 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3426 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3427 | return 0; |
| 3428 | } |
| 3429 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3430 | /* soft profile changing mode */ |
| 3431 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3432 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3433 | return 0; |
| 3434 | } |
| 3435 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3436 | /* |
| 3437 | * limited by count, must be the last filter |
| 3438 | */ |
| 3439 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3440 | if (bargs->limit == 0) |
| 3441 | return 0; |
| 3442 | else |
| 3443 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3444 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3445 | /* |
| 3446 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3447 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3448 | * about the count of all chunks that satisfy the filters. |
| 3449 | */ |
| 3450 | if (bargs->limit_max == 0) |
| 3451 | return 0; |
| 3452 | else |
| 3453 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3454 | } |
| 3455 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3456 | return 1; |
| 3457 | } |
| 3458 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3459 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3460 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3461 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3462 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3463 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3464 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3465 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3466 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3467 | struct btrfs_key found_key; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3468 | struct extent_buffer *leaf; |
| 3469 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3470 | int ret; |
| 3471 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3472 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3473 | /* 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] | 3474 | u64 limit_data = bctl->data.limit; |
| 3475 | u64 limit_meta = bctl->meta.limit; |
| 3476 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3477 | u32 count_data = 0; |
| 3478 | u32 count_meta = 0; |
| 3479 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3480 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3481 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3482 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3483 | if (!path) { |
| 3484 | ret = -ENOMEM; |
| 3485 | goto error; |
| 3486 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3487 | |
| 3488 | /* zero out stat counters */ |
| 3489 | spin_lock(&fs_info->balance_lock); |
| 3490 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3491 | spin_unlock(&fs_info->balance_lock); |
| 3492 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3493 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3494 | /* |
| 3495 | * The single value limit and min/max limits use the same bytes |
| 3496 | * in the |
| 3497 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3498 | bctl->data.limit = limit_data; |
| 3499 | bctl->meta.limit = limit_meta; |
| 3500 | bctl->sys.limit = limit_sys; |
| 3501 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3502 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3503 | key.offset = (u64)-1; |
| 3504 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3505 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3506 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3507 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3508 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3509 | ret = -ECANCELED; |
| 3510 | goto error; |
| 3511 | } |
| 3512 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3513 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3514 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3515 | if (ret < 0) { |
| 3516 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3517 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3518 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3519 | |
| 3520 | /* |
| 3521 | * this shouldn't happen, it means the last relocate |
| 3522 | * failed |
| 3523 | */ |
| 3524 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3525 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3526 | |
| 3527 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3528 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3529 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3530 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3531 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3532 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3533 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3534 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3535 | leaf = path->nodes[0]; |
| 3536 | slot = path->slots[0]; |
| 3537 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3538 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3539 | if (found_key.objectid != key.objectid) { |
| 3540 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3541 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3542 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3543 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3544 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3545 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3546 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3547 | if (!counting) { |
| 3548 | spin_lock(&fs_info->balance_lock); |
| 3549 | bctl->stat.considered++; |
| 3550 | spin_unlock(&fs_info->balance_lock); |
| 3551 | } |
| 3552 | |
David Sterba | 6ec0896 | 2019-03-20 16:38:52 +0100 | [diff] [blame] | 3553 | ret = should_balance_chunk(leaf, chunk, found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3554 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3555 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3556 | if (!ret) { |
| 3557 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3558 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3559 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3560 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3561 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3562 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3563 | spin_lock(&fs_info->balance_lock); |
| 3564 | bctl->stat.expected++; |
| 3565 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3566 | |
| 3567 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3568 | count_data++; |
| 3569 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3570 | count_sys++; |
| 3571 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3572 | count_meta++; |
| 3573 | |
| 3574 | goto loop; |
| 3575 | } |
| 3576 | |
| 3577 | /* |
| 3578 | * Apply limit_min filter, no need to check if the LIMITS |
| 3579 | * filter is used, limit_min is 0 by default |
| 3580 | */ |
| 3581 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3582 | count_data < bctl->data.limit_min) |
| 3583 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3584 | count_meta < bctl->meta.limit_min) |
| 3585 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3586 | count_sys < bctl->sys.limit_min)) { |
| 3587 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3588 | goto loop; |
| 3589 | } |
| 3590 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3591 | if (!chunk_reserved) { |
| 3592 | /* |
| 3593 | * We may be relocating the only data chunk we have, |
| 3594 | * which could potentially end up with losing data's |
| 3595 | * raid profile, so lets allocate an empty one in |
| 3596 | * advance. |
| 3597 | */ |
| 3598 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3599 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3600 | if (ret < 0) { |
| 3601 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3602 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3603 | } else if (ret == 1) { |
| 3604 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3605 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3608 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3609 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3610 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3611 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3612 | } else if (ret == -ETXTBSY) { |
| 3613 | btrfs_info(fs_info, |
| 3614 | "skipping relocation of block group %llu due to active swapfile", |
| 3615 | found_key.offset); |
| 3616 | ret = 0; |
| 3617 | } else if (ret) { |
| 3618 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3619 | } else { |
| 3620 | spin_lock(&fs_info->balance_lock); |
| 3621 | bctl->stat.completed++; |
| 3622 | spin_unlock(&fs_info->balance_lock); |
| 3623 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3624 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3625 | if (found_key.offset == 0) |
| 3626 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3627 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3628 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3629 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3630 | if (counting) { |
| 3631 | btrfs_release_path(path); |
| 3632 | counting = false; |
| 3633 | goto again; |
| 3634 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3635 | error: |
| 3636 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3637 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3638 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3639 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3640 | if (!ret) |
| 3641 | ret = -ENOSPC; |
| 3642 | } |
| 3643 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3644 | return ret; |
| 3645 | } |
| 3646 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3647 | /** |
| 3648 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3649 | * @flags: profile to validate |
| 3650 | * @extended: if true @flags is treated as an extended profile |
| 3651 | */ |
| 3652 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3653 | { |
| 3654 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3655 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3656 | |
| 3657 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3658 | |
| 3659 | /* 1) check that all other bits are zeroed */ |
| 3660 | if (flags & ~mask) |
| 3661 | return 0; |
| 3662 | |
| 3663 | /* 2) see if profile is reduced */ |
| 3664 | if (flags == 0) |
| 3665 | return !extended; /* "0" is valid for usual profiles */ |
| 3666 | |
David Sterba | c149916 | 2019-10-01 19:44:42 +0200 | [diff] [blame] | 3667 | return has_single_bit_set(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3668 | } |
| 3669 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3670 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3671 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3672 | /* cancel requested || normal exit path */ |
| 3673 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3674 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3675 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3678 | /* Non-zero return value signifies invalidity */ |
| 3679 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3680 | u64 allowed) |
| 3681 | { |
| 3682 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3683 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3684 | (bctl_arg->target & ~allowed))); |
| 3685 | } |
| 3686 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3687 | /* |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3688 | * Fill @buf with textual description of balance filter flags @bargs, up to |
| 3689 | * @size_buf including the terminating null. The output may be trimmed if it |
| 3690 | * does not fit into the provided buffer. |
| 3691 | */ |
| 3692 | static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf, |
| 3693 | u32 size_buf) |
| 3694 | { |
| 3695 | int ret; |
| 3696 | u32 size_bp = size_buf; |
| 3697 | char *bp = buf; |
| 3698 | u64 flags = bargs->flags; |
| 3699 | char tmp_buf[128] = {'\0'}; |
| 3700 | |
| 3701 | if (!flags) |
| 3702 | return; |
| 3703 | |
| 3704 | #define CHECK_APPEND_NOARG(a) \ |
| 3705 | do { \ |
| 3706 | ret = snprintf(bp, size_bp, (a)); \ |
| 3707 | if (ret < 0 || ret >= size_bp) \ |
| 3708 | goto out_overflow; \ |
| 3709 | size_bp -= ret; \ |
| 3710 | bp += ret; \ |
| 3711 | } while (0) |
| 3712 | |
| 3713 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3714 | do { \ |
| 3715 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3716 | if (ret < 0 || ret >= size_bp) \ |
| 3717 | goto out_overflow; \ |
| 3718 | size_bp -= ret; \ |
| 3719 | bp += ret; \ |
| 3720 | } while (0) |
| 3721 | |
| 3722 | #define CHECK_APPEND_2ARG(a, v1, v2) \ |
| 3723 | do { \ |
| 3724 | ret = snprintf(bp, size_bp, (a), (v1), (v2)); \ |
| 3725 | if (ret < 0 || ret >= size_bp) \ |
| 3726 | goto out_overflow; \ |
| 3727 | size_bp -= ret; \ |
| 3728 | bp += ret; \ |
| 3729 | } while (0) |
| 3730 | |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3731 | if (flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3732 | CHECK_APPEND_1ARG("convert=%s,", |
| 3733 | btrfs_bg_type_to_raid_name(bargs->target)); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3734 | |
| 3735 | if (flags & BTRFS_BALANCE_ARGS_SOFT) |
| 3736 | CHECK_APPEND_NOARG("soft,"); |
| 3737 | |
| 3738 | if (flags & BTRFS_BALANCE_ARGS_PROFILES) { |
| 3739 | btrfs_describe_block_groups(bargs->profiles, tmp_buf, |
| 3740 | sizeof(tmp_buf)); |
| 3741 | CHECK_APPEND_1ARG("profiles=%s,", tmp_buf); |
| 3742 | } |
| 3743 | |
| 3744 | if (flags & BTRFS_BALANCE_ARGS_USAGE) |
| 3745 | CHECK_APPEND_1ARG("usage=%llu,", bargs->usage); |
| 3746 | |
| 3747 | if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) |
| 3748 | CHECK_APPEND_2ARG("usage=%u..%u,", |
| 3749 | bargs->usage_min, bargs->usage_max); |
| 3750 | |
| 3751 | if (flags & BTRFS_BALANCE_ARGS_DEVID) |
| 3752 | CHECK_APPEND_1ARG("devid=%llu,", bargs->devid); |
| 3753 | |
| 3754 | if (flags & BTRFS_BALANCE_ARGS_DRANGE) |
| 3755 | CHECK_APPEND_2ARG("drange=%llu..%llu,", |
| 3756 | bargs->pstart, bargs->pend); |
| 3757 | |
| 3758 | if (flags & BTRFS_BALANCE_ARGS_VRANGE) |
| 3759 | CHECK_APPEND_2ARG("vrange=%llu..%llu,", |
| 3760 | bargs->vstart, bargs->vend); |
| 3761 | |
| 3762 | if (flags & BTRFS_BALANCE_ARGS_LIMIT) |
| 3763 | CHECK_APPEND_1ARG("limit=%llu,", bargs->limit); |
| 3764 | |
| 3765 | if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) |
| 3766 | CHECK_APPEND_2ARG("limit=%u..%u,", |
| 3767 | bargs->limit_min, bargs->limit_max); |
| 3768 | |
| 3769 | if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) |
| 3770 | CHECK_APPEND_2ARG("stripes=%u..%u,", |
| 3771 | bargs->stripes_min, bargs->stripes_max); |
| 3772 | |
| 3773 | #undef CHECK_APPEND_2ARG |
| 3774 | #undef CHECK_APPEND_1ARG |
| 3775 | #undef CHECK_APPEND_NOARG |
| 3776 | |
| 3777 | out_overflow: |
| 3778 | |
| 3779 | if (size_bp < size_buf) |
| 3780 | buf[size_buf - size_bp - 1] = '\0'; /* remove last , */ |
| 3781 | else |
| 3782 | buf[0] = '\0'; |
| 3783 | } |
| 3784 | |
| 3785 | static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info) |
| 3786 | { |
| 3787 | u32 size_buf = 1024; |
| 3788 | char tmp_buf[192] = {'\0'}; |
| 3789 | char *buf; |
| 3790 | char *bp; |
| 3791 | u32 size_bp = size_buf; |
| 3792 | int ret; |
| 3793 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3794 | |
| 3795 | buf = kzalloc(size_buf, GFP_KERNEL); |
| 3796 | if (!buf) |
| 3797 | return; |
| 3798 | |
| 3799 | bp = buf; |
| 3800 | |
| 3801 | #define CHECK_APPEND_1ARG(a, v1) \ |
| 3802 | do { \ |
| 3803 | ret = snprintf(bp, size_bp, (a), (v1)); \ |
| 3804 | if (ret < 0 || ret >= size_bp) \ |
| 3805 | goto out_overflow; \ |
| 3806 | size_bp -= ret; \ |
| 3807 | bp += ret; \ |
| 3808 | } while (0) |
| 3809 | |
| 3810 | if (bctl->flags & BTRFS_BALANCE_FORCE) |
| 3811 | CHECK_APPEND_1ARG("%s", "-f "); |
| 3812 | |
| 3813 | if (bctl->flags & BTRFS_BALANCE_DATA) { |
| 3814 | describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf)); |
| 3815 | CHECK_APPEND_1ARG("-d%s ", tmp_buf); |
| 3816 | } |
| 3817 | |
| 3818 | if (bctl->flags & BTRFS_BALANCE_METADATA) { |
| 3819 | describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf)); |
| 3820 | CHECK_APPEND_1ARG("-m%s ", tmp_buf); |
| 3821 | } |
| 3822 | |
| 3823 | if (bctl->flags & BTRFS_BALANCE_SYSTEM) { |
| 3824 | describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf)); |
| 3825 | CHECK_APPEND_1ARG("-s%s ", tmp_buf); |
| 3826 | } |
| 3827 | |
| 3828 | #undef CHECK_APPEND_1ARG |
| 3829 | |
| 3830 | out_overflow: |
| 3831 | |
| 3832 | if (size_bp < size_buf) |
| 3833 | buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */ |
| 3834 | btrfs_info(fs_info, "balance: %s %s", |
| 3835 | (bctl->flags & BTRFS_BALANCE_RESUME) ? |
| 3836 | "resume" : "start", buf); |
| 3837 | |
| 3838 | kfree(buf); |
| 3839 | } |
| 3840 | |
| 3841 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3842 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3843 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3844 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 3845 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3846 | struct btrfs_ioctl_balance_args *bargs) |
| 3847 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3848 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3849 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3850 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3851 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3852 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3853 | unsigned seq; |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3854 | bool reducing_redundancy; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 3855 | int i; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3856 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3857 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3858 | atomic_read(&fs_info->balance_pause_req) || |
| 3859 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3860 | ret = -EINVAL; |
| 3861 | goto out; |
| 3862 | } |
| 3863 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3864 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3865 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3866 | mixed = 1; |
| 3867 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3868 | /* |
| 3869 | * In case of mixed groups both data and meta should be picked, |
| 3870 | * and identical options should be given for both of them. |
| 3871 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3872 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3873 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3874 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3875 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3876 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3877 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3878 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3879 | ret = -EINVAL; |
| 3880 | goto out; |
| 3881 | } |
| 3882 | } |
| 3883 | |
Josef Bacik | b35cf1f | 2020-01-10 11:11:24 -0500 | [diff] [blame] | 3884 | /* |
| 3885 | * rw_devices will not change at the moment, device add/delete/replace |
| 3886 | * are excluded by EXCL_OP |
| 3887 | */ |
| 3888 | num_devices = fs_info->fs_devices->rw_devices; |
Qu Wenruo | fab2735 | 2019-09-25 10:13:27 +0800 | [diff] [blame] | 3889 | |
| 3890 | /* |
| 3891 | * SINGLE profile on-disk has no profile bit, but in-memory we have a |
| 3892 | * special bit for it, to make it easier to distinguish. Thus we need |
| 3893 | * to set it manually, or balance would refuse the profile. |
| 3894 | */ |
| 3895 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
David Sterba | 081db89 | 2019-05-17 11:43:27 +0200 | [diff] [blame] | 3896 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) |
| 3897 | if (num_devices >= btrfs_raid_array[i].devs_min) |
| 3898 | allowed |= btrfs_raid_array[i].bg_flag; |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 3899 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3900 | if (validate_convert_profile(&bctl->data, allowed)) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3901 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3902 | "balance: invalid convert data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3903 | btrfs_bg_type_to_raid_name(bctl->data.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3904 | ret = -EINVAL; |
| 3905 | goto out; |
| 3906 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3907 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3908 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3909 | "balance: invalid convert metadata profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3910 | btrfs_bg_type_to_raid_name(bctl->meta.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3911 | ret = -EINVAL; |
| 3912 | goto out; |
| 3913 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3914 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3915 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3916 | "balance: invalid convert system profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3917 | btrfs_bg_type_to_raid_name(bctl->sys.target)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3918 | ret = -EINVAL; |
| 3919 | goto out; |
| 3920 | } |
| 3921 | |
David Sterba | 6079e12 | 2019-05-17 11:43:29 +0200 | [diff] [blame] | 3922 | /* |
| 3923 | * Allow to reduce metadata or system integrity only if force set for |
| 3924 | * profiles with redundancy (copies, parity) |
| 3925 | */ |
| 3926 | allowed = 0; |
| 3927 | for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) { |
| 3928 | if (btrfs_raid_array[i].ncopies >= 2 || |
| 3929 | btrfs_raid_array[i].tolerated_failures >= 1) |
| 3930 | allowed |= btrfs_raid_array[i].bg_flag; |
| 3931 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3932 | do { |
| 3933 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3934 | |
| 3935 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3936 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3937 | !(bctl->sys.target & allowed)) || |
| 3938 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3939 | (fs_info->avail_metadata_alloc_bits & allowed) && |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 3940 | !(bctl->meta.target & allowed))) |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3941 | reducing_redundancy = true; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 3942 | else |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3943 | reducing_redundancy = false; |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 3944 | |
| 3945 | /* if we're not converting, the target field is uninitialized */ |
| 3946 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 3947 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 3948 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 3949 | bctl->data.target : fs_info->avail_data_alloc_bits; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3950 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3951 | |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3952 | if (reducing_redundancy) { |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 3953 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
| 3954 | btrfs_info(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3955 | "balance: force reducing metadata redundancy"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 3956 | } else { |
| 3957 | btrfs_err(fs_info, |
Anand Jain | e62869b | 2019-09-25 14:29:28 +0800 | [diff] [blame] | 3958 | "balance: reduces metadata redundancy, use --force if you want this"); |
Filipe Manana | 5a8067c | 2018-11-19 09:48:12 +0000 | [diff] [blame] | 3959 | ret = -EINVAL; |
| 3960 | goto out; |
| 3961 | } |
| 3962 | } |
| 3963 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3964 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 3965 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3966 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3967 | "balance: metadata profile %s has lower redundancy than data profile %s", |
David Sterba | 158da51 | 2019-05-17 11:43:41 +0200 | [diff] [blame] | 3968 | btrfs_bg_type_to_raid_name(meta_target), |
| 3969 | btrfs_bg_type_to_raid_name(data_target)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
Filipe Manana | 9e96749 | 2019-04-22 16:44:09 +0100 | [diff] [blame] | 3972 | if (fs_info->send_in_progress) { |
| 3973 | btrfs_warn_rl(fs_info, |
| 3974 | "cannot run balance while send operations are in progress (%d in progress)", |
| 3975 | fs_info->send_in_progress); |
| 3976 | ret = -EAGAIN; |
| 3977 | goto out; |
| 3978 | } |
| 3979 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3980 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3981 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3982 | goto out; |
| 3983 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3984 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3985 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 3986 | BUG_ON(fs_info->balance_ctl); |
| 3987 | spin_lock(&fs_info->balance_lock); |
| 3988 | fs_info->balance_ctl = bctl; |
| 3989 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3990 | } else { |
| 3991 | BUG_ON(ret != -EEXIST); |
| 3992 | spin_lock(&fs_info->balance_lock); |
| 3993 | update_balance_args(bctl); |
| 3994 | spin_unlock(&fs_info->balance_lock); |
| 3995 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3996 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3997 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 3998 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 3999 | describe_balance_start_or_resume(fs_info); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4000 | mutex_unlock(&fs_info->balance_mutex); |
| 4001 | |
| 4002 | ret = __btrfs_balance(fs_info); |
| 4003 | |
| 4004 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 7333bd0 | 2018-11-20 16:12:57 +0800 | [diff] [blame] | 4005 | if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req)) |
| 4006 | btrfs_info(fs_info, "balance: paused"); |
| 4007 | else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req)) |
| 4008 | btrfs_info(fs_info, "balance: canceled"); |
| 4009 | else |
| 4010 | btrfs_info(fs_info, "balance: ended with status: %d", ret); |
| 4011 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4012 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4013 | |
| 4014 | if (bargs) { |
| 4015 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 4016 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4017 | } |
| 4018 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4019 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 4020 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4021 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4022 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4025 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 4026 | |
| 4027 | return ret; |
| 4028 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4029 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4030 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4031 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4032 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4033 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 4034 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4035 | return ret; |
| 4036 | } |
| 4037 | |
| 4038 | static int balance_kthread(void *data) |
| 4039 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4040 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4041 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4042 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4043 | mutex_lock(&fs_info->balance_mutex); |
Anand Jain | 56fc37d | 2018-11-20 16:12:56 +0800 | [diff] [blame] | 4044 | if (fs_info->balance_ctl) |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 4045 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4046 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4047 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4048 | return ret; |
| 4049 | } |
| 4050 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4051 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4052 | { |
| 4053 | struct task_struct *tsk; |
| 4054 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4055 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4056 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4057 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4058 | return 0; |
| 4059 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4060 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4061 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4062 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4063 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4064 | return 0; |
| 4065 | } |
| 4066 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4067 | /* |
| 4068 | * A ro->rw remount sequence should continue with the paused balance |
| 4069 | * regardless of who pauses it, system or the user as of now, so set |
| 4070 | * the resume flag. |
| 4071 | */ |
| 4072 | spin_lock(&fs_info->balance_lock); |
| 4073 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4074 | spin_unlock(&fs_info->balance_lock); |
| 4075 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4076 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4077 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4078 | } |
| 4079 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4080 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4081 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4082 | struct btrfs_balance_control *bctl; |
| 4083 | struct btrfs_balance_item *item; |
| 4084 | struct btrfs_disk_balance_args disk_bargs; |
| 4085 | struct btrfs_path *path; |
| 4086 | struct extent_buffer *leaf; |
| 4087 | struct btrfs_key key; |
| 4088 | int ret; |
| 4089 | |
| 4090 | path = btrfs_alloc_path(); |
| 4091 | if (!path) |
| 4092 | return -ENOMEM; |
| 4093 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4094 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4095 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4096 | key.offset = 0; |
| 4097 | |
| 4098 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4099 | if (ret < 0) |
| 4100 | goto out; |
| 4101 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4102 | ret = 0; |
| 4103 | goto out; |
| 4104 | } |
| 4105 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4106 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4107 | if (!bctl) { |
| 4108 | ret = -ENOMEM; |
| 4109 | goto out; |
| 4110 | } |
| 4111 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4112 | leaf = path->nodes[0]; |
| 4113 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4114 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4115 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4116 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4117 | |
| 4118 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4119 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4120 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4121 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4122 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4123 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4124 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4125 | /* |
| 4126 | * This should never happen, as the paused balance state is recovered |
| 4127 | * during mount without any chance of other exclusive ops to collide. |
| 4128 | * |
| 4129 | * This gives the exclusive op status to balance and keeps in paused |
| 4130 | * state until user intervention (cancel or umount). If the ownership |
| 4131 | * cannot be assigned, show a message but do not fail. The balance |
| 4132 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4133 | * set up. |
| 4134 | */ |
| 4135 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4136 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4137 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4138 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4139 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4140 | BUG_ON(fs_info->balance_ctl); |
| 4141 | spin_lock(&fs_info->balance_lock); |
| 4142 | fs_info->balance_ctl = bctl; |
| 4143 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4144 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4145 | out: |
| 4146 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4147 | return ret; |
| 4148 | } |
| 4149 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4150 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4151 | { |
| 4152 | int ret = 0; |
| 4153 | |
| 4154 | mutex_lock(&fs_info->balance_mutex); |
| 4155 | if (!fs_info->balance_ctl) { |
| 4156 | mutex_unlock(&fs_info->balance_mutex); |
| 4157 | return -ENOTCONN; |
| 4158 | } |
| 4159 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4160 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4161 | atomic_inc(&fs_info->balance_pause_req); |
| 4162 | mutex_unlock(&fs_info->balance_mutex); |
| 4163 | |
| 4164 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4165 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4166 | |
| 4167 | mutex_lock(&fs_info->balance_mutex); |
| 4168 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4169 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4170 | atomic_dec(&fs_info->balance_pause_req); |
| 4171 | } else { |
| 4172 | ret = -ENOTCONN; |
| 4173 | } |
| 4174 | |
| 4175 | mutex_unlock(&fs_info->balance_mutex); |
| 4176 | return ret; |
| 4177 | } |
| 4178 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4179 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4180 | { |
| 4181 | mutex_lock(&fs_info->balance_mutex); |
| 4182 | if (!fs_info->balance_ctl) { |
| 4183 | mutex_unlock(&fs_info->balance_mutex); |
| 4184 | return -ENOTCONN; |
| 4185 | } |
| 4186 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4187 | /* |
| 4188 | * A paused balance with the item stored on disk can be resumed at |
| 4189 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4190 | * and we must not allow cancelling as it deletes the item. |
| 4191 | */ |
| 4192 | if (sb_rdonly(fs_info->sb)) { |
| 4193 | mutex_unlock(&fs_info->balance_mutex); |
| 4194 | return -EROFS; |
| 4195 | } |
| 4196 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4197 | atomic_inc(&fs_info->balance_cancel_req); |
| 4198 | /* |
| 4199 | * if we are running just wait and return, balance item is |
| 4200 | * deleted in btrfs_balance in this case |
| 4201 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4202 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4203 | mutex_unlock(&fs_info->balance_mutex); |
| 4204 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4205 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4206 | mutex_lock(&fs_info->balance_mutex); |
| 4207 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4208 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4209 | /* |
| 4210 | * Lock released to allow other waiters to continue, we'll |
| 4211 | * reexamine the status again. |
| 4212 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4213 | mutex_lock(&fs_info->balance_mutex); |
| 4214 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4215 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4216 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4217 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4218 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4219 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4220 | } |
| 4221 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4222 | BUG_ON(fs_info->balance_ctl || |
| 4223 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4224 | atomic_dec(&fs_info->balance_cancel_req); |
| 4225 | mutex_unlock(&fs_info->balance_mutex); |
| 4226 | return 0; |
| 4227 | } |
| 4228 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4229 | static int btrfs_uuid_scan_kthread(void *data) |
| 4230 | { |
| 4231 | struct btrfs_fs_info *fs_info = data; |
| 4232 | struct btrfs_root *root = fs_info->tree_root; |
| 4233 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4234 | struct btrfs_path *path = NULL; |
| 4235 | int ret = 0; |
| 4236 | struct extent_buffer *eb; |
| 4237 | int slot; |
| 4238 | struct btrfs_root_item root_item; |
| 4239 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4240 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4241 | |
| 4242 | path = btrfs_alloc_path(); |
| 4243 | if (!path) { |
| 4244 | ret = -ENOMEM; |
| 4245 | goto out; |
| 4246 | } |
| 4247 | |
| 4248 | key.objectid = 0; |
| 4249 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4250 | key.offset = 0; |
| 4251 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4252 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4253 | ret = btrfs_search_forward(root, &key, path, |
| 4254 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4255 | if (ret) { |
| 4256 | if (ret > 0) |
| 4257 | ret = 0; |
| 4258 | break; |
| 4259 | } |
| 4260 | |
| 4261 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4262 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4263 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4264 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4265 | goto skip; |
| 4266 | |
| 4267 | eb = path->nodes[0]; |
| 4268 | slot = path->slots[0]; |
| 4269 | item_size = btrfs_item_size_nr(eb, slot); |
| 4270 | if (item_size < sizeof(root_item)) |
| 4271 | goto skip; |
| 4272 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4273 | read_extent_buffer(eb, &root_item, |
| 4274 | btrfs_item_ptr_offset(eb, slot), |
| 4275 | (int)sizeof(root_item)); |
| 4276 | if (btrfs_root_refs(&root_item) == 0) |
| 4277 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4278 | |
| 4279 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4280 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4281 | if (trans) |
| 4282 | goto update_tree; |
| 4283 | |
| 4284 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4285 | /* |
| 4286 | * 1 - subvol uuid item |
| 4287 | * 1 - received_subvol uuid item |
| 4288 | */ |
| 4289 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4290 | if (IS_ERR(trans)) { |
| 4291 | ret = PTR_ERR(trans); |
| 4292 | break; |
| 4293 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4294 | continue; |
| 4295 | } else { |
| 4296 | goto skip; |
| 4297 | } |
| 4298 | update_tree: |
| 4299 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4300 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4301 | BTRFS_UUID_KEY_SUBVOL, |
| 4302 | key.objectid); |
| 4303 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4304 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4305 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4306 | break; |
| 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4311 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4312 | root_item.received_uuid, |
| 4313 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4314 | key.objectid); |
| 4315 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4316 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4317 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4318 | break; |
| 4319 | } |
| 4320 | } |
| 4321 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4322 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4323 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4324 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4325 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4326 | if (ret) |
| 4327 | break; |
| 4328 | } |
| 4329 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4330 | btrfs_release_path(path); |
| 4331 | if (key.offset < (u64)-1) { |
| 4332 | key.offset++; |
| 4333 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4334 | key.offset = 0; |
| 4335 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4336 | } else if (key.objectid < (u64)-1) { |
| 4337 | key.offset = 0; |
| 4338 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4339 | key.objectid++; |
| 4340 | } else { |
| 4341 | break; |
| 4342 | } |
| 4343 | cond_resched(); |
| 4344 | } |
| 4345 | |
| 4346 | out: |
| 4347 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4348 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4349 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4350 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4351 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4352 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4353 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4354 | up(&fs_info->uuid_tree_rescan_sem); |
| 4355 | return 0; |
| 4356 | } |
| 4357 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4358 | /* |
| 4359 | * Callback for btrfs_uuid_tree_iterate(). |
| 4360 | * returns: |
| 4361 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4362 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4363 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4364 | */ |
| 4365 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4366 | u8 *uuid, u8 type, u64 subid) |
| 4367 | { |
| 4368 | struct btrfs_key key; |
| 4369 | int ret = 0; |
| 4370 | struct btrfs_root *subvol_root; |
| 4371 | |
| 4372 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4373 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4374 | goto out; |
| 4375 | |
| 4376 | key.objectid = subid; |
| 4377 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4378 | key.offset = (u64)-1; |
| 4379 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4380 | if (IS_ERR(subvol_root)) { |
| 4381 | ret = PTR_ERR(subvol_root); |
| 4382 | if (ret == -ENOENT) |
| 4383 | ret = 1; |
| 4384 | goto out; |
| 4385 | } |
| 4386 | |
| 4387 | switch (type) { |
| 4388 | case BTRFS_UUID_KEY_SUBVOL: |
| 4389 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4390 | ret = 1; |
| 4391 | break; |
| 4392 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4393 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4394 | BTRFS_UUID_SIZE)) |
| 4395 | ret = 1; |
| 4396 | break; |
| 4397 | } |
| 4398 | |
| 4399 | out: |
| 4400 | return ret; |
| 4401 | } |
| 4402 | |
| 4403 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4404 | { |
| 4405 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4406 | int ret; |
| 4407 | |
| 4408 | /* |
| 4409 | * 1st step is to iterate through the existing UUID tree and |
| 4410 | * to delete all entries that contain outdated data. |
| 4411 | * 2nd step is to add all missing entries to the UUID tree. |
| 4412 | */ |
| 4413 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4414 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4415 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4416 | up(&fs_info->uuid_tree_rescan_sem); |
| 4417 | return ret; |
| 4418 | } |
| 4419 | return btrfs_uuid_scan_kthread(data); |
| 4420 | } |
| 4421 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4422 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4423 | { |
| 4424 | struct btrfs_trans_handle *trans; |
| 4425 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4426 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4427 | struct task_struct *task; |
| 4428 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4429 | |
| 4430 | /* |
| 4431 | * 1 - root node |
| 4432 | * 1 - root item |
| 4433 | */ |
| 4434 | trans = btrfs_start_transaction(tree_root, 2); |
| 4435 | if (IS_ERR(trans)) |
| 4436 | return PTR_ERR(trans); |
| 4437 | |
David Sterba | 9b7a244 | 2019-03-20 13:20:49 +0100 | [diff] [blame] | 4438 | uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID); |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4439 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4440 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4441 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4442 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4443 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | fs_info->uuid_root = uuid_root; |
| 4447 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4448 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4449 | if (ret) |
| 4450 | return ret; |
| 4451 | |
| 4452 | down(&fs_info->uuid_tree_rescan_sem); |
| 4453 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4454 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4455 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4456 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4457 | up(&fs_info->uuid_tree_rescan_sem); |
| 4458 | return PTR_ERR(task); |
| 4459 | } |
| 4460 | |
| 4461 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4462 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4463 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4464 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4465 | { |
| 4466 | struct task_struct *task; |
| 4467 | |
| 4468 | down(&fs_info->uuid_tree_rescan_sem); |
| 4469 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4470 | if (IS_ERR(task)) { |
| 4471 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4472 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4473 | up(&fs_info->uuid_tree_rescan_sem); |
| 4474 | return PTR_ERR(task); |
| 4475 | } |
| 4476 | |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4480 | /* |
| 4481 | * shrinking a device means finding all of the device extents past |
| 4482 | * the new size, and then following the back refs to the chunks. |
| 4483 | * The chunk relocation code actually frees the device extent |
| 4484 | */ |
| 4485 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4486 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4487 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4488 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4489 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4490 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4491 | struct btrfs_path *path; |
| 4492 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4493 | u64 chunk_offset; |
| 4494 | int ret; |
| 4495 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4496 | int failed = 0; |
| 4497 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4498 | struct extent_buffer *l; |
| 4499 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4500 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4501 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4502 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4503 | u64 diff; |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4504 | u64 start; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4505 | |
| 4506 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4507 | start = new_size; |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4508 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4509 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4510 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4511 | return -EINVAL; |
| 4512 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4513 | path = btrfs_alloc_path(); |
| 4514 | if (!path) |
| 4515 | return -ENOMEM; |
| 4516 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4517 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4518 | |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4519 | trans = btrfs_start_transaction(root, 0); |
| 4520 | if (IS_ERR(trans)) { |
| 4521 | btrfs_free_path(path); |
| 4522 | return PTR_ERR(trans); |
| 4523 | } |
| 4524 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4525 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4526 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4527 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4528 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4529 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4530 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4531 | } |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4532 | |
| 4533 | /* |
| 4534 | * Once the device's size has been set to the new size, ensure all |
| 4535 | * in-memory chunks are synced to disk so that the loop below sees them |
| 4536 | * and relocates them accordingly. |
| 4537 | */ |
Jeff Mahoney | 1c11b63 | 2019-03-27 14:24:12 +0200 | [diff] [blame] | 4538 | if (contains_pending_extent(device, &start, diff)) { |
Nikolay Borisov | 61d0d0d | 2019-03-25 14:31:23 +0200 | [diff] [blame] | 4539 | mutex_unlock(&fs_info->chunk_mutex); |
| 4540 | ret = btrfs_commit_transaction(trans); |
| 4541 | if (ret) |
| 4542 | goto done; |
| 4543 | } else { |
| 4544 | mutex_unlock(&fs_info->chunk_mutex); |
| 4545 | btrfs_end_transaction(trans); |
| 4546 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4547 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4548 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4549 | key.objectid = device->devid; |
| 4550 | key.offset = (u64)-1; |
| 4551 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4552 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4553 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4554 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4555 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4556 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4557 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4558 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4559 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4560 | |
| 4561 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4562 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4563 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4564 | if (ret < 0) |
| 4565 | goto done; |
| 4566 | if (ret) { |
| 4567 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4568 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4569 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | l = path->nodes[0]; |
| 4573 | slot = path->slots[0]; |
| 4574 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4575 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4576 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4577 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4578 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4579 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4580 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4581 | |
| 4582 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4583 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4584 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4585 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4586 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4587 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4588 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4589 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4590 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4591 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4592 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4593 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4594 | /* |
| 4595 | * We may be relocating the only data chunk we have, |
| 4596 | * which could potentially end up with losing data's |
| 4597 | * raid profile, so lets allocate an empty one in |
| 4598 | * advance. |
| 4599 | */ |
| 4600 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4601 | if (ret < 0) { |
| 4602 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4603 | goto done; |
| 4604 | } |
| 4605 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4606 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4607 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4608 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4609 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4610 | } else if (ret) { |
| 4611 | if (ret == -ETXTBSY) { |
| 4612 | btrfs_warn(fs_info, |
| 4613 | "could not shrink block group %llu due to active swapfile", |
| 4614 | chunk_offset); |
| 4615 | } |
| 4616 | goto done; |
| 4617 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4618 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4619 | |
| 4620 | if (failed && !retried) { |
| 4621 | failed = 0; |
| 4622 | retried = true; |
| 4623 | goto again; |
| 4624 | } else if (failed && retried) { |
| 4625 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4626 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4627 | } |
| 4628 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4629 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4630 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4631 | if (IS_ERR(trans)) { |
| 4632 | ret = PTR_ERR(trans); |
| 4633 | goto done; |
| 4634 | } |
| 4635 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4636 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4637 | btrfs_device_set_disk_total_bytes(device, new_size); |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4638 | if (list_empty(&device->post_commit_list)) |
| 4639 | list_add_tail(&device->post_commit_list, |
| 4640 | &trans->transaction->dev_update_list); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4641 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4642 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4643 | btrfs_set_super_total_bytes(super_copy, |
| 4644 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4645 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4646 | |
| 4647 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4648 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4649 | if (ret < 0) { |
| 4650 | btrfs_abort_transaction(trans, ret); |
| 4651 | btrfs_end_transaction(trans); |
| 4652 | } else { |
| 4653 | ret = btrfs_commit_transaction(trans); |
| 4654 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4655 | done: |
| 4656 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4657 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4658 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4659 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4660 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4661 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4662 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4663 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4664 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4665 | return ret; |
| 4666 | } |
| 4667 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4668 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4669 | struct btrfs_key *key, |
| 4670 | struct btrfs_chunk *chunk, int item_size) |
| 4671 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4672 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4673 | struct btrfs_disk_key disk_key; |
| 4674 | u32 array_size; |
| 4675 | u8 *ptr; |
| 4676 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4677 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4678 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4679 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4680 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4681 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4682 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4683 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4684 | |
| 4685 | ptr = super_copy->sys_chunk_array + array_size; |
| 4686 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4687 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4688 | ptr += sizeof(disk_key); |
| 4689 | memcpy(ptr, chunk, item_size); |
| 4690 | item_size += sizeof(disk_key); |
| 4691 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4692 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4693 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4694 | return 0; |
| 4695 | } |
| 4696 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4697 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4698 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4699 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4700 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4701 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4702 | const struct btrfs_device_info *di_a = a; |
| 4703 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4704 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4705 | if (di_a->max_avail > di_b->max_avail) |
| 4706 | return -1; |
| 4707 | if (di_a->max_avail < di_b->max_avail) |
| 4708 | return 1; |
| 4709 | if (di_a->total_avail > di_b->total_avail) |
| 4710 | return -1; |
| 4711 | if (di_a->total_avail < di_b->total_avail) |
| 4712 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4713 | return 0; |
| 4714 | } |
| 4715 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4716 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4717 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4718 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4719 | return; |
| 4720 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4721 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4722 | } |
| 4723 | |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 4724 | static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4725 | { |
| 4726 | if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4))) |
| 4727 | return; |
| 4728 | |
| 4729 | btrfs_set_fs_incompat(info, RAID1C34); |
| 4730 | } |
| 4731 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4732 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4733 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4734 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4735 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4736 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4737 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4738 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4739 | struct extent_map_tree *em_tree; |
| 4740 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4741 | struct btrfs_device_info *devices_info = NULL; |
| 4742 | u64 total_avail; |
| 4743 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4744 | int data_stripes; /* number of stripes that count for |
| 4745 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4746 | int sub_stripes; /* sub_stripes info for map */ |
| 4747 | int dev_stripes; /* stripes per dev */ |
| 4748 | int devs_max; /* max devs to use */ |
| 4749 | int devs_min; /* min devs needed */ |
| 4750 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4751 | int ncopies; /* how many copies to data has */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4752 | int nparity; /* number of stripes worth of bytes to |
| 4753 | store parity information */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4754 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4755 | u64 max_stripe_size; |
| 4756 | u64 max_chunk_size; |
| 4757 | u64 stripe_size; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4758 | u64 chunk_size; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4759 | int ndevs; |
| 4760 | int i; |
| 4761 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4762 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4763 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4764 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4765 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4766 | if (list_empty(&fs_devices->alloc_list)) { |
| 4767 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4768 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4769 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4770 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4771 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4772 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4773 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4774 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4775 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4776 | devs_max = btrfs_raid_array[index].devs_max; |
David Sterba | e3ecdb3 | 2019-05-17 11:43:38 +0200 | [diff] [blame] | 4777 | if (!devs_max) |
| 4778 | devs_max = BTRFS_MAX_DEVS(info); |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4779 | devs_min = btrfs_raid_array[index].devs_min; |
| 4780 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4781 | ncopies = btrfs_raid_array[index].ncopies; |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4782 | nparity = btrfs_raid_array[index].nparity; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4783 | |
| 4784 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4785 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4786 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4787 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4788 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4789 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4790 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4791 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4792 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4793 | max_chunk_size = max_stripe_size; |
| 4794 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4795 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4796 | max_chunk_size = 2 * max_stripe_size; |
Qu Wenruo | c17add7 | 2019-08-28 10:33:12 +0800 | [diff] [blame] | 4797 | devs_max = min_t(int, devs_max, BTRFS_MAX_DEVS_SYS_CHUNK); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4798 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4799 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4800 | type); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4801 | BUG(); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4802 | } |
| 4803 | |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 4804 | /* We don't want a chunk larger than 10% of writable space */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4805 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4806 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4807 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4808 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4809 | GFP_NOFS); |
| 4810 | if (!devices_info) |
| 4811 | return -ENOMEM; |
| 4812 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4813 | /* |
| 4814 | * in the first pass through the devices list, we gather information |
| 4815 | * about the available holes on each device. |
| 4816 | */ |
| 4817 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4818 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4819 | u64 max_avail; |
| 4820 | u64 dev_offset; |
| 4821 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4822 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4823 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4824 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4825 | continue; |
| 4826 | } |
| 4827 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 4828 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 4829 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4830 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4831 | continue; |
| 4832 | |
| 4833 | if (device->total_bytes > device->bytes_used) |
| 4834 | total_avail = device->total_bytes - device->bytes_used; |
| 4835 | else |
| 4836 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4837 | |
| 4838 | /* If there is no space on this device, skip it. */ |
| 4839 | if (total_avail == 0) |
| 4840 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4841 | |
Nikolay Borisov | 60dfdf2 | 2019-03-27 14:24:14 +0200 | [diff] [blame] | 4842 | ret = find_free_dev_extent(device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4843 | max_stripe_size * dev_stripes, |
| 4844 | &dev_offset, &max_avail); |
| 4845 | if (ret && ret != -ENOSPC) |
| 4846 | goto error; |
| 4847 | |
| 4848 | if (ret == 0) |
| 4849 | max_avail = max_stripe_size * dev_stripes; |
| 4850 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4851 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 4852 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4853 | btrfs_debug(info, |
| 4854 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 4855 | __func__, device->devid, max_avail, |
| 4856 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4857 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4858 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4859 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4860 | if (ndevs == fs_devices->rw_devices) { |
| 4861 | WARN(1, "%s: found more than %llu devices\n", |
| 4862 | __func__, fs_devices->rw_devices); |
| 4863 | break; |
| 4864 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4865 | devices_info[ndevs].dev_offset = dev_offset; |
| 4866 | devices_info[ndevs].max_avail = max_avail; |
| 4867 | devices_info[ndevs].total_avail = total_avail; |
| 4868 | devices_info[ndevs].dev = device; |
| 4869 | ++ndevs; |
| 4870 | } |
| 4871 | |
| 4872 | /* |
| 4873 | * now sort the devices by hole size / available space |
| 4874 | */ |
| 4875 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4876 | btrfs_cmp_device_info, NULL); |
| 4877 | |
David Sterba | 47e6f74 | 2018-03-02 22:56:53 +0100 | [diff] [blame] | 4878 | /* |
| 4879 | * Round down to number of usable stripes, devs_increment can be any |
| 4880 | * number so we can't use round_down() |
| 4881 | */ |
| 4882 | ndevs -= ndevs % devs_increment; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4883 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4884 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4885 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4886 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 4887 | btrfs_debug(info, |
| 4888 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4889 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4890 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4891 | goto error; |
| 4892 | } |
| 4893 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 4894 | ndevs = min(ndevs, devs_max); |
| 4895 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4896 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4897 | * The primary goal is to maximize the number of stripes, so use as |
| 4898 | * many devices as possible, even if the stripes are not maximum sized. |
| 4899 | * |
| 4900 | * The DUP profile stores more than one stripe per device, the |
| 4901 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4902 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4903 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4904 | num_stripes = ndevs * dev_stripes; |
| 4905 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4906 | /* |
| 4907 | * this will have to be fixed for RAID1 and RAID10 over |
| 4908 | * more drives |
| 4909 | */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4910 | data_stripes = (num_stripes - nparity) / ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4911 | |
| 4912 | /* |
| 4913 | * Use the number of data stripes to figure out how big this chunk |
| 4914 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4915 | * and compare that answer with the max chunk size. If it's higher, |
| 4916 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4917 | */ |
| 4918 | if (stripe_size * data_stripes > max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4919 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4920 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 4921 | * then use it, unless it ends up being even bigger than the |
| 4922 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4923 | */ |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4924 | stripe_size = min(round_up(div_u64(max_chunk_size, |
| 4925 | data_stripes), SZ_16M), |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4926 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4927 | } |
| 4928 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4929 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4930 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4931 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4932 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4933 | if (!map) { |
| 4934 | ret = -ENOMEM; |
| 4935 | goto error; |
| 4936 | } |
| 4937 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4938 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4939 | for (i = 0; i < ndevs; ++i) { |
| 4940 | for (j = 0; j < dev_stripes; ++j) { |
| 4941 | int s = i * dev_stripes + j; |
| 4942 | map->stripes[s].dev = devices_info[i].dev; |
| 4943 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4944 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4945 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4946 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4947 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 4948 | map->io_align = BTRFS_STRIPE_LEN; |
| 4949 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4950 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4951 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4952 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4953 | chunk_size = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4954 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4955 | trace_btrfs_chunk_alloc(info, map, start, chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4956 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4957 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4958 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4959 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4960 | ret = -ENOMEM; |
| 4961 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4962 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4963 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 4964 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4965 | em->start = start; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4966 | em->len = chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4967 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4968 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4969 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4970 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 4971 | em_tree = &info->mapping_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4972 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4973 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4974 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 4975 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4976 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4977 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4978 | } |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 4979 | write_unlock(&em_tree->lock); |
| 4980 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4981 | ret = btrfs_make_block_group(trans, 0, type, start, chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4982 | if (ret) |
| 4983 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4984 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 4985 | for (i = 0; i < map->num_stripes; i++) { |
| 4986 | struct btrfs_device *dev = map->stripes[i].dev; |
| 4987 | |
| 4988 | btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size); |
| 4989 | if (list_empty(&dev->post_commit_list)) |
| 4990 | list_add_tail(&dev->post_commit_list, |
| 4991 | &trans->transaction->dev_update_list); |
| 4992 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4993 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4994 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4995 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4996 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4997 | check_raid56_incompat_flag(info, type); |
David Sterba | cfbb825 | 2018-07-10 18:15:05 +0200 | [diff] [blame] | 4998 | check_raid1c34_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4999 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5000 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5001 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5002 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5003 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 5004 | write_lock(&em_tree->lock); |
| 5005 | remove_extent_mapping(em_tree, em); |
| 5006 | write_unlock(&em_tree->lock); |
| 5007 | |
| 5008 | /* One for our allocation */ |
| 5009 | free_extent_map(em); |
| 5010 | /* One for the tree reference */ |
| 5011 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5012 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 5013 | kfree(devices_info); |
| 5014 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5015 | } |
| 5016 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5017 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5018 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5019 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 5020 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 5021 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 5022 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5023 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5024 | struct btrfs_device *device; |
| 5025 | struct btrfs_chunk *chunk; |
| 5026 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5027 | struct extent_map *em; |
| 5028 | struct map_lookup *map; |
| 5029 | size_t item_size; |
| 5030 | u64 dev_offset; |
| 5031 | u64 stripe_size; |
| 5032 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 5033 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5034 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5035 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5036 | if (IS_ERR(em)) |
| 5037 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5038 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5039 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5040 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5041 | stripe_size = em->orig_block_len; |
| 5042 | |
| 5043 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5044 | if (!chunk) { |
| 5045 | ret = -ENOMEM; |
| 5046 | goto out; |
| 5047 | } |
| 5048 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5049 | /* |
| 5050 | * Take the device list mutex to prevent races with the final phase of |
| 5051 | * a device replace operation that replaces the device object associated |
| 5052 | * with the map's stripes, because the device object's id can change |
| 5053 | * at any time during that final phase of the device replace operation |
| 5054 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5055 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5056 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5057 | for (i = 0; i < map->num_stripes; i++) { |
| 5058 | device = map->stripes[i].dev; |
| 5059 | dev_offset = map->stripes[i].physical; |
| 5060 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5061 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5062 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5063 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5064 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5065 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5066 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5067 | break; |
| 5068 | } |
| 5069 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5070 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5071 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5072 | } |
| 5073 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5074 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5075 | for (i = 0; i < map->num_stripes; i++) { |
| 5076 | device = map->stripes[i].dev; |
| 5077 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5078 | |
| 5079 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5080 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5081 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5082 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5083 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5084 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5085 | |
| 5086 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5087 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5088 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5089 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5090 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5091 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5092 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5093 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5094 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5095 | |
| 5096 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5097 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5098 | key.offset = chunk_offset; |
| 5099 | |
| 5100 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5101 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5102 | /* |
| 5103 | * TODO: Cleanup of inserted chunk root in case of |
| 5104 | * failure. |
| 5105 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5106 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5107 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5108 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5109 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5110 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5111 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5112 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 5116 | * Chunk allocation falls into two parts. The first part does work |
| 5117 | * that makes the new allocated chunk usable, but does not do any operation |
| 5118 | * that modifies the chunk tree. The second part does the work that |
| 5119 | * requires modifying the chunk tree. This division is important for the |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5120 | * bootstrap process of adding storage to a seed btrfs. |
| 5121 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5122 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5123 | { |
| 5124 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5125 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5126 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5127 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5128 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5129 | } |
| 5130 | |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5131 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5132 | { |
David Sterba | 6f8e0fc | 2019-03-20 16:29:13 +0100 | [diff] [blame] | 5133 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5134 | u64 chunk_offset; |
| 5135 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5136 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5137 | int ret; |
| 5138 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5139 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5140 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5141 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5142 | if (ret) |
| 5143 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5144 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5145 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5146 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5147 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5148 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5149 | } |
| 5150 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5151 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5152 | { |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5153 | const int index = btrfs_bg_flags_to_raid_index(map->type); |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5154 | |
David Sterba | fc9a2ac | 2019-05-17 11:43:22 +0200 | [diff] [blame] | 5155 | return btrfs_raid_array[index].tolerated_failures; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5156 | } |
| 5157 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5158 | 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] | 5159 | { |
| 5160 | struct extent_map *em; |
| 5161 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5162 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5163 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5164 | int i; |
| 5165 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5166 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5167 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5168 | return 1; |
| 5169 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5170 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5171 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5172 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5173 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5174 | miss_ndevs++; |
| 5175 | continue; |
| 5176 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5177 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5178 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5179 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5180 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5181 | } |
| 5182 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5183 | |
| 5184 | /* |
| 5185 | * If the number of missing devices is larger than max errors, |
| 5186 | * we can not write the data into that chunk successfully, so |
| 5187 | * set it readonly. |
| 5188 | */ |
| 5189 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5190 | readonly = 1; |
| 5191 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5192 | free_extent_map(em); |
| 5193 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5194 | } |
| 5195 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5196 | void btrfs_mapping_tree_free(struct extent_map_tree *tree) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5197 | { |
| 5198 | struct extent_map *em; |
| 5199 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5200 | while (1) { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5201 | write_lock(&tree->lock); |
| 5202 | em = lookup_extent_mapping(tree, 0, (u64)-1); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5203 | if (em) |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 5204 | remove_extent_mapping(tree, em); |
| 5205 | write_unlock(&tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5206 | if (!em) |
| 5207 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5208 | /* once for us */ |
| 5209 | free_extent_map(em); |
| 5210 | /* once for the tree */ |
| 5211 | free_extent_map(em); |
| 5212 | } |
| 5213 | } |
| 5214 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5215 | 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] | 5216 | { |
| 5217 | struct extent_map *em; |
| 5218 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5219 | int ret; |
| 5220 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5221 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5222 | if (IS_ERR(em)) |
| 5223 | /* |
| 5224 | * We could return errors for these cases, but that could get |
| 5225 | * ugly and we'd probably do the same thing which is just not do |
| 5226 | * anything else and exit, so return 1 so the callers don't try |
| 5227 | * to use other copies. |
| 5228 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5229 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5230 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5231 | map = em->map_lookup; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5232 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK)) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5233 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5234 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5235 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5236 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5237 | ret = 2; |
| 5238 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5239 | /* |
| 5240 | * There could be two corrupted data stripes, we need |
| 5241 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5242 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5243 | * Fail a stripe at a time on every retry except the |
| 5244 | * stripe under reconstruction. |
| 5245 | */ |
| 5246 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5247 | else |
| 5248 | ret = 1; |
| 5249 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5250 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5251 | down_read(&fs_info->dev_replace.rwsem); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5252 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5253 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5254 | ret++; |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5255 | up_read(&fs_info->dev_replace.rwsem); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5256 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5257 | return ret; |
| 5258 | } |
| 5259 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5260 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5261 | u64 logical) |
| 5262 | { |
| 5263 | struct extent_map *em; |
| 5264 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5265 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5266 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5267 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5268 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5269 | if (!WARN_ON(IS_ERR(em))) { |
| 5270 | map = em->map_lookup; |
| 5271 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5272 | len = map->stripe_len * nr_data_stripes(map); |
| 5273 | free_extent_map(em); |
| 5274 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5275 | return len; |
| 5276 | } |
| 5277 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5278 | 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] | 5279 | { |
| 5280 | struct extent_map *em; |
| 5281 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5282 | int ret = 0; |
| 5283 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5284 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5285 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5286 | if(!WARN_ON(IS_ERR(em))) { |
| 5287 | map = em->map_lookup; |
| 5288 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5289 | ret = 1; |
| 5290 | free_extent_map(em); |
| 5291 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5292 | return ret; |
| 5293 | } |
| 5294 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5295 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5296 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5297 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5298 | { |
| 5299 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5300 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5301 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5302 | int tolerance; |
| 5303 | struct btrfs_device *srcdev; |
| 5304 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5305 | ASSERT((map->type & |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5306 | (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10))); |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5307 | |
| 5308 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5309 | num_stripes = map->sub_stripes; |
| 5310 | else |
| 5311 | num_stripes = map->num_stripes; |
| 5312 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5313 | preferred_mirror = first + current->pid % num_stripes; |
| 5314 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5315 | if (dev_replace_is_ongoing && |
| 5316 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5317 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5318 | srcdev = fs_info->dev_replace.srcdev; |
| 5319 | else |
| 5320 | srcdev = NULL; |
| 5321 | |
| 5322 | /* |
| 5323 | * try to avoid the drive that is the source drive for a |
| 5324 | * dev-replace procedure, only choose it if no other non-missing |
| 5325 | * mirror is available |
| 5326 | */ |
| 5327 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5328 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5329 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5330 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5331 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5332 | if (map->stripes[i].dev->bdev && |
| 5333 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5334 | return i; |
| 5335 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5336 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5337 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5338 | /* we couldn't find one that doesn't fail. Just return something |
| 5339 | * and the io error handling code will clean up eventually |
| 5340 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5341 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5342 | } |
| 5343 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5344 | static inline int parity_smaller(u64 a, u64 b) |
| 5345 | { |
| 5346 | return a > b; |
| 5347 | } |
| 5348 | |
| 5349 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5350 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5351 | { |
| 5352 | struct btrfs_bio_stripe s; |
| 5353 | int i; |
| 5354 | u64 l; |
| 5355 | int again = 1; |
| 5356 | |
| 5357 | while (again) { |
| 5358 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5359 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5360 | if (parity_smaller(bbio->raid_map[i], |
| 5361 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5362 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5363 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5364 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5365 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5366 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5367 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5368 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5369 | again = 1; |
| 5370 | } |
| 5371 | } |
| 5372 | } |
| 5373 | } |
| 5374 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5375 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5376 | { |
| 5377 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5378 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5379 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5380 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5381 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5382 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5383 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5384 | /* |
| 5385 | * plus the raid_map, which includes both the tgt dev |
| 5386 | * and the stripes |
| 5387 | */ |
| 5388 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5389 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5390 | |
| 5391 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5392 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5393 | |
| 5394 | return bbio; |
| 5395 | } |
| 5396 | |
| 5397 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5398 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5399 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5400 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5404 | { |
| 5405 | if (!bbio) |
| 5406 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5407 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5408 | kfree(bbio); |
| 5409 | } |
| 5410 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5411 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5412 | /* |
| 5413 | * Please note that, discard won't be sent to target device of device |
| 5414 | * replace. |
| 5415 | */ |
| 5416 | 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] | 5417 | u64 logical, u64 *length_ret, |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5418 | struct btrfs_bio **bbio_ret) |
| 5419 | { |
| 5420 | struct extent_map *em; |
| 5421 | struct map_lookup *map; |
| 5422 | struct btrfs_bio *bbio; |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5423 | u64 length = *length_ret; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5424 | u64 offset; |
| 5425 | u64 stripe_nr; |
| 5426 | u64 stripe_nr_end; |
| 5427 | u64 stripe_end_offset; |
| 5428 | u64 stripe_cnt; |
| 5429 | u64 stripe_len; |
| 5430 | u64 stripe_offset; |
| 5431 | u64 num_stripes; |
| 5432 | u32 stripe_index; |
| 5433 | u32 factor = 0; |
| 5434 | u32 sub_stripes = 0; |
| 5435 | u64 stripes_per_dev = 0; |
| 5436 | u32 remaining_stripes = 0; |
| 5437 | u32 last_stripe = 0; |
| 5438 | int ret = 0; |
| 5439 | int i; |
| 5440 | |
| 5441 | /* discard always return a bbio */ |
| 5442 | ASSERT(bbio_ret); |
| 5443 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5444 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5445 | if (IS_ERR(em)) |
| 5446 | return PTR_ERR(em); |
| 5447 | |
| 5448 | map = em->map_lookup; |
| 5449 | /* we don't discard raid56 yet */ |
| 5450 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5451 | ret = -EOPNOTSUPP; |
| 5452 | goto out; |
| 5453 | } |
| 5454 | |
| 5455 | offset = logical - em->start; |
Qu Wenruo | 2d97461 | 2019-10-23 21:57:26 +0800 | [diff] [blame] | 5456 | length = min_t(u64, em->start + em->len - logical, length); |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5457 | *length_ret = length; |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5458 | |
| 5459 | stripe_len = map->stripe_len; |
| 5460 | /* |
| 5461 | * stripe_nr counts the total number of stripes we have to stride |
| 5462 | * to get to this block |
| 5463 | */ |
| 5464 | stripe_nr = div64_u64(offset, stripe_len); |
| 5465 | |
| 5466 | /* stripe_offset is the offset of this block in its stripe */ |
| 5467 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5468 | |
| 5469 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5470 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5471 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5472 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5473 | (offset + length); |
| 5474 | /* |
| 5475 | * after this, stripe_nr is the number of stripes on this |
| 5476 | * device we have to walk to find the data, and stripe_index is |
| 5477 | * the number of our device in the stripe array |
| 5478 | */ |
| 5479 | num_stripes = 1; |
| 5480 | stripe_index = 0; |
| 5481 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5482 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5483 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5484 | sub_stripes = 1; |
| 5485 | else |
| 5486 | sub_stripes = map->sub_stripes; |
| 5487 | |
| 5488 | factor = map->num_stripes / sub_stripes; |
| 5489 | num_stripes = min_t(u64, map->num_stripes, |
| 5490 | sub_stripes * stripe_cnt); |
| 5491 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5492 | stripe_index *= sub_stripes; |
| 5493 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5494 | &remaining_stripes); |
| 5495 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5496 | last_stripe *= sub_stripes; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5497 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5498 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5499 | num_stripes = map->num_stripes; |
| 5500 | } else { |
| 5501 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5502 | &stripe_index); |
| 5503 | } |
| 5504 | |
| 5505 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5506 | if (!bbio) { |
| 5507 | ret = -ENOMEM; |
| 5508 | goto out; |
| 5509 | } |
| 5510 | |
| 5511 | for (i = 0; i < num_stripes; i++) { |
| 5512 | bbio->stripes[i].physical = |
| 5513 | map->stripes[stripe_index].physical + |
| 5514 | stripe_offset + stripe_nr * map->stripe_len; |
| 5515 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5516 | |
| 5517 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5518 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5519 | bbio->stripes[i].length = stripes_per_dev * |
| 5520 | map->stripe_len; |
| 5521 | |
| 5522 | if (i / sub_stripes < remaining_stripes) |
| 5523 | bbio->stripes[i].length += |
| 5524 | map->stripe_len; |
| 5525 | |
| 5526 | /* |
| 5527 | * Special for the first stripe and |
| 5528 | * the last stripe: |
| 5529 | * |
| 5530 | * |-------|...|-------| |
| 5531 | * |----------| |
| 5532 | * off end_off |
| 5533 | */ |
| 5534 | if (i < sub_stripes) |
| 5535 | bbio->stripes[i].length -= |
| 5536 | stripe_offset; |
| 5537 | |
| 5538 | if (stripe_index >= last_stripe && |
| 5539 | stripe_index <= (last_stripe + |
| 5540 | sub_stripes - 1)) |
| 5541 | bbio->stripes[i].length -= |
| 5542 | stripe_end_offset; |
| 5543 | |
| 5544 | if (i == sub_stripes - 1) |
| 5545 | stripe_offset = 0; |
| 5546 | } else { |
| 5547 | bbio->stripes[i].length = length; |
| 5548 | } |
| 5549 | |
| 5550 | stripe_index++; |
| 5551 | if (stripe_index == map->num_stripes) { |
| 5552 | stripe_index = 0; |
| 5553 | stripe_nr++; |
| 5554 | } |
| 5555 | } |
| 5556 | |
| 5557 | *bbio_ret = bbio; |
| 5558 | bbio->map_type = map->type; |
| 5559 | bbio->num_stripes = num_stripes; |
| 5560 | out: |
| 5561 | free_extent_map(em); |
| 5562 | return ret; |
| 5563 | } |
| 5564 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5565 | /* |
| 5566 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5567 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5568 | * left cursor can also be read from the target drive. |
| 5569 | * |
| 5570 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5571 | * array of stripes. |
| 5572 | * For READ, it also needs to be supported using the same mirror number. |
| 5573 | * |
| 5574 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5575 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5576 | * case. |
| 5577 | */ |
| 5578 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5579 | u64 logical, u64 length, |
| 5580 | u64 srcdev_devid, int *mirror_num, |
| 5581 | u64 *physical) |
| 5582 | { |
| 5583 | struct btrfs_bio *bbio = NULL; |
| 5584 | int num_stripes; |
| 5585 | int index_srcdev = 0; |
| 5586 | int found = 0; |
| 5587 | u64 physical_of_found = 0; |
| 5588 | int i; |
| 5589 | int ret = 0; |
| 5590 | |
| 5591 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5592 | logical, &length, &bbio, 0, 0); |
| 5593 | if (ret) { |
| 5594 | ASSERT(bbio == NULL); |
| 5595 | return ret; |
| 5596 | } |
| 5597 | |
| 5598 | num_stripes = bbio->num_stripes; |
| 5599 | if (*mirror_num > num_stripes) { |
| 5600 | /* |
| 5601 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5602 | * that means that the requested area is not left of the left |
| 5603 | * cursor |
| 5604 | */ |
| 5605 | btrfs_put_bbio(bbio); |
| 5606 | return -EIO; |
| 5607 | } |
| 5608 | |
| 5609 | /* |
| 5610 | * process the rest of the function using the mirror_num of the source |
| 5611 | * drive. Therefore look it up first. At the end, patch the device |
| 5612 | * pointer to the one of the target drive. |
| 5613 | */ |
| 5614 | for (i = 0; i < num_stripes; i++) { |
| 5615 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5616 | continue; |
| 5617 | |
| 5618 | /* |
| 5619 | * In case of DUP, in order to keep it simple, only add the |
| 5620 | * mirror with the lowest physical address |
| 5621 | */ |
| 5622 | if (found && |
| 5623 | physical_of_found <= bbio->stripes[i].physical) |
| 5624 | continue; |
| 5625 | |
| 5626 | index_srcdev = i; |
| 5627 | found = 1; |
| 5628 | physical_of_found = bbio->stripes[i].physical; |
| 5629 | } |
| 5630 | |
| 5631 | btrfs_put_bbio(bbio); |
| 5632 | |
| 5633 | ASSERT(found); |
| 5634 | if (!found) |
| 5635 | return -EIO; |
| 5636 | |
| 5637 | *mirror_num = index_srcdev + 1; |
| 5638 | *physical = physical_of_found; |
| 5639 | return ret; |
| 5640 | } |
| 5641 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5642 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5643 | struct btrfs_bio **bbio_ret, |
| 5644 | struct btrfs_dev_replace *dev_replace, |
| 5645 | int *num_stripes_ret, int *max_errors_ret) |
| 5646 | { |
| 5647 | struct btrfs_bio *bbio = *bbio_ret; |
| 5648 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5649 | int tgtdev_indexes = 0; |
| 5650 | int num_stripes = *num_stripes_ret; |
| 5651 | int max_errors = *max_errors_ret; |
| 5652 | int i; |
| 5653 | |
| 5654 | if (op == BTRFS_MAP_WRITE) { |
| 5655 | int index_where_to_add; |
| 5656 | |
| 5657 | /* |
| 5658 | * duplicate the write operations while the dev replace |
| 5659 | * procedure is running. Since the copying of the old disk to |
| 5660 | * the new disk takes place at run time while the filesystem is |
| 5661 | * mounted writable, the regular write operations to the old |
| 5662 | * disk have to be duplicated to go to the new disk as well. |
| 5663 | * |
| 5664 | * Note that device->missing is handled by the caller, and that |
| 5665 | * the write to the old disk is already set up in the stripes |
| 5666 | * array. |
| 5667 | */ |
| 5668 | index_where_to_add = num_stripes; |
| 5669 | for (i = 0; i < num_stripes; i++) { |
| 5670 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5671 | /* write to new disk, too */ |
| 5672 | struct btrfs_bio_stripe *new = |
| 5673 | bbio->stripes + index_where_to_add; |
| 5674 | struct btrfs_bio_stripe *old = |
| 5675 | bbio->stripes + i; |
| 5676 | |
| 5677 | new->physical = old->physical; |
| 5678 | new->length = old->length; |
| 5679 | new->dev = dev_replace->tgtdev; |
| 5680 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5681 | index_where_to_add++; |
| 5682 | max_errors++; |
| 5683 | tgtdev_indexes++; |
| 5684 | } |
| 5685 | } |
| 5686 | num_stripes = index_where_to_add; |
| 5687 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5688 | int index_srcdev = 0; |
| 5689 | int found = 0; |
| 5690 | u64 physical_of_found = 0; |
| 5691 | |
| 5692 | /* |
| 5693 | * During the dev-replace procedure, the target drive can also |
| 5694 | * be used to read data in case it is needed to repair a corrupt |
| 5695 | * block elsewhere. This is possible if the requested area is |
| 5696 | * left of the left cursor. In this area, the target drive is a |
| 5697 | * full copy of the source drive. |
| 5698 | */ |
| 5699 | for (i = 0; i < num_stripes; i++) { |
| 5700 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5701 | /* |
| 5702 | * In case of DUP, in order to keep it simple, |
| 5703 | * only add the mirror with the lowest physical |
| 5704 | * address |
| 5705 | */ |
| 5706 | if (found && |
| 5707 | physical_of_found <= |
| 5708 | bbio->stripes[i].physical) |
| 5709 | continue; |
| 5710 | index_srcdev = i; |
| 5711 | found = 1; |
| 5712 | physical_of_found = bbio->stripes[i].physical; |
| 5713 | } |
| 5714 | } |
| 5715 | if (found) { |
| 5716 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5717 | bbio->stripes + num_stripes; |
| 5718 | |
| 5719 | tgtdev_stripe->physical = physical_of_found; |
| 5720 | tgtdev_stripe->length = |
| 5721 | bbio->stripes[index_srcdev].length; |
| 5722 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5723 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5724 | |
| 5725 | tgtdev_indexes++; |
| 5726 | num_stripes++; |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | *num_stripes_ret = num_stripes; |
| 5731 | *max_errors_ret = max_errors; |
| 5732 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5733 | *bbio_ret = bbio; |
| 5734 | } |
| 5735 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5736 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5737 | { |
| 5738 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5739 | } |
| 5740 | |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5741 | /* |
| 5742 | * btrfs_get_io_geometry - calculates the geomery of a particular (address, len) |
| 5743 | * tuple. This information is used to calculate how big a |
| 5744 | * particular bio can get before it straddles a stripe. |
| 5745 | * |
| 5746 | * @fs_info - the filesystem |
| 5747 | * @logical - address that we want to figure out the geometry of |
| 5748 | * @len - the length of IO we are going to perform, starting at @logical |
| 5749 | * @op - type of operation - write or read |
| 5750 | * @io_geom - pointer used to return values |
| 5751 | * |
| 5752 | * Returns < 0 in case a chunk for the given logical address cannot be found, |
| 5753 | * usually shouldn't happen unless @logical is corrupted, 0 otherwise. |
| 5754 | */ |
| 5755 | 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] | 5756 | u64 logical, u64 len, struct btrfs_io_geometry *io_geom) |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5757 | { |
| 5758 | struct extent_map *em; |
| 5759 | struct map_lookup *map; |
| 5760 | u64 offset; |
| 5761 | u64 stripe_offset; |
| 5762 | u64 stripe_nr; |
| 5763 | u64 stripe_len; |
| 5764 | u64 raid56_full_stripe_start = (u64)-1; |
| 5765 | int data_stripes; |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5766 | int ret = 0; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5767 | |
| 5768 | ASSERT(op != BTRFS_MAP_DISCARD); |
| 5769 | |
| 5770 | em = btrfs_get_chunk_map(fs_info, logical, len); |
| 5771 | if (IS_ERR(em)) |
| 5772 | return PTR_ERR(em); |
| 5773 | |
| 5774 | map = em->map_lookup; |
| 5775 | /* Offset of this logical address in the chunk */ |
| 5776 | offset = logical - em->start; |
| 5777 | /* Len of a stripe in a chunk */ |
| 5778 | stripe_len = map->stripe_len; |
| 5779 | /* Stripe wher this block falls in */ |
| 5780 | stripe_nr = div64_u64(offset, stripe_len); |
| 5781 | /* Offset of stripe in the chunk */ |
| 5782 | stripe_offset = stripe_nr * stripe_len; |
| 5783 | if (offset < stripe_offset) { |
| 5784 | btrfs_crit(fs_info, |
| 5785 | "stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu", |
| 5786 | stripe_offset, offset, em->start, logical, stripe_len); |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5787 | ret = -EINVAL; |
| 5788 | goto out; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | /* stripe_offset is the offset of this block in its stripe */ |
| 5792 | stripe_offset = offset - stripe_offset; |
| 5793 | data_stripes = nr_data_stripes(map); |
| 5794 | |
| 5795 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5796 | u64 max_len = stripe_len - stripe_offset; |
| 5797 | |
| 5798 | /* |
| 5799 | * In case of raid56, we need to know the stripe aligned start |
| 5800 | */ |
| 5801 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5802 | unsigned long full_stripe_len = stripe_len * data_stripes; |
| 5803 | raid56_full_stripe_start = offset; |
| 5804 | |
| 5805 | /* |
| 5806 | * Allow a write of a full stripe, but make sure we |
| 5807 | * don't allow straddling of stripes |
| 5808 | */ |
| 5809 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5810 | full_stripe_len); |
| 5811 | raid56_full_stripe_start *= full_stripe_len; |
| 5812 | |
| 5813 | /* |
| 5814 | * For writes to RAID[56], allow a full stripeset across |
| 5815 | * all disks. For other RAID types and for RAID[56] |
| 5816 | * reads, just allow a single stripe (on a single disk). |
| 5817 | */ |
| 5818 | if (op == BTRFS_MAP_WRITE) { |
| 5819 | max_len = stripe_len * data_stripes - |
| 5820 | (offset - raid56_full_stripe_start); |
| 5821 | } |
| 5822 | } |
| 5823 | len = min_t(u64, em->len - offset, max_len); |
| 5824 | } else { |
| 5825 | len = em->len - offset; |
| 5826 | } |
| 5827 | |
| 5828 | io_geom->len = len; |
| 5829 | io_geom->offset = offset; |
| 5830 | io_geom->stripe_len = stripe_len; |
| 5831 | io_geom->stripe_nr = stripe_nr; |
| 5832 | io_geom->stripe_offset = stripe_offset; |
| 5833 | io_geom->raid56_stripe_offset = raid56_full_stripe_start; |
| 5834 | |
Johannes Thumshirn | 373c3b8 | 2019-07-15 15:16:12 +0200 | [diff] [blame] | 5835 | out: |
| 5836 | /* once for us */ |
| 5837 | free_extent_map(em); |
| 5838 | return ret; |
Nikolay Borisov | 5f14112 | 2019-06-03 12:05:03 +0300 | [diff] [blame] | 5839 | } |
| 5840 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5841 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 5842 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5843 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5844 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5845 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5846 | { |
| 5847 | struct extent_map *em; |
| 5848 | struct map_lookup *map; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5849 | u64 stripe_offset; |
| 5850 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5851 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5852 | u32 stripe_index; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5853 | int data_stripes; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5854 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5855 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5856 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5857 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5858 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5859 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5860 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5861 | int dev_replace_is_ongoing = 0; |
| 5862 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5863 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5864 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5865 | u64 raid56_full_stripe_start = (u64)-1; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5866 | struct btrfs_io_geometry geom; |
| 5867 | |
| 5868 | ASSERT(bbio_ret); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5869 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5870 | if (op == BTRFS_MAP_DISCARD) |
| 5871 | return __btrfs_map_block_for_discard(fs_info, logical, |
Qu Wenruo | 6b7faad | 2019-10-23 21:57:27 +0800 | [diff] [blame] | 5872 | length, bbio_ret); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5873 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5874 | ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom); |
| 5875 | if (ret < 0) |
| 5876 | return ret; |
| 5877 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5878 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Dan Carpenter | f113698 | 2019-08-09 17:07:39 +0300 | [diff] [blame] | 5879 | ASSERT(!IS_ERR(em)); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5880 | map = em->map_lookup; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5881 | |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5882 | *length = geom.len; |
Nikolay Borisov | 89b798a | 2019-06-03 12:05:05 +0300 | [diff] [blame] | 5883 | stripe_len = geom.stripe_len; |
| 5884 | stripe_nr = geom.stripe_nr; |
| 5885 | stripe_offset = geom.stripe_offset; |
| 5886 | raid56_full_stripe_start = geom.raid56_stripe_offset; |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5887 | data_stripes = nr_data_stripes(map); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5888 | |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5889 | down_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5890 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 5891 | /* |
| 5892 | * Hold the semaphore for read during the whole operation, write is |
| 5893 | * requested at commit time but must wait. |
| 5894 | */ |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5895 | if (!dev_replace_is_ongoing) |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 5896 | up_read(&dev_replace->rwsem); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5897 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5898 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5899 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5900 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 5901 | dev_replace->srcdev->devid, |
| 5902 | &mirror_num, |
| 5903 | &physical_to_patch_in_first_stripe); |
| 5904 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5905 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5906 | else |
| 5907 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5908 | } else if (mirror_num > map->num_stripes) { |
| 5909 | mirror_num = 0; |
| 5910 | } |
| 5911 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5912 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5913 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5914 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5915 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5916 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5917 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5918 | mirror_num = 1; |
David Sterba | c7369b3 | 2019-05-31 15:39:31 +0200 | [diff] [blame] | 5919 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5920 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5921 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5922 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5923 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5924 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5925 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5926 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5927 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5928 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5929 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5930 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5931 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5932 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5933 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5934 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5935 | } else { |
| 5936 | mirror_num = 1; |
| 5937 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5938 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5939 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5940 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5941 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5942 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5943 | stripe_index *= map->sub_stripes; |
| 5944 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5945 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5946 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5947 | else if (mirror_num) |
| 5948 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5949 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5950 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5951 | stripe_index = find_live_mirror(fs_info, map, |
| 5952 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5953 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5954 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5955 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5956 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5957 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5958 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5959 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5960 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5961 | stripe_len * data_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5962 | |
| 5963 | /* RAID[56] write or recovery. Return all stripes */ |
| 5964 | num_stripes = map->num_stripes; |
| 5965 | max_errors = nr_parity_stripes(map); |
| 5966 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5967 | *length = map->stripe_len; |
| 5968 | stripe_index = 0; |
| 5969 | stripe_offset = 0; |
| 5970 | } else { |
| 5971 | /* |
| 5972 | * Mirror #0 or #1 means the original data block. |
| 5973 | * Mirror #2 is RAID5 parity block. |
| 5974 | * Mirror #3 is RAID6 Q block. |
| 5975 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5976 | stripe_nr = div_u64_rem(stripe_nr, |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5977 | data_stripes, &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5978 | if (mirror_num > 1) |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 5979 | stripe_index = data_stripes + mirror_num - 2; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5980 | |
| 5981 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5982 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5983 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5984 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5985 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5986 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5987 | } else { |
| 5988 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5989 | * after this, stripe_nr is the number of stripes on this |
| 5990 | * device we have to walk to find the data, and stripe_index is |
| 5991 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5992 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5993 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5994 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5995 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5996 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5997 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5998 | btrfs_crit(fs_info, |
| 5999 | "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] | 6000 | stripe_index, map->num_stripes); |
| 6001 | ret = -EINVAL; |
| 6002 | goto out; |
| 6003 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6004 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6005 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6006 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6007 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6008 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6009 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6010 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6011 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6012 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6013 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6014 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6015 | if (!bbio) { |
| 6016 | ret = -ENOMEM; |
| 6017 | goto out; |
| 6018 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 6019 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 6020 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6021 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6022 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6023 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 6024 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6025 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 6026 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6027 | |
| 6028 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 6029 | sizeof(struct btrfs_bio_stripe) * |
| 6030 | num_alloc_stripes + |
| 6031 | sizeof(int) * tgtdev_indexes); |
| 6032 | |
| 6033 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 6034 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6035 | |
| 6036 | /* Fill in the logical address of each stripe */ |
David Sterba | cff8267 | 2019-05-17 11:43:45 +0200 | [diff] [blame] | 6037 | tmp = stripe_nr * data_stripes; |
| 6038 | for (i = 0; i < data_stripes; i++) |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6039 | bbio->raid_map[(i+rot) % num_stripes] = |
| 6040 | em->start + (tmp + i) * map->stripe_len; |
| 6041 | |
| 6042 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 6043 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 6044 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 6045 | RAID6_Q_STRIPE; |
| 6046 | } |
| 6047 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 6048 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 6049 | for (i = 0; i < num_stripes; i++) { |
| 6050 | bbio->stripes[i].physical = |
| 6051 | map->stripes[stripe_index].physical + |
| 6052 | stripe_offset + |
| 6053 | stripe_nr * map->stripe_len; |
| 6054 | bbio->stripes[i].dev = |
| 6055 | map->stripes[stripe_index].dev; |
| 6056 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6057 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6058 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6059 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 6060 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6061 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6062 | if (bbio->raid_map) |
| 6063 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 6064 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6065 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 6066 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 6067 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 6068 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 6069 | } |
| 6070 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6071 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 6072 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6073 | bbio->num_stripes = num_stripes; |
| 6074 | bbio->max_errors = max_errors; |
| 6075 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 6076 | |
| 6077 | /* |
| 6078 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 6079 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6080 | * available as a mirror |
| 6081 | */ |
| 6082 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6083 | WARN_ON(num_stripes > 1); |
| 6084 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6085 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6086 | bbio->mirror_num = map->num_stripes + 1; |
| 6087 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6088 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6089 | if (dev_replace_is_ongoing) { |
David Sterba | 53176dd | 2018-04-05 01:41:06 +0200 | [diff] [blame] | 6090 | lockdep_assert_held(&dev_replace->rwsem); |
| 6091 | /* Unlock and let waiting writers proceed */ |
David Sterba | cb5583d | 2018-09-07 16:11:23 +0200 | [diff] [blame] | 6092 | up_read(&dev_replace->rwsem); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6093 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6094 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6095 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6096 | } |
| 6097 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6098 | 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] | 6099 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6100 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6101 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6102 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6103 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6104 | } |
| 6105 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6106 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6107 | 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] | 6108 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6109 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6110 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6111 | 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] | 6112 | } |
| 6113 | |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 6114 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 6115 | u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6116 | { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6117 | struct extent_map *em; |
| 6118 | struct map_lookup *map; |
| 6119 | u64 *buf; |
| 6120 | u64 bytenr; |
| 6121 | u64 length; |
| 6122 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6123 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6124 | int i, j, nr = 0; |
| 6125 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6126 | em = btrfs_get_chunk_map(fs_info, chunk_start, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 6127 | if (IS_ERR(em)) |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6128 | return -EIO; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6129 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6130 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6131 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6132 | rmap_len = map->stripe_len; |
| 6133 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6134 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6135 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6136 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6137 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6138 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6139 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6140 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 6141 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6142 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 6143 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6144 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6145 | |
| 6146 | for (i = 0; i < map->num_stripes; i++) { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6147 | if (map->stripes[i].physical > physical || |
| 6148 | map->stripes[i].physical + length <= physical) |
| 6149 | continue; |
| 6150 | |
| 6151 | stripe_nr = physical - map->stripes[i].physical; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6152 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6153 | |
| 6154 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 6155 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6156 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6157 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 6158 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6159 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 6160 | * Alternatively, just use rmap_len below instead of |
| 6161 | * map->stripe_len */ |
| 6162 | |
| 6163 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6164 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6165 | for (j = 0; j < nr; j++) { |
| 6166 | if (buf[j] == bytenr) |
| 6167 | break; |
| 6168 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6169 | if (j == nr) { |
| 6170 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6171 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6172 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6173 | } |
| 6174 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6175 | *logical = buf; |
| 6176 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6177 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6178 | |
| 6179 | free_extent_map(em); |
| 6180 | return 0; |
| 6181 | } |
| 6182 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6183 | 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] | 6184 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6185 | bio->bi_private = bbio->private; |
| 6186 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6187 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6188 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6189 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6190 | } |
| 6191 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6192 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6193 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6194 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6195 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6196 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6197 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6198 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6199 | if (bio->bi_status == BLK_STS_IOERR || |
| 6200 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6201 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6202 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6203 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6204 | |
| 6205 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6206 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6207 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6208 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6209 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6210 | BTRFS_DEV_STAT_WRITE_ERRS); |
David Sterba | 0cc068e | 2019-03-07 15:40:50 +0100 | [diff] [blame] | 6211 | else if (!(bio->bi_opf & REQ_RAHEAD)) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6212 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6213 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6214 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6215 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6216 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6217 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6218 | } |
| 6219 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6220 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6221 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6222 | is_orig_bio = 1; |
| 6223 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6224 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6225 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6226 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6227 | if (!is_orig_bio) { |
| 6228 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6229 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6230 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6231 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6232 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6233 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6234 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6235 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6236 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6237 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6238 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6239 | /* |
| 6240 | * this bio is actually up to date, we didn't |
| 6241 | * go over the max number of errors |
| 6242 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6243 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6244 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6245 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6246 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6247 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6248 | bio_put(bio); |
| 6249 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6250 | } |
| 6251 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6252 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6253 | u64 physical, int dev_nr) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6254 | { |
| 6255 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6256 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6257 | |
| 6258 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6259 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6260 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6261 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6262 | btrfs_debug_in_rcu(fs_info, |
| 6263 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6264 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
| 6265 | (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid, |
| 6266 | bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6267 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6268 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6269 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6270 | |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6271 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6272 | } |
| 6273 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6274 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6275 | { |
| 6276 | atomic_inc(&bbio->error); |
| 6277 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6278 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6279 | WARN_ON(bio != bbio->orig_bio); |
| 6280 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6281 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6282 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6283 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6284 | bio->bi_status = BLK_STS_IOERR; |
| 6285 | else |
| 6286 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6287 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6288 | } |
| 6289 | } |
| 6290 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6291 | 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] | 6292 | int mirror_num) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6293 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6294 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6295 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6296 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6297 | u64 length = 0; |
| 6298 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6299 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6300 | int dev_nr; |
| 6301 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6302 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6303 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6304 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6305 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6306 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6307 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6308 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6309 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6310 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6311 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6312 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6313 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6314 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6315 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6316 | bbio->orig_bio = first_bio; |
| 6317 | bbio->private = first_bio->bi_private; |
| 6318 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6319 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6320 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6321 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6322 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6323 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6324 | /* In this case, map_length has been set to the length of |
| 6325 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6326 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6327 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6328 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6329 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6330 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6331 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6332 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6333 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6334 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6335 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6336 | } |
| 6337 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6338 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6339 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6340 | "mapping failed logical %llu bio len %llu len %llu", |
| 6341 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6342 | BUG(); |
| 6343 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6344 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6345 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6346 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6347 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6348 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6349 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6350 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6351 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6352 | continue; |
| 6353 | } |
| 6354 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6355 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6356 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6357 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6358 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6359 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6360 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
Chris Mason | 08635ba | 2019-07-10 12:28:14 -0700 | [diff] [blame] | 6361 | dev_nr); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6362 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6363 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6364 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6365 | } |
| 6366 | |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6367 | /* |
| 6368 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 6369 | * return NULL. |
| 6370 | * |
| 6371 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 6372 | * only devid is used. |
| 6373 | * |
| 6374 | * If @seed is true, traverse through the seed devices. |
| 6375 | */ |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6376 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6377 | u64 devid, u8 *uuid, u8 *fsid, |
| 6378 | bool seed) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6379 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6380 | struct btrfs_device *device; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6381 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6382 | while (fs_devices) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6383 | if (!fsid || |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6384 | !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6385 | list_for_each_entry(device, &fs_devices->devices, |
| 6386 | dev_list) { |
| 6387 | if (device->devid == devid && |
| 6388 | (!uuid || memcmp(device->uuid, uuid, |
| 6389 | BTRFS_UUID_SIZE) == 0)) |
| 6390 | return device; |
| 6391 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6392 | } |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6393 | if (seed) |
| 6394 | fs_devices = fs_devices->seed; |
| 6395 | else |
| 6396 | return NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6397 | } |
| 6398 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6399 | } |
| 6400 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6401 | 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] | 6402 | u64 devid, u8 *dev_uuid) |
| 6403 | { |
| 6404 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6405 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6406 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6407 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6408 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6409 | |
| 6410 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6411 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6412 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6413 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6414 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6415 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6416 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6417 | return device; |
| 6418 | } |
| 6419 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6420 | /** |
| 6421 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6422 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6423 | * devid is provided (i.e. @devid != NULL). |
| 6424 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6425 | * is generated. |
| 6426 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6427 | * is generated. |
| 6428 | * |
| 6429 | * 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] | 6430 | * 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] | 6431 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6432 | */ |
| 6433 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6434 | const u64 *devid, |
| 6435 | const u8 *uuid) |
| 6436 | { |
| 6437 | struct btrfs_device *dev; |
| 6438 | u64 tmp; |
| 6439 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6440 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6441 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6442 | |
| 6443 | dev = __alloc_device(); |
| 6444 | if (IS_ERR(dev)) |
| 6445 | return dev; |
| 6446 | |
| 6447 | if (devid) |
| 6448 | tmp = *devid; |
| 6449 | else { |
| 6450 | int ret; |
| 6451 | |
| 6452 | ret = find_next_devid(fs_info, &tmp); |
| 6453 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6454 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6455 | return ERR_PTR(ret); |
| 6456 | } |
| 6457 | } |
| 6458 | dev->devid = tmp; |
| 6459 | |
| 6460 | if (uuid) |
| 6461 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6462 | else |
| 6463 | generate_random_uuid(dev->uuid); |
| 6464 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6465 | return dev; |
| 6466 | } |
| 6467 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6468 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6469 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6470 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6471 | if (error) |
| 6472 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6473 | devid, uuid); |
| 6474 | else |
| 6475 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6476 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6477 | } |
| 6478 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6479 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 6480 | { |
| 6481 | int index = btrfs_bg_flags_to_raid_index(type); |
| 6482 | int ncopies = btrfs_raid_array[index].ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6483 | const int nparity = btrfs_raid_array[index].nparity; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6484 | int data_stripes; |
| 6485 | |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6486 | if (nparity) |
| 6487 | data_stripes = num_stripes - nparity; |
| 6488 | else |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6489 | data_stripes = num_stripes / ncopies; |
David Sterba | e4f6c6b | 2019-05-14 01:59:54 +0200 | [diff] [blame] | 6490 | |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6491 | return div_u64(chunk_len, data_stripes); |
| 6492 | } |
| 6493 | |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6494 | 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] | 6495 | struct btrfs_chunk *chunk) |
| 6496 | { |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 6497 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6498 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6499 | struct map_lookup *map; |
| 6500 | struct extent_map *em; |
| 6501 | u64 logical; |
| 6502 | u64 length; |
| 6503 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6504 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6505 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6506 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6507 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6508 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6509 | logical = key->offset; |
| 6510 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6511 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6512 | |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6513 | /* |
| 6514 | * Only need to verify chunk item if we're reading from sys chunk array, |
| 6515 | * as chunk item in tree block is already verified by tree-checker. |
| 6516 | */ |
| 6517 | if (leaf->start == BTRFS_SUPER_INFO_OFFSET) { |
David Sterba | ddaf1d5 | 2019-03-20 16:40:48 +0100 | [diff] [blame] | 6518 | ret = btrfs_check_chunk_valid(leaf, chunk, logical); |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 6519 | if (ret) |
| 6520 | return ret; |
| 6521 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6522 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6523 | read_lock(&map_tree->lock); |
| 6524 | em = lookup_extent_mapping(map_tree, logical, 1); |
| 6525 | read_unlock(&map_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6526 | |
| 6527 | /* already mapped? */ |
| 6528 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6529 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6530 | return 0; |
| 6531 | } else if (em) { |
| 6532 | free_extent_map(em); |
| 6533 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6534 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6535 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6536 | if (!em) |
| 6537 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6538 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6539 | if (!map) { |
| 6540 | free_extent_map(em); |
| 6541 | return -ENOMEM; |
| 6542 | } |
| 6543 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6544 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6545 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6546 | em->start = logical; |
| 6547 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6548 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6549 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6550 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6551 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6552 | map->num_stripes = num_stripes; |
| 6553 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6554 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6555 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6556 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6557 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6558 | map->verified_stripes = 0; |
Nikolay Borisov | 39e264a | 2019-03-25 14:31:25 +0200 | [diff] [blame] | 6559 | em->orig_block_len = calc_stripe_length(map->type, em->len, |
| 6560 | map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6561 | for (i = 0; i < num_stripes; i++) { |
| 6562 | map->stripes[i].physical = |
| 6563 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6564 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6565 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6566 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6567 | BTRFS_UUID_SIZE); |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6568 | map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6569 | devid, uuid, NULL, true); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6570 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6571 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6572 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6573 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6574 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6575 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6576 | if (!map->stripes[i].dev) { |
| 6577 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6578 | add_missing_dev(fs_info->fs_devices, devid, |
| 6579 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6580 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6581 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6582 | btrfs_err(fs_info, |
| 6583 | "failed to init missing dev %llu: %ld", |
| 6584 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6585 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6586 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6587 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6588 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6589 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6590 | &(map->stripes[i].dev->dev_state)); |
| 6591 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6592 | } |
| 6593 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6594 | write_lock(&map_tree->lock); |
| 6595 | ret = add_extent_mapping(map_tree, em, 0); |
| 6596 | write_unlock(&map_tree->lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6597 | if (ret < 0) { |
| 6598 | btrfs_err(fs_info, |
| 6599 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6600 | em->start, em->len, ret); |
| 6601 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6602 | free_extent_map(em); |
| 6603 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6604 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6605 | } |
| 6606 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6607 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6608 | struct btrfs_dev_item *dev_item, |
| 6609 | struct btrfs_device *device) |
| 6610 | { |
| 6611 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6612 | |
| 6613 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6614 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6615 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6616 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6617 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6618 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6619 | device->type = btrfs_device_type(leaf, dev_item); |
| 6620 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6621 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6622 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6623 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6624 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6625 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6626 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6627 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6628 | } |
| 6629 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6630 | 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] | 6631 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6632 | { |
| 6633 | struct btrfs_fs_devices *fs_devices; |
| 6634 | int ret; |
| 6635 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6636 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6637 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6638 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6639 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6640 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6641 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6642 | return fs_devices; |
| 6643 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6644 | fs_devices = fs_devices->seed; |
| 6645 | } |
| 6646 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6647 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6648 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6649 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6650 | return ERR_PTR(-ENOENT); |
| 6651 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6652 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6653 | if (IS_ERR(fs_devices)) |
| 6654 | return fs_devices; |
| 6655 | |
Johannes Thumshirn | 0395d84 | 2019-11-13 11:27:27 +0100 | [diff] [blame] | 6656 | fs_devices->seeding = true; |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6657 | fs_devices->opened = 1; |
| 6658 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6659 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6660 | |
| 6661 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6662 | if (IS_ERR(fs_devices)) |
| 6663 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6664 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6665 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6666 | if (ret) { |
| 6667 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6668 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6669 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6670 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6671 | |
| 6672 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6673 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6674 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6675 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6676 | goto out; |
| 6677 | } |
| 6678 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6679 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6680 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6681 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6682 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6683 | } |
| 6684 | |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6685 | static int read_one_dev(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6686 | struct btrfs_dev_item *dev_item) |
| 6687 | { |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 6688 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6689 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6690 | struct btrfs_device *device; |
| 6691 | u64 devid; |
| 6692 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6693 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6694 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6695 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6696 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6697 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6698 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6699 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6700 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6701 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6702 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6703 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6704 | if (IS_ERR(fs_devices)) |
| 6705 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6706 | } |
| 6707 | |
Anand Jain | e4319cd | 2019-01-17 23:32:31 +0800 | [diff] [blame] | 6708 | device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid, |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 6709 | fs_uuid, true); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6710 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6711 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6712 | btrfs_report_missing_device(fs_info, devid, |
| 6713 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6714 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6715 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6716 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6717 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6718 | if (IS_ERR(device)) { |
| 6719 | btrfs_err(fs_info, |
| 6720 | "failed to add missing dev %llu: %ld", |
| 6721 | devid, PTR_ERR(device)); |
| 6722 | return PTR_ERR(device); |
| 6723 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6724 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6725 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6726 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6727 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6728 | btrfs_report_missing_device(fs_info, |
| 6729 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6730 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6731 | } |
| 6732 | btrfs_report_missing_device(fs_info, devid, |
| 6733 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6734 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6735 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6736 | if (!device->bdev && |
| 6737 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6738 | /* |
| 6739 | * this happens when a device that was properly setup |
| 6740 | * in the device info lists suddenly goes bad. |
| 6741 | * device->bdev is NULL, and so we have to set |
| 6742 | * device->missing to one here |
| 6743 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6744 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6745 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6746 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6747 | |
| 6748 | /* Move the device to its own fs_devices */ |
| 6749 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6750 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6751 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6752 | |
| 6753 | list_move(&device->dev_list, &fs_devices->devices); |
| 6754 | device->fs_devices->num_devices--; |
| 6755 | fs_devices->num_devices++; |
| 6756 | |
| 6757 | device->fs_devices->missing_devices--; |
| 6758 | fs_devices->missing_devices++; |
| 6759 | |
| 6760 | device->fs_devices = fs_devices; |
| 6761 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6762 | } |
| 6763 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6764 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6765 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6766 | if (device->generation != |
| 6767 | btrfs_device_generation(leaf, dev_item)) |
| 6768 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6769 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6770 | |
| 6771 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6772 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6773 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6774 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6775 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6776 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6777 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6778 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6779 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6780 | return ret; |
| 6781 | } |
| 6782 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6783 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6784 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6785 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6786 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6787 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6788 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6789 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6790 | u8 *array_ptr; |
| 6791 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6792 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6793 | u32 num_stripes; |
| 6794 | u32 array_size; |
| 6795 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6796 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6797 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6798 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6799 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6800 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6801 | /* |
| 6802 | * This will create extent buffer of nodesize, superblock size is |
| 6803 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6804 | * overallocate but we can keep it as-is, only the first page is used. |
| 6805 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6806 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6807 | if (IS_ERR(sb)) |
| 6808 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6809 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6810 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6811 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6812 | * 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] | 6813 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6814 | * pages up-to-date when the page is larger: extent does not cover the |
| 6815 | * whole page and consequently check_page_uptodate does not find all |
| 6816 | * the page's extents up-to-date (the hole beyond sb), |
| 6817 | * write_extent_buffer then triggers a WARN_ON. |
| 6818 | * |
| 6819 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6820 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6821 | * to silence the warning eg. on PowerPC 64. |
| 6822 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6823 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6824 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6825 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6826 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6827 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6828 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6829 | array_ptr = super_copy->sys_chunk_array; |
| 6830 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6831 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6832 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6833 | while (cur_offset < array_size) { |
| 6834 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6835 | len = sizeof(*disk_key); |
| 6836 | if (cur_offset + len > array_size) |
| 6837 | goto out_short_read; |
| 6838 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6839 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6840 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6841 | array_ptr += len; |
| 6842 | sb_array_offset += len; |
| 6843 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6844 | |
Johannes Thumshirn | 32ab3d1 | 2019-10-18 11:58:23 +0200 | [diff] [blame] | 6845 | if (key.type != BTRFS_CHUNK_ITEM_KEY) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6846 | btrfs_err(fs_info, |
| 6847 | "unexpected item type %u in sys_array at offset %u", |
| 6848 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6849 | ret = -EIO; |
| 6850 | break; |
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 | |
| 6853 | chunk = (struct btrfs_chunk *)sb_array_offset; |
| 6854 | /* |
| 6855 | * At least one btrfs_chunk with one stripe must be present, |
| 6856 | * exact stripe count check comes afterwards |
| 6857 | */ |
| 6858 | len = btrfs_chunk_item_size(1); |
| 6859 | if (cur_offset + len > array_size) |
| 6860 | goto out_short_read; |
| 6861 | |
| 6862 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6863 | if (!num_stripes) { |
| 6864 | btrfs_err(fs_info, |
| 6865 | "invalid number of stripes %u in sys_array at offset %u", |
| 6866 | num_stripes, cur_offset); |
| 6867 | ret = -EIO; |
| 6868 | break; |
| 6869 | } |
| 6870 | |
| 6871 | type = btrfs_chunk_type(sb, chunk); |
| 6872 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
| 6873 | btrfs_err(fs_info, |
| 6874 | "invalid chunk type %llu in sys_array at offset %u", |
| 6875 | type, cur_offset); |
| 6876 | ret = -EIO; |
| 6877 | break; |
| 6878 | } |
| 6879 | |
| 6880 | len = btrfs_chunk_item_size(num_stripes); |
| 6881 | if (cur_offset + len > array_size) |
| 6882 | goto out_short_read; |
| 6883 | |
| 6884 | ret = read_one_chunk(&key, sb, chunk); |
| 6885 | if (ret) |
| 6886 | break; |
| 6887 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6888 | array_ptr += len; |
| 6889 | sb_array_offset += len; |
| 6890 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6891 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6892 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6893 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6894 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6895 | |
| 6896 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6897 | 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] | 6898 | len, cur_offset); |
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); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6901 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6902 | } |
| 6903 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6904 | /* |
| 6905 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 6906 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6907 | * If the @failing_dev is specified, it's accounted as missing. |
| 6908 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6909 | * Return true if all chunks meet the minimal RW mount requirements. |
| 6910 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 6911 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6912 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 6913 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6914 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6915 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6916 | struct extent_map *em; |
| 6917 | u64 next_start = 0; |
| 6918 | bool ret = true; |
| 6919 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6920 | read_lock(&map_tree->lock); |
| 6921 | em = lookup_extent_mapping(map_tree, 0, (u64)-1); |
| 6922 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6923 | /* No chunk at all? Return false anyway */ |
| 6924 | if (!em) { |
| 6925 | ret = false; |
| 6926 | goto out; |
| 6927 | } |
| 6928 | while (em) { |
| 6929 | struct map_lookup *map; |
| 6930 | int missing = 0; |
| 6931 | int max_tolerated; |
| 6932 | int i; |
| 6933 | |
| 6934 | map = em->map_lookup; |
| 6935 | max_tolerated = |
| 6936 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 6937 | map->type); |
| 6938 | for (i = 0; i < map->num_stripes; i++) { |
| 6939 | struct btrfs_device *dev = map->stripes[i].dev; |
| 6940 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6941 | if (!dev || !dev->bdev || |
| 6942 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6943 | dev->last_flush_error) |
| 6944 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6945 | else if (failing_dev && failing_dev == dev) |
| 6946 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6947 | } |
| 6948 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6949 | if (!failing_dev) |
| 6950 | btrfs_warn(fs_info, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 6951 | "chunk %llu missing %d devices, max tolerance is %d for writable mount", |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6952 | em->start, missing, max_tolerated); |
| 6953 | free_extent_map(em); |
| 6954 | ret = false; |
| 6955 | goto out; |
| 6956 | } |
| 6957 | next_start = extent_map_end(em); |
| 6958 | free_extent_map(em); |
| 6959 | |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6960 | read_lock(&map_tree->lock); |
| 6961 | em = lookup_extent_mapping(map_tree, next_start, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6962 | (u64)(-1) - next_start); |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 6963 | read_unlock(&map_tree->lock); |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6964 | } |
| 6965 | out: |
| 6966 | return ret; |
| 6967 | } |
| 6968 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6969 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6970 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6971 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6972 | struct btrfs_path *path; |
| 6973 | struct extent_buffer *leaf; |
| 6974 | struct btrfs_key key; |
| 6975 | struct btrfs_key found_key; |
| 6976 | int ret; |
| 6977 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6978 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6979 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6980 | path = btrfs_alloc_path(); |
| 6981 | if (!path) |
| 6982 | return -ENOMEM; |
| 6983 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 6984 | /* |
| 6985 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 6986 | * otherwise we don't need it. |
| 6987 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6988 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 6989 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6990 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6991 | /* |
| 6992 | * Read all device items, and then all the chunk items. All |
| 6993 | * device items are found before any chunk item (their object id |
| 6994 | * is smaller than the lowest possible object id for a chunk |
| 6995 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6996 | */ |
| 6997 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6998 | key.offset = 0; |
| 6999 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7000 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7001 | if (ret < 0) |
| 7002 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7003 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7004 | leaf = path->nodes[0]; |
| 7005 | slot = path->slots[0]; |
| 7006 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7007 | ret = btrfs_next_leaf(root, path); |
| 7008 | if (ret == 0) |
| 7009 | continue; |
| 7010 | if (ret < 0) |
| 7011 | goto error; |
| 7012 | break; |
| 7013 | } |
| 7014 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7015 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7016 | struct btrfs_dev_item *dev_item; |
| 7017 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7018 | struct btrfs_dev_item); |
David Sterba | 1785075 | 2019-03-20 16:45:15 +0100 | [diff] [blame] | 7019 | ret = read_one_dev(leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7020 | if (ret) |
| 7021 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7022 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7023 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7024 | struct btrfs_chunk *chunk; |
| 7025 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 9690ac0 | 2019-03-20 16:43:07 +0100 | [diff] [blame] | 7026 | ret = read_one_chunk(&found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7027 | if (ret) |
| 7028 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7029 | } |
| 7030 | path->slots[0]++; |
| 7031 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7032 | |
| 7033 | /* |
| 7034 | * After loading chunk tree, we've got all device information, |
| 7035 | * do another round of validation checks. |
| 7036 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7037 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7038 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7039 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7040 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7041 | total_dev); |
| 7042 | ret = -EINVAL; |
| 7043 | goto error; |
| 7044 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7045 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7046 | fs_info->fs_devices->total_rw_bytes) { |
| 7047 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7048 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7049 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7050 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7051 | ret = -EINVAL; |
| 7052 | goto error; |
| 7053 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7054 | ret = 0; |
| 7055 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7056 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7057 | mutex_unlock(&uuid_mutex); |
| 7058 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7059 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7060 | return ret; |
| 7061 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7062 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7063 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7064 | { |
| 7065 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7066 | struct btrfs_device *device; |
| 7067 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7068 | while (fs_devices) { |
| 7069 | mutex_lock(&fs_devices->device_list_mutex); |
| 7070 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7071 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7072 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7073 | |
| 7074 | fs_devices = fs_devices->seed; |
| 7075 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7076 | } |
| 7077 | |
David Sterba | 1dc990d | 2019-08-21 20:05:32 +0200 | [diff] [blame] | 7078 | static u64 btrfs_dev_stats_value(const struct extent_buffer *eb, |
| 7079 | const struct btrfs_dev_stats_item *ptr, |
| 7080 | int index) |
| 7081 | { |
| 7082 | u64 val; |
| 7083 | |
| 7084 | read_extent_buffer(eb, &val, |
| 7085 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7086 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7087 | sizeof(val)); |
| 7088 | return val; |
| 7089 | } |
| 7090 | |
| 7091 | static void btrfs_set_dev_stats_value(struct extent_buffer *eb, |
| 7092 | struct btrfs_dev_stats_item *ptr, |
| 7093 | int index, u64 val) |
| 7094 | { |
| 7095 | write_extent_buffer(eb, &val, |
| 7096 | offsetof(struct btrfs_dev_stats_item, values) + |
| 7097 | ((unsigned long)ptr) + (index * sizeof(u64)), |
| 7098 | sizeof(val)); |
| 7099 | } |
| 7100 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7101 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7102 | { |
| 7103 | struct btrfs_key key; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7104 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7105 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7106 | struct extent_buffer *eb; |
| 7107 | int slot; |
| 7108 | int ret = 0; |
| 7109 | struct btrfs_device *device; |
| 7110 | struct btrfs_path *path = NULL; |
| 7111 | int i; |
| 7112 | |
| 7113 | path = btrfs_alloc_path(); |
Anand Jain | 3b80a984 | 2019-08-21 17:26:32 +0800 | [diff] [blame] | 7114 | if (!path) |
| 7115 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7116 | |
| 7117 | mutex_lock(&fs_devices->device_list_mutex); |
| 7118 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7119 | int item_size; |
| 7120 | struct btrfs_dev_stats_item *ptr; |
| 7121 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7122 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7123 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7124 | key.offset = device->devid; |
| 7125 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7126 | if (ret) { |
Anand Jain | ae4b9b4 | 2019-08-07 16:21:20 +0800 | [diff] [blame] | 7127 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7128 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7129 | device->dev_stats_valid = 1; |
| 7130 | btrfs_release_path(path); |
| 7131 | continue; |
| 7132 | } |
| 7133 | slot = path->slots[0]; |
| 7134 | eb = path->nodes[0]; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7135 | item_size = btrfs_item_size_nr(eb, slot); |
| 7136 | |
| 7137 | ptr = btrfs_item_ptr(eb, slot, |
| 7138 | struct btrfs_dev_stats_item); |
| 7139 | |
| 7140 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7141 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7142 | btrfs_dev_stat_set(device, i, |
| 7143 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7144 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7145 | btrfs_dev_stat_set(device, i, 0); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7146 | } |
| 7147 | |
| 7148 | device->dev_stats_valid = 1; |
| 7149 | btrfs_dev_stat_print_on_load(device); |
| 7150 | btrfs_release_path(path); |
| 7151 | } |
| 7152 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7153 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7154 | btrfs_free_path(path); |
| 7155 | return ret < 0 ? ret : 0; |
| 7156 | } |
| 7157 | |
| 7158 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7159 | struct btrfs_device *device) |
| 7160 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7161 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7162 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7163 | struct btrfs_path *path; |
| 7164 | struct btrfs_key key; |
| 7165 | struct extent_buffer *eb; |
| 7166 | struct btrfs_dev_stats_item *ptr; |
| 7167 | int ret; |
| 7168 | int i; |
| 7169 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7170 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7171 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7172 | key.offset = device->devid; |
| 7173 | |
| 7174 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7175 | if (!path) |
| 7176 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7177 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7178 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7179 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7180 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7181 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7182 | goto out; |
| 7183 | } |
| 7184 | |
| 7185 | if (ret == 0 && |
| 7186 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7187 | /* need to delete old one and insert a new one */ |
| 7188 | ret = btrfs_del_item(trans, dev_root, path); |
| 7189 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7190 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7191 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7192 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7193 | goto out; |
| 7194 | } |
| 7195 | ret = 1; |
| 7196 | } |
| 7197 | |
| 7198 | if (ret == 1) { |
| 7199 | /* need to insert a new item */ |
| 7200 | btrfs_release_path(path); |
| 7201 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7202 | &key, sizeof(*ptr)); |
| 7203 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7204 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7205 | "insert dev_stats item for device %s failed %d", |
| 7206 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7207 | goto out; |
| 7208 | } |
| 7209 | } |
| 7210 | |
| 7211 | eb = path->nodes[0]; |
| 7212 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7213 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7214 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7215 | btrfs_dev_stat_read(device, i)); |
| 7216 | btrfs_mark_buffer_dirty(eb); |
| 7217 | |
| 7218 | out: |
| 7219 | btrfs_free_path(path); |
| 7220 | return ret; |
| 7221 | } |
| 7222 | |
| 7223 | /* |
| 7224 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7225 | */ |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7226 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7227 | { |
David Sterba | 196c9d8 | 2019-03-20 16:50:38 +0100 | [diff] [blame] | 7228 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7229 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7230 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7231 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7232 | int ret = 0; |
| 7233 | |
| 7234 | mutex_lock(&fs_devices->device_list_mutex); |
| 7235 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7236 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7237 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7238 | continue; |
| 7239 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7240 | |
| 7241 | /* |
| 7242 | * There is a LOAD-LOAD control dependency between the value of |
| 7243 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7244 | * reading the in-memory counters. Such control dependencies |
| 7245 | * require explicit read memory barriers. |
| 7246 | * |
| 7247 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7248 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7249 | * barrier implied by atomic_xchg in |
| 7250 | * btrfs_dev_stats_read_and_reset |
| 7251 | */ |
| 7252 | smp_rmb(); |
| 7253 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7254 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7255 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7256 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7257 | } |
| 7258 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7259 | |
| 7260 | return ret; |
| 7261 | } |
| 7262 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7263 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7264 | { |
| 7265 | btrfs_dev_stat_inc(dev, index); |
| 7266 | btrfs_dev_stat_print_on_error(dev); |
| 7267 | } |
| 7268 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7269 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7270 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7271 | if (!dev->dev_stats_valid) |
| 7272 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7273 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7274 | "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] | 7275 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7276 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7277 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7278 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7279 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7280 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7281 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7282 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7283 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7284 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7285 | int i; |
| 7286 | |
| 7287 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7288 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7289 | break; |
| 7290 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7291 | return; /* all values == 0, suppress message */ |
| 7292 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7293 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7294 | "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] | 7295 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7296 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7297 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7298 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7299 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7300 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7301 | } |
| 7302 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7303 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7304 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7305 | { |
| 7306 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7307 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7308 | int i; |
| 7309 | |
| 7310 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7311 | dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL, |
| 7312 | true); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7313 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7314 | |
| 7315 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7316 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7317 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7318 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7319 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7320 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7321 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7322 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7323 | if (stats->nr_items > i) |
| 7324 | stats->values[i] = |
| 7325 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7326 | else |
Anand Jain | 4e411a7 | 2019-08-07 16:21:19 +0800 | [diff] [blame] | 7327 | btrfs_dev_stat_set(dev, i, 0); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7328 | } |
| 7329 | } else { |
| 7330 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7331 | if (stats->nr_items > i) |
| 7332 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7333 | } |
| 7334 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7335 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7336 | return 0; |
| 7337 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7338 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 7339 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7340 | { |
| 7341 | struct buffer_head *bh; |
| 7342 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7343 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7344 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7345 | if (!bdev) |
| 7346 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7347 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7348 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 7349 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7350 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7351 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 7352 | continue; |
| 7353 | |
| 7354 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 7355 | |
| 7356 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 7357 | set_buffer_dirty(bh); |
| 7358 | sync_dirty_buffer(bh); |
| 7359 | brelse(bh); |
| 7360 | } |
| 7361 | |
| 7362 | /* Notify udev that device has changed */ |
| 7363 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 7364 | |
| 7365 | /* Update ctime/mtime for device path for libblkid */ |
| 7366 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7367 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7368 | |
| 7369 | /* |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7370 | * Update the size and bytes used for each device where it changed. This is |
| 7371 | * delayed since we would otherwise get errors while writing out the |
| 7372 | * superblocks. |
| 7373 | * |
| 7374 | * Must be invoked during transaction commit. |
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 | void btrfs_commit_device_sizes(struct btrfs_transaction *trans) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7377 | { |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7378 | struct btrfs_device *curr, *next; |
| 7379 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7380 | ASSERT(trans->state == TRANS_STATE_COMMIT_DOING); |
| 7381 | |
| 7382 | if (list_empty(&trans->dev_update_list)) |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7383 | return; |
| 7384 | |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7385 | /* |
| 7386 | * We don't need the device_list_mutex here. This list is owned by the |
| 7387 | * transaction and the transaction must complete before the device is |
| 7388 | * released. |
| 7389 | */ |
| 7390 | mutex_lock(&trans->fs_info->chunk_mutex); |
| 7391 | list_for_each_entry_safe(curr, next, &trans->dev_update_list, |
| 7392 | post_commit_list) { |
| 7393 | list_del_init(&curr->post_commit_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7394 | curr->commit_total_bytes = curr->disk_total_bytes; |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7395 | curr->commit_bytes_used = curr->bytes_used; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7396 | } |
Nikolay Borisov | bbbf724 | 2019-03-25 14:31:22 +0200 | [diff] [blame] | 7397 | mutex_unlock(&trans->fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7398 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7399 | |
| 7400 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7401 | { |
| 7402 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7403 | while (fs_devices) { |
| 7404 | fs_devices->fs_info = fs_info; |
| 7405 | fs_devices = fs_devices->seed; |
| 7406 | } |
| 7407 | } |
| 7408 | |
| 7409 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7410 | { |
| 7411 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7412 | while (fs_devices) { |
| 7413 | fs_devices->fs_info = NULL; |
| 7414 | fs_devices = fs_devices->seed; |
| 7415 | } |
| 7416 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7417 | |
| 7418 | /* |
| 7419 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7420 | */ |
| 7421 | int btrfs_bg_type_to_factor(u64 flags) |
| 7422 | { |
David Sterba | 44b28ad | 2019-05-17 11:43:31 +0200 | [diff] [blame] | 7423 | const int index = btrfs_bg_flags_to_raid_index(flags); |
| 7424 | |
| 7425 | return btrfs_raid_array[index].ncopies; |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7426 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7427 | |
| 7428 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7429 | |
| 7430 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7431 | u64 chunk_offset, u64 devid, |
| 7432 | u64 physical_offset, u64 physical_len) |
| 7433 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7434 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7435 | struct extent_map *em; |
| 7436 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7437 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7438 | u64 stripe_len; |
| 7439 | bool found = false; |
| 7440 | int ret = 0; |
| 7441 | int i; |
| 7442 | |
| 7443 | read_lock(&em_tree->lock); |
| 7444 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7445 | read_unlock(&em_tree->lock); |
| 7446 | |
| 7447 | if (!em) { |
| 7448 | btrfs_err(fs_info, |
| 7449 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7450 | physical_offset, devid); |
| 7451 | ret = -EUCLEAN; |
| 7452 | goto out; |
| 7453 | } |
| 7454 | |
| 7455 | map = em->map_lookup; |
| 7456 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7457 | if (physical_len != stripe_len) { |
| 7458 | btrfs_err(fs_info, |
| 7459 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7460 | physical_offset, devid, em->start, physical_len, |
| 7461 | stripe_len); |
| 7462 | ret = -EUCLEAN; |
| 7463 | goto out; |
| 7464 | } |
| 7465 | |
| 7466 | for (i = 0; i < map->num_stripes; i++) { |
| 7467 | if (map->stripes[i].dev->devid == devid && |
| 7468 | map->stripes[i].physical == physical_offset) { |
| 7469 | found = true; |
| 7470 | if (map->verified_stripes >= map->num_stripes) { |
| 7471 | btrfs_err(fs_info, |
| 7472 | "too many dev extents for chunk %llu found", |
| 7473 | em->start); |
| 7474 | ret = -EUCLEAN; |
| 7475 | goto out; |
| 7476 | } |
| 7477 | map->verified_stripes++; |
| 7478 | break; |
| 7479 | } |
| 7480 | } |
| 7481 | if (!found) { |
| 7482 | btrfs_err(fs_info, |
| 7483 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7484 | physical_offset, devid); |
| 7485 | ret = -EUCLEAN; |
| 7486 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7487 | |
| 7488 | /* Make sure no dev extent is beyond device bondary */ |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7489 | dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true); |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7490 | if (!dev) { |
| 7491 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7492 | ret = -EUCLEAN; |
| 7493 | goto out; |
| 7494 | } |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7495 | |
| 7496 | /* It's possible this device is a dummy for seed device */ |
| 7497 | if (dev->disk_total_bytes == 0) { |
Anand Jain | 09ba3bc | 2019-01-19 14:48:55 +0800 | [diff] [blame] | 7498 | dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL, |
| 7499 | NULL, false); |
Qu Wenruo | 1b3922a | 2019-01-08 14:08:18 +0800 | [diff] [blame] | 7500 | if (!dev) { |
| 7501 | btrfs_err(fs_info, "failed to find seed devid %llu", |
| 7502 | devid); |
| 7503 | ret = -EUCLEAN; |
| 7504 | goto out; |
| 7505 | } |
| 7506 | } |
| 7507 | |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7508 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7509 | btrfs_err(fs_info, |
| 7510 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7511 | devid, physical_offset, physical_len, |
| 7512 | dev->disk_total_bytes); |
| 7513 | ret = -EUCLEAN; |
| 7514 | goto out; |
| 7515 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7516 | out: |
| 7517 | free_extent_map(em); |
| 7518 | return ret; |
| 7519 | } |
| 7520 | |
| 7521 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7522 | { |
David Sterba | c8bf1b6 | 2019-05-17 11:43:17 +0200 | [diff] [blame] | 7523 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7524 | struct extent_map *em; |
| 7525 | struct rb_node *node; |
| 7526 | int ret = 0; |
| 7527 | |
| 7528 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7529 | 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] | 7530 | em = rb_entry(node, struct extent_map, rb_node); |
| 7531 | if (em->map_lookup->num_stripes != |
| 7532 | em->map_lookup->verified_stripes) { |
| 7533 | btrfs_err(fs_info, |
| 7534 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7535 | em->start, em->map_lookup->verified_stripes, |
| 7536 | em->map_lookup->num_stripes); |
| 7537 | ret = -EUCLEAN; |
| 7538 | goto out; |
| 7539 | } |
| 7540 | } |
| 7541 | out: |
| 7542 | read_unlock(&em_tree->lock); |
| 7543 | return ret; |
| 7544 | } |
| 7545 | |
| 7546 | /* |
| 7547 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7548 | * later chunk allocation/free would cause unexpected behavior. |
| 7549 | * |
| 7550 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7551 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7552 | */ |
| 7553 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7554 | { |
| 7555 | struct btrfs_path *path; |
| 7556 | struct btrfs_root *root = fs_info->dev_root; |
| 7557 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7558 | u64 prev_devid = 0; |
| 7559 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7560 | int ret = 0; |
| 7561 | |
| 7562 | key.objectid = 1; |
| 7563 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7564 | key.offset = 0; |
| 7565 | |
| 7566 | path = btrfs_alloc_path(); |
| 7567 | if (!path) |
| 7568 | return -ENOMEM; |
| 7569 | |
| 7570 | path->reada = READA_FORWARD; |
| 7571 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7572 | if (ret < 0) |
| 7573 | goto out; |
| 7574 | |
| 7575 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7576 | ret = btrfs_next_item(root, path); |
| 7577 | if (ret < 0) |
| 7578 | goto out; |
| 7579 | /* No dev extents at all? Not good */ |
| 7580 | if (ret > 0) { |
| 7581 | ret = -EUCLEAN; |
| 7582 | goto out; |
| 7583 | } |
| 7584 | } |
| 7585 | while (1) { |
| 7586 | struct extent_buffer *leaf = path->nodes[0]; |
| 7587 | struct btrfs_dev_extent *dext; |
| 7588 | int slot = path->slots[0]; |
| 7589 | u64 chunk_offset; |
| 7590 | u64 physical_offset; |
| 7591 | u64 physical_len; |
| 7592 | u64 devid; |
| 7593 | |
| 7594 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7595 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7596 | break; |
| 7597 | devid = key.objectid; |
| 7598 | physical_offset = key.offset; |
| 7599 | |
| 7600 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7601 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7602 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7603 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7604 | /* Check if this dev extent overlaps with the previous one */ |
| 7605 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7606 | btrfs_err(fs_info, |
| 7607 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7608 | devid, physical_offset, prev_dev_ext_end); |
| 7609 | ret = -EUCLEAN; |
| 7610 | goto out; |
| 7611 | } |
| 7612 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7613 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7614 | physical_offset, physical_len); |
| 7615 | if (ret < 0) |
| 7616 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7617 | prev_devid = devid; |
| 7618 | prev_dev_ext_end = physical_offset + physical_len; |
| 7619 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7620 | ret = btrfs_next_item(root, path); |
| 7621 | if (ret < 0) |
| 7622 | goto out; |
| 7623 | if (ret > 0) { |
| 7624 | ret = 0; |
| 7625 | break; |
| 7626 | } |
| 7627 | } |
| 7628 | |
| 7629 | /* Ensure all chunks have corresponding dev extents */ |
| 7630 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7631 | out: |
| 7632 | btrfs_free_path(path); |
| 7633 | return ret; |
| 7634 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7635 | |
| 7636 | /* |
| 7637 | * Check whether the given block group or device is pinned by any inode being |
| 7638 | * used as a swapfile. |
| 7639 | */ |
| 7640 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7641 | { |
| 7642 | struct btrfs_swapfile_pin *sp; |
| 7643 | struct rb_node *node; |
| 7644 | |
| 7645 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7646 | node = fs_info->swapfile_pins.rb_node; |
| 7647 | while (node) { |
| 7648 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7649 | if (ptr < sp->ptr) |
| 7650 | node = node->rb_left; |
| 7651 | else if (ptr > sp->ptr) |
| 7652 | node = node->rb_right; |
| 7653 | else |
| 7654 | break; |
| 7655 | } |
| 7656 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7657 | return node != NULL; |
| 7658 | } |