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> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 17 | #include "ctree.h" |
| 18 | #include "extent_map.h" |
| 19 | #include "disk-io.h" |
| 20 | #include "transaction.h" |
| 21 | #include "print-tree.h" |
| 22 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 23 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 24 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 25 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 26 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 27 | #include "math.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" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 30 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 31 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 32 | [BTRFS_RAID_RAID10] = { |
| 33 | .sub_stripes = 2, |
| 34 | .dev_stripes = 1, |
| 35 | .devs_max = 0, /* 0 == as many as possible */ |
| 36 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 37 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 38 | .devs_increment = 2, |
| 39 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 40 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 41 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 42 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 43 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 44 | }, |
| 45 | [BTRFS_RAID_RAID1] = { |
| 46 | .sub_stripes = 1, |
| 47 | .dev_stripes = 1, |
| 48 | .devs_max = 2, |
| 49 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 50 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 51 | .devs_increment = 2, |
| 52 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 53 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 54 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 55 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 56 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 57 | }, |
| 58 | [BTRFS_RAID_DUP] = { |
| 59 | .sub_stripes = 1, |
| 60 | .dev_stripes = 2, |
| 61 | .devs_max = 1, |
| 62 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 63 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 64 | .devs_increment = 1, |
| 65 | .ncopies = 2, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 66 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 67 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 68 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 69 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 70 | }, |
| 71 | [BTRFS_RAID_RAID0] = { |
| 72 | .sub_stripes = 1, |
| 73 | .dev_stripes = 1, |
| 74 | .devs_max = 0, |
| 75 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 76 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 77 | .devs_increment = 1, |
| 78 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 79 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 80 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 81 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 82 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 83 | }, |
| 84 | [BTRFS_RAID_SINGLE] = { |
| 85 | .sub_stripes = 1, |
| 86 | .dev_stripes = 1, |
| 87 | .devs_max = 1, |
| 88 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 89 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 90 | .devs_increment = 1, |
| 91 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 92 | .nparity = 0, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 93 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 94 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 95 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 96 | }, |
| 97 | [BTRFS_RAID_RAID5] = { |
| 98 | .sub_stripes = 1, |
| 99 | .dev_stripes = 1, |
| 100 | .devs_max = 0, |
| 101 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 102 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 103 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 104 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 105 | .nparity = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 106 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 107 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 108 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 109 | }, |
| 110 | [BTRFS_RAID_RAID6] = { |
| 111 | .sub_stripes = 1, |
| 112 | .dev_stripes = 1, |
| 113 | .devs_max = 0, |
| 114 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 115 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 116 | .devs_increment = 1, |
Hans van Kranenburg | da612e3 | 2018-10-04 23:24:41 +0200 | [diff] [blame] | 117 | .ncopies = 1, |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 118 | .nparity = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 119 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 120 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 121 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 122 | }, |
| 123 | }; |
| 124 | |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 125 | const char *get_raid_name(enum btrfs_raid_types type) |
| 126 | { |
| 127 | if (type >= BTRFS_NR_RAID_TYPES) |
| 128 | return NULL; |
| 129 | |
| 130 | return btrfs_raid_array[type].raid_name; |
| 131 | } |
| 132 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 133 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 134 | struct btrfs_fs_info *fs_info); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 135 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 136 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 137 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 138 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 139 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 140 | enum btrfs_map_op op, |
| 141 | u64 logical, u64 *length, |
| 142 | struct btrfs_bio **bbio_ret, |
| 143 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 144 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 145 | /* |
| 146 | * Device locking |
| 147 | * ============== |
| 148 | * |
| 149 | * There are several mutexes that protect manipulation of devices and low-level |
| 150 | * structures like chunks but not block groups, extents or files |
| 151 | * |
| 152 | * uuid_mutex (global lock) |
| 153 | * ------------------------ |
| 154 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 155 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 156 | * device) or requested by the device= mount option |
| 157 | * |
| 158 | * the mutex can be very coarse and can cover long-running operations |
| 159 | * |
| 160 | * protects: updates to fs_devices counters like missing devices, rw devices, |
| 161 | * seeding, structure cloning, openning/closing devices at mount/umount time |
| 162 | * |
| 163 | * global::fs_devs - add, remove, updates to the global list |
| 164 | * |
| 165 | * does not protect: manipulation of the fs_devices::devices list! |
| 166 | * |
| 167 | * btrfs_device::name - renames (write side), read is RCU |
| 168 | * |
| 169 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 170 | * ------------------------------------------------ |
| 171 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 172 | * |
| 173 | * simple list traversal with read-only actions can be done with RCU protection |
| 174 | * |
| 175 | * may be used to exclude some operations from running concurrently without any |
| 176 | * modifications to the list (see write_all_supers) |
| 177 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 178 | * balance_mutex |
| 179 | * ------------- |
| 180 | * protects balance structures (status, state) and context accessed from |
| 181 | * several places (internally, ioctl) |
| 182 | * |
| 183 | * chunk_mutex |
| 184 | * ----------- |
| 185 | * protects chunks, adding or removing during allocation, trim or when a new |
| 186 | * device is added/removed |
| 187 | * |
| 188 | * cleaner_mutex |
| 189 | * ------------- |
| 190 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 191 | * cleaning together with relocation or delayed iputs |
| 192 | * |
| 193 | * |
| 194 | * Lock nesting |
| 195 | * ============ |
| 196 | * |
| 197 | * uuid_mutex |
| 198 | * volume_mutex |
| 199 | * device_list_mutex |
| 200 | * chunk_mutex |
| 201 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 202 | * |
| 203 | * |
| 204 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 205 | * ====================================== |
| 206 | * |
| 207 | * Maintains the exclusivity of the following operations that apply to the |
| 208 | * whole filesystem and cannot run in parallel. |
| 209 | * |
| 210 | * - Balance (*) |
| 211 | * - Device add |
| 212 | * - Device remove |
| 213 | * - Device replace (*) |
| 214 | * - Resize |
| 215 | * |
| 216 | * The device operations (as above) can be in one of the following states: |
| 217 | * |
| 218 | * - Running state |
| 219 | * - Paused state |
| 220 | * - Completed state |
| 221 | * |
| 222 | * Only device operations marked with (*) can go into the Paused state for the |
| 223 | * following reasons: |
| 224 | * |
| 225 | * - ioctl (only Balance can be Paused through ioctl) |
| 226 | * - filesystem remounted as read-only |
| 227 | * - filesystem unmounted and mounted as read-only |
| 228 | * - system power-cycle and filesystem mounted as read-only |
| 229 | * - filesystem or device errors leading to forced read-only |
| 230 | * |
| 231 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 232 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 233 | * A device operation in Paused or Running state can be canceled or resumed |
| 234 | * either by ioctl (Balance only) or when remounted as read-write. |
| 235 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 236 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 237 | */ |
| 238 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 239 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 240 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 241 | struct list_head *btrfs_get_fs_uuids(void) |
| 242 | { |
| 243 | return &fs_uuids; |
| 244 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 245 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 246 | /* |
| 247 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 248 | * @fsid: if not NULL, copy the UUID to fs_devices::fsid |
| 249 | * @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] | 250 | * |
| 251 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 252 | * The returned struct is not linked onto any lists and can be destroyed with |
| 253 | * kfree() right away. |
| 254 | */ |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 255 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, |
| 256 | const u8 *metadata_fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 257 | { |
| 258 | struct btrfs_fs_devices *fs_devs; |
| 259 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 260 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 261 | if (!fs_devs) |
| 262 | return ERR_PTR(-ENOMEM); |
| 263 | |
| 264 | mutex_init(&fs_devs->device_list_mutex); |
| 265 | |
| 266 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 267 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 268 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 269 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 270 | if (fsid) |
| 271 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 272 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 273 | if (metadata_fsid) |
| 274 | memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE); |
| 275 | else if (fsid) |
| 276 | memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE); |
| 277 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 278 | return fs_devs; |
| 279 | } |
| 280 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 281 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 282 | { |
| 283 | rcu_string_free(device->name); |
| 284 | bio_put(device->flush_bio); |
| 285 | kfree(device); |
| 286 | } |
| 287 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 288 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 289 | { |
| 290 | struct btrfs_device *device; |
| 291 | WARN_ON(fs_devices->opened); |
| 292 | while (!list_empty(&fs_devices->devices)) { |
| 293 | device = list_entry(fs_devices->devices.next, |
| 294 | struct btrfs_device, dev_list); |
| 295 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 296 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 297 | } |
| 298 | kfree(fs_devices); |
| 299 | } |
| 300 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 301 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 302 | enum kobject_action action) |
| 303 | { |
| 304 | int ret; |
| 305 | |
| 306 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 307 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 308 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 309 | action, |
| 310 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 311 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 312 | } |
| 313 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 314 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 315 | { |
| 316 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 317 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 318 | while (!list_empty(&fs_uuids)) { |
| 319 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 320 | struct btrfs_fs_devices, fs_list); |
| 321 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 322 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 323 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 324 | } |
| 325 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 326 | /* |
| 327 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 328 | * 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] | 329 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 330 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 331 | static struct btrfs_device *__alloc_device(void) |
| 332 | { |
| 333 | struct btrfs_device *dev; |
| 334 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 335 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 336 | if (!dev) |
| 337 | return ERR_PTR(-ENOMEM); |
| 338 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 339 | /* |
| 340 | * Preallocate a bio that's always going to be used for flushing device |
| 341 | * barriers and matches the device lifespan |
| 342 | */ |
| 343 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 344 | if (!dev->flush_bio) { |
| 345 | kfree(dev); |
| 346 | return ERR_PTR(-ENOMEM); |
| 347 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 348 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 349 | INIT_LIST_HEAD(&dev->dev_list); |
| 350 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 351 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 352 | |
| 353 | spin_lock_init(&dev->io_lock); |
| 354 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 355 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 356 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 357 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 358 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 359 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 360 | |
| 361 | return dev; |
| 362 | } |
| 363 | |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 364 | /* |
| 365 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 366 | * return NULL. |
| 367 | * |
| 368 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 369 | * only devid is used. |
| 370 | */ |
| 371 | static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices, |
| 372 | u64 devid, const u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 373 | { |
| 374 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 375 | |
Anand Jain | 636d2c9 | 2018-04-12 10:29:29 +0800 | [diff] [blame] | 376 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 377 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 378 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 379 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 380 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 381 | } |
| 382 | return NULL; |
| 383 | } |
| 384 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 385 | static noinline struct btrfs_fs_devices *find_fsid( |
| 386 | const u8 *fsid, const u8 *metadata_fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 387 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 388 | struct btrfs_fs_devices *fs_devices; |
| 389 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 390 | ASSERT(fsid); |
| 391 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 392 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 393 | if (metadata_fsid) { |
| 394 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0 |
| 395 | && memcmp(metadata_fsid, fs_devices->metadata_uuid, |
| 396 | BTRFS_FSID_SIZE) == 0) |
| 397 | return fs_devices; |
| 398 | } else { |
| 399 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 400 | return fs_devices; |
| 401 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 402 | } |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 406 | static int |
| 407 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 408 | int flush, struct block_device **bdev, |
| 409 | struct buffer_head **bh) |
| 410 | { |
| 411 | int ret; |
| 412 | |
| 413 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 414 | |
| 415 | if (IS_ERR(*bdev)) { |
| 416 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 417 | goto error; |
| 418 | } |
| 419 | |
| 420 | if (flush) |
| 421 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 422 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 423 | if (ret) { |
| 424 | blkdev_put(*bdev, flags); |
| 425 | goto error; |
| 426 | } |
| 427 | invalidate_bdev(*bdev); |
| 428 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 429 | if (IS_ERR(*bh)) { |
| 430 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 431 | blkdev_put(*bdev, flags); |
| 432 | goto error; |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | |
| 437 | error: |
| 438 | *bdev = NULL; |
| 439 | *bh = NULL; |
| 440 | return ret; |
| 441 | } |
| 442 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 443 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 444 | struct bio *head, struct bio *tail) |
| 445 | { |
| 446 | |
| 447 | struct bio *old_head; |
| 448 | |
| 449 | old_head = pending_bios->head; |
| 450 | pending_bios->head = head; |
| 451 | if (pending_bios->tail) |
| 452 | tail->bi_next = old_head; |
| 453 | else |
| 454 | pending_bios->tail = tail; |
| 455 | } |
| 456 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 457 | /* |
| 458 | * we try to collect pending bios for a device so we don't get a large |
| 459 | * number of procs sending bios down to the same device. This greatly |
| 460 | * improves the schedulers ability to collect and merge the bios. |
| 461 | * |
| 462 | * But, it also turns into a long list of bios to process and that is sure |
| 463 | * to eventually make the worker thread block. The solution here is to |
| 464 | * make some progress and then put this work struct back at the end of |
| 465 | * the list if the block device is congested. This way, multiple devices |
| 466 | * can make progress from a single worker thread. |
| 467 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 468 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 469 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 470 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 471 | struct bio *pending; |
| 472 | struct backing_dev_info *bdi; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 473 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 474 | struct bio *tail; |
| 475 | struct bio *cur; |
| 476 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 477 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 478 | unsigned long batch_run = 0; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 479 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 480 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 481 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 482 | struct blk_plug plug; |
| 483 | |
| 484 | /* |
| 485 | * this function runs all the bios we've collected for |
| 486 | * a particular device. We don't want to wander off to |
| 487 | * another device without first sending all of these down. |
| 488 | * So, setup a plug here and finish it off before we return |
| 489 | */ |
| 490 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 491 | |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 492 | bdi = device->bdev->bd_bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 493 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 494 | loop: |
| 495 | spin_lock(&device->io_lock); |
| 496 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 497 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 498 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 499 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 500 | /* take all the bios off the list at once and process them |
| 501 | * later on (without the lock held). But, remember the |
| 502 | * tail and other pointers so the bios can be properly reinserted |
| 503 | * into the list if we hit congestion |
| 504 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 505 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 506 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 507 | force_reg = 1; |
| 508 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 509 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 510 | force_reg = 0; |
| 511 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 512 | |
| 513 | pending = pending_bios->head; |
| 514 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 515 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 516 | |
| 517 | /* |
| 518 | * if pending was null this time around, no bios need processing |
| 519 | * at all and we can stop. Otherwise it'll loop back up again |
| 520 | * and do an additional check so no bios are missed. |
| 521 | * |
| 522 | * device->running_pending is used to synchronize with the |
| 523 | * schedule_bio code. |
| 524 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 525 | if (device->pending_sync_bios.head == NULL && |
| 526 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 527 | again = 0; |
| 528 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 529 | } else { |
| 530 | again = 1; |
| 531 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 532 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 533 | |
| 534 | pending_bios->head = NULL; |
| 535 | pending_bios->tail = NULL; |
| 536 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 537 | spin_unlock(&device->io_lock); |
| 538 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 539 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 540 | |
| 541 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 542 | /* we want to work on both lists, but do more bios on the |
| 543 | * sync list than the regular list |
| 544 | */ |
| 545 | if ((num_run > 32 && |
| 546 | pending_bios != &device->pending_sync_bios && |
| 547 | device->pending_sync_bios.head) || |
| 548 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 549 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 550 | spin_lock(&device->io_lock); |
| 551 | requeue_list(pending_bios, pending, tail); |
| 552 | goto loop_lock; |
| 553 | } |
| 554 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 555 | cur = pending; |
| 556 | pending = pending->bi_next; |
| 557 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 558 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 559 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 560 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 561 | /* |
| 562 | * if we're doing the sync list, record that our |
| 563 | * plug has some sync requests on it |
| 564 | * |
| 565 | * If we're doing the regular list and there are |
| 566 | * sync requests sitting around, unplug before |
| 567 | * we add more |
| 568 | */ |
| 569 | if (pending_bios == &device->pending_sync_bios) { |
| 570 | sync_pending = 1; |
| 571 | } else if (sync_pending) { |
| 572 | blk_finish_plug(&plug); |
| 573 | blk_start_plug(&plug); |
| 574 | sync_pending = 0; |
| 575 | } |
| 576 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 577 | btrfsic_submit_bio(cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 578 | num_run++; |
| 579 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 580 | |
| 581 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * we made progress, there is more work to do and the bdi |
| 585 | * is now congested. Back off and let other work structs |
| 586 | * run instead |
| 587 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 588 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 589 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 590 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 591 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 592 | ioc = current->io_context; |
| 593 | |
| 594 | /* |
| 595 | * the main goal here is that we don't want to |
| 596 | * block if we're going to be able to submit |
| 597 | * more requests without blocking. |
| 598 | * |
| 599 | * This code does two great things, it pokes into |
| 600 | * the elevator code from a filesystem _and_ |
| 601 | * it makes assumptions about how batching works. |
| 602 | */ |
| 603 | if (ioc && ioc->nr_batch_requests > 0 && |
| 604 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 605 | (last_waited == 0 || |
| 606 | ioc->last_waited == last_waited)) { |
| 607 | /* |
| 608 | * we want to go through our batch of |
| 609 | * requests and stop. So, we copy out |
| 610 | * the ioc->last_waited time and test |
| 611 | * against it before looping |
| 612 | */ |
| 613 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 614 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 615 | continue; |
| 616 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 617 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 618 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 619 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 620 | |
| 621 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 622 | btrfs_queue_work(fs_info->submit_workers, |
| 623 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 624 | goto done; |
| 625 | } |
| 626 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 627 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 628 | cond_resched(); |
| 629 | if (again) |
| 630 | goto loop; |
| 631 | |
| 632 | spin_lock(&device->io_lock); |
| 633 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 634 | goto loop_lock; |
| 635 | spin_unlock(&device->io_lock); |
| 636 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 637 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 638 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 639 | } |
| 640 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 641 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 642 | { |
| 643 | struct btrfs_device *device; |
| 644 | |
| 645 | device = container_of(work, struct btrfs_device, work); |
| 646 | run_scheduled_bios(device); |
| 647 | } |
| 648 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 649 | /* |
| 650 | * Search and remove all stale (devices which are not mounted) devices. |
| 651 | * When both inputs are NULL, it will search and release all stale devices. |
| 652 | * path: Optional. When provided will it release all unmounted devices |
| 653 | * matching this path only. |
| 654 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 655 | * devices. |
| 656 | */ |
| 657 | static void btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 658 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 659 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 660 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 661 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 662 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 663 | list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) { |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 664 | mutex_lock(&fs_devices->device_list_mutex); |
| 665 | if (fs_devices->opened) { |
| 666 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 667 | continue; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 668 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 669 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 670 | list_for_each_entry_safe(device, tmp_device, |
| 671 | &fs_devices->devices, dev_list) { |
Anand Jain | 522f1b4 | 2018-01-18 22:00:35 +0800 | [diff] [blame] | 672 | int not_found = 0; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 673 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 674 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 675 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 676 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 677 | continue; |
| 678 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 679 | rcu_read_lock(); |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 680 | if (path) |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 681 | not_found = strcmp(rcu_str_deref(device->name), |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 682 | path); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 683 | rcu_read_unlock(); |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 684 | if (not_found) |
| 685 | continue; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 686 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 687 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 688 | fs_devices->num_devices--; |
| 689 | list_del(&device->dev_list); |
| 690 | btrfs_free_device(device); |
| 691 | |
| 692 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 693 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 694 | } |
| 695 | mutex_unlock(&fs_devices->device_list_mutex); |
| 696 | if (fs_devices->num_devices == 0) { |
| 697 | btrfs_sysfs_remove_fsid(fs_devices); |
| 698 | list_del(&fs_devices->fs_list); |
| 699 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 704 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 705 | struct btrfs_device *device, fmode_t flags, |
| 706 | void *holder) |
| 707 | { |
| 708 | struct request_queue *q; |
| 709 | struct block_device *bdev; |
| 710 | struct buffer_head *bh; |
| 711 | struct btrfs_super_block *disk_super; |
| 712 | u64 devid; |
| 713 | int ret; |
| 714 | |
| 715 | if (device->bdev) |
| 716 | return -EINVAL; |
| 717 | if (!device->name) |
| 718 | return -EINVAL; |
| 719 | |
| 720 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 721 | &bdev, &bh); |
| 722 | if (ret) |
| 723 | return ret; |
| 724 | |
| 725 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 726 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 727 | if (devid != device->devid) |
| 728 | goto error_brelse; |
| 729 | |
| 730 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 731 | goto error_brelse; |
| 732 | |
| 733 | device->generation = btrfs_super_generation(disk_super); |
| 734 | |
| 735 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 736 | if (btrfs_super_incompat_flags(disk_super) & |
| 737 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID) { |
| 738 | pr_err( |
| 739 | "BTRFS: Invalid seeding and uuid-changed device detected\n"); |
| 740 | goto error_brelse; |
| 741 | } |
| 742 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 743 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 744 | fs_devices->seeding = 1; |
| 745 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 746 | if (bdev_read_only(bdev)) |
| 747 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 748 | else |
| 749 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 753 | if (!blk_queue_nonrot(q)) |
| 754 | fs_devices->rotating = 1; |
| 755 | |
| 756 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 757 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 758 | device->mode = flags; |
| 759 | |
| 760 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 761 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 762 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 763 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 764 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 765 | } |
| 766 | brelse(bh); |
| 767 | |
| 768 | return 0; |
| 769 | |
| 770 | error_brelse: |
| 771 | brelse(bh); |
| 772 | blkdev_put(bdev, flags); |
| 773 | |
| 774 | return -EINVAL; |
| 775 | } |
| 776 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 777 | /* |
| 778 | * Add new device to list of registered devices |
| 779 | * |
| 780 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 781 | * device pointer which was just added or updated when successful |
| 782 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 783 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 784 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 785 | struct btrfs_super_block *disk_super, |
| 786 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 787 | { |
| 788 | struct btrfs_device *device; |
| 789 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 790 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 791 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 792 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 793 | bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) & |
| 794 | BTRFS_FEATURE_INCOMPAT_METADATA_UUID); |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame^] | 795 | bool fsid_change_in_progress = (btrfs_super_flags(disk_super) & |
| 796 | BTRFS_SUPER_FLAG_CHANGING_FSID_V2); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 797 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 798 | if (has_metadata_uuid) |
| 799 | fs_devices = find_fsid(disk_super->fsid, disk_super->metadata_uuid); |
| 800 | else |
| 801 | fs_devices = find_fsid(disk_super->fsid, NULL); |
| 802 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 803 | if (!fs_devices) { |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 804 | if (has_metadata_uuid) |
| 805 | fs_devices = alloc_fs_devices(disk_super->fsid, |
| 806 | disk_super->metadata_uuid); |
| 807 | else |
| 808 | fs_devices = alloc_fs_devices(disk_super->fsid, NULL); |
| 809 | |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame^] | 810 | fs_devices->fsid_change = fsid_change_in_progress; |
| 811 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 812 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 813 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 814 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 815 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 816 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 817 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 818 | device = NULL; |
| 819 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 820 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 821 | device = find_device(fs_devices, devid, |
| 822 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 823 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 824 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 825 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 826 | if (fs_devices->opened) { |
| 827 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 828 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 829 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 830 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 831 | device = btrfs_alloc_device(NULL, &devid, |
| 832 | disk_super->dev_item.uuid); |
| 833 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 834 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 835 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 836 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 837 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 838 | |
| 839 | name = rcu_string_strdup(path, GFP_NOFS); |
| 840 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 841 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 842 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 843 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 844 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 845 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 846 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 847 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 848 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 849 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 850 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 851 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 852 | |
| 853 | if (disk_super->label[0]) |
| 854 | pr_info("BTRFS: device label %s devid %llu transid %llu %s\n", |
| 855 | disk_super->label, devid, found_transid, path); |
| 856 | else |
| 857 | pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n", |
| 858 | disk_super->fsid, devid, found_transid, path); |
| 859 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 860 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 861 | /* |
| 862 | * When FS is already mounted. |
| 863 | * 1. If you are here and if the device->name is NULL that |
| 864 | * means this device was missing at time of FS mount. |
| 865 | * 2. If you are here and if the device->name is different |
| 866 | * from 'path' that means either |
| 867 | * a. The same device disappeared and reappeared with |
| 868 | * different name. or |
| 869 | * b. The missing-disk-which-was-replaced, has |
| 870 | * reappeared now. |
| 871 | * |
| 872 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 873 | * and unintentional. |
| 874 | * |
| 875 | * Further in case of 1 and 2a above, the disk at 'path' |
| 876 | * would have missed some transaction when it was away and |
| 877 | * in case of 2a the stale bdev has to be updated as well. |
| 878 | * 2b must not be allowed at all time. |
| 879 | */ |
| 880 | |
| 881 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 882 | * For now, we do allow update to btrfs_fs_device through the |
| 883 | * btrfs dev scan cli after FS has been mounted. We're still |
| 884 | * tracking a problem where systems fail mount by subvolume id |
| 885 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 886 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 887 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 888 | /* |
| 889 | * That is if the FS is _not_ mounted and if you |
| 890 | * are here, that means there is more than one |
| 891 | * disk with same uuid and devid.We keep the one |
| 892 | * with larger generation number or the last-in if |
| 893 | * generation are equal. |
| 894 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 895 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 896 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 897 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 898 | |
Anand Jain | a9261d4 | 2018-10-15 10:45:17 +0800 | [diff] [blame] | 899 | /* |
| 900 | * We are going to replace the device path for a given devid, |
| 901 | * make sure it's the same device if the device is mounted |
| 902 | */ |
| 903 | if (device->bdev) { |
| 904 | struct block_device *path_bdev; |
| 905 | |
| 906 | path_bdev = lookup_bdev(path); |
| 907 | if (IS_ERR(path_bdev)) { |
| 908 | mutex_unlock(&fs_devices->device_list_mutex); |
| 909 | return ERR_CAST(path_bdev); |
| 910 | } |
| 911 | |
| 912 | if (device->bdev != path_bdev) { |
| 913 | bdput(path_bdev); |
| 914 | mutex_unlock(&fs_devices->device_list_mutex); |
| 915 | btrfs_warn_in_rcu(device->fs_info, |
| 916 | "duplicate device fsid:devid for %pU:%llu old:%s new:%s", |
| 917 | disk_super->fsid, devid, |
| 918 | rcu_str_deref(device->name), path); |
| 919 | return ERR_PTR(-EEXIST); |
| 920 | } |
| 921 | bdput(path_bdev); |
| 922 | btrfs_info_in_rcu(device->fs_info, |
| 923 | "device fsid %pU devid %llu moved old:%s new:%s", |
| 924 | disk_super->fsid, devid, |
| 925 | rcu_str_deref(device->name), path); |
| 926 | } |
| 927 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 928 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 929 | if (!name) { |
| 930 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 931 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 932 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 933 | rcu_string_free(device->name); |
| 934 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 935 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 936 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 937 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 938 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 939 | } |
| 940 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 941 | /* |
| 942 | * Unmount does not free the btrfs_device struct but would zero |
| 943 | * generation along with most of the other members. So just update |
| 944 | * it back. We need it to pick the disk with largest generation |
| 945 | * (as above). |
| 946 | */ |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame^] | 947 | if (!fs_devices->opened) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 948 | device->generation = found_transid; |
Nikolay Borisov | d1a6300 | 2018-10-30 16:43:26 +0200 | [diff] [blame^] | 949 | fs_devices->latest_generation = max_t(u64, found_transid, |
| 950 | fs_devices->latest_generation); |
| 951 | } |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 952 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 953 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 954 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 955 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 956 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 957 | } |
| 958 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 959 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 960 | { |
| 961 | struct btrfs_fs_devices *fs_devices; |
| 962 | struct btrfs_device *device; |
| 963 | struct btrfs_device *orig_dev; |
| 964 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 965 | fs_devices = alloc_fs_devices(orig->fsid, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 966 | if (IS_ERR(fs_devices)) |
| 967 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 968 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 969 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 970 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 971 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 972 | /* We have held the volume lock, it is safe to get the devices. */ |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 973 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 974 | struct rcu_string *name; |
| 975 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 976 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 977 | orig_dev->uuid); |
| 978 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 979 | goto error; |
| 980 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 981 | /* |
| 982 | * This is ok to do without rcu read locked because we hold the |
| 983 | * uuid mutex so nothing we touch in here is going to disappear. |
| 984 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 985 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 986 | name = rcu_string_strdup(orig_dev->name->str, |
| 987 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 988 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 989 | btrfs_free_device(device); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 990 | goto error; |
| 991 | } |
| 992 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 993 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 994 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 995 | list_add(&device->dev_list, &fs_devices->devices); |
| 996 | device->fs_devices = fs_devices; |
| 997 | fs_devices->num_devices++; |
| 998 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 999 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1000 | return fs_devices; |
| 1001 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 1002 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1003 | free_fs_devices(fs_devices); |
| 1004 | return ERR_PTR(-ENOMEM); |
| 1005 | } |
| 1006 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 1007 | /* |
| 1008 | * After we have read the system tree and know devids belonging to |
| 1009 | * this filesystem, remove the device which does not belong there. |
| 1010 | */ |
| 1011 | 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] | 1012 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1013 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1014 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1015 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1016 | mutex_lock(&uuid_mutex); |
| 1017 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1018 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1019 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1020 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 1021 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1022 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1023 | &device->dev_state) && |
| 1024 | (!latest_dev || |
| 1025 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1026 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1027 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1028 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1029 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1030 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1031 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 1032 | /* |
| 1033 | * In the first step, keep the device which has |
| 1034 | * the correct fsid and the devid that is used |
| 1035 | * for the dev_replace procedure. |
| 1036 | * In the second step, the dev_replace state is |
| 1037 | * read from the device tree and it is known |
| 1038 | * whether the procedure is really active or |
| 1039 | * not, which means whether this device is |
| 1040 | * used or whether it should be removed. |
| 1041 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1042 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1043 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1044 | continue; |
| 1045 | } |
| 1046 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1047 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1048 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1049 | device->bdev = NULL; |
| 1050 | fs_devices->open_devices--; |
| 1051 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1052 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1053 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1054 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1055 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 1056 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1057 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1058 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1059 | list_del_init(&device->dev_list); |
| 1060 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1061 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1062 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1063 | |
| 1064 | if (fs_devices->seed) { |
| 1065 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1066 | goto again; |
| 1067 | } |
| 1068 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1069 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 1070 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1071 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1072 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1073 | |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 1074 | static void free_device_rcu(struct rcu_head *head) |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1075 | { |
| 1076 | struct btrfs_device *device; |
| 1077 | |
| 1078 | device = container_of(head, struct btrfs_device, rcu); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1079 | btrfs_free_device(device); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1082 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1083 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1084 | if (!device->bdev) |
| 1085 | return; |
| 1086 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1087 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1088 | sync_blockdev(device->bdev); |
| 1089 | invalidate_bdev(device->bdev); |
| 1090 | } |
| 1091 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1092 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1095 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1096 | { |
| 1097 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 1098 | struct btrfs_device *new_device; |
| 1099 | struct rcu_string *name; |
| 1100 | |
| 1101 | if (device->bdev) |
| 1102 | fs_devices->open_devices--; |
| 1103 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1104 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1105 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1106 | list_del_init(&device->dev_alloc_list); |
| 1107 | fs_devices->rw_devices--; |
| 1108 | } |
| 1109 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1110 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1111 | fs_devices->missing_devices--; |
| 1112 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1113 | btrfs_close_bdev(device); |
| 1114 | |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1115 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 1116 | device->uuid); |
| 1117 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 1118 | |
| 1119 | /* Safe because we are under uuid_mutex */ |
| 1120 | if (device->name) { |
| 1121 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 1122 | BUG_ON(!name); /* -ENOMEM */ |
| 1123 | rcu_assign_pointer(new_device->name, name); |
| 1124 | } |
| 1125 | |
| 1126 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 1127 | new_device->fs_devices = device->fs_devices; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1128 | |
| 1129 | call_rcu(&device->rcu, free_device_rcu); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1132 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1133 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1134 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1135 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1136 | if (--fs_devices->opened > 0) |
| 1137 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1138 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1139 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1140 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1141 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1142 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1143 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1144 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1145 | WARN_ON(fs_devices->open_devices); |
| 1146 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1147 | fs_devices->opened = 0; |
| 1148 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1149 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1150 | return 0; |
| 1151 | } |
| 1152 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1153 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1154 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1155 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1156 | int ret; |
| 1157 | |
| 1158 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1159 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1160 | if (!fs_devices->opened) { |
| 1161 | seed_devices = fs_devices->seed; |
| 1162 | fs_devices->seed = NULL; |
| 1163 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1164 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1165 | |
| 1166 | while (seed_devices) { |
| 1167 | fs_devices = seed_devices; |
| 1168 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1169 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1170 | free_fs_devices(fs_devices); |
| 1171 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1172 | return ret; |
| 1173 | } |
| 1174 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1175 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1176 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1177 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1178 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1179 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1180 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1181 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1182 | flags |= FMODE_EXCL; |
| 1183 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1184 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1185 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1186 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1187 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1188 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1189 | if (!latest_dev || |
| 1190 | device->generation > latest_dev->generation) |
| 1191 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1192 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1193 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1194 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1195 | goto out; |
| 1196 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1197 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1198 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1199 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1200 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1201 | return ret; |
| 1202 | } |
| 1203 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1204 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1205 | { |
| 1206 | struct btrfs_device *dev1, *dev2; |
| 1207 | |
| 1208 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1209 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1210 | |
| 1211 | if (dev1->devid < dev2->devid) |
| 1212 | return -1; |
| 1213 | else if (dev1->devid > dev2->devid) |
| 1214 | return 1; |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1218 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1219 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1220 | { |
| 1221 | int ret; |
| 1222 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1223 | lockdep_assert_held(&uuid_mutex); |
| 1224 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1225 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1226 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1227 | fs_devices->opened++; |
| 1228 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1229 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1230 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1231 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1232 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1233 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1234 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1235 | return ret; |
| 1236 | } |
| 1237 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1238 | static void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1239 | { |
| 1240 | kunmap(page); |
| 1241 | put_page(page); |
| 1242 | } |
| 1243 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1244 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1245 | struct page **page, |
| 1246 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1247 | { |
| 1248 | void *p; |
| 1249 | pgoff_t index; |
| 1250 | |
| 1251 | /* make sure our super fits in the device */ |
| 1252 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1253 | return 1; |
| 1254 | |
| 1255 | /* make sure our super fits in the page */ |
| 1256 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1257 | return 1; |
| 1258 | |
| 1259 | /* make sure our super doesn't straddle pages on disk */ |
| 1260 | index = bytenr >> PAGE_SHIFT; |
| 1261 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1262 | return 1; |
| 1263 | |
| 1264 | /* pull in the page with our super */ |
| 1265 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1266 | index, GFP_KERNEL); |
| 1267 | |
| 1268 | if (IS_ERR_OR_NULL(*page)) |
| 1269 | return 1; |
| 1270 | |
| 1271 | p = kmap(*page); |
| 1272 | |
| 1273 | /* align our pointer to the offset of the super block */ |
| 1274 | *disk_super = p + (bytenr & ~PAGE_MASK); |
| 1275 | |
| 1276 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1277 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1278 | btrfs_release_disk_super(*page); |
| 1279 | return 1; |
| 1280 | } |
| 1281 | |
| 1282 | if ((*disk_super)->label[0] && |
| 1283 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1284 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1285 | |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1289 | /* |
| 1290 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1291 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1292 | * is read via pagecache |
| 1293 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1294 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1295 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1296 | { |
| 1297 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1298 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1299 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1300 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1301 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1302 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1303 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1304 | lockdep_assert_held(&uuid_mutex); |
| 1305 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1306 | /* |
| 1307 | * we would like to check all the supers, but that would make |
| 1308 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1309 | * So, we need to add a special mount option to scan for |
| 1310 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1311 | */ |
| 1312 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1313 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1314 | |
| 1315 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1316 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1317 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1318 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1319 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1320 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1321 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1322 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1323 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1324 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1325 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1326 | if (new_device_added) |
| 1327 | btrfs_free_stale_devices(path, device); |
| 1328 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1329 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1330 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1331 | |
| 1332 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1333 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1334 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1335 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1336 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1337 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1338 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1339 | struct btrfs_device *device, |
| 1340 | u64 *start, u64 len) |
| 1341 | { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 1342 | struct btrfs_fs_info *fs_info = device->fs_info; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1343 | struct extent_map *em; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1344 | struct list_head *search_list = &fs_info->pinned_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1345 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1346 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1347 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1348 | if (transaction) |
| 1349 | search_list = &transaction->pending_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1350 | again: |
| 1351 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1352 | struct map_lookup *map; |
| 1353 | int i; |
| 1354 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 1355 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1356 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1357 | u64 end; |
| 1358 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1359 | if (map->stripes[i].dev != device) |
| 1360 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1361 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1362 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1363 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1364 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1365 | /* |
| 1366 | * Make sure that while processing the pinned list we do |
| 1367 | * not override our *start with a lower value, because |
| 1368 | * we can have pinned chunks that fall within this |
| 1369 | * device hole and that have lower physical addresses |
| 1370 | * than the pending chunks we processed before. If we |
| 1371 | * do not take this special care we can end up getting |
| 1372 | * 2 pending chunks that start at the same physical |
| 1373 | * device offsets because the end offset of a pinned |
| 1374 | * chunk can be equal to the start offset of some |
| 1375 | * pending chunk. |
| 1376 | */ |
| 1377 | end = map->stripes[i].physical + em->orig_block_len; |
| 1378 | if (end > *start) { |
| 1379 | *start = end; |
| 1380 | ret = 1; |
| 1381 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1382 | } |
| 1383 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1384 | if (search_list != &fs_info->pinned_chunks) { |
| 1385 | search_list = &fs_info->pinned_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1386 | goto again; |
| 1387 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1388 | |
| 1389 | return ret; |
| 1390 | } |
| 1391 | |
| 1392 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1393 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1394 | * find_free_dev_extent_start - find free space in the specified device |
| 1395 | * @device: the device which we search the free space in |
| 1396 | * @num_bytes: the size of the free space that we need |
| 1397 | * @search_start: the position from which to begin the search |
| 1398 | * @start: store the start of the free space. |
| 1399 | * @len: the size of the free space. that we find, or the size |
| 1400 | * 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] | 1401 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1402 | * this uses a pretty simple search, the expectation is that it is |
| 1403 | * called very infrequently and that a given device has a small number |
| 1404 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1405 | * |
| 1406 | * @start is used to store the start of the free space if we find. But if we |
| 1407 | * don't find suitable free space, it will be used to store the start position |
| 1408 | * of the max free space. |
| 1409 | * |
| 1410 | * @len is used to store the size of the free space that we find. |
| 1411 | * But if we don't find suitable free space, it is used to store the size of |
| 1412 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1414 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
| 1415 | struct btrfs_device *device, u64 num_bytes, |
| 1416 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1417 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1418 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1419 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1420 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1421 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1422 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1423 | u64 hole_size; |
| 1424 | u64 max_hole_start; |
| 1425 | u64 max_hole_size; |
| 1426 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1427 | u64 search_end = device->total_bytes; |
| 1428 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1429 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1430 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1431 | |
| 1432 | /* |
| 1433 | * We don't want to overwrite the superblock on the drive nor any area |
| 1434 | * used by the boot loader (grub for example), so we make sure to start |
| 1435 | * at an offset of at least 1MB. |
| 1436 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1437 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1438 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1439 | path = btrfs_alloc_path(); |
| 1440 | if (!path) |
| 1441 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1442 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1443 | max_hole_start = search_start; |
| 1444 | max_hole_size = 0; |
| 1445 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1446 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1447 | if (search_start >= search_end || |
| 1448 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1449 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1450 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1453 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1454 | path->search_commit_root = 1; |
| 1455 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1456 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1457 | key.objectid = device->devid; |
| 1458 | key.offset = search_start; |
| 1459 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1460 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1461 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1462 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1463 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1464 | if (ret > 0) { |
| 1465 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1466 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1467 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1468 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1469 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1470 | while (1) { |
| 1471 | l = path->nodes[0]; |
| 1472 | slot = path->slots[0]; |
| 1473 | if (slot >= btrfs_header_nritems(l)) { |
| 1474 | ret = btrfs_next_leaf(root, path); |
| 1475 | if (ret == 0) |
| 1476 | continue; |
| 1477 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1478 | goto out; |
| 1479 | |
| 1480 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1481 | } |
| 1482 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1483 | |
| 1484 | if (key.objectid < device->devid) |
| 1485 | goto next; |
| 1486 | |
| 1487 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1488 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1489 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1490 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1491 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1492 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1493 | if (key.offset > search_start) { |
| 1494 | hole_size = key.offset - search_start; |
| 1495 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1496 | /* |
| 1497 | * Have to check before we set max_hole_start, otherwise |
| 1498 | * we could end up sending back this offset anyway. |
| 1499 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1500 | if (contains_pending_extent(transaction, device, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1501 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1502 | hole_size)) { |
| 1503 | if (key.offset >= search_start) { |
| 1504 | hole_size = key.offset - search_start; |
| 1505 | } else { |
| 1506 | WARN_ON_ONCE(1); |
| 1507 | hole_size = 0; |
| 1508 | } |
| 1509 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1510 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1511 | if (hole_size > max_hole_size) { |
| 1512 | max_hole_start = search_start; |
| 1513 | max_hole_size = hole_size; |
| 1514 | } |
| 1515 | |
| 1516 | /* |
| 1517 | * If this free space is greater than which we need, |
| 1518 | * it must be the max free space that we have found |
| 1519 | * until now, so max_hole_start must point to the start |
| 1520 | * of this free space and the length of this free space |
| 1521 | * is stored in max_hole_size. Thus, we return |
| 1522 | * max_hole_start and max_hole_size and go back to the |
| 1523 | * caller. |
| 1524 | */ |
| 1525 | if (hole_size >= num_bytes) { |
| 1526 | ret = 0; |
| 1527 | goto out; |
| 1528 | } |
| 1529 | } |
| 1530 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1531 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1532 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1533 | dev_extent); |
| 1534 | if (extent_end > search_start) |
| 1535 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1536 | next: |
| 1537 | path->slots[0]++; |
| 1538 | cond_resched(); |
| 1539 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1540 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1541 | /* |
| 1542 | * At this point, search_start should be the end of |
| 1543 | * allocated dev extents, and when shrinking the device, |
| 1544 | * search_end may be smaller than search_start. |
| 1545 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1546 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1547 | hole_size = search_end - search_start; |
| 1548 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1549 | if (contains_pending_extent(transaction, device, &search_start, |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1550 | hole_size)) { |
| 1551 | btrfs_release_path(path); |
| 1552 | goto again; |
| 1553 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1554 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1555 | if (hole_size > max_hole_size) { |
| 1556 | max_hole_start = search_start; |
| 1557 | max_hole_size = hole_size; |
| 1558 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1559 | } |
| 1560 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1561 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1562 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1563 | ret = -ENOSPC; |
| 1564 | else |
| 1565 | ret = 0; |
| 1566 | |
| 1567 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1568 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1569 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1570 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1571 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1572 | return ret; |
| 1573 | } |
| 1574 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1575 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1576 | struct btrfs_device *device, u64 num_bytes, |
| 1577 | u64 *start, u64 *len) |
| 1578 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1579 | /* FIXME use last free of some kind */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1580 | return find_free_dev_extent_start(trans->transaction, device, |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1581 | num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1582 | } |
| 1583 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1584 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1585 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1586 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1587 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1588 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1589 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1590 | int ret; |
| 1591 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1592 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1593 | struct btrfs_key found_key; |
| 1594 | struct extent_buffer *leaf = NULL; |
| 1595 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1596 | |
| 1597 | path = btrfs_alloc_path(); |
| 1598 | if (!path) |
| 1599 | return -ENOMEM; |
| 1600 | |
| 1601 | key.objectid = device->devid; |
| 1602 | key.offset = start; |
| 1603 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1604 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1605 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1606 | if (ret > 0) { |
| 1607 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1608 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1609 | if (ret) |
| 1610 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1611 | leaf = path->nodes[0]; |
| 1612 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1613 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1614 | struct btrfs_dev_extent); |
| 1615 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1616 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1617 | key = found_key; |
| 1618 | btrfs_release_path(path); |
| 1619 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1620 | } else if (ret == 0) { |
| 1621 | leaf = path->nodes[0]; |
| 1622 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1623 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1624 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1625 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1626 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1627 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1628 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1629 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1630 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1631 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1632 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1633 | btrfs_handle_fs_error(fs_info, ret, |
| 1634 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1635 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1636 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1637 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1638 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1639 | btrfs_free_path(path); |
| 1640 | return ret; |
| 1641 | } |
| 1642 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1643 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1644 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1645 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1646 | { |
| 1647 | int ret; |
| 1648 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1649 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1650 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1651 | struct btrfs_dev_extent *extent; |
| 1652 | struct extent_buffer *leaf; |
| 1653 | struct btrfs_key key; |
| 1654 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1655 | 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] | 1656 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1657 | path = btrfs_alloc_path(); |
| 1658 | if (!path) |
| 1659 | return -ENOMEM; |
| 1660 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1661 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1662 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1663 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1664 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1665 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1666 | if (ret) |
| 1667 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1668 | |
| 1669 | leaf = path->nodes[0]; |
| 1670 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1671 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1672 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1673 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1674 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1675 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1676 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1677 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1678 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1679 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1680 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1681 | btrfs_free_path(path); |
| 1682 | return ret; |
| 1683 | } |
| 1684 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1685 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1686 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1687 | struct extent_map_tree *em_tree; |
| 1688 | struct extent_map *em; |
| 1689 | struct rb_node *n; |
| 1690 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1691 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1692 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1693 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 1694 | n = rb_last(&em_tree->map.rb_root); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1695 | if (n) { |
| 1696 | em = rb_entry(n, struct extent_map, rb_node); |
| 1697 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1698 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1699 | read_unlock(&em_tree->lock); |
| 1700 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1701 | return ret; |
| 1702 | } |
| 1703 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1704 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1705 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1706 | { |
| 1707 | int ret; |
| 1708 | struct btrfs_key key; |
| 1709 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1710 | struct btrfs_path *path; |
| 1711 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1712 | path = btrfs_alloc_path(); |
| 1713 | if (!path) |
| 1714 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1715 | |
| 1716 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1717 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1718 | key.offset = (u64)-1; |
| 1719 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1720 | 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] | 1721 | if (ret < 0) |
| 1722 | goto error; |
| 1723 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1724 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1725 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1726 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1727 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1728 | BTRFS_DEV_ITEM_KEY); |
| 1729 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1730 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1731 | } else { |
| 1732 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1733 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1734 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1735 | } |
| 1736 | ret = 0; |
| 1737 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1738 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1739 | return ret; |
| 1740 | } |
| 1741 | |
| 1742 | /* |
| 1743 | * the device information is stored in the chunk root |
| 1744 | * the btrfs_device struct should be fully filled in |
| 1745 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1746 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1747 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1748 | { |
| 1749 | int ret; |
| 1750 | struct btrfs_path *path; |
| 1751 | struct btrfs_dev_item *dev_item; |
| 1752 | struct extent_buffer *leaf; |
| 1753 | struct btrfs_key key; |
| 1754 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1755 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1756 | path = btrfs_alloc_path(); |
| 1757 | if (!path) |
| 1758 | return -ENOMEM; |
| 1759 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1760 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1761 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1762 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1763 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1764 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1765 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1766 | if (ret) |
| 1767 | goto out; |
| 1768 | |
| 1769 | leaf = path->nodes[0]; |
| 1770 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1771 | |
| 1772 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1773 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1774 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1775 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1776 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1777 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1778 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1779 | btrfs_device_get_disk_total_bytes(device)); |
| 1780 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1781 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1782 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1783 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1784 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1785 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1786 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1787 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1788 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1789 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1790 | write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid, |
| 1791 | ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1792 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1793 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1794 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1795 | out: |
| 1796 | btrfs_free_path(path); |
| 1797 | return ret; |
| 1798 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1799 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1800 | /* |
| 1801 | * Function to update ctime/mtime for a given device path. |
| 1802 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1803 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1804 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1805 | { |
| 1806 | struct file *filp; |
| 1807 | |
| 1808 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1809 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1810 | return; |
| 1811 | file_update_time(filp); |
| 1812 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1813 | } |
| 1814 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 1815 | static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1816 | struct btrfs_device *device) |
| 1817 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 1818 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1819 | int ret; |
| 1820 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1821 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1822 | struct btrfs_trans_handle *trans; |
| 1823 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1824 | path = btrfs_alloc_path(); |
| 1825 | if (!path) |
| 1826 | return -ENOMEM; |
| 1827 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1828 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1829 | if (IS_ERR(trans)) { |
| 1830 | btrfs_free_path(path); |
| 1831 | return PTR_ERR(trans); |
| 1832 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1833 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1834 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1835 | key.offset = device->devid; |
| 1836 | |
| 1837 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1838 | if (ret) { |
| 1839 | if (ret > 0) |
| 1840 | ret = -ENOENT; |
| 1841 | btrfs_abort_transaction(trans, ret); |
| 1842 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1843 | goto out; |
| 1844 | } |
| 1845 | |
| 1846 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1847 | if (ret) { |
| 1848 | btrfs_abort_transaction(trans, ret); |
| 1849 | btrfs_end_transaction(trans); |
| 1850 | } |
| 1851 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1852 | out: |
| 1853 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1854 | if (!ret) |
| 1855 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1856 | return ret; |
| 1857 | } |
| 1858 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1859 | /* |
| 1860 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 1861 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 1862 | * replace. |
| 1863 | */ |
| 1864 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 1865 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1866 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1867 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1868 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1869 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1870 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1871 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1872 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1873 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1874 | all_avail = fs_info->avail_data_alloc_bits | |
| 1875 | fs_info->avail_system_alloc_bits | |
| 1876 | fs_info->avail_metadata_alloc_bits; |
| 1877 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1878 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1879 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 1880 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1881 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1882 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1883 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 1884 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1885 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1886 | if (ret) |
| 1887 | return ret; |
| 1888 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1892 | } |
| 1893 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1894 | static struct btrfs_device * btrfs_find_next_active_device( |
| 1895 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1896 | { |
| 1897 | struct btrfs_device *next_device; |
| 1898 | |
| 1899 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 1900 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1901 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 1902 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1903 | return next_device; |
| 1904 | } |
| 1905 | |
| 1906 | return NULL; |
| 1907 | } |
| 1908 | |
| 1909 | /* |
| 1910 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 1911 | * and replace it with the provided or the next active device, in the context |
| 1912 | * where this function called, there should be always be another device (or |
| 1913 | * this_dev) which is active. |
| 1914 | */ |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1915 | void btrfs_assign_next_active_device(struct btrfs_device *device, |
| 1916 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1917 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 1918 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1919 | struct btrfs_device *next_device; |
| 1920 | |
| 1921 | if (this_dev) |
| 1922 | next_device = this_dev; |
| 1923 | else |
| 1924 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 1925 | device); |
| 1926 | ASSERT(next_device); |
| 1927 | |
| 1928 | if (fs_info->sb->s_bdev && |
| 1929 | (fs_info->sb->s_bdev == device->bdev)) |
| 1930 | fs_info->sb->s_bdev = next_device->bdev; |
| 1931 | |
| 1932 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 1933 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1934 | } |
| 1935 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1936 | /* |
| 1937 | * Return btrfs_fs_devices::num_devices excluding the device that's being |
| 1938 | * currently replaced. |
| 1939 | */ |
| 1940 | static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info) |
| 1941 | { |
| 1942 | u64 num_devices = fs_info->fs_devices->num_devices; |
| 1943 | |
| 1944 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
| 1945 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 1946 | ASSERT(num_devices > 1); |
| 1947 | num_devices--; |
| 1948 | } |
| 1949 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
| 1950 | |
| 1951 | return num_devices; |
| 1952 | } |
| 1953 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1954 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 1955 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1956 | { |
| 1957 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1958 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1959 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1960 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1961 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1962 | |
| 1963 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1964 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 1965 | num_devices = btrfs_num_devices(fs_info); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1966 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1967 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1968 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1969 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1970 | |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 1971 | device = btrfs_find_device_by_devspec(fs_info, devid, device_path); |
| 1972 | |
| 1973 | if (IS_ERR(device)) { |
| 1974 | if (PTR_ERR(device) == -ENOENT && |
| 1975 | strcmp(device_path, "missing") == 0) |
| 1976 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
| 1977 | else |
| 1978 | ret = PTR_ERR(device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1979 | goto out; |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 1980 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1981 | |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 1982 | if (btrfs_pinned_by_swapfile(fs_info, device)) { |
| 1983 | btrfs_warn_in_rcu(fs_info, |
| 1984 | "cannot remove device %s (devid %llu) due to active swapfile", |
| 1985 | rcu_str_deref(device->name), device->devid); |
| 1986 | ret = -ETXTBSY; |
| 1987 | goto out; |
| 1988 | } |
| 1989 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1990 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1991 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1992 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1993 | } |
| 1994 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1995 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 1996 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1997 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1998 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1999 | } |
| 2000 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2001 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2002 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2003 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2004 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2005 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2006 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2007 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2008 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2009 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 2010 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2011 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2012 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2013 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2014 | /* |
| 2015 | * TODO: the superblock still includes this device in its num_devices |
| 2016 | * counter although write_all_supers() is not locked out. This |
| 2017 | * could give a filesystem state which requires a degraded mount. |
| 2018 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2019 | ret = btrfs_rm_dev_item(fs_info, device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2020 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2021 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2022 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2023 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2024 | btrfs_scrub_cancel_dev(fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2025 | |
| 2026 | /* |
| 2027 | * the device list mutex makes sure that we don't change |
| 2028 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 2029 | * the device supers. Whoever is writing all supers, should |
| 2030 | * lock the device list mutex before getting the number of |
| 2031 | * devices in the super block (super_copy). Conversely, |
| 2032 | * whoever updates the number of devices in the super block |
| 2033 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2034 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2035 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2036 | /* |
| 2037 | * In normal cases the cur_devices == fs_devices. But in case |
| 2038 | * of deleting a seed device, the cur_devices should point to |
| 2039 | * its own fs_devices listed under the fs_devices->seed. |
| 2040 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2041 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2042 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2043 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2044 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2045 | cur_devices->num_devices--; |
| 2046 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 2047 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 2048 | if (cur_devices != fs_devices) |
| 2049 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2050 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2051 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2052 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2053 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2054 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2055 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2056 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 2057 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2058 | /* remove sysfs entry */ |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2059 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 2060 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 2061 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2062 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 2063 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2064 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2065 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2066 | /* |
| 2067 | * at this point, the device is zero sized and detached from |
| 2068 | * the devices list. All that's left is to zero out the old |
| 2069 | * supers and free the device. |
| 2070 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2071 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2072 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 2073 | |
| 2074 | btrfs_close_bdev(device); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2075 | call_rcu(&device->rcu, free_device_rcu); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 2076 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2077 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2078 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2079 | if (fs_devices->seed == cur_devices) { |
| 2080 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2081 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 2082 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2083 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2084 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2085 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2086 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2087 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2088 | } |
| 2089 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2090 | out: |
| 2091 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2092 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2093 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2094 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2095 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2096 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2097 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 2098 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 2099 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2100 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 2101 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 2102 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2103 | } |
| 2104 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2105 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2106 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2107 | struct btrfs_fs_devices *fs_devices; |
| 2108 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2109 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2110 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2111 | /* |
| 2112 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2113 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2114 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2115 | * srcdev will have its correct fs_devices in both the cases. |
| 2116 | */ |
| 2117 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2118 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2119 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2120 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2121 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2122 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2123 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2124 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2125 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2126 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2127 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2128 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2129 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 2133 | struct btrfs_device *srcdev) |
| 2134 | { |
| 2135 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2136 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2137 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2138 | /* zero out the old super if it is writable */ |
| 2139 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2140 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2141 | |
| 2142 | btrfs_close_bdev(srcdev); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2143 | call_rcu(&srcdev->rcu, free_device_rcu); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2144 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2145 | /* if this is no devs we rather delete the fs_devices */ |
| 2146 | if (!fs_devices->num_devices) { |
| 2147 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2148 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2149 | /* |
| 2150 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2151 | * seed. In case of a seed device being replaced, the replace |
| 2152 | * target added to the sprout FS, so there will be no more |
| 2153 | * device left under the seed FS. |
| 2154 | */ |
| 2155 | ASSERT(fs_devices->seeding); |
| 2156 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2157 | tmp_fs_devices = fs_info->fs_devices; |
| 2158 | while (tmp_fs_devices) { |
| 2159 | if (tmp_fs_devices->seed == fs_devices) { |
| 2160 | tmp_fs_devices->seed = fs_devices->seed; |
| 2161 | break; |
| 2162 | } |
| 2163 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2164 | } |
| 2165 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2166 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2167 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2168 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2169 | } |
| 2170 | |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2171 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2172 | { |
Nikolay Borisov | 4f5ad7b | 2018-07-20 19:37:51 +0300 | [diff] [blame] | 2173 | struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2174 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2175 | WARN_ON(!tgtdev); |
| 2176 | mutex_lock(&fs_devices->device_list_mutex); |
| 2177 | |
| 2178 | btrfs_sysfs_rm_device_link(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2179 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2180 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2181 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2182 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2183 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2184 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame] | 2185 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2186 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2187 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2188 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2189 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2190 | |
| 2191 | /* |
| 2192 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2193 | * may lead to a call to btrfs_show_devname() which will try |
| 2194 | * to hold device_list_mutex. And here this device |
| 2195 | * is already out of device list, so we don't have to hold |
| 2196 | * the device_list_mutex lock. |
| 2197 | */ |
| 2198 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2199 | |
| 2200 | btrfs_close_bdev(tgtdev); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2201 | call_rcu(&tgtdev->rcu, free_device_rcu); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2202 | } |
| 2203 | |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2204 | static struct btrfs_device *btrfs_find_device_by_path( |
| 2205 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2206 | { |
| 2207 | int ret = 0; |
| 2208 | struct btrfs_super_block *disk_super; |
| 2209 | u64 devid; |
| 2210 | u8 *dev_uuid; |
| 2211 | struct block_device *bdev; |
| 2212 | struct buffer_head *bh; |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2213 | struct btrfs_device *device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2214 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2215 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2216 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2217 | if (ret) |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2218 | return ERR_PTR(ret); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2219 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2220 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2221 | dev_uuid = disk_super->dev_item.uuid; |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2222 | if (btrfs_fs_incompat(fs_info, METADATA_UUID)) |
| 2223 | device = btrfs_find_device(fs_info, devid, dev_uuid, |
| 2224 | disk_super->metadata_uuid); |
| 2225 | else |
| 2226 | device = btrfs_find_device(fs_info, devid, |
| 2227 | dev_uuid, disk_super->fsid); |
| 2228 | |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2229 | brelse(bh); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2230 | if (!device) |
| 2231 | device = ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2232 | blkdev_put(bdev, FMODE_READ); |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2233 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2234 | } |
| 2235 | |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2236 | static struct btrfs_device *btrfs_find_device_missing_or_by_path( |
| 2237 | struct btrfs_fs_info *fs_info, const char *device_path) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2238 | { |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2239 | struct btrfs_device *device = NULL; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2240 | if (strcmp(device_path, "missing") == 0) { |
| 2241 | struct list_head *devices; |
| 2242 | struct btrfs_device *tmp; |
| 2243 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2244 | devices = &fs_info->fs_devices->devices; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2245 | list_for_each_entry(tmp, devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2246 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2247 | &tmp->dev_state) && !tmp->bdev) { |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2248 | device = tmp; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2249 | break; |
| 2250 | } |
| 2251 | } |
| 2252 | |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2253 | if (!device) |
| 2254 | return ERR_PTR(-ENOENT); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2255 | } else { |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2256 | device = btrfs_find_device_by_path(fs_info, device_path); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2257 | } |
Nikolay Borisov | b444ad4 | 2018-09-03 12:46:12 +0300 | [diff] [blame] | 2258 | |
Nikolay Borisov | 6c05040 | 2018-09-03 12:46:13 +0300 | [diff] [blame] | 2259 | return device; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2260 | } |
| 2261 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2262 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2263 | * Lookup a device given by device id, or the path if the id is 0. |
| 2264 | */ |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2265 | struct btrfs_device *btrfs_find_device_by_devspec( |
| 2266 | struct btrfs_fs_info *fs_info, u64 devid, const char *devpath) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2267 | { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2268 | struct btrfs_device *device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2269 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2270 | if (devid) { |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2271 | device = btrfs_find_device(fs_info, devid, NULL, NULL); |
| 2272 | if (!device) |
| 2273 | return ERR_PTR(-ENOENT); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2274 | } else { |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2275 | if (!devpath || !devpath[0]) |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2276 | return ERR_PTR(-EINVAL); |
| 2277 | device = btrfs_find_device_missing_or_by_path(fs_info, devpath); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2278 | } |
Nikolay Borisov | a27a94c | 2018-09-03 12:46:14 +0300 | [diff] [blame] | 2279 | return device; |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2280 | } |
| 2281 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2282 | /* |
| 2283 | * does all the dirty work required for changing file system's UUID. |
| 2284 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2285 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2286 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2287 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2288 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2289 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2290 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2291 | struct btrfs_device *device; |
| 2292 | u64 super_flags; |
| 2293 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2294 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2295 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2296 | return -EINVAL; |
| 2297 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2298 | seed_devices = alloc_fs_devices(NULL, NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2299 | if (IS_ERR(seed_devices)) |
| 2300 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2301 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2302 | old_devices = clone_fs_devices(fs_devices); |
| 2303 | if (IS_ERR(old_devices)) { |
| 2304 | kfree(seed_devices); |
| 2305 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2306 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2307 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2308 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2309 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2310 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2311 | seed_devices->opened = 1; |
| 2312 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2313 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2314 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2315 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2316 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2317 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2318 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2319 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2320 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2321 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2322 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2323 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2324 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2325 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2326 | fs_devices->seeding = 0; |
| 2327 | fs_devices->num_devices = 0; |
| 2328 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2329 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2330 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2331 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2332 | |
| 2333 | generate_random_uuid(fs_devices->fsid); |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 2334 | memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2335 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2336 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2337 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2338 | super_flags = btrfs_super_flags(disk_super) & |
| 2339 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2340 | btrfs_set_super_flags(disk_super, super_flags); |
| 2341 | |
| 2342 | return 0; |
| 2343 | } |
| 2344 | |
| 2345 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2346 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2347 | */ |
| 2348 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2349 | struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2350 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2351 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2352 | struct btrfs_path *path; |
| 2353 | struct extent_buffer *leaf; |
| 2354 | struct btrfs_dev_item *dev_item; |
| 2355 | struct btrfs_device *device; |
| 2356 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2357 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2358 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2359 | u64 devid; |
| 2360 | int ret; |
| 2361 | |
| 2362 | path = btrfs_alloc_path(); |
| 2363 | if (!path) |
| 2364 | return -ENOMEM; |
| 2365 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2366 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2367 | key.offset = 0; |
| 2368 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2369 | |
| 2370 | while (1) { |
| 2371 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2372 | if (ret < 0) |
| 2373 | goto error; |
| 2374 | |
| 2375 | leaf = path->nodes[0]; |
| 2376 | next_slot: |
| 2377 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2378 | ret = btrfs_next_leaf(root, path); |
| 2379 | if (ret > 0) |
| 2380 | break; |
| 2381 | if (ret < 0) |
| 2382 | goto error; |
| 2383 | leaf = path->nodes[0]; |
| 2384 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2385 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2386 | continue; |
| 2387 | } |
| 2388 | |
| 2389 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2390 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2391 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2392 | break; |
| 2393 | |
| 2394 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2395 | struct btrfs_dev_item); |
| 2396 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2397 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2398 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2399 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2400 | BTRFS_FSID_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2401 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2402 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2403 | |
| 2404 | if (device->fs_devices->seeding) { |
| 2405 | btrfs_set_device_generation(leaf, dev_item, |
| 2406 | device->generation); |
| 2407 | btrfs_mark_buffer_dirty(leaf); |
| 2408 | } |
| 2409 | |
| 2410 | path->slots[0]++; |
| 2411 | goto next_slot; |
| 2412 | } |
| 2413 | ret = 0; |
| 2414 | error: |
| 2415 | btrfs_free_path(path); |
| 2416 | return ret; |
| 2417 | } |
| 2418 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2419 | 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] | 2420 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2421 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2422 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2423 | struct btrfs_trans_handle *trans; |
| 2424 | struct btrfs_device *device; |
| 2425 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2426 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2427 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2428 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2429 | u64 orig_super_total_bytes; |
| 2430 | u64 orig_super_num_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2431 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2432 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2433 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2434 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2435 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2436 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2437 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2438 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2439 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2440 | if (IS_ERR(bdev)) |
| 2441 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2442 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2443 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2444 | seeding_dev = 1; |
| 2445 | down_write(&sb->s_umount); |
| 2446 | mutex_lock(&uuid_mutex); |
| 2447 | } |
| 2448 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2449 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2450 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2451 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2452 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2453 | if (device->bdev == bdev) { |
| 2454 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2455 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2456 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2457 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2458 | } |
| 2459 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2460 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2461 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2462 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2463 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2464 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2465 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2466 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2467 | } |
| 2468 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2469 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2470 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2471 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2472 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2473 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2474 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2475 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2476 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2477 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2478 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2479 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2482 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2483 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2484 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2485 | device->io_width = fs_info->sectorsize; |
| 2486 | device->io_align = fs_info->sectorsize; |
| 2487 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2488 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2489 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2490 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2491 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2492 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2493 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2494 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2495 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2496 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2497 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2498 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2499 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2500 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2501 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2502 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2503 | if (ret) { |
| 2504 | btrfs_abort_transaction(trans, ret); |
| 2505 | goto error_trans; |
| 2506 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2507 | } |
| 2508 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2509 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2510 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2511 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2512 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2513 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2514 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2515 | fs_devices->num_devices++; |
| 2516 | fs_devices->open_devices++; |
| 2517 | fs_devices->rw_devices++; |
| 2518 | fs_devices->total_devices++; |
| 2519 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2520 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2521 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2522 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2523 | if (!blk_queue_nonrot(q)) |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2524 | fs_devices->rotating = 1; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2525 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2526 | orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2527 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2528 | round_down(orig_super_total_bytes + device->total_bytes, |
| 2529 | fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2530 | |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2531 | orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy); |
| 2532 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2533 | orig_super_num_devices + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2534 | |
| 2535 | /* add sysfs device entry */ |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2536 | btrfs_sysfs_add_device_link(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2537 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2538 | /* |
| 2539 | * we've got more storage, clear any full flags on the space |
| 2540 | * infos |
| 2541 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2542 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2543 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2544 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2545 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2546 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2547 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2548 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 2549 | ret = init_first_rw_device(trans, fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2550 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2551 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2552 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2553 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2554 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2555 | } |
| 2556 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2557 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2558 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2559 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2560 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | if (seeding_dev) { |
| 2564 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2565 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2566 | ret = btrfs_finish_sprout(trans, fs_info); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2567 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2568 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2569 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2570 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2571 | |
| 2572 | /* Sprouting would change fsid of the mounted root, |
| 2573 | * so rename the fsid on the sysfs |
| 2574 | */ |
| 2575 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 2576 | fs_info->fs_devices->fsid); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2577 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2578 | btrfs_warn(fs_info, |
| 2579 | "sysfs: failed to create fsid for sprout"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2580 | } |
| 2581 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2582 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2583 | |
| 2584 | if (seeding_dev) { |
| 2585 | mutex_unlock(&uuid_mutex); |
| 2586 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2587 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2588 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2589 | if (ret) /* transaction commit */ |
| 2590 | return ret; |
| 2591 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2592 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2593 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2594 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2595 | "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] | 2596 | trans = btrfs_attach_transaction(root); |
| 2597 | if (IS_ERR(trans)) { |
| 2598 | if (PTR_ERR(trans) == -ENOENT) |
| 2599 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2600 | ret = PTR_ERR(trans); |
| 2601 | trans = NULL; |
| 2602 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2603 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2604 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2605 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2606 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2607 | /* Update ctime/mtime for libblkid */ |
| 2608 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2609 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2610 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2611 | error_sysfs: |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2612 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Naohiro Aota | 39379fa | 2018-07-27 09:04:55 +0900 | [diff] [blame] | 2613 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 2614 | mutex_lock(&fs_info->chunk_mutex); |
| 2615 | list_del_rcu(&device->dev_list); |
| 2616 | list_del(&device->dev_alloc_list); |
| 2617 | fs_info->fs_devices->num_devices--; |
| 2618 | fs_info->fs_devices->open_devices--; |
| 2619 | fs_info->fs_devices->rw_devices--; |
| 2620 | fs_info->fs_devices->total_devices--; |
| 2621 | fs_info->fs_devices->total_rw_bytes -= device->total_bytes; |
| 2622 | atomic64_sub(device->total_bytes, &fs_info->free_chunk_space); |
| 2623 | btrfs_set_super_total_bytes(fs_info->super_copy, |
| 2624 | orig_super_total_bytes); |
| 2625 | btrfs_set_super_num_devices(fs_info->super_copy, |
| 2626 | orig_super_num_devices); |
| 2627 | mutex_unlock(&fs_info->chunk_mutex); |
| 2628 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2629 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2630 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2631 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2632 | if (trans) |
| 2633 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2634 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2635 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2636 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2637 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2638 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2639 | mutex_unlock(&uuid_mutex); |
| 2640 | up_write(&sb->s_umount); |
| 2641 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2642 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2643 | } |
| 2644 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2645 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2646 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2647 | { |
| 2648 | int ret; |
| 2649 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2650 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2651 | struct btrfs_dev_item *dev_item; |
| 2652 | struct extent_buffer *leaf; |
| 2653 | struct btrfs_key key; |
| 2654 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2655 | path = btrfs_alloc_path(); |
| 2656 | if (!path) |
| 2657 | return -ENOMEM; |
| 2658 | |
| 2659 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2660 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2661 | key.offset = device->devid; |
| 2662 | |
| 2663 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2664 | if (ret < 0) |
| 2665 | goto out; |
| 2666 | |
| 2667 | if (ret > 0) { |
| 2668 | ret = -ENOENT; |
| 2669 | goto out; |
| 2670 | } |
| 2671 | |
| 2672 | leaf = path->nodes[0]; |
| 2673 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2674 | |
| 2675 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2676 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2677 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2678 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2679 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2680 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2681 | btrfs_device_get_disk_total_bytes(device)); |
| 2682 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2683 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2684 | btrfs_mark_buffer_dirty(leaf); |
| 2685 | |
| 2686 | out: |
| 2687 | btrfs_free_path(path); |
| 2688 | return ret; |
| 2689 | } |
| 2690 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2691 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2692 | struct btrfs_device *device, u64 new_size) |
| 2693 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2694 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2695 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2696 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2697 | u64 old_total; |
| 2698 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2699 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2700 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2701 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2702 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2703 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2704 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2705 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2706 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2707 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2708 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2709 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2710 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2711 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2712 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2713 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2714 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2715 | fs_devices = fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2716 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2717 | btrfs_set_super_total_bytes(super_copy, |
| 2718 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2719 | device->fs_devices->total_rw_bytes += diff; |
| 2720 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2721 | btrfs_device_set_total_bytes(device, new_size); |
| 2722 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2723 | btrfs_clear_space_info_full(device->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2724 | if (list_empty(&device->resized_list)) |
| 2725 | list_add_tail(&device->resized_list, |
| 2726 | &fs_devices->resized_devices); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2727 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2728 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2729 | return btrfs_update_device(trans, device); |
| 2730 | } |
| 2731 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2732 | 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] | 2733 | { |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2734 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2735 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2736 | int ret; |
| 2737 | struct btrfs_path *path; |
| 2738 | struct btrfs_key key; |
| 2739 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2740 | path = btrfs_alloc_path(); |
| 2741 | if (!path) |
| 2742 | return -ENOMEM; |
| 2743 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2744 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2745 | key.offset = chunk_offset; |
| 2746 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2747 | |
| 2748 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2749 | if (ret < 0) |
| 2750 | goto out; |
| 2751 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2752 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2753 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2754 | ret = -ENOENT; |
| 2755 | goto out; |
| 2756 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2757 | |
| 2758 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2759 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2760 | btrfs_handle_fs_error(fs_info, ret, |
| 2761 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2762 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2763 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2764 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2765 | } |
| 2766 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2767 | 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] | 2768 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2769 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2770 | struct btrfs_disk_key *disk_key; |
| 2771 | struct btrfs_chunk *chunk; |
| 2772 | u8 *ptr; |
| 2773 | int ret = 0; |
| 2774 | u32 num_stripes; |
| 2775 | u32 array_size; |
| 2776 | u32 len = 0; |
| 2777 | u32 cur; |
| 2778 | struct btrfs_key key; |
| 2779 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2780 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2781 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2782 | |
| 2783 | ptr = super_copy->sys_chunk_array; |
| 2784 | cur = 0; |
| 2785 | |
| 2786 | while (cur < array_size) { |
| 2787 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2788 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2789 | |
| 2790 | len = sizeof(*disk_key); |
| 2791 | |
| 2792 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2793 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2794 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2795 | len += btrfs_chunk_item_size(num_stripes); |
| 2796 | } else { |
| 2797 | ret = -EIO; |
| 2798 | break; |
| 2799 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2800 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2801 | key.offset == chunk_offset) { |
| 2802 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2803 | array_size -= len; |
| 2804 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2805 | } else { |
| 2806 | ptr += len; |
| 2807 | cur += len; |
| 2808 | } |
| 2809 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2810 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2811 | return ret; |
| 2812 | } |
| 2813 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2814 | /* |
| 2815 | * btrfs_get_chunk_map() - Find the mapping containing the given logical extent. |
| 2816 | * @logical: Logical block offset in bytes. |
| 2817 | * @length: Length of extent in bytes. |
| 2818 | * |
| 2819 | * Return: Chunk mapping or ERR_PTR. |
| 2820 | */ |
| 2821 | struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2822 | u64 logical, u64 length) |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2823 | { |
| 2824 | struct extent_map_tree *em_tree; |
| 2825 | struct extent_map *em; |
| 2826 | |
| 2827 | em_tree = &fs_info->mapping_tree.map_tree; |
| 2828 | read_lock(&em_tree->lock); |
| 2829 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2830 | read_unlock(&em_tree->lock); |
| 2831 | |
| 2832 | if (!em) { |
| 2833 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2834 | logical, length); |
| 2835 | return ERR_PTR(-EINVAL); |
| 2836 | } |
| 2837 | |
| 2838 | if (em->start > logical || em->start + em->len < logical) { |
| 2839 | btrfs_crit(fs_info, |
| 2840 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2841 | logical, length, em->start, em->start + em->len); |
| 2842 | free_extent_map(em); |
| 2843 | return ERR_PTR(-EINVAL); |
| 2844 | } |
| 2845 | |
| 2846 | /* callers are responsible for dropping em's ref. */ |
| 2847 | return em; |
| 2848 | } |
| 2849 | |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2850 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2851 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2852 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2853 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2854 | struct map_lookup *map; |
| 2855 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2856 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2857 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2858 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 2859 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2860 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2861 | /* |
| 2862 | * 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] | 2863 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2864 | * do anything we still error out. |
| 2865 | */ |
| 2866 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2867 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2868 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2869 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2870 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2871 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2872 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2873 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2874 | /* |
| 2875 | * Take the device list mutex to prevent races with the final phase of |
| 2876 | * a device replace operation that replaces the device object associated |
| 2877 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2878 | */ |
| 2879 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2880 | for (i = 0; i < map->num_stripes; i++) { |
| 2881 | struct btrfs_device *device = map->stripes[i].dev; |
| 2882 | ret = btrfs_free_dev_extent(trans, device, |
| 2883 | map->stripes[i].physical, |
| 2884 | &dev_extent_len); |
| 2885 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2886 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2887 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2888 | goto out; |
| 2889 | } |
| 2890 | |
| 2891 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2892 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2893 | btrfs_device_set_bytes_used(device, |
| 2894 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2895 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2896 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2897 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2898 | } |
| 2899 | |
Nikolay Borisov | 64bc6c2 | 2018-10-26 14:43:19 +0300 | [diff] [blame] | 2900 | ret = btrfs_update_device(trans, device); |
| 2901 | if (ret) { |
| 2902 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2903 | btrfs_abort_transaction(trans, ret); |
| 2904 | goto out; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2905 | } |
| 2906 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2907 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2908 | |
Nikolay Borisov | f420879 | 2018-07-20 19:37:52 +0300 | [diff] [blame] | 2909 | ret = btrfs_free_chunk(trans, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2910 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2911 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2912 | goto out; |
| 2913 | } |
| 2914 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2915 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2916 | |
| 2917 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2918 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2919 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2920 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2921 | goto out; |
| 2922 | } |
| 2923 | } |
| 2924 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 2925 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2926 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2927 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2928 | goto out; |
| 2929 | } |
| 2930 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2931 | out: |
| 2932 | /* once for us */ |
| 2933 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2934 | return ret; |
| 2935 | } |
| 2936 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2937 | 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] | 2938 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2939 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2940 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2941 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2942 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2943 | /* |
| 2944 | * Prevent races with automatic removal of unused block groups. |
| 2945 | * After we relocate and before we remove the chunk with offset |
| 2946 | * chunk_offset, automatic removal of the block group can kick in, |
| 2947 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2948 | * |
| 2949 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2950 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2951 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2952 | * we release the path used to search the chunk/dev tree and before |
| 2953 | * the current task acquires this mutex and calls us. |
| 2954 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2955 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2956 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2957 | ret = btrfs_can_relocate(fs_info, chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2958 | if (ret) |
| 2959 | return -ENOSPC; |
| 2960 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2961 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2962 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2963 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2964 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2965 | if (ret) |
| 2966 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2967 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 2968 | /* |
| 2969 | * We add the kobjects here (and after forcing data chunk creation) |
| 2970 | * since relocation is the only place we'll create chunks of a new |
| 2971 | * type at runtime. The only place where we'll remove the last |
| 2972 | * chunk of a type is the call immediately below this one. Even |
| 2973 | * so, we're protected against races with the cleaner thread since |
| 2974 | * we're covered by the delete_unused_bgs_mutex. |
| 2975 | */ |
| 2976 | btrfs_add_raid_kobjects(fs_info); |
| 2977 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2978 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 2979 | chunk_offset); |
| 2980 | if (IS_ERR(trans)) { |
| 2981 | ret = PTR_ERR(trans); |
| 2982 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 2983 | return ret; |
| 2984 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 2985 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2986 | /* |
| 2987 | * step two, delete the device extents and the |
| 2988 | * chunk tree entries |
| 2989 | */ |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 2990 | ret = btrfs_remove_chunk(trans, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2991 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2992 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2993 | } |
| 2994 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2995 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2996 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2997 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2998 | struct btrfs_path *path; |
| 2999 | struct extent_buffer *leaf; |
| 3000 | struct btrfs_chunk *chunk; |
| 3001 | struct btrfs_key key; |
| 3002 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3003 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3004 | bool retried = false; |
| 3005 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3006 | int ret; |
| 3007 | |
| 3008 | path = btrfs_alloc_path(); |
| 3009 | if (!path) |
| 3010 | return -ENOMEM; |
| 3011 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3012 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3013 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3014 | key.offset = (u64)-1; |
| 3015 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3016 | |
| 3017 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3018 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3019 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3020 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3021 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3022 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3023 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3024 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3025 | |
| 3026 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 3027 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3028 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3029 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3030 | if (ret < 0) |
| 3031 | goto error; |
| 3032 | if (ret > 0) |
| 3033 | break; |
| 3034 | |
| 3035 | leaf = path->nodes[0]; |
| 3036 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3037 | |
| 3038 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 3039 | struct btrfs_chunk); |
| 3040 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3041 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3042 | |
| 3043 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3044 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3045 | if (ret == -ENOSPC) |
| 3046 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 3047 | else |
| 3048 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3049 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3050 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3051 | |
| 3052 | if (found_key.offset == 0) |
| 3053 | break; |
| 3054 | key.offset = found_key.offset - 1; |
| 3055 | } |
| 3056 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3057 | if (failed && !retried) { |
| 3058 | failed = 0; |
| 3059 | retried = true; |
| 3060 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3061 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3062 | ret = -ENOSPC; |
| 3063 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3064 | error: |
| 3065 | btrfs_free_path(path); |
| 3066 | return ret; |
| 3067 | } |
| 3068 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3069 | /* |
| 3070 | * return 1 : allocate a data chunk successfully, |
| 3071 | * return <0: errors during allocating a data chunk, |
| 3072 | * return 0 : no need to allocate a data chunk. |
| 3073 | */ |
| 3074 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 3075 | u64 chunk_offset) |
| 3076 | { |
| 3077 | struct btrfs_block_group_cache *cache; |
| 3078 | u64 bytes_used; |
| 3079 | u64 chunk_type; |
| 3080 | |
| 3081 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3082 | ASSERT(cache); |
| 3083 | chunk_type = cache->flags; |
| 3084 | btrfs_put_block_group(cache); |
| 3085 | |
| 3086 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) { |
| 3087 | spin_lock(&fs_info->data_sinfo->lock); |
| 3088 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 3089 | spin_unlock(&fs_info->data_sinfo->lock); |
| 3090 | |
| 3091 | if (!bytes_used) { |
| 3092 | struct btrfs_trans_handle *trans; |
| 3093 | int ret; |
| 3094 | |
| 3095 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 3096 | if (IS_ERR(trans)) |
| 3097 | return PTR_ERR(trans); |
| 3098 | |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 3099 | ret = btrfs_force_chunk_alloc(trans, |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3100 | BTRFS_BLOCK_GROUP_DATA); |
| 3101 | btrfs_end_transaction(trans); |
| 3102 | if (ret < 0) |
| 3103 | return ret; |
| 3104 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 3105 | btrfs_add_raid_kobjects(fs_info); |
| 3106 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3107 | return 1; |
| 3108 | } |
| 3109 | } |
| 3110 | return 0; |
| 3111 | } |
| 3112 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3113 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3114 | struct btrfs_balance_control *bctl) |
| 3115 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3116 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3117 | struct btrfs_trans_handle *trans; |
| 3118 | struct btrfs_balance_item *item; |
| 3119 | struct btrfs_disk_balance_args disk_bargs; |
| 3120 | struct btrfs_path *path; |
| 3121 | struct extent_buffer *leaf; |
| 3122 | struct btrfs_key key; |
| 3123 | int ret, err; |
| 3124 | |
| 3125 | path = btrfs_alloc_path(); |
| 3126 | if (!path) |
| 3127 | return -ENOMEM; |
| 3128 | |
| 3129 | trans = btrfs_start_transaction(root, 0); |
| 3130 | if (IS_ERR(trans)) { |
| 3131 | btrfs_free_path(path); |
| 3132 | return PTR_ERR(trans); |
| 3133 | } |
| 3134 | |
| 3135 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3136 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3137 | key.offset = 0; |
| 3138 | |
| 3139 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3140 | sizeof(*item)); |
| 3141 | if (ret) |
| 3142 | goto out; |
| 3143 | |
| 3144 | leaf = path->nodes[0]; |
| 3145 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3146 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3147 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3148 | |
| 3149 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3150 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3151 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3152 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3153 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3154 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3155 | |
| 3156 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3157 | |
| 3158 | btrfs_mark_buffer_dirty(leaf); |
| 3159 | out: |
| 3160 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3161 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3162 | if (err && !ret) |
| 3163 | ret = err; |
| 3164 | return ret; |
| 3165 | } |
| 3166 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3167 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3168 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3169 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3170 | struct btrfs_trans_handle *trans; |
| 3171 | struct btrfs_path *path; |
| 3172 | struct btrfs_key key; |
| 3173 | int ret, err; |
| 3174 | |
| 3175 | path = btrfs_alloc_path(); |
| 3176 | if (!path) |
| 3177 | return -ENOMEM; |
| 3178 | |
| 3179 | trans = btrfs_start_transaction(root, 0); |
| 3180 | if (IS_ERR(trans)) { |
| 3181 | btrfs_free_path(path); |
| 3182 | return PTR_ERR(trans); |
| 3183 | } |
| 3184 | |
| 3185 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3186 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3187 | key.offset = 0; |
| 3188 | |
| 3189 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3190 | if (ret < 0) |
| 3191 | goto out; |
| 3192 | if (ret > 0) { |
| 3193 | ret = -ENOENT; |
| 3194 | goto out; |
| 3195 | } |
| 3196 | |
| 3197 | ret = btrfs_del_item(trans, root, path); |
| 3198 | out: |
| 3199 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3200 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3201 | if (err && !ret) |
| 3202 | ret = err; |
| 3203 | return ret; |
| 3204 | } |
| 3205 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3206 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3207 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3208 | * resume after balance was interrupted. |
| 3209 | */ |
| 3210 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3211 | { |
| 3212 | /* |
| 3213 | * Turn on soft mode for chunk types that were being converted. |
| 3214 | */ |
| 3215 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3216 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3217 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3218 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3219 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3220 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3221 | |
| 3222 | /* |
| 3223 | * Turn on usage filter if is not already used. The idea is |
| 3224 | * that chunks that we have already balanced should be |
| 3225 | * reasonably full. Don't do it for chunks that are being |
| 3226 | * converted - that will keep us from relocating unconverted |
| 3227 | * (albeit full) chunks. |
| 3228 | */ |
| 3229 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3230 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3231 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3232 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3233 | bctl->data.usage = 90; |
| 3234 | } |
| 3235 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3236 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3237 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3238 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3239 | bctl->sys.usage = 90; |
| 3240 | } |
| 3241 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3242 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3243 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3244 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3245 | bctl->meta.usage = 90; |
| 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3250 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3251 | */ |
| 3252 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3253 | { |
| 3254 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3255 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3256 | |
| 3257 | BUG_ON(!fs_info->balance_ctl); |
| 3258 | |
| 3259 | spin_lock(&fs_info->balance_lock); |
| 3260 | fs_info->balance_ctl = NULL; |
| 3261 | spin_unlock(&fs_info->balance_lock); |
| 3262 | |
| 3263 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3264 | ret = del_balance_item(fs_info); |
| 3265 | if (ret) |
| 3266 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3267 | } |
| 3268 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3269 | /* |
| 3270 | * Balance filters. Return 1 if chunk should be filtered out |
| 3271 | * (should not be balanced). |
| 3272 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3273 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3274 | struct btrfs_balance_args *bargs) |
| 3275 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3276 | chunk_type = chunk_to_extended(chunk_type) & |
| 3277 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3278 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3279 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3280 | return 0; |
| 3281 | |
| 3282 | return 1; |
| 3283 | } |
| 3284 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3285 | 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] | 3286 | struct btrfs_balance_args *bargs) |
| 3287 | { |
| 3288 | struct btrfs_block_group_cache *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3289 | u64 chunk_used; |
| 3290 | u64 user_thresh_min; |
| 3291 | u64 user_thresh_max; |
| 3292 | int ret = 1; |
| 3293 | |
| 3294 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3295 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3296 | |
| 3297 | if (bargs->usage_min == 0) |
| 3298 | user_thresh_min = 0; |
| 3299 | else |
| 3300 | user_thresh_min = div_factor_fine(cache->key.offset, |
| 3301 | bargs->usage_min); |
| 3302 | |
| 3303 | if (bargs->usage_max == 0) |
| 3304 | user_thresh_max = 1; |
| 3305 | else if (bargs->usage_max > 100) |
| 3306 | user_thresh_max = cache->key.offset; |
| 3307 | else |
| 3308 | user_thresh_max = div_factor_fine(cache->key.offset, |
| 3309 | bargs->usage_max); |
| 3310 | |
| 3311 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3312 | ret = 0; |
| 3313 | |
| 3314 | btrfs_put_block_group(cache); |
| 3315 | return ret; |
| 3316 | } |
| 3317 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3318 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3319 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3320 | { |
| 3321 | struct btrfs_block_group_cache *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3322 | u64 chunk_used, user_thresh; |
| 3323 | int ret = 1; |
| 3324 | |
| 3325 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3326 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3327 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3328 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3329 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3330 | else if (bargs->usage > 100) |
| 3331 | user_thresh = cache->key.offset; |
| 3332 | else |
| 3333 | user_thresh = div_factor_fine(cache->key.offset, |
| 3334 | bargs->usage); |
| 3335 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3336 | if (chunk_used < user_thresh) |
| 3337 | ret = 0; |
| 3338 | |
| 3339 | btrfs_put_block_group(cache); |
| 3340 | return ret; |
| 3341 | } |
| 3342 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3343 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3344 | struct btrfs_chunk *chunk, |
| 3345 | struct btrfs_balance_args *bargs) |
| 3346 | { |
| 3347 | struct btrfs_stripe *stripe; |
| 3348 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3349 | int i; |
| 3350 | |
| 3351 | for (i = 0; i < num_stripes; i++) { |
| 3352 | stripe = btrfs_stripe_nr(chunk, i); |
| 3353 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3354 | return 0; |
| 3355 | } |
| 3356 | |
| 3357 | return 1; |
| 3358 | } |
| 3359 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3360 | /* [pstart, pend) */ |
| 3361 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3362 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3363 | struct btrfs_balance_args *bargs) |
| 3364 | { |
| 3365 | struct btrfs_stripe *stripe; |
| 3366 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3367 | u64 stripe_offset; |
| 3368 | u64 stripe_length; |
| 3369 | int factor; |
| 3370 | int i; |
| 3371 | |
| 3372 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3373 | return 0; |
| 3374 | |
| 3375 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3376 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3377 | factor = num_stripes / 2; |
| 3378 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3379 | factor = num_stripes - 1; |
| 3380 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3381 | factor = num_stripes - 2; |
| 3382 | } else { |
| 3383 | factor = num_stripes; |
| 3384 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3385 | |
| 3386 | for (i = 0; i < num_stripes; i++) { |
| 3387 | stripe = btrfs_stripe_nr(chunk, i); |
| 3388 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3389 | continue; |
| 3390 | |
| 3391 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3392 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3393 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3394 | |
| 3395 | if (stripe_offset < bargs->pend && |
| 3396 | stripe_offset + stripe_length > bargs->pstart) |
| 3397 | return 0; |
| 3398 | } |
| 3399 | |
| 3400 | return 1; |
| 3401 | } |
| 3402 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3403 | /* [vstart, vend) */ |
| 3404 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3405 | struct btrfs_chunk *chunk, |
| 3406 | u64 chunk_offset, |
| 3407 | struct btrfs_balance_args *bargs) |
| 3408 | { |
| 3409 | if (chunk_offset < bargs->vend && |
| 3410 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3411 | /* at least part of the chunk is inside this vrange */ |
| 3412 | return 0; |
| 3413 | |
| 3414 | return 1; |
| 3415 | } |
| 3416 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3417 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3418 | struct btrfs_chunk *chunk, |
| 3419 | struct btrfs_balance_args *bargs) |
| 3420 | { |
| 3421 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3422 | |
| 3423 | if (bargs->stripes_min <= num_stripes |
| 3424 | && num_stripes <= bargs->stripes_max) |
| 3425 | return 0; |
| 3426 | |
| 3427 | return 1; |
| 3428 | } |
| 3429 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3430 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3431 | struct btrfs_balance_args *bargs) |
| 3432 | { |
| 3433 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3434 | return 0; |
| 3435 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3436 | chunk_type = chunk_to_extended(chunk_type) & |
| 3437 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3438 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3439 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3440 | return 1; |
| 3441 | |
| 3442 | return 0; |
| 3443 | } |
| 3444 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3445 | static int should_balance_chunk(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3446 | struct extent_buffer *leaf, |
| 3447 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3448 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3449 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3450 | struct btrfs_balance_args *bargs = NULL; |
| 3451 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3452 | |
| 3453 | /* type filter */ |
| 3454 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3455 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3456 | return 0; |
| 3457 | } |
| 3458 | |
| 3459 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3460 | bargs = &bctl->data; |
| 3461 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3462 | bargs = &bctl->sys; |
| 3463 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3464 | bargs = &bctl->meta; |
| 3465 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3466 | /* profiles filter */ |
| 3467 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3468 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3469 | return 0; |
| 3470 | } |
| 3471 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3472 | /* usage filter */ |
| 3473 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3474 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3475 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3476 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3477 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3478 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3481 | /* devid filter */ |
| 3482 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3483 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3484 | return 0; |
| 3485 | } |
| 3486 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3487 | /* drange filter, makes sense only with devid filter */ |
| 3488 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3489 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3490 | return 0; |
| 3491 | } |
| 3492 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3493 | /* vrange filter */ |
| 3494 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3495 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3496 | return 0; |
| 3497 | } |
| 3498 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3499 | /* stripes filter */ |
| 3500 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3501 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3502 | return 0; |
| 3503 | } |
| 3504 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3505 | /* soft profile changing mode */ |
| 3506 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3507 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3508 | return 0; |
| 3509 | } |
| 3510 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3511 | /* |
| 3512 | * limited by count, must be the last filter |
| 3513 | */ |
| 3514 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3515 | if (bargs->limit == 0) |
| 3516 | return 0; |
| 3517 | else |
| 3518 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3519 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3520 | /* |
| 3521 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3522 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3523 | * about the count of all chunks that satisfy the filters. |
| 3524 | */ |
| 3525 | if (bargs->limit_max == 0) |
| 3526 | return 0; |
| 3527 | else |
| 3528 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3529 | } |
| 3530 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3531 | return 1; |
| 3532 | } |
| 3533 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3534 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3535 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3536 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3537 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3538 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3539 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3540 | struct btrfs_device *device; |
| 3541 | u64 old_size; |
| 3542 | u64 size_to_free; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3543 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3544 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3545 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3546 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3547 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3548 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3549 | struct extent_buffer *leaf; |
| 3550 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3551 | int ret; |
| 3552 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3553 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3554 | /* 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] | 3555 | u64 limit_data = bctl->data.limit; |
| 3556 | u64 limit_meta = bctl->meta.limit; |
| 3557 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3558 | u32 count_data = 0; |
| 3559 | u32 count_meta = 0; |
| 3560 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3561 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3562 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3563 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3564 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3565 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3566 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3567 | size_to_free = div_factor(old_size, 1); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3568 | size_to_free = min_t(u64, size_to_free, SZ_1M); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 3569 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3570 | btrfs_device_get_total_bytes(device) - |
| 3571 | btrfs_device_get_bytes_used(device) > size_to_free || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 3572 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3573 | continue; |
| 3574 | |
| 3575 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3576 | if (ret == -ENOSPC) |
| 3577 | break; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3578 | if (ret) { |
| 3579 | /* btrfs_shrink_device never returns ret > 0 */ |
| 3580 | WARN_ON(ret > 0); |
| 3581 | goto error; |
| 3582 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3583 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3584 | trans = btrfs_start_transaction(dev_root, 0); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3585 | if (IS_ERR(trans)) { |
| 3586 | ret = PTR_ERR(trans); |
| 3587 | btrfs_info_in_rcu(fs_info, |
| 3588 | "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu", |
| 3589 | rcu_str_deref(device->name), ret, |
| 3590 | old_size, old_size - size_to_free); |
| 3591 | goto error; |
| 3592 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3593 | |
| 3594 | ret = btrfs_grow_device(trans, device, old_size); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3595 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3596 | btrfs_end_transaction(trans); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3597 | /* btrfs_grow_device never returns ret > 0 */ |
| 3598 | WARN_ON(ret > 0); |
| 3599 | btrfs_info_in_rcu(fs_info, |
| 3600 | "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu", |
| 3601 | rcu_str_deref(device->name), ret, |
| 3602 | old_size, old_size - size_to_free); |
| 3603 | goto error; |
| 3604 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3605 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3606 | btrfs_end_transaction(trans); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | /* step two, relocate all the chunks */ |
| 3610 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3611 | if (!path) { |
| 3612 | ret = -ENOMEM; |
| 3613 | goto error; |
| 3614 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3615 | |
| 3616 | /* zero out stat counters */ |
| 3617 | spin_lock(&fs_info->balance_lock); |
| 3618 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3619 | spin_unlock(&fs_info->balance_lock); |
| 3620 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3621 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3622 | /* |
| 3623 | * The single value limit and min/max limits use the same bytes |
| 3624 | * in the |
| 3625 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3626 | bctl->data.limit = limit_data; |
| 3627 | bctl->meta.limit = limit_meta; |
| 3628 | bctl->sys.limit = limit_sys; |
| 3629 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3630 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3631 | key.offset = (u64)-1; |
| 3632 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3633 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3634 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3635 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3636 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3637 | ret = -ECANCELED; |
| 3638 | goto error; |
| 3639 | } |
| 3640 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3641 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3642 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3643 | if (ret < 0) { |
| 3644 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3645 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3646 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3647 | |
| 3648 | /* |
| 3649 | * this shouldn't happen, it means the last relocate |
| 3650 | * failed |
| 3651 | */ |
| 3652 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3653 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3654 | |
| 3655 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3656 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3657 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3658 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3659 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3660 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3661 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3662 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3663 | leaf = path->nodes[0]; |
| 3664 | slot = path->slots[0]; |
| 3665 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3666 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3667 | if (found_key.objectid != key.objectid) { |
| 3668 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3669 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3670 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3671 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3672 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3673 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3674 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3675 | if (!counting) { |
| 3676 | spin_lock(&fs_info->balance_lock); |
| 3677 | bctl->stat.considered++; |
| 3678 | spin_unlock(&fs_info->balance_lock); |
| 3679 | } |
| 3680 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3681 | ret = should_balance_chunk(fs_info, leaf, chunk, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3682 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3683 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3684 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3685 | if (!ret) { |
| 3686 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3687 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3688 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3689 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3690 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3691 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3692 | spin_lock(&fs_info->balance_lock); |
| 3693 | bctl->stat.expected++; |
| 3694 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3695 | |
| 3696 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3697 | count_data++; |
| 3698 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3699 | count_sys++; |
| 3700 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3701 | count_meta++; |
| 3702 | |
| 3703 | goto loop; |
| 3704 | } |
| 3705 | |
| 3706 | /* |
| 3707 | * Apply limit_min filter, no need to check if the LIMITS |
| 3708 | * filter is used, limit_min is 0 by default |
| 3709 | */ |
| 3710 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3711 | count_data < bctl->data.limit_min) |
| 3712 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3713 | count_meta < bctl->meta.limit_min) |
| 3714 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3715 | count_sys < bctl->sys.limit_min)) { |
| 3716 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3717 | goto loop; |
| 3718 | } |
| 3719 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3720 | if (!chunk_reserved) { |
| 3721 | /* |
| 3722 | * We may be relocating the only data chunk we have, |
| 3723 | * which could potentially end up with losing data's |
| 3724 | * raid profile, so lets allocate an empty one in |
| 3725 | * advance. |
| 3726 | */ |
| 3727 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3728 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3729 | if (ret < 0) { |
| 3730 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3731 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3732 | } else if (ret == 1) { |
| 3733 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3734 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3735 | } |
| 3736 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3737 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3738 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3739 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3740 | enospc_errors++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 3741 | } else if (ret == -ETXTBSY) { |
| 3742 | btrfs_info(fs_info, |
| 3743 | "skipping relocation of block group %llu due to active swapfile", |
| 3744 | found_key.offset); |
| 3745 | ret = 0; |
| 3746 | } else if (ret) { |
| 3747 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3748 | } else { |
| 3749 | spin_lock(&fs_info->balance_lock); |
| 3750 | bctl->stat.completed++; |
| 3751 | spin_unlock(&fs_info->balance_lock); |
| 3752 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3753 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3754 | if (found_key.offset == 0) |
| 3755 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3756 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3757 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3758 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3759 | if (counting) { |
| 3760 | btrfs_release_path(path); |
| 3761 | counting = false; |
| 3762 | goto again; |
| 3763 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3764 | error: |
| 3765 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3766 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3767 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3768 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3769 | if (!ret) |
| 3770 | ret = -ENOSPC; |
| 3771 | } |
| 3772 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3773 | return ret; |
| 3774 | } |
| 3775 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3776 | /** |
| 3777 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3778 | * @flags: profile to validate |
| 3779 | * @extended: if true @flags is treated as an extended profile |
| 3780 | */ |
| 3781 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3782 | { |
| 3783 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3784 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3785 | |
| 3786 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3787 | |
| 3788 | /* 1) check that all other bits are zeroed */ |
| 3789 | if (flags & ~mask) |
| 3790 | return 0; |
| 3791 | |
| 3792 | /* 2) see if profile is reduced */ |
| 3793 | if (flags == 0) |
| 3794 | return !extended; /* "0" is valid for usual profiles */ |
| 3795 | |
| 3796 | /* true if exactly one bit set */ |
David Sterba | 818255f | 2018-09-21 14:26:34 +0200 | [diff] [blame] | 3797 | return is_power_of_2(flags); |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3798 | } |
| 3799 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3800 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3801 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3802 | /* cancel requested || normal exit path */ |
| 3803 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3804 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3805 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3806 | } |
| 3807 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3808 | /* Non-zero return value signifies invalidity */ |
| 3809 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3810 | u64 allowed) |
| 3811 | { |
| 3812 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3813 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3814 | (bctl_arg->target & ~allowed))); |
| 3815 | } |
| 3816 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3817 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3818 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3819 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3820 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 3821 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3822 | struct btrfs_ioctl_balance_args *bargs) |
| 3823 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3824 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3825 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3826 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3827 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3828 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3829 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3830 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3831 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3832 | atomic_read(&fs_info->balance_pause_req) || |
| 3833 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3834 | ret = -EINVAL; |
| 3835 | goto out; |
| 3836 | } |
| 3837 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3838 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3839 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3840 | mixed = 1; |
| 3841 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3842 | /* |
| 3843 | * In case of mixed groups both data and meta should be picked, |
| 3844 | * and identical options should be given for both of them. |
| 3845 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3846 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3847 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3848 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3849 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3850 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3851 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3852 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3853 | ret = -EINVAL; |
| 3854 | goto out; |
| 3855 | } |
| 3856 | } |
| 3857 | |
Anand Jain | 1da7396 | 2018-08-10 13:53:21 +0800 | [diff] [blame] | 3858 | num_devices = btrfs_num_devices(fs_info); |
| 3859 | |
Austin S. Hemmelgarn | 88be159 | 2016-03-23 14:22:59 -0400 | [diff] [blame] | 3860 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP; |
| 3861 | if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3862 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3863 | if (num_devices > 2) |
| 3864 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3865 | if (num_devices > 3) |
| 3866 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3867 | BTRFS_BLOCK_GROUP_RAID6); |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3868 | if (validate_convert_profile(&bctl->data, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3869 | int index = btrfs_bg_flags_to_raid_index(bctl->data.target); |
| 3870 | |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3871 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3872 | "balance: invalid convert data profile %s", |
| 3873 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3874 | ret = -EINVAL; |
| 3875 | goto out; |
| 3876 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3877 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3878 | int index = btrfs_bg_flags_to_raid_index(bctl->meta.target); |
| 3879 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3880 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3881 | "balance: invalid convert metadata profile %s", |
| 3882 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3883 | ret = -EINVAL; |
| 3884 | goto out; |
| 3885 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3886 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3887 | int index = btrfs_bg_flags_to_raid_index(bctl->sys.target); |
| 3888 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3889 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3890 | "balance: invalid convert system profile %s", |
| 3891 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3892 | ret = -EINVAL; |
| 3893 | goto out; |
| 3894 | } |
| 3895 | |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3896 | /* allow to reduce meta or sys integrity only if force set */ |
| 3897 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3898 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3899 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3900 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3901 | do { |
| 3902 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3903 | |
| 3904 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3905 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3906 | !(bctl->sys.target & allowed)) || |
| 3907 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3908 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3909 | !(bctl->meta.target & allowed))) { |
| 3910 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3911 | btrfs_info(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3912 | "balance: force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3913 | } else { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3914 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3915 | "balance: reduces metadata integrity, use --force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3916 | ret = -EINVAL; |
| 3917 | goto out; |
| 3918 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3919 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3920 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3921 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3922 | /* if we're not converting, the target field is uninitialized */ |
| 3923 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 3924 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 3925 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 3926 | bctl->data.target : fs_info->avail_data_alloc_bits; |
| 3927 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 3928 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3929 | int meta_index = btrfs_bg_flags_to_raid_index(meta_target); |
| 3930 | int data_index = btrfs_bg_flags_to_raid_index(data_target); |
| 3931 | |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3932 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3933 | "balance: metadata profile %s has lower redundancy than data profile %s", |
| 3934 | get_raid_name(meta_index), get_raid_name(data_index)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3937 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3938 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3939 | goto out; |
| 3940 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3941 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3942 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 3943 | BUG_ON(fs_info->balance_ctl); |
| 3944 | spin_lock(&fs_info->balance_lock); |
| 3945 | fs_info->balance_ctl = bctl; |
| 3946 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3947 | } else { |
| 3948 | BUG_ON(ret != -EEXIST); |
| 3949 | spin_lock(&fs_info->balance_lock); |
| 3950 | update_balance_args(bctl); |
| 3951 | spin_unlock(&fs_info->balance_lock); |
| 3952 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3953 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3954 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 3955 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3956 | mutex_unlock(&fs_info->balance_mutex); |
| 3957 | |
| 3958 | ret = __btrfs_balance(fs_info); |
| 3959 | |
| 3960 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3961 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3962 | |
| 3963 | if (bargs) { |
| 3964 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 3965 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3966 | } |
| 3967 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3968 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3969 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3970 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 3971 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3972 | } |
| 3973 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3974 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3975 | |
| 3976 | return ret; |
| 3977 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3978 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3979 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 3980 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3981 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 3982 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 3983 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3984 | return ret; |
| 3985 | } |
| 3986 | |
| 3987 | static int balance_kthread(void *data) |
| 3988 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3989 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3990 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3991 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3992 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3993 | if (fs_info->balance_ctl) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3994 | btrfs_info(fs_info, "balance: resuming"); |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3995 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3996 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3997 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3998 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3999 | return ret; |
| 4000 | } |
| 4001 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4002 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 4003 | { |
| 4004 | struct task_struct *tsk; |
| 4005 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4006 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4007 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4008 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4009 | return 0; |
| 4010 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 4011 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4012 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 4013 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4014 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4015 | return 0; |
| 4016 | } |
| 4017 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 4018 | /* |
| 4019 | * A ro->rw remount sequence should continue with the paused balance |
| 4020 | * regardless of who pauses it, system or the user as of now, so set |
| 4021 | * the resume flag. |
| 4022 | */ |
| 4023 | spin_lock(&fs_info->balance_lock); |
| 4024 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 4025 | spin_unlock(&fs_info->balance_lock); |
| 4026 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4027 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 4028 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 4029 | } |
| 4030 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4031 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4032 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4033 | struct btrfs_balance_control *bctl; |
| 4034 | struct btrfs_balance_item *item; |
| 4035 | struct btrfs_disk_balance_args disk_bargs; |
| 4036 | struct btrfs_path *path; |
| 4037 | struct extent_buffer *leaf; |
| 4038 | struct btrfs_key key; |
| 4039 | int ret; |
| 4040 | |
| 4041 | path = btrfs_alloc_path(); |
| 4042 | if (!path) |
| 4043 | return -ENOMEM; |
| 4044 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4045 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 4046 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4047 | key.offset = 0; |
| 4048 | |
| 4049 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 4050 | if (ret < 0) |
| 4051 | goto out; |
| 4052 | if (ret > 0) { /* ret = -ENOENT; */ |
| 4053 | ret = 0; |
| 4054 | goto out; |
| 4055 | } |
| 4056 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4057 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 4058 | if (!bctl) { |
| 4059 | ret = -ENOMEM; |
| 4060 | goto out; |
| 4061 | } |
| 4062 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4063 | leaf = path->nodes[0]; |
| 4064 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 4065 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4066 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 4067 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4068 | |
| 4069 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 4070 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 4071 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 4072 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 4073 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 4074 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 4075 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 4076 | /* |
| 4077 | * This should never happen, as the paused balance state is recovered |
| 4078 | * during mount without any chance of other exclusive ops to collide. |
| 4079 | * |
| 4080 | * This gives the exclusive op status to balance and keeps in paused |
| 4081 | * state until user intervention (cancel or umount). If the ownership |
| 4082 | * cannot be assigned, show a message but do not fail. The balance |
| 4083 | * is in a paused state and must have fs_info::balance_ctl properly |
| 4084 | * set up. |
| 4085 | */ |
| 4086 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 4087 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4088 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 4089 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4090 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 4091 | BUG_ON(fs_info->balance_ctl); |
| 4092 | spin_lock(&fs_info->balance_lock); |
| 4093 | fs_info->balance_ctl = bctl; |
| 4094 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 4095 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 4096 | out: |
| 4097 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4098 | return ret; |
| 4099 | } |
| 4100 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4101 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 4102 | { |
| 4103 | int ret = 0; |
| 4104 | |
| 4105 | mutex_lock(&fs_info->balance_mutex); |
| 4106 | if (!fs_info->balance_ctl) { |
| 4107 | mutex_unlock(&fs_info->balance_mutex); |
| 4108 | return -ENOTCONN; |
| 4109 | } |
| 4110 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4111 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4112 | atomic_inc(&fs_info->balance_pause_req); |
| 4113 | mutex_unlock(&fs_info->balance_mutex); |
| 4114 | |
| 4115 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4116 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4117 | |
| 4118 | mutex_lock(&fs_info->balance_mutex); |
| 4119 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4120 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4121 | atomic_dec(&fs_info->balance_pause_req); |
| 4122 | } else { |
| 4123 | ret = -ENOTCONN; |
| 4124 | } |
| 4125 | |
| 4126 | mutex_unlock(&fs_info->balance_mutex); |
| 4127 | return ret; |
| 4128 | } |
| 4129 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4130 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4131 | { |
| 4132 | mutex_lock(&fs_info->balance_mutex); |
| 4133 | if (!fs_info->balance_ctl) { |
| 4134 | mutex_unlock(&fs_info->balance_mutex); |
| 4135 | return -ENOTCONN; |
| 4136 | } |
| 4137 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4138 | /* |
| 4139 | * A paused balance with the item stored on disk can be resumed at |
| 4140 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4141 | * and we must not allow cancelling as it deletes the item. |
| 4142 | */ |
| 4143 | if (sb_rdonly(fs_info->sb)) { |
| 4144 | mutex_unlock(&fs_info->balance_mutex); |
| 4145 | return -EROFS; |
| 4146 | } |
| 4147 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4148 | atomic_inc(&fs_info->balance_cancel_req); |
| 4149 | /* |
| 4150 | * if we are running just wait and return, balance item is |
| 4151 | * deleted in btrfs_balance in this case |
| 4152 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4153 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4154 | mutex_unlock(&fs_info->balance_mutex); |
| 4155 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4156 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4157 | mutex_lock(&fs_info->balance_mutex); |
| 4158 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4159 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4160 | /* |
| 4161 | * Lock released to allow other waiters to continue, we'll |
| 4162 | * reexamine the status again. |
| 4163 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4164 | mutex_lock(&fs_info->balance_mutex); |
| 4165 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4166 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4167 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4168 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4169 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4170 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4171 | } |
| 4172 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4173 | BUG_ON(fs_info->balance_ctl || |
| 4174 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4175 | atomic_dec(&fs_info->balance_cancel_req); |
| 4176 | mutex_unlock(&fs_info->balance_mutex); |
| 4177 | return 0; |
| 4178 | } |
| 4179 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4180 | static int btrfs_uuid_scan_kthread(void *data) |
| 4181 | { |
| 4182 | struct btrfs_fs_info *fs_info = data; |
| 4183 | struct btrfs_root *root = fs_info->tree_root; |
| 4184 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4185 | struct btrfs_path *path = NULL; |
| 4186 | int ret = 0; |
| 4187 | struct extent_buffer *eb; |
| 4188 | int slot; |
| 4189 | struct btrfs_root_item root_item; |
| 4190 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4191 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4192 | |
| 4193 | path = btrfs_alloc_path(); |
| 4194 | if (!path) { |
| 4195 | ret = -ENOMEM; |
| 4196 | goto out; |
| 4197 | } |
| 4198 | |
| 4199 | key.objectid = 0; |
| 4200 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4201 | key.offset = 0; |
| 4202 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4203 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4204 | ret = btrfs_search_forward(root, &key, path, |
| 4205 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4206 | if (ret) { |
| 4207 | if (ret > 0) |
| 4208 | ret = 0; |
| 4209 | break; |
| 4210 | } |
| 4211 | |
| 4212 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4213 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4214 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4215 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4216 | goto skip; |
| 4217 | |
| 4218 | eb = path->nodes[0]; |
| 4219 | slot = path->slots[0]; |
| 4220 | item_size = btrfs_item_size_nr(eb, slot); |
| 4221 | if (item_size < sizeof(root_item)) |
| 4222 | goto skip; |
| 4223 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4224 | read_extent_buffer(eb, &root_item, |
| 4225 | btrfs_item_ptr_offset(eb, slot), |
| 4226 | (int)sizeof(root_item)); |
| 4227 | if (btrfs_root_refs(&root_item) == 0) |
| 4228 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4229 | |
| 4230 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4231 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4232 | if (trans) |
| 4233 | goto update_tree; |
| 4234 | |
| 4235 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4236 | /* |
| 4237 | * 1 - subvol uuid item |
| 4238 | * 1 - received_subvol uuid item |
| 4239 | */ |
| 4240 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4241 | if (IS_ERR(trans)) { |
| 4242 | ret = PTR_ERR(trans); |
| 4243 | break; |
| 4244 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4245 | continue; |
| 4246 | } else { |
| 4247 | goto skip; |
| 4248 | } |
| 4249 | update_tree: |
| 4250 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4251 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4252 | BTRFS_UUID_KEY_SUBVOL, |
| 4253 | key.objectid); |
| 4254 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4255 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4256 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4257 | break; |
| 4258 | } |
| 4259 | } |
| 4260 | |
| 4261 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4262 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4263 | root_item.received_uuid, |
| 4264 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4265 | key.objectid); |
| 4266 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4267 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4268 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4269 | break; |
| 4270 | } |
| 4271 | } |
| 4272 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4273 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4274 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4275 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4276 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4277 | if (ret) |
| 4278 | break; |
| 4279 | } |
| 4280 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4281 | btrfs_release_path(path); |
| 4282 | if (key.offset < (u64)-1) { |
| 4283 | key.offset++; |
| 4284 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4285 | key.offset = 0; |
| 4286 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4287 | } else if (key.objectid < (u64)-1) { |
| 4288 | key.offset = 0; |
| 4289 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4290 | key.objectid++; |
| 4291 | } else { |
| 4292 | break; |
| 4293 | } |
| 4294 | cond_resched(); |
| 4295 | } |
| 4296 | |
| 4297 | out: |
| 4298 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4299 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4300 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4301 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4302 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4303 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4304 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4305 | up(&fs_info->uuid_tree_rescan_sem); |
| 4306 | return 0; |
| 4307 | } |
| 4308 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4309 | /* |
| 4310 | * Callback for btrfs_uuid_tree_iterate(). |
| 4311 | * returns: |
| 4312 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4313 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4314 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4315 | */ |
| 4316 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4317 | u8 *uuid, u8 type, u64 subid) |
| 4318 | { |
| 4319 | struct btrfs_key key; |
| 4320 | int ret = 0; |
| 4321 | struct btrfs_root *subvol_root; |
| 4322 | |
| 4323 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4324 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4325 | goto out; |
| 4326 | |
| 4327 | key.objectid = subid; |
| 4328 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4329 | key.offset = (u64)-1; |
| 4330 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4331 | if (IS_ERR(subvol_root)) { |
| 4332 | ret = PTR_ERR(subvol_root); |
| 4333 | if (ret == -ENOENT) |
| 4334 | ret = 1; |
| 4335 | goto out; |
| 4336 | } |
| 4337 | |
| 4338 | switch (type) { |
| 4339 | case BTRFS_UUID_KEY_SUBVOL: |
| 4340 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4341 | ret = 1; |
| 4342 | break; |
| 4343 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4344 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4345 | BTRFS_UUID_SIZE)) |
| 4346 | ret = 1; |
| 4347 | break; |
| 4348 | } |
| 4349 | |
| 4350 | out: |
| 4351 | return ret; |
| 4352 | } |
| 4353 | |
| 4354 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4355 | { |
| 4356 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4357 | int ret; |
| 4358 | |
| 4359 | /* |
| 4360 | * 1st step is to iterate through the existing UUID tree and |
| 4361 | * to delete all entries that contain outdated data. |
| 4362 | * 2nd step is to add all missing entries to the UUID tree. |
| 4363 | */ |
| 4364 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4365 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4366 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4367 | up(&fs_info->uuid_tree_rescan_sem); |
| 4368 | return ret; |
| 4369 | } |
| 4370 | return btrfs_uuid_scan_kthread(data); |
| 4371 | } |
| 4372 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4373 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4374 | { |
| 4375 | struct btrfs_trans_handle *trans; |
| 4376 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4377 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4378 | struct task_struct *task; |
| 4379 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4380 | |
| 4381 | /* |
| 4382 | * 1 - root node |
| 4383 | * 1 - root item |
| 4384 | */ |
| 4385 | trans = btrfs_start_transaction(tree_root, 2); |
| 4386 | if (IS_ERR(trans)) |
| 4387 | return PTR_ERR(trans); |
| 4388 | |
| 4389 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4390 | BTRFS_UUID_TREE_OBJECTID); |
| 4391 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4392 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4393 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4394 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4395 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | fs_info->uuid_root = uuid_root; |
| 4399 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4400 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4401 | if (ret) |
| 4402 | return ret; |
| 4403 | |
| 4404 | down(&fs_info->uuid_tree_rescan_sem); |
| 4405 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4406 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4407 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4408 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4409 | up(&fs_info->uuid_tree_rescan_sem); |
| 4410 | return PTR_ERR(task); |
| 4411 | } |
| 4412 | |
| 4413 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4414 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4415 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4416 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4417 | { |
| 4418 | struct task_struct *task; |
| 4419 | |
| 4420 | down(&fs_info->uuid_tree_rescan_sem); |
| 4421 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4422 | if (IS_ERR(task)) { |
| 4423 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4424 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4425 | up(&fs_info->uuid_tree_rescan_sem); |
| 4426 | return PTR_ERR(task); |
| 4427 | } |
| 4428 | |
| 4429 | return 0; |
| 4430 | } |
| 4431 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4432 | /* |
| 4433 | * shrinking a device means finding all of the device extents past |
| 4434 | * the new size, and then following the back refs to the chunks. |
| 4435 | * The chunk relocation code actually frees the device extent |
| 4436 | */ |
| 4437 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4438 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4439 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4440 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4441 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4442 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4443 | struct btrfs_path *path; |
| 4444 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4445 | u64 chunk_offset; |
| 4446 | int ret; |
| 4447 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4448 | int failed = 0; |
| 4449 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4450 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4451 | struct extent_buffer *l; |
| 4452 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4453 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4454 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4455 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4456 | u64 diff; |
| 4457 | |
| 4458 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4459 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4460 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4461 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4462 | return -EINVAL; |
| 4463 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4464 | path = btrfs_alloc_path(); |
| 4465 | if (!path) |
| 4466 | return -ENOMEM; |
| 4467 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4468 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4469 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4470 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4471 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4472 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4473 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4474 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4475 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4476 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4477 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4478 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4479 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4480 | key.objectid = device->devid; |
| 4481 | key.offset = (u64)-1; |
| 4482 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4483 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4484 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4485 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4486 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4487 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4488 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4489 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4490 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4491 | |
| 4492 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4493 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4494 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4495 | if (ret < 0) |
| 4496 | goto done; |
| 4497 | if (ret) { |
| 4498 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4499 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4500 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | l = path->nodes[0]; |
| 4504 | slot = path->slots[0]; |
| 4505 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4506 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4507 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4508 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4509 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4510 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4511 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4512 | |
| 4513 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4514 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4515 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4516 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4517 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4518 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4519 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4520 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4521 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4522 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4523 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4524 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4525 | /* |
| 4526 | * We may be relocating the only data chunk we have, |
| 4527 | * which could potentially end up with losing data's |
| 4528 | * raid profile, so lets allocate an empty one in |
| 4529 | * advance. |
| 4530 | */ |
| 4531 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4532 | if (ret < 0) { |
| 4533 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4534 | goto done; |
| 4535 | } |
| 4536 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4537 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4538 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4539 | if (ret == -ENOSPC) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4540 | failed++; |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 4541 | } else if (ret) { |
| 4542 | if (ret == -ETXTBSY) { |
| 4543 | btrfs_warn(fs_info, |
| 4544 | "could not shrink block group %llu due to active swapfile", |
| 4545 | chunk_offset); |
| 4546 | } |
| 4547 | goto done; |
| 4548 | } |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4549 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4550 | |
| 4551 | if (failed && !retried) { |
| 4552 | failed = 0; |
| 4553 | retried = true; |
| 4554 | goto again; |
| 4555 | } else if (failed && retried) { |
| 4556 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4557 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4558 | } |
| 4559 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4560 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4561 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4562 | if (IS_ERR(trans)) { |
| 4563 | ret = PTR_ERR(trans); |
| 4564 | goto done; |
| 4565 | } |
| 4566 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4567 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4568 | |
| 4569 | /* |
| 4570 | * We checked in the above loop all device extents that were already in |
| 4571 | * the device tree. However before we have updated the device's |
| 4572 | * total_bytes to the new size, we might have had chunk allocations that |
| 4573 | * have not complete yet (new block groups attached to transaction |
| 4574 | * handles), and therefore their device extents were not yet in the |
| 4575 | * device tree and we missed them in the loop above. So if we have any |
| 4576 | * pending chunk using a device extent that overlaps the device range |
| 4577 | * that we can not use anymore, commit the current transaction and |
| 4578 | * repeat the search on the device tree - this way we guarantee we will |
| 4579 | * not have chunks using device extents that end beyond 'new_size'. |
| 4580 | */ |
| 4581 | if (!checked_pending_chunks) { |
| 4582 | u64 start = new_size; |
| 4583 | u64 len = old_size - new_size; |
| 4584 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 4585 | if (contains_pending_extent(trans->transaction, device, |
| 4586 | &start, len)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4587 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4588 | checked_pending_chunks = true; |
| 4589 | failed = 0; |
| 4590 | retried = false; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4591 | ret = btrfs_commit_transaction(trans); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4592 | if (ret) |
| 4593 | goto done; |
| 4594 | goto again; |
| 4595 | } |
| 4596 | } |
| 4597 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4598 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4599 | if (list_empty(&device->resized_list)) |
| 4600 | list_add_tail(&device->resized_list, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4601 | &fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4602 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4603 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4604 | btrfs_set_super_total_bytes(super_copy, |
| 4605 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4606 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4607 | |
| 4608 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4609 | ret = btrfs_update_device(trans, device); |
Anand Jain | 801660b | 2018-08-06 18:12:37 +0800 | [diff] [blame] | 4610 | if (ret < 0) { |
| 4611 | btrfs_abort_transaction(trans, ret); |
| 4612 | btrfs_end_transaction(trans); |
| 4613 | } else { |
| 4614 | ret = btrfs_commit_transaction(trans); |
| 4615 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4616 | done: |
| 4617 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4618 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4619 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4620 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4621 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4622 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4623 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4624 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4625 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4626 | return ret; |
| 4627 | } |
| 4628 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4629 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4630 | struct btrfs_key *key, |
| 4631 | struct btrfs_chunk *chunk, int item_size) |
| 4632 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4633 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4634 | struct btrfs_disk_key disk_key; |
| 4635 | u32 array_size; |
| 4636 | u8 *ptr; |
| 4637 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4638 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4639 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4640 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4641 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4642 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4643 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4644 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4645 | |
| 4646 | ptr = super_copy->sys_chunk_array + array_size; |
| 4647 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4648 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4649 | ptr += sizeof(disk_key); |
| 4650 | memcpy(ptr, chunk, item_size); |
| 4651 | item_size += sizeof(disk_key); |
| 4652 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4653 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4654 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4655 | return 0; |
| 4656 | } |
| 4657 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4658 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4659 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4660 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4661 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4662 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4663 | const struct btrfs_device_info *di_a = a; |
| 4664 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4665 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4666 | if (di_a->max_avail > di_b->max_avail) |
| 4667 | return -1; |
| 4668 | if (di_a->max_avail < di_b->max_avail) |
| 4669 | return 1; |
| 4670 | if (di_a->total_avail > di_b->total_avail) |
| 4671 | return -1; |
| 4672 | if (di_a->total_avail < di_b->total_avail) |
| 4673 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4674 | return 0; |
| 4675 | } |
| 4676 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4677 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4678 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4679 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4680 | return; |
| 4681 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4682 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4683 | } |
| 4684 | |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4685 | #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \ |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4686 | - sizeof(struct btrfs_chunk)) \ |
| 4687 | / sizeof(struct btrfs_stripe) + 1) |
| 4688 | |
| 4689 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4690 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4691 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4692 | / sizeof(struct btrfs_stripe) + 1) |
| 4693 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4694 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4695 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4696 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4697 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4698 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4699 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4700 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4701 | struct extent_map_tree *em_tree; |
| 4702 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4703 | struct btrfs_device_info *devices_info = NULL; |
| 4704 | u64 total_avail; |
| 4705 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4706 | int data_stripes; /* number of stripes that count for |
| 4707 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4708 | int sub_stripes; /* sub_stripes info for map */ |
| 4709 | int dev_stripes; /* stripes per dev */ |
| 4710 | int devs_max; /* max devs to use */ |
| 4711 | int devs_min; /* min devs needed */ |
| 4712 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4713 | int ncopies; /* how many copies to data has */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4714 | int nparity; /* number of stripes worth of bytes to |
| 4715 | store parity information */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4716 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4717 | u64 max_stripe_size; |
| 4718 | u64 max_chunk_size; |
| 4719 | u64 stripe_size; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4720 | u64 chunk_size; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4721 | int ndevs; |
| 4722 | int i; |
| 4723 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4724 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4725 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4726 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4727 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4728 | if (list_empty(&fs_devices->alloc_list)) { |
| 4729 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4730 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4731 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4732 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4733 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4734 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4735 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4736 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4737 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4738 | devs_max = btrfs_raid_array[index].devs_max; |
| 4739 | devs_min = btrfs_raid_array[index].devs_min; |
| 4740 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4741 | ncopies = btrfs_raid_array[index].ncopies; |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4742 | nparity = btrfs_raid_array[index].nparity; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4743 | |
| 4744 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4745 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4746 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4747 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4748 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4749 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4750 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4751 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4752 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4753 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4754 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4755 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4756 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4757 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4758 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4759 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4760 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4761 | if (!devs_max) |
| 4762 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4763 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4764 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4765 | type); |
| 4766 | BUG_ON(1); |
| 4767 | } |
| 4768 | |
| 4769 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4770 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4771 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4772 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4773 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4774 | GFP_NOFS); |
| 4775 | if (!devices_info) |
| 4776 | return -ENOMEM; |
| 4777 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4778 | /* |
| 4779 | * in the first pass through the devices list, we gather information |
| 4780 | * about the available holes on each device. |
| 4781 | */ |
| 4782 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4783 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4784 | u64 max_avail; |
| 4785 | u64 dev_offset; |
| 4786 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4787 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4788 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4789 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4790 | continue; |
| 4791 | } |
| 4792 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 4793 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 4794 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4795 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4796 | continue; |
| 4797 | |
| 4798 | if (device->total_bytes > device->bytes_used) |
| 4799 | total_avail = device->total_bytes - device->bytes_used; |
| 4800 | else |
| 4801 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4802 | |
| 4803 | /* If there is no space on this device, skip it. */ |
| 4804 | if (total_avail == 0) |
| 4805 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4806 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4807 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4808 | max_stripe_size * dev_stripes, |
| 4809 | &dev_offset, &max_avail); |
| 4810 | if (ret && ret != -ENOSPC) |
| 4811 | goto error; |
| 4812 | |
| 4813 | if (ret == 0) |
| 4814 | max_avail = max_stripe_size * dev_stripes; |
| 4815 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4816 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 4817 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4818 | btrfs_debug(info, |
| 4819 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 4820 | __func__, device->devid, max_avail, |
| 4821 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4822 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4823 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4824 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4825 | if (ndevs == fs_devices->rw_devices) { |
| 4826 | WARN(1, "%s: found more than %llu devices\n", |
| 4827 | __func__, fs_devices->rw_devices); |
| 4828 | break; |
| 4829 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4830 | devices_info[ndevs].dev_offset = dev_offset; |
| 4831 | devices_info[ndevs].max_avail = max_avail; |
| 4832 | devices_info[ndevs].total_avail = total_avail; |
| 4833 | devices_info[ndevs].dev = device; |
| 4834 | ++ndevs; |
| 4835 | } |
| 4836 | |
| 4837 | /* |
| 4838 | * now sort the devices by hole size / available space |
| 4839 | */ |
| 4840 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4841 | btrfs_cmp_device_info, NULL); |
| 4842 | |
| 4843 | /* round down to number of usable stripes */ |
Nikolay Borisov | e5600fd | 2017-06-27 10:02:25 +0300 | [diff] [blame] | 4844 | ndevs = round_down(ndevs, devs_increment); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4845 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4846 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4847 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4848 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 4849 | btrfs_debug(info, |
| 4850 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4851 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4852 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4853 | goto error; |
| 4854 | } |
| 4855 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 4856 | ndevs = min(ndevs, devs_max); |
| 4857 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4858 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4859 | * The primary goal is to maximize the number of stripes, so use as |
| 4860 | * many devices as possible, even if the stripes are not maximum sized. |
| 4861 | * |
| 4862 | * The DUP profile stores more than one stripe per device, the |
| 4863 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4864 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4865 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4866 | num_stripes = ndevs * dev_stripes; |
| 4867 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4868 | /* |
| 4869 | * this will have to be fixed for RAID1 and RAID10 over |
| 4870 | * more drives |
| 4871 | */ |
Hans van Kranenburg | b50836e | 2018-10-04 23:24:42 +0200 | [diff] [blame] | 4872 | data_stripes = (num_stripes - nparity) / ncopies; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4873 | |
| 4874 | /* |
| 4875 | * Use the number of data stripes to figure out how big this chunk |
| 4876 | * is really going to be in terms of logical address space, |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4877 | * and compare that answer with the max chunk size. If it's higher, |
| 4878 | * we try to reduce stripe_size. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4879 | */ |
| 4880 | if (stripe_size * data_stripes > max_chunk_size) { |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4881 | /* |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4882 | * Reduce stripe_size, round it up to a 16MB boundary again and |
| 4883 | * then use it, unless it ends up being even bigger than the |
| 4884 | * previous value we had already. |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4885 | */ |
Hans van Kranenburg | baf9211 | 2018-10-04 23:24:40 +0200 | [diff] [blame] | 4886 | stripe_size = min(round_up(div_u64(max_chunk_size, |
| 4887 | data_stripes), SZ_16M), |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4888 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4889 | } |
| 4890 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4891 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4892 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4893 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4894 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4895 | if (!map) { |
| 4896 | ret = -ENOMEM; |
| 4897 | goto error; |
| 4898 | } |
| 4899 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4900 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4901 | for (i = 0; i < ndevs; ++i) { |
| 4902 | for (j = 0; j < dev_stripes; ++j) { |
| 4903 | int s = i * dev_stripes + j; |
| 4904 | map->stripes[s].dev = devices_info[i].dev; |
| 4905 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4906 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4907 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4908 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4909 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 4910 | map->io_align = BTRFS_STRIPE_LEN; |
| 4911 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4912 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4913 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4914 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4915 | chunk_size = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4916 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4917 | trace_btrfs_chunk_alloc(info, map, start, chunk_size); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4918 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4919 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4920 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4921 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4922 | ret = -ENOMEM; |
| 4923 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4924 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4925 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 4926 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4927 | em->start = start; |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4928 | em->len = chunk_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4929 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4930 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4931 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4932 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4933 | em_tree = &info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4934 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4935 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4936 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 4937 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4938 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4939 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4940 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4941 | |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 4942 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4943 | refcount_inc(&em->refs); |
| 4944 | write_unlock(&em_tree->lock); |
| 4945 | |
Hans van Kranenburg | 23f0ff1 | 2018-10-04 23:24:39 +0200 | [diff] [blame] | 4946 | ret = btrfs_make_block_group(trans, 0, type, start, chunk_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4947 | if (ret) |
| 4948 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4949 | |
Hans van Kranenburg | 2f29df4 | 2018-10-04 23:24:38 +0200 | [diff] [blame] | 4950 | for (i = 0; i < map->num_stripes; i++) |
| 4951 | btrfs_device_set_bytes_used(map->stripes[i].dev, |
| 4952 | map->stripes[i].dev->bytes_used + stripe_size); |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4953 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4954 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4955 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4956 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4957 | check_raid56_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4958 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4959 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4960 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4961 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4962 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4963 | write_lock(&em_tree->lock); |
| 4964 | remove_extent_mapping(em_tree, em); |
| 4965 | write_unlock(&em_tree->lock); |
| 4966 | |
| 4967 | /* One for our allocation */ |
| 4968 | free_extent_map(em); |
| 4969 | /* One for the tree reference */ |
| 4970 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4971 | /* One for the pending_chunks list reference */ |
| 4972 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4973 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4974 | kfree(devices_info); |
| 4975 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4976 | } |
| 4977 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4978 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 4979 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4980 | { |
Nikolay Borisov | 97aff91 | 2018-07-20 19:37:53 +0300 | [diff] [blame] | 4981 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4982 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4983 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4984 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4985 | struct btrfs_device *device; |
| 4986 | struct btrfs_chunk *chunk; |
| 4987 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4988 | struct extent_map *em; |
| 4989 | struct map_lookup *map; |
| 4990 | size_t item_size; |
| 4991 | u64 dev_offset; |
| 4992 | u64 stripe_size; |
| 4993 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 4994 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4995 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 4996 | em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 4997 | if (IS_ERR(em)) |
| 4998 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4999 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5000 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5001 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 5002 | stripe_size = em->orig_block_len; |
| 5003 | |
| 5004 | chunk = kzalloc(item_size, GFP_NOFS); |
| 5005 | if (!chunk) { |
| 5006 | ret = -ENOMEM; |
| 5007 | goto out; |
| 5008 | } |
| 5009 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5010 | /* |
| 5011 | * Take the device list mutex to prevent races with the final phase of |
| 5012 | * a device replace operation that replaces the device object associated |
| 5013 | * with the map's stripes, because the device object's id can change |
| 5014 | * at any time during that final phase of the device replace operation |
| 5015 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 5016 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5017 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5018 | for (i = 0; i < map->num_stripes; i++) { |
| 5019 | device = map->stripes[i].dev; |
| 5020 | dev_offset = map->stripes[i].physical; |
| 5021 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5022 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 5023 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5024 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 5025 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 5026 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5027 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5028 | break; |
| 5029 | } |
| 5030 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5031 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 5032 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5033 | } |
| 5034 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5035 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5036 | for (i = 0; i < map->num_stripes; i++) { |
| 5037 | device = map->stripes[i].dev; |
| 5038 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5039 | |
| 5040 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 5041 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 5042 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 5043 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5044 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5045 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5046 | |
| 5047 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 5048 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 5049 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 5050 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 5051 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 5052 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 5053 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5054 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5055 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 5056 | |
| 5057 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 5058 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 5059 | key.offset = chunk_offset; |
| 5060 | |
| 5061 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5062 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 5063 | /* |
| 5064 | * TODO: Cleanup of inserted chunk root in case of |
| 5065 | * failure. |
| 5066 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5067 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5068 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5069 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5070 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5071 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5072 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 5073 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | /* |
| 5077 | * Chunk allocation falls into two parts. The first part does works |
| 5078 | * that make the new allocated chunk useable, but not do any operation |
| 5079 | * that modifies the chunk tree. The second part does the works that |
| 5080 | * require modifying the chunk tree. This division is important for the |
| 5081 | * bootstrap process of adding storage to a seed btrfs. |
| 5082 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5083 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5084 | { |
| 5085 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5086 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 5087 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 5088 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5089 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5090 | } |
| 5091 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5092 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 5093 | struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5094 | { |
| 5095 | u64 chunk_offset; |
| 5096 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5097 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5098 | int ret; |
| 5099 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 5100 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5101 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5102 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5103 | if (ret) |
| 5104 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5105 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5106 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 5107 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 5108 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5109 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5110 | } |
| 5111 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5112 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 5113 | { |
| 5114 | int max_errors; |
| 5115 | |
| 5116 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5117 | BTRFS_BLOCK_GROUP_RAID10 | |
| 5118 | BTRFS_BLOCK_GROUP_RAID5 | |
| 5119 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5120 | max_errors = 1; |
| 5121 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 5122 | max_errors = 2; |
| 5123 | } else { |
| 5124 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5125 | } |
| 5126 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5127 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5128 | } |
| 5129 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5130 | 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] | 5131 | { |
| 5132 | struct extent_map *em; |
| 5133 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5134 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5135 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5136 | int i; |
| 5137 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5138 | em = btrfs_get_chunk_map(fs_info, chunk_offset, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5139 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5140 | return 1; |
| 5141 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5142 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5143 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5144 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5145 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5146 | miss_ndevs++; |
| 5147 | continue; |
| 5148 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5149 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5150 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5151 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5152 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5153 | } |
| 5154 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5155 | |
| 5156 | /* |
| 5157 | * If the number of missing devices is larger than max errors, |
| 5158 | * we can not write the data into that chunk successfully, so |
| 5159 | * set it readonly. |
| 5160 | */ |
| 5161 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5162 | readonly = 1; |
| 5163 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5164 | free_extent_map(em); |
| 5165 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 5169 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 5170 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 5174 | { |
| 5175 | struct extent_map *em; |
| 5176 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5177 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5178 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5179 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 5180 | if (em) |
| 5181 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5182 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5183 | if (!em) |
| 5184 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5185 | /* once for us */ |
| 5186 | free_extent_map(em); |
| 5187 | /* once for the tree */ |
| 5188 | free_extent_map(em); |
| 5189 | } |
| 5190 | } |
| 5191 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5192 | 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] | 5193 | { |
| 5194 | struct extent_map *em; |
| 5195 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5196 | int ret; |
| 5197 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5198 | em = btrfs_get_chunk_map(fs_info, logical, len); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5199 | if (IS_ERR(em)) |
| 5200 | /* |
| 5201 | * We could return errors for these cases, but that could get |
| 5202 | * ugly and we'd probably do the same thing which is just not do |
| 5203 | * anything else and exit, so return 1 so the callers don't try |
| 5204 | * to use other copies. |
| 5205 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5206 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5207 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5208 | map = em->map_lookup; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5209 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 5210 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5211 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5212 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5213 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5214 | ret = 2; |
| 5215 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5216 | /* |
| 5217 | * There could be two corrupted data stripes, we need |
| 5218 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5219 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5220 | * Fail a stripe at a time on every retry except the |
| 5221 | * stripe under reconstruction. |
| 5222 | */ |
| 5223 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5224 | else |
| 5225 | ret = 1; |
| 5226 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5227 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5228 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5229 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5230 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5231 | ret++; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5232 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5233 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5234 | return ret; |
| 5235 | } |
| 5236 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5237 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5238 | u64 logical) |
| 5239 | { |
| 5240 | struct extent_map *em; |
| 5241 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5242 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5243 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5244 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5245 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5246 | if (!WARN_ON(IS_ERR(em))) { |
| 5247 | map = em->map_lookup; |
| 5248 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5249 | len = map->stripe_len * nr_data_stripes(map); |
| 5250 | free_extent_map(em); |
| 5251 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5252 | return len; |
| 5253 | } |
| 5254 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5255 | 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] | 5256 | { |
| 5257 | struct extent_map *em; |
| 5258 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5259 | int ret = 0; |
| 5260 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5261 | em = btrfs_get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5262 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5263 | if(!WARN_ON(IS_ERR(em))) { |
| 5264 | map = em->map_lookup; |
| 5265 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5266 | ret = 1; |
| 5267 | free_extent_map(em); |
| 5268 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5269 | return ret; |
| 5270 | } |
| 5271 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5272 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5273 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5274 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5275 | { |
| 5276 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5277 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5278 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5279 | int tolerance; |
| 5280 | struct btrfs_device *srcdev; |
| 5281 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5282 | ASSERT((map->type & |
| 5283 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))); |
| 5284 | |
| 5285 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5286 | num_stripes = map->sub_stripes; |
| 5287 | else |
| 5288 | num_stripes = map->num_stripes; |
| 5289 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5290 | preferred_mirror = first + current->pid % num_stripes; |
| 5291 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5292 | if (dev_replace_is_ongoing && |
| 5293 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5294 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5295 | srcdev = fs_info->dev_replace.srcdev; |
| 5296 | else |
| 5297 | srcdev = NULL; |
| 5298 | |
| 5299 | /* |
| 5300 | * try to avoid the drive that is the source drive for a |
| 5301 | * dev-replace procedure, only choose it if no other non-missing |
| 5302 | * mirror is available |
| 5303 | */ |
| 5304 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5305 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5306 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5307 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5308 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5309 | if (map->stripes[i].dev->bdev && |
| 5310 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5311 | return i; |
| 5312 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5313 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5314 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5315 | /* we couldn't find one that doesn't fail. Just return something |
| 5316 | * and the io error handling code will clean up eventually |
| 5317 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5318 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5319 | } |
| 5320 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5321 | static inline int parity_smaller(u64 a, u64 b) |
| 5322 | { |
| 5323 | return a > b; |
| 5324 | } |
| 5325 | |
| 5326 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5327 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5328 | { |
| 5329 | struct btrfs_bio_stripe s; |
| 5330 | int i; |
| 5331 | u64 l; |
| 5332 | int again = 1; |
| 5333 | |
| 5334 | while (again) { |
| 5335 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5336 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5337 | if (parity_smaller(bbio->raid_map[i], |
| 5338 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5339 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5340 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5341 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5342 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5343 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5344 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5345 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5346 | again = 1; |
| 5347 | } |
| 5348 | } |
| 5349 | } |
| 5350 | } |
| 5351 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5352 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5353 | { |
| 5354 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5355 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5356 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5357 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5358 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5359 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5360 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5361 | /* |
| 5362 | * plus the raid_map, which includes both the tgt dev |
| 5363 | * and the stripes |
| 5364 | */ |
| 5365 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5366 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5367 | |
| 5368 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5369 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5370 | |
| 5371 | return bbio; |
| 5372 | } |
| 5373 | |
| 5374 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5375 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5376 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5377 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5381 | { |
| 5382 | if (!bbio) |
| 5383 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5384 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5385 | kfree(bbio); |
| 5386 | } |
| 5387 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5388 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5389 | /* |
| 5390 | * Please note that, discard won't be sent to target device of device |
| 5391 | * replace. |
| 5392 | */ |
| 5393 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, |
| 5394 | u64 logical, u64 length, |
| 5395 | struct btrfs_bio **bbio_ret) |
| 5396 | { |
| 5397 | struct extent_map *em; |
| 5398 | struct map_lookup *map; |
| 5399 | struct btrfs_bio *bbio; |
| 5400 | u64 offset; |
| 5401 | u64 stripe_nr; |
| 5402 | u64 stripe_nr_end; |
| 5403 | u64 stripe_end_offset; |
| 5404 | u64 stripe_cnt; |
| 5405 | u64 stripe_len; |
| 5406 | u64 stripe_offset; |
| 5407 | u64 num_stripes; |
| 5408 | u32 stripe_index; |
| 5409 | u32 factor = 0; |
| 5410 | u32 sub_stripes = 0; |
| 5411 | u64 stripes_per_dev = 0; |
| 5412 | u32 remaining_stripes = 0; |
| 5413 | u32 last_stripe = 0; |
| 5414 | int ret = 0; |
| 5415 | int i; |
| 5416 | |
| 5417 | /* discard always return a bbio */ |
| 5418 | ASSERT(bbio_ret); |
| 5419 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5420 | em = btrfs_get_chunk_map(fs_info, logical, length); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5421 | if (IS_ERR(em)) |
| 5422 | return PTR_ERR(em); |
| 5423 | |
| 5424 | map = em->map_lookup; |
| 5425 | /* we don't discard raid56 yet */ |
| 5426 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5427 | ret = -EOPNOTSUPP; |
| 5428 | goto out; |
| 5429 | } |
| 5430 | |
| 5431 | offset = logical - em->start; |
| 5432 | length = min_t(u64, em->len - offset, length); |
| 5433 | |
| 5434 | stripe_len = map->stripe_len; |
| 5435 | /* |
| 5436 | * stripe_nr counts the total number of stripes we have to stride |
| 5437 | * to get to this block |
| 5438 | */ |
| 5439 | stripe_nr = div64_u64(offset, stripe_len); |
| 5440 | |
| 5441 | /* stripe_offset is the offset of this block in its stripe */ |
| 5442 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5443 | |
| 5444 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5445 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5446 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5447 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5448 | (offset + length); |
| 5449 | /* |
| 5450 | * after this, stripe_nr is the number of stripes on this |
| 5451 | * device we have to walk to find the data, and stripe_index is |
| 5452 | * the number of our device in the stripe array |
| 5453 | */ |
| 5454 | num_stripes = 1; |
| 5455 | stripe_index = 0; |
| 5456 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5457 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5458 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5459 | sub_stripes = 1; |
| 5460 | else |
| 5461 | sub_stripes = map->sub_stripes; |
| 5462 | |
| 5463 | factor = map->num_stripes / sub_stripes; |
| 5464 | num_stripes = min_t(u64, map->num_stripes, |
| 5465 | sub_stripes * stripe_cnt); |
| 5466 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5467 | stripe_index *= sub_stripes; |
| 5468 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5469 | &remaining_stripes); |
| 5470 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5471 | last_stripe *= sub_stripes; |
| 5472 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5473 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5474 | num_stripes = map->num_stripes; |
| 5475 | } else { |
| 5476 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5477 | &stripe_index); |
| 5478 | } |
| 5479 | |
| 5480 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5481 | if (!bbio) { |
| 5482 | ret = -ENOMEM; |
| 5483 | goto out; |
| 5484 | } |
| 5485 | |
| 5486 | for (i = 0; i < num_stripes; i++) { |
| 5487 | bbio->stripes[i].physical = |
| 5488 | map->stripes[stripe_index].physical + |
| 5489 | stripe_offset + stripe_nr * map->stripe_len; |
| 5490 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5491 | |
| 5492 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5493 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5494 | bbio->stripes[i].length = stripes_per_dev * |
| 5495 | map->stripe_len; |
| 5496 | |
| 5497 | if (i / sub_stripes < remaining_stripes) |
| 5498 | bbio->stripes[i].length += |
| 5499 | map->stripe_len; |
| 5500 | |
| 5501 | /* |
| 5502 | * Special for the first stripe and |
| 5503 | * the last stripe: |
| 5504 | * |
| 5505 | * |-------|...|-------| |
| 5506 | * |----------| |
| 5507 | * off end_off |
| 5508 | */ |
| 5509 | if (i < sub_stripes) |
| 5510 | bbio->stripes[i].length -= |
| 5511 | stripe_offset; |
| 5512 | |
| 5513 | if (stripe_index >= last_stripe && |
| 5514 | stripe_index <= (last_stripe + |
| 5515 | sub_stripes - 1)) |
| 5516 | bbio->stripes[i].length -= |
| 5517 | stripe_end_offset; |
| 5518 | |
| 5519 | if (i == sub_stripes - 1) |
| 5520 | stripe_offset = 0; |
| 5521 | } else { |
| 5522 | bbio->stripes[i].length = length; |
| 5523 | } |
| 5524 | |
| 5525 | stripe_index++; |
| 5526 | if (stripe_index == map->num_stripes) { |
| 5527 | stripe_index = 0; |
| 5528 | stripe_nr++; |
| 5529 | } |
| 5530 | } |
| 5531 | |
| 5532 | *bbio_ret = bbio; |
| 5533 | bbio->map_type = map->type; |
| 5534 | bbio->num_stripes = num_stripes; |
| 5535 | out: |
| 5536 | free_extent_map(em); |
| 5537 | return ret; |
| 5538 | } |
| 5539 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5540 | /* |
| 5541 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5542 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5543 | * left cursor can also be read from the target drive. |
| 5544 | * |
| 5545 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5546 | * array of stripes. |
| 5547 | * For READ, it also needs to be supported using the same mirror number. |
| 5548 | * |
| 5549 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5550 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5551 | * case. |
| 5552 | */ |
| 5553 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5554 | u64 logical, u64 length, |
| 5555 | u64 srcdev_devid, int *mirror_num, |
| 5556 | u64 *physical) |
| 5557 | { |
| 5558 | struct btrfs_bio *bbio = NULL; |
| 5559 | int num_stripes; |
| 5560 | int index_srcdev = 0; |
| 5561 | int found = 0; |
| 5562 | u64 physical_of_found = 0; |
| 5563 | int i; |
| 5564 | int ret = 0; |
| 5565 | |
| 5566 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5567 | logical, &length, &bbio, 0, 0); |
| 5568 | if (ret) { |
| 5569 | ASSERT(bbio == NULL); |
| 5570 | return ret; |
| 5571 | } |
| 5572 | |
| 5573 | num_stripes = bbio->num_stripes; |
| 5574 | if (*mirror_num > num_stripes) { |
| 5575 | /* |
| 5576 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5577 | * that means that the requested area is not left of the left |
| 5578 | * cursor |
| 5579 | */ |
| 5580 | btrfs_put_bbio(bbio); |
| 5581 | return -EIO; |
| 5582 | } |
| 5583 | |
| 5584 | /* |
| 5585 | * process the rest of the function using the mirror_num of the source |
| 5586 | * drive. Therefore look it up first. At the end, patch the device |
| 5587 | * pointer to the one of the target drive. |
| 5588 | */ |
| 5589 | for (i = 0; i < num_stripes; i++) { |
| 5590 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5591 | continue; |
| 5592 | |
| 5593 | /* |
| 5594 | * In case of DUP, in order to keep it simple, only add the |
| 5595 | * mirror with the lowest physical address |
| 5596 | */ |
| 5597 | if (found && |
| 5598 | physical_of_found <= bbio->stripes[i].physical) |
| 5599 | continue; |
| 5600 | |
| 5601 | index_srcdev = i; |
| 5602 | found = 1; |
| 5603 | physical_of_found = bbio->stripes[i].physical; |
| 5604 | } |
| 5605 | |
| 5606 | btrfs_put_bbio(bbio); |
| 5607 | |
| 5608 | ASSERT(found); |
| 5609 | if (!found) |
| 5610 | return -EIO; |
| 5611 | |
| 5612 | *mirror_num = index_srcdev + 1; |
| 5613 | *physical = physical_of_found; |
| 5614 | return ret; |
| 5615 | } |
| 5616 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5617 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5618 | struct btrfs_bio **bbio_ret, |
| 5619 | struct btrfs_dev_replace *dev_replace, |
| 5620 | int *num_stripes_ret, int *max_errors_ret) |
| 5621 | { |
| 5622 | struct btrfs_bio *bbio = *bbio_ret; |
| 5623 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5624 | int tgtdev_indexes = 0; |
| 5625 | int num_stripes = *num_stripes_ret; |
| 5626 | int max_errors = *max_errors_ret; |
| 5627 | int i; |
| 5628 | |
| 5629 | if (op == BTRFS_MAP_WRITE) { |
| 5630 | int index_where_to_add; |
| 5631 | |
| 5632 | /* |
| 5633 | * duplicate the write operations while the dev replace |
| 5634 | * procedure is running. Since the copying of the old disk to |
| 5635 | * the new disk takes place at run time while the filesystem is |
| 5636 | * mounted writable, the regular write operations to the old |
| 5637 | * disk have to be duplicated to go to the new disk as well. |
| 5638 | * |
| 5639 | * Note that device->missing is handled by the caller, and that |
| 5640 | * the write to the old disk is already set up in the stripes |
| 5641 | * array. |
| 5642 | */ |
| 5643 | index_where_to_add = num_stripes; |
| 5644 | for (i = 0; i < num_stripes; i++) { |
| 5645 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5646 | /* write to new disk, too */ |
| 5647 | struct btrfs_bio_stripe *new = |
| 5648 | bbio->stripes + index_where_to_add; |
| 5649 | struct btrfs_bio_stripe *old = |
| 5650 | bbio->stripes + i; |
| 5651 | |
| 5652 | new->physical = old->physical; |
| 5653 | new->length = old->length; |
| 5654 | new->dev = dev_replace->tgtdev; |
| 5655 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5656 | index_where_to_add++; |
| 5657 | max_errors++; |
| 5658 | tgtdev_indexes++; |
| 5659 | } |
| 5660 | } |
| 5661 | num_stripes = index_where_to_add; |
| 5662 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5663 | int index_srcdev = 0; |
| 5664 | int found = 0; |
| 5665 | u64 physical_of_found = 0; |
| 5666 | |
| 5667 | /* |
| 5668 | * During the dev-replace procedure, the target drive can also |
| 5669 | * be used to read data in case it is needed to repair a corrupt |
| 5670 | * block elsewhere. This is possible if the requested area is |
| 5671 | * left of the left cursor. In this area, the target drive is a |
| 5672 | * full copy of the source drive. |
| 5673 | */ |
| 5674 | for (i = 0; i < num_stripes; i++) { |
| 5675 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5676 | /* |
| 5677 | * In case of DUP, in order to keep it simple, |
| 5678 | * only add the mirror with the lowest physical |
| 5679 | * address |
| 5680 | */ |
| 5681 | if (found && |
| 5682 | physical_of_found <= |
| 5683 | bbio->stripes[i].physical) |
| 5684 | continue; |
| 5685 | index_srcdev = i; |
| 5686 | found = 1; |
| 5687 | physical_of_found = bbio->stripes[i].physical; |
| 5688 | } |
| 5689 | } |
| 5690 | if (found) { |
| 5691 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5692 | bbio->stripes + num_stripes; |
| 5693 | |
| 5694 | tgtdev_stripe->physical = physical_of_found; |
| 5695 | tgtdev_stripe->length = |
| 5696 | bbio->stripes[index_srcdev].length; |
| 5697 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5698 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5699 | |
| 5700 | tgtdev_indexes++; |
| 5701 | num_stripes++; |
| 5702 | } |
| 5703 | } |
| 5704 | |
| 5705 | *num_stripes_ret = num_stripes; |
| 5706 | *max_errors_ret = max_errors; |
| 5707 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5708 | *bbio_ret = bbio; |
| 5709 | } |
| 5710 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5711 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5712 | { |
| 5713 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5714 | } |
| 5715 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5716 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 5717 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5718 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5719 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5720 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5721 | { |
| 5722 | struct extent_map *em; |
| 5723 | struct map_lookup *map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5724 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5725 | u64 stripe_offset; |
| 5726 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5727 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5728 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5729 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5730 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5731 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5732 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5733 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5734 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5735 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5736 | int dev_replace_is_ongoing = 0; |
| 5737 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5738 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5739 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5740 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5741 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5742 | if (op == BTRFS_MAP_DISCARD) |
| 5743 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 5744 | *length, bbio_ret); |
| 5745 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 5746 | em = btrfs_get_chunk_map(fs_info, logical, *length); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5747 | if (IS_ERR(em)) |
| 5748 | return PTR_ERR(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5749 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5750 | map = em->map_lookup; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5751 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5752 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5753 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5754 | stripe_nr = offset; |
| 5755 | /* |
| 5756 | * stripe_nr counts the total number of stripes we have to stride |
| 5757 | * to get to this block |
| 5758 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5759 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5760 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5761 | stripe_offset = stripe_nr * stripe_len; |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5762 | if (offset < stripe_offset) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5763 | btrfs_crit(fs_info, |
| 5764 | "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu", |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5765 | stripe_offset, offset, em->start, logical, |
| 5766 | stripe_len); |
| 5767 | free_extent_map(em); |
| 5768 | return -EINVAL; |
| 5769 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5770 | |
| 5771 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5772 | stripe_offset = offset - stripe_offset; |
| 5773 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5774 | /* if we're here for raid56, we need to know the stripe aligned start */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5775 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5776 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5777 | raid56_full_stripe_start = offset; |
| 5778 | |
| 5779 | /* allow a write of a full stripe, but make sure we don't |
| 5780 | * allow straddling of stripes |
| 5781 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5782 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5783 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5784 | raid56_full_stripe_start *= full_stripe_len; |
| 5785 | } |
| 5786 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5787 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5788 | u64 max_len; |
| 5789 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5790 | For other RAID types and for RAID[56] reads, just allow a single |
| 5791 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5792 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5793 | (op == BTRFS_MAP_WRITE)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5794 | max_len = stripe_len * nr_data_stripes(map) - |
| 5795 | (offset - raid56_full_stripe_start); |
| 5796 | } else { |
| 5797 | /* we limit the length of each bio to what fits in a stripe */ |
| 5798 | max_len = stripe_len - stripe_offset; |
| 5799 | } |
| 5800 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5801 | } else { |
| 5802 | *length = em->len - offset; |
| 5803 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5804 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5805 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5806 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5807 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5808 | goto out; |
| 5809 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5810 | btrfs_dev_replace_read_lock(dev_replace); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5811 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5812 | if (!dev_replace_is_ongoing) |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5813 | btrfs_dev_replace_read_unlock(dev_replace); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5814 | else |
| 5815 | btrfs_dev_replace_set_lock_blocking(dev_replace); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5816 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5817 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5818 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5819 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 5820 | dev_replace->srcdev->devid, |
| 5821 | &mirror_num, |
| 5822 | &physical_to_patch_in_first_stripe); |
| 5823 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5824 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5825 | else |
| 5826 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5827 | } else if (mirror_num > map->num_stripes) { |
| 5828 | mirror_num = 0; |
| 5829 | } |
| 5830 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5831 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5832 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5833 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5834 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5835 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5836 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5837 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5838 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5839 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5840 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5841 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5842 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5843 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5844 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5845 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5846 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5847 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5848 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5849 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5850 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5851 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5852 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5853 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5854 | } else { |
| 5855 | mirror_num = 1; |
| 5856 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5857 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5858 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5859 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5860 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5861 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5862 | stripe_index *= map->sub_stripes; |
| 5863 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5864 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5865 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5866 | else if (mirror_num) |
| 5867 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5868 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5869 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5870 | stripe_index = find_live_mirror(fs_info, map, |
| 5871 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5872 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5873 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5874 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5875 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5876 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5877 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5878 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5879 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5880 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5881 | |
| 5882 | /* RAID[56] write or recovery. Return all stripes */ |
| 5883 | num_stripes = map->num_stripes; |
| 5884 | max_errors = nr_parity_stripes(map); |
| 5885 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5886 | *length = map->stripe_len; |
| 5887 | stripe_index = 0; |
| 5888 | stripe_offset = 0; |
| 5889 | } else { |
| 5890 | /* |
| 5891 | * Mirror #0 or #1 means the original data block. |
| 5892 | * Mirror #2 is RAID5 parity block. |
| 5893 | * Mirror #3 is RAID6 Q block. |
| 5894 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5895 | stripe_nr = div_u64_rem(stripe_nr, |
| 5896 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5897 | if (mirror_num > 1) |
| 5898 | stripe_index = nr_data_stripes(map) + |
| 5899 | mirror_num - 2; |
| 5900 | |
| 5901 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5902 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5903 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5904 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5905 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5906 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5907 | } else { |
| 5908 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5909 | * after this, stripe_nr is the number of stripes on this |
| 5910 | * device we have to walk to find the data, and stripe_index is |
| 5911 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5912 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5913 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5914 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5915 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5916 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5917 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5918 | btrfs_crit(fs_info, |
| 5919 | "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] | 5920 | stripe_index, map->num_stripes); |
| 5921 | ret = -EINVAL; |
| 5922 | goto out; |
| 5923 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5924 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5925 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5926 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5927 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5928 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5929 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5930 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5931 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5932 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5933 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5934 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5935 | if (!bbio) { |
| 5936 | ret = -ENOMEM; |
| 5937 | goto out; |
| 5938 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5939 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5940 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5941 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5942 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5943 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 5944 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5945 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5946 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5947 | |
| 5948 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5949 | sizeof(struct btrfs_bio_stripe) * |
| 5950 | num_alloc_stripes + |
| 5951 | sizeof(int) * tgtdev_indexes); |
| 5952 | |
| 5953 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5954 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5955 | |
| 5956 | /* Fill in the logical address of each stripe */ |
| 5957 | tmp = stripe_nr * nr_data_stripes(map); |
| 5958 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5959 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5960 | em->start + (tmp + i) * map->stripe_len; |
| 5961 | |
| 5962 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5963 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5964 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5965 | RAID6_Q_STRIPE; |
| 5966 | } |
| 5967 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5968 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5969 | for (i = 0; i < num_stripes; i++) { |
| 5970 | bbio->stripes[i].physical = |
| 5971 | map->stripes[stripe_index].physical + |
| 5972 | stripe_offset + |
| 5973 | stripe_nr * map->stripe_len; |
| 5974 | bbio->stripes[i].dev = |
| 5975 | map->stripes[stripe_index].dev; |
| 5976 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5977 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5978 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5979 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5980 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5981 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5982 | if (bbio->raid_map) |
| 5983 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5984 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5985 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5986 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5987 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 5988 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5989 | } |
| 5990 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5991 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5992 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5993 | bbio->num_stripes = num_stripes; |
| 5994 | bbio->max_errors = max_errors; |
| 5995 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5996 | |
| 5997 | /* |
| 5998 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5999 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 6000 | * available as a mirror |
| 6001 | */ |
| 6002 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 6003 | WARN_ON(num_stripes > 1); |
| 6004 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 6005 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 6006 | bbio->mirror_num = map->num_stripes + 1; |
| 6007 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6008 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6009 | if (dev_replace_is_ongoing) { |
David Sterba | 7fb2ece | 2018-08-24 17:33:58 +0200 | [diff] [blame] | 6010 | ASSERT(atomic_read(&dev_replace->blocking_readers) > 0); |
| 6011 | btrfs_dev_replace_read_lock(dev_replace); |
| 6012 | /* Barrier implied by atomic_dec_and_test */ |
| 6013 | if (atomic_dec_and_test(&dev_replace->blocking_readers)) |
| 6014 | cond_wake_up_nomb(&dev_replace->read_lock_wq); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 6015 | btrfs_dev_replace_read_unlock(dev_replace); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 6016 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6017 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 6018 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6019 | } |
| 6020 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6021 | 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] | 6022 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6023 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6024 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 6025 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6026 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 6027 | } |
| 6028 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6029 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 6030 | 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] | 6031 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6032 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 6033 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 6034 | 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] | 6035 | } |
| 6036 | |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 6037 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 6038 | u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6039 | { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6040 | struct extent_map *em; |
| 6041 | struct map_lookup *map; |
| 6042 | u64 *buf; |
| 6043 | u64 bytenr; |
| 6044 | u64 length; |
| 6045 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6046 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6047 | int i, j, nr = 0; |
| 6048 | |
Omar Sandoval | 60ca842 | 2018-05-16 16:34:31 -0700 | [diff] [blame] | 6049 | em = btrfs_get_chunk_map(fs_info, chunk_start, 1); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 6050 | if (IS_ERR(em)) |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6051 | return -EIO; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 6052 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6053 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6054 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6055 | rmap_len = map->stripe_len; |
| 6056 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6057 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6058 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6059 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6060 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 6061 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6062 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6063 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 6064 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6065 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 6066 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6067 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6068 | |
| 6069 | for (i = 0; i < map->num_stripes; i++) { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6070 | if (map->stripes[i].physical > physical || |
| 6071 | map->stripes[i].physical + length <= physical) |
| 6072 | continue; |
| 6073 | |
| 6074 | stripe_nr = physical - map->stripes[i].physical; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 6075 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6076 | |
| 6077 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 6078 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 6079 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6080 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 6081 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6082 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 6083 | * Alternatively, just use rmap_len below instead of |
| 6084 | * map->stripe_len */ |
| 6085 | |
| 6086 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6087 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6088 | for (j = 0; j < nr; j++) { |
| 6089 | if (buf[j] == bytenr) |
| 6090 | break; |
| 6091 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6092 | if (j == nr) { |
| 6093 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6094 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 6095 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6096 | } |
| 6097 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6098 | *logical = buf; |
| 6099 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6100 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 6101 | |
| 6102 | free_extent_map(em); |
| 6103 | return 0; |
| 6104 | } |
| 6105 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6106 | 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] | 6107 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6108 | bio->bi_private = bbio->private; |
| 6109 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6110 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6111 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 6112 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6113 | } |
| 6114 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6115 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6116 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6117 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6118 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6119 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6120 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6121 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6122 | if (bio->bi_status == BLK_STS_IOERR || |
| 6123 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6124 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6125 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 6126 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6127 | |
| 6128 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 6129 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6130 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6131 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6132 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6133 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 6134 | else |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6135 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6136 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6137 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6138 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6139 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6140 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6141 | } |
| 6142 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6143 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6144 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6145 | is_orig_bio = 1; |
| 6146 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6147 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6148 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6149 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6150 | if (!is_orig_bio) { |
| 6151 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6152 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6153 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6154 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6155 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6156 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6157 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6158 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6159 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6160 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6161 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6162 | /* |
| 6163 | * this bio is actually up to date, we didn't |
| 6164 | * go over the max number of errors |
| 6165 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6166 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6167 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6168 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6169 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6170 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6171 | bio_put(bio); |
| 6172 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6173 | } |
| 6174 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6175 | /* |
| 6176 | * see run_scheduled_bios for a description of why bios are collected for |
| 6177 | * async submit. |
| 6178 | * |
| 6179 | * This will add one bio to the pending list for a device and make sure |
| 6180 | * the work struct is scheduled. |
| 6181 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6182 | static noinline void btrfs_schedule_bio(struct btrfs_device *device, |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6183 | struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6184 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6185 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6186 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6187 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6188 | |
| 6189 | /* don't bother with additional async steps for reads, right now */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6190 | if (bio_op(bio) == REQ_OP_READ) { |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6191 | btrfsic_submit_bio(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6192 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6193 | } |
| 6194 | |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 6195 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6196 | bio->bi_next = NULL; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6197 | |
| 6198 | spin_lock(&device->io_lock); |
Christoph Hellwig | 67f055c | 2016-11-01 07:40:06 -0600 | [diff] [blame] | 6199 | if (op_is_sync(bio->bi_opf)) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6200 | pending_bios = &device->pending_sync_bios; |
| 6201 | else |
| 6202 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6203 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6204 | if (pending_bios->tail) |
| 6205 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6206 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6207 | pending_bios->tail = bio; |
| 6208 | if (!pending_bios->head) |
| 6209 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6210 | if (device->running_pending) |
| 6211 | should_queue = 0; |
| 6212 | |
| 6213 | spin_unlock(&device->io_lock); |
| 6214 | |
| 6215 | if (should_queue) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6216 | btrfs_queue_work(fs_info->submit_workers, &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6217 | } |
| 6218 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6219 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
| 6220 | u64 physical, int dev_nr, int async) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6221 | { |
| 6222 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6223 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6224 | |
| 6225 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6226 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6227 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6228 | bio->bi_iter.bi_sector = physical >> 9; |
Misono Tomohiro | 672d599 | 2018-08-02 16:19:07 +0900 | [diff] [blame] | 6229 | btrfs_debug_in_rcu(fs_info, |
| 6230 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6231 | bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector, |
| 6232 | (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid, |
| 6233 | bio->bi_iter.bi_size); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6234 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6235 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6236 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6237 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6238 | if (async) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6239 | btrfs_schedule_bio(dev, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6240 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6241 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6242 | } |
| 6243 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6244 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6245 | { |
| 6246 | atomic_inc(&bbio->error); |
| 6247 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6248 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6249 | WARN_ON(bio != bbio->orig_bio); |
| 6250 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6251 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6252 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6253 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6254 | bio->bi_status = BLK_STS_IOERR; |
| 6255 | else |
| 6256 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6257 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6258 | } |
| 6259 | } |
| 6260 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6261 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
| 6262 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6263 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6264 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6265 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6266 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6267 | u64 length = 0; |
| 6268 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6269 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6270 | int dev_nr; |
| 6271 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6272 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6273 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6274 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6275 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6276 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6277 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6278 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6279 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6280 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6281 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6282 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6283 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6284 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6285 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6286 | bbio->orig_bio = first_bio; |
| 6287 | bbio->private = first_bio->bi_private; |
| 6288 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6289 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6290 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6291 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6292 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6293 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6294 | /* In this case, map_length has been set to the length of |
| 6295 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6296 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6297 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6298 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6299 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6300 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6301 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6302 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6303 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6304 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6305 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6306 | } |
| 6307 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6308 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6309 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6310 | "mapping failed logical %llu bio len %llu len %llu", |
| 6311 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6312 | BUG(); |
| 6313 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6314 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6315 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6316 | dev = bbio->stripes[dev_nr].dev; |
Nikolay Borisov | fc8a168 | 2018-11-08 16:16:38 +0200 | [diff] [blame] | 6317 | if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING, |
| 6318 | &dev->dev_state) || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6319 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6320 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6321 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6322 | continue; |
| 6323 | } |
| 6324 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6325 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6326 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6327 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6328 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6329 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6330 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
| 6331 | dev_nr, async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6332 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6333 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6334 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6335 | } |
| 6336 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6337 | struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6338 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6339 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6340 | struct btrfs_device *device; |
| 6341 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6342 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6343 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6344 | while (cur_devices) { |
| 6345 | if (!fsid || |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6346 | !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) { |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 6347 | device = find_device(cur_devices, devid, uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6348 | if (device) |
| 6349 | return device; |
| 6350 | } |
| 6351 | cur_devices = cur_devices->seed; |
| 6352 | } |
| 6353 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6354 | } |
| 6355 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6356 | 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] | 6357 | u64 devid, u8 *dev_uuid) |
| 6358 | { |
| 6359 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6360 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6361 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6362 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6363 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6364 | |
| 6365 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6366 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6367 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6368 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6369 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6370 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6371 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6372 | return device; |
| 6373 | } |
| 6374 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6375 | /** |
| 6376 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6377 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6378 | * devid is provided (i.e. @devid != NULL). |
| 6379 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6380 | * is generated. |
| 6381 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6382 | * is generated. |
| 6383 | * |
| 6384 | * 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] | 6385 | * 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] | 6386 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6387 | */ |
| 6388 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6389 | const u64 *devid, |
| 6390 | const u8 *uuid) |
| 6391 | { |
| 6392 | struct btrfs_device *dev; |
| 6393 | u64 tmp; |
| 6394 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6395 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6396 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6397 | |
| 6398 | dev = __alloc_device(); |
| 6399 | if (IS_ERR(dev)) |
| 6400 | return dev; |
| 6401 | |
| 6402 | if (devid) |
| 6403 | tmp = *devid; |
| 6404 | else { |
| 6405 | int ret; |
| 6406 | |
| 6407 | ret = find_next_devid(fs_info, &tmp); |
| 6408 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6409 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6410 | return ERR_PTR(ret); |
| 6411 | } |
| 6412 | } |
| 6413 | dev->devid = tmp; |
| 6414 | |
| 6415 | if (uuid) |
| 6416 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6417 | else |
| 6418 | generate_random_uuid(dev->uuid); |
| 6419 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6420 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6421 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6422 | |
| 6423 | return dev; |
| 6424 | } |
| 6425 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6426 | /* Return -EIO if any error, otherwise return 0. */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6427 | static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6428 | struct extent_buffer *leaf, |
| 6429 | struct btrfs_chunk *chunk, u64 logical) |
| 6430 | { |
| 6431 | u64 length; |
| 6432 | u64 stripe_len; |
| 6433 | u16 num_stripes; |
| 6434 | u16 sub_stripes; |
| 6435 | u64 type; |
Gu Jinxiang | 315409b | 2018-07-04 18:16:39 +0800 | [diff] [blame] | 6436 | u64 features; |
| 6437 | bool mixed = false; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6438 | |
| 6439 | length = btrfs_chunk_length(leaf, chunk); |
| 6440 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6441 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6442 | sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
| 6443 | type = btrfs_chunk_type(leaf, chunk); |
| 6444 | |
| 6445 | if (!num_stripes) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6446 | btrfs_err(fs_info, "invalid chunk num_stripes: %u", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6447 | num_stripes); |
| 6448 | return -EIO; |
| 6449 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6450 | if (!IS_ALIGNED(logical, fs_info->sectorsize)) { |
| 6451 | btrfs_err(fs_info, "invalid chunk logical %llu", logical); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6452 | return -EIO; |
| 6453 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6454 | if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) { |
| 6455 | btrfs_err(fs_info, "invalid chunk sectorsize %u", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6456 | btrfs_chunk_sector_size(leaf, chunk)); |
| 6457 | return -EIO; |
| 6458 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6459 | if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) { |
| 6460 | btrfs_err(fs_info, "invalid chunk length %llu", length); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6461 | return -EIO; |
| 6462 | } |
| 6463 | if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6464 | btrfs_err(fs_info, "invalid chunk stripe length: %llu", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6465 | stripe_len); |
| 6466 | return -EIO; |
| 6467 | } |
| 6468 | if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6469 | type) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6470 | btrfs_err(fs_info, "unrecognized chunk type: %llu", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6471 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
| 6472 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6473 | btrfs_chunk_type(leaf, chunk)); |
| 6474 | return -EIO; |
| 6475 | } |
Gu Jinxiang | 315409b | 2018-07-04 18:16:39 +0800 | [diff] [blame] | 6476 | |
| 6477 | if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) { |
| 6478 | btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type); |
| 6479 | return -EIO; |
| 6480 | } |
| 6481 | |
| 6482 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 6483 | (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) { |
| 6484 | btrfs_err(fs_info, |
| 6485 | "system chunk with data or metadata type: 0x%llx", type); |
| 6486 | return -EIO; |
| 6487 | } |
| 6488 | |
| 6489 | features = btrfs_super_incompat_flags(fs_info->super_copy); |
| 6490 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 6491 | mixed = true; |
| 6492 | |
| 6493 | if (!mixed) { |
| 6494 | if ((type & BTRFS_BLOCK_GROUP_METADATA) && |
| 6495 | (type & BTRFS_BLOCK_GROUP_DATA)) { |
| 6496 | btrfs_err(fs_info, |
| 6497 | "mixed chunk type in non-mixed mode: 0x%llx", type); |
| 6498 | return -EIO; |
| 6499 | } |
| 6500 | } |
| 6501 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6502 | if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || |
| 6503 | (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || |
| 6504 | (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || |
| 6505 | (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || |
| 6506 | (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || |
| 6507 | ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && |
| 6508 | num_stripes != 1)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6509 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6510 | "invalid num_stripes:sub_stripes %u:%u for profile %llu", |
| 6511 | num_stripes, sub_stripes, |
| 6512 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 6513 | return -EIO; |
| 6514 | } |
| 6515 | |
| 6516 | return 0; |
| 6517 | } |
| 6518 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6519 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6520 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6521 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6522 | if (error) |
| 6523 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6524 | devid, uuid); |
| 6525 | else |
| 6526 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6527 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6528 | } |
| 6529 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6530 | static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6531 | struct extent_buffer *leaf, |
| 6532 | struct btrfs_chunk *chunk) |
| 6533 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6534 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6535 | struct map_lookup *map; |
| 6536 | struct extent_map *em; |
| 6537 | u64 logical; |
| 6538 | u64 length; |
| 6539 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6540 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6541 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6542 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6543 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6544 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6545 | logical = key->offset; |
| 6546 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6547 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6548 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6549 | ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6550 | if (ret) |
| 6551 | return ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6552 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6553 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6554 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6555 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6556 | |
| 6557 | /* already mapped? */ |
| 6558 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6559 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6560 | return 0; |
| 6561 | } else if (em) { |
| 6562 | free_extent_map(em); |
| 6563 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6564 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6565 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6566 | if (!em) |
| 6567 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6568 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6569 | if (!map) { |
| 6570 | free_extent_map(em); |
| 6571 | return -ENOMEM; |
| 6572 | } |
| 6573 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6574 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6575 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6576 | em->start = logical; |
| 6577 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6578 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6579 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6580 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6581 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6582 | map->num_stripes = num_stripes; |
| 6583 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6584 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6585 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6586 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6587 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 6588 | map->verified_stripes = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6589 | for (i = 0; i < num_stripes; i++) { |
| 6590 | map->stripes[i].physical = |
| 6591 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6592 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6593 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6594 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6595 | BTRFS_UUID_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6596 | map->stripes[i].dev = btrfs_find_device(fs_info, devid, |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6597 | uuid, NULL); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6598 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6599 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6600 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6601 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6602 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6603 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6604 | if (!map->stripes[i].dev) { |
| 6605 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6606 | add_missing_dev(fs_info->fs_devices, devid, |
| 6607 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6608 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6609 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6610 | btrfs_err(fs_info, |
| 6611 | "failed to init missing dev %llu: %ld", |
| 6612 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6613 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6614 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6615 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6616 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6617 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6618 | &(map->stripes[i].dev->dev_state)); |
| 6619 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6620 | } |
| 6621 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6622 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6623 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6624 | write_unlock(&map_tree->map_tree.lock); |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6625 | if (ret < 0) { |
| 6626 | btrfs_err(fs_info, |
| 6627 | "failed to add chunk map, start=%llu len=%llu: %d", |
| 6628 | em->start, em->len, ret); |
| 6629 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6630 | free_extent_map(em); |
| 6631 | |
Qu Wenruo | 64f64f4 | 2018-08-01 10:37:20 +0800 | [diff] [blame] | 6632 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6633 | } |
| 6634 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6635 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6636 | struct btrfs_dev_item *dev_item, |
| 6637 | struct btrfs_device *device) |
| 6638 | { |
| 6639 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6640 | |
| 6641 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6642 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6643 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6644 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6645 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6646 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6647 | device->type = btrfs_device_type(leaf, dev_item); |
| 6648 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6649 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6650 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6651 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6652 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6653 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6654 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6655 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6656 | } |
| 6657 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6658 | 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] | 6659 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6660 | { |
| 6661 | struct btrfs_fs_devices *fs_devices; |
| 6662 | int ret; |
| 6663 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6664 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6665 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6666 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6667 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6668 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6669 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6670 | return fs_devices; |
| 6671 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6672 | fs_devices = fs_devices->seed; |
| 6673 | } |
| 6674 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6675 | fs_devices = find_fsid(fsid, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6676 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6677 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6678 | return ERR_PTR(-ENOENT); |
| 6679 | |
Nikolay Borisov | 7239ff4 | 2018-10-30 16:43:23 +0200 | [diff] [blame] | 6680 | fs_devices = alloc_fs_devices(fsid, NULL); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6681 | if (IS_ERR(fs_devices)) |
| 6682 | return fs_devices; |
| 6683 | |
| 6684 | fs_devices->seeding = 1; |
| 6685 | fs_devices->opened = 1; |
| 6686 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6687 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6688 | |
| 6689 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6690 | if (IS_ERR(fs_devices)) |
| 6691 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6692 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6693 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6694 | if (ret) { |
| 6695 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6696 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6697 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6698 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6699 | |
| 6700 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6701 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6702 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6703 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6704 | goto out; |
| 6705 | } |
| 6706 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6707 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6708 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6709 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6710 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6711 | } |
| 6712 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6713 | static int read_one_dev(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6714 | struct extent_buffer *leaf, |
| 6715 | struct btrfs_dev_item *dev_item) |
| 6716 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6717 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6718 | struct btrfs_device *device; |
| 6719 | u64 devid; |
| 6720 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6721 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6722 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6723 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6724 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6725 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6726 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6727 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6728 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6729 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 6730 | if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6731 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6732 | if (IS_ERR(fs_devices)) |
| 6733 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6734 | } |
| 6735 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6736 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6737 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6738 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6739 | btrfs_report_missing_device(fs_info, devid, |
| 6740 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6741 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6742 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6743 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6744 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6745 | if (IS_ERR(device)) { |
| 6746 | btrfs_err(fs_info, |
| 6747 | "failed to add missing dev %llu: %ld", |
| 6748 | devid, PTR_ERR(device)); |
| 6749 | return PTR_ERR(device); |
| 6750 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6751 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6752 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6753 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6754 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6755 | btrfs_report_missing_device(fs_info, |
| 6756 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6757 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6758 | } |
| 6759 | btrfs_report_missing_device(fs_info, devid, |
| 6760 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6761 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6762 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6763 | if (!device->bdev && |
| 6764 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6765 | /* |
| 6766 | * this happens when a device that was properly setup |
| 6767 | * in the device info lists suddenly goes bad. |
| 6768 | * device->bdev is NULL, and so we have to set |
| 6769 | * device->missing to one here |
| 6770 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6771 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6772 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6773 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6774 | |
| 6775 | /* Move the device to its own fs_devices */ |
| 6776 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6777 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6778 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6779 | |
| 6780 | list_move(&device->dev_list, &fs_devices->devices); |
| 6781 | device->fs_devices->num_devices--; |
| 6782 | fs_devices->num_devices++; |
| 6783 | |
| 6784 | device->fs_devices->missing_devices--; |
| 6785 | fs_devices->missing_devices++; |
| 6786 | |
| 6787 | device->fs_devices = fs_devices; |
| 6788 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6789 | } |
| 6790 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6791 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6792 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6793 | if (device->generation != |
| 6794 | btrfs_device_generation(leaf, dev_item)) |
| 6795 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6796 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6797 | |
| 6798 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6799 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6800 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6801 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6802 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6803 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6804 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6805 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6806 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6807 | return ret; |
| 6808 | } |
| 6809 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6810 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6811 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6812 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6813 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6814 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6815 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6816 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6817 | u8 *array_ptr; |
| 6818 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6819 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6820 | u32 num_stripes; |
| 6821 | u32 array_size; |
| 6822 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6823 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6824 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6825 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6826 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6827 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6828 | /* |
| 6829 | * This will create extent buffer of nodesize, superblock size is |
| 6830 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6831 | * overallocate but we can keep it as-is, only the first page is used. |
| 6832 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6833 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6834 | if (IS_ERR(sb)) |
| 6835 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6836 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6837 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6838 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6839 | * 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] | 6840 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6841 | * pages up-to-date when the page is larger: extent does not cover the |
| 6842 | * whole page and consequently check_page_uptodate does not find all |
| 6843 | * the page's extents up-to-date (the hole beyond sb), |
| 6844 | * write_extent_buffer then triggers a WARN_ON. |
| 6845 | * |
| 6846 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6847 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6848 | * to silence the warning eg. on PowerPC 64. |
| 6849 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6850 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6851 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6852 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6853 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6854 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6855 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6856 | array_ptr = super_copy->sys_chunk_array; |
| 6857 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6858 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6859 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6860 | while (cur_offset < array_size) { |
| 6861 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6862 | len = sizeof(*disk_key); |
| 6863 | if (cur_offset + len > array_size) |
| 6864 | goto out_short_read; |
| 6865 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6866 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6867 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6868 | array_ptr += len; |
| 6869 | sb_array_offset += len; |
| 6870 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6871 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6872 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6873 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6874 | /* |
| 6875 | * At least one btrfs_chunk with one stripe must be |
| 6876 | * present, exact stripe count check comes afterwards |
| 6877 | */ |
| 6878 | len = btrfs_chunk_item_size(1); |
| 6879 | if (cur_offset + len > array_size) |
| 6880 | goto out_short_read; |
| 6881 | |
| 6882 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 6883 | if (!num_stripes) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6884 | btrfs_err(fs_info, |
| 6885 | "invalid number of stripes %u in sys_array at offset %u", |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 6886 | num_stripes, cur_offset); |
| 6887 | ret = -EIO; |
| 6888 | break; |
| 6889 | } |
| 6890 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6891 | type = btrfs_chunk_type(sb, chunk); |
| 6892 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6893 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6894 | "invalid chunk type %llu in sys_array at offset %u", |
| 6895 | type, cur_offset); |
| 6896 | ret = -EIO; |
| 6897 | break; |
| 6898 | } |
| 6899 | |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6900 | len = btrfs_chunk_item_size(num_stripes); |
| 6901 | if (cur_offset + len > array_size) |
| 6902 | goto out_short_read; |
| 6903 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6904 | ret = read_one_chunk(fs_info, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6905 | if (ret) |
| 6906 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6907 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6908 | btrfs_err(fs_info, |
| 6909 | "unexpected item type %u in sys_array at offset %u", |
| 6910 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6911 | ret = -EIO; |
| 6912 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6913 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6914 | array_ptr += len; |
| 6915 | sb_array_offset += len; |
| 6916 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6917 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6918 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6919 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6920 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6921 | |
| 6922 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6923 | 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] | 6924 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6925 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6926 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6927 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6928 | } |
| 6929 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6930 | /* |
| 6931 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 6932 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6933 | * If the @failing_dev is specified, it's accounted as missing. |
| 6934 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6935 | * Return true if all chunks meet the minimal RW mount requirements. |
| 6936 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 6937 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6938 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 6939 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6940 | { |
| 6941 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
| 6942 | struct extent_map *em; |
| 6943 | u64 next_start = 0; |
| 6944 | bool ret = true; |
| 6945 | |
| 6946 | read_lock(&map_tree->map_tree.lock); |
| 6947 | em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1); |
| 6948 | read_unlock(&map_tree->map_tree.lock); |
| 6949 | /* No chunk at all? Return false anyway */ |
| 6950 | if (!em) { |
| 6951 | ret = false; |
| 6952 | goto out; |
| 6953 | } |
| 6954 | while (em) { |
| 6955 | struct map_lookup *map; |
| 6956 | int missing = 0; |
| 6957 | int max_tolerated; |
| 6958 | int i; |
| 6959 | |
| 6960 | map = em->map_lookup; |
| 6961 | max_tolerated = |
| 6962 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 6963 | map->type); |
| 6964 | for (i = 0; i < map->num_stripes; i++) { |
| 6965 | struct btrfs_device *dev = map->stripes[i].dev; |
| 6966 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6967 | if (!dev || !dev->bdev || |
| 6968 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6969 | dev->last_flush_error) |
| 6970 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6971 | else if (failing_dev && failing_dev == dev) |
| 6972 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6973 | } |
| 6974 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6975 | if (!failing_dev) |
| 6976 | btrfs_warn(fs_info, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6977 | "chunk %llu missing %d devices, max tolerance is %d for writeable mount", |
| 6978 | em->start, missing, max_tolerated); |
| 6979 | free_extent_map(em); |
| 6980 | ret = false; |
| 6981 | goto out; |
| 6982 | } |
| 6983 | next_start = extent_map_end(em); |
| 6984 | free_extent_map(em); |
| 6985 | |
| 6986 | read_lock(&map_tree->map_tree.lock); |
| 6987 | em = lookup_extent_mapping(&map_tree->map_tree, next_start, |
| 6988 | (u64)(-1) - next_start); |
| 6989 | read_unlock(&map_tree->map_tree.lock); |
| 6990 | } |
| 6991 | out: |
| 6992 | return ret; |
| 6993 | } |
| 6994 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6995 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6996 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6997 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6998 | struct btrfs_path *path; |
| 6999 | struct extent_buffer *leaf; |
| 7000 | struct btrfs_key key; |
| 7001 | struct btrfs_key found_key; |
| 7002 | int ret; |
| 7003 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7004 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7005 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7006 | path = btrfs_alloc_path(); |
| 7007 | if (!path) |
| 7008 | return -ENOMEM; |
| 7009 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 7010 | /* |
| 7011 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 7012 | * otherwise we don't need it. |
| 7013 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7014 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7015 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7016 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7017 | /* |
| 7018 | * Read all device items, and then all the chunk items. All |
| 7019 | * device items are found before any chunk item (their object id |
| 7020 | * is smaller than the lowest possible object id for a chunk |
| 7021 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7022 | */ |
| 7023 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 7024 | key.offset = 0; |
| 7025 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7026 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 7027 | if (ret < 0) |
| 7028 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7029 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7030 | leaf = path->nodes[0]; |
| 7031 | slot = path->slots[0]; |
| 7032 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7033 | ret = btrfs_next_leaf(root, path); |
| 7034 | if (ret == 0) |
| 7035 | continue; |
| 7036 | if (ret < 0) |
| 7037 | goto error; |
| 7038 | break; |
| 7039 | } |
| 7040 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7041 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 7042 | struct btrfs_dev_item *dev_item; |
| 7043 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7044 | struct btrfs_dev_item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7045 | ret = read_one_dev(fs_info, leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 7046 | if (ret) |
| 7047 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7048 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7049 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 7050 | struct btrfs_chunk *chunk; |
| 7051 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7052 | ret = read_one_chunk(fs_info, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7053 | if (ret) |
| 7054 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7055 | } |
| 7056 | path->slots[0]++; |
| 7057 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7058 | |
| 7059 | /* |
| 7060 | * After loading chunk tree, we've got all device information, |
| 7061 | * do another round of validation checks. |
| 7062 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7063 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 7064 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7065 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7066 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7067 | total_dev); |
| 7068 | ret = -EINVAL; |
| 7069 | goto error; |
| 7070 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7071 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 7072 | fs_info->fs_devices->total_rw_bytes) { |
| 7073 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7074 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7075 | btrfs_super_total_bytes(fs_info->super_copy), |
| 7076 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 7077 | ret = -EINVAL; |
| 7078 | goto error; |
| 7079 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7080 | ret = 0; |
| 7081 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7082 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 7083 | mutex_unlock(&uuid_mutex); |
| 7084 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7085 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7086 | return ret; |
| 7087 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7088 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7089 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 7090 | { |
| 7091 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7092 | struct btrfs_device *device; |
| 7093 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7094 | while (fs_devices) { |
| 7095 | mutex_lock(&fs_devices->device_list_mutex); |
| 7096 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7097 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 7098 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7099 | |
| 7100 | fs_devices = fs_devices->seed; |
| 7101 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7104 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 7105 | { |
| 7106 | int i; |
| 7107 | |
| 7108 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7109 | btrfs_dev_stat_reset(dev, i); |
| 7110 | } |
| 7111 | |
| 7112 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 7113 | { |
| 7114 | struct btrfs_key key; |
| 7115 | struct btrfs_key found_key; |
| 7116 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 7117 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7118 | struct extent_buffer *eb; |
| 7119 | int slot; |
| 7120 | int ret = 0; |
| 7121 | struct btrfs_device *device; |
| 7122 | struct btrfs_path *path = NULL; |
| 7123 | int i; |
| 7124 | |
| 7125 | path = btrfs_alloc_path(); |
| 7126 | if (!path) { |
| 7127 | ret = -ENOMEM; |
| 7128 | goto out; |
| 7129 | } |
| 7130 | |
| 7131 | mutex_lock(&fs_devices->device_list_mutex); |
| 7132 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7133 | int item_size; |
| 7134 | struct btrfs_dev_stats_item *ptr; |
| 7135 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7136 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7137 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7138 | key.offset = device->devid; |
| 7139 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7140 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7141 | __btrfs_reset_dev_stats(device); |
| 7142 | device->dev_stats_valid = 1; |
| 7143 | btrfs_release_path(path); |
| 7144 | continue; |
| 7145 | } |
| 7146 | slot = path->slots[0]; |
| 7147 | eb = path->nodes[0]; |
| 7148 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 7149 | item_size = btrfs_item_size_nr(eb, slot); |
| 7150 | |
| 7151 | ptr = btrfs_item_ptr(eb, slot, |
| 7152 | struct btrfs_dev_stats_item); |
| 7153 | |
| 7154 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7155 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7156 | btrfs_dev_stat_set(device, i, |
| 7157 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7158 | else |
| 7159 | btrfs_dev_stat_reset(device, i); |
| 7160 | } |
| 7161 | |
| 7162 | device->dev_stats_valid = 1; |
| 7163 | btrfs_dev_stat_print_on_load(device); |
| 7164 | btrfs_release_path(path); |
| 7165 | } |
| 7166 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7167 | |
| 7168 | out: |
| 7169 | btrfs_free_path(path); |
| 7170 | return ret < 0 ? ret : 0; |
| 7171 | } |
| 7172 | |
| 7173 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7174 | struct btrfs_device *device) |
| 7175 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7176 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7177 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7178 | struct btrfs_path *path; |
| 7179 | struct btrfs_key key; |
| 7180 | struct extent_buffer *eb; |
| 7181 | struct btrfs_dev_stats_item *ptr; |
| 7182 | int ret; |
| 7183 | int i; |
| 7184 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7185 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7186 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7187 | key.offset = device->devid; |
| 7188 | |
| 7189 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7190 | if (!path) |
| 7191 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7192 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7193 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7194 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7195 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7196 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7197 | goto out; |
| 7198 | } |
| 7199 | |
| 7200 | if (ret == 0 && |
| 7201 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7202 | /* need to delete old one and insert a new one */ |
| 7203 | ret = btrfs_del_item(trans, dev_root, path); |
| 7204 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7205 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7206 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7207 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7208 | goto out; |
| 7209 | } |
| 7210 | ret = 1; |
| 7211 | } |
| 7212 | |
| 7213 | if (ret == 1) { |
| 7214 | /* need to insert a new item */ |
| 7215 | btrfs_release_path(path); |
| 7216 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7217 | &key, sizeof(*ptr)); |
| 7218 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7219 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7220 | "insert dev_stats item for device %s failed %d", |
| 7221 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7222 | goto out; |
| 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | eb = path->nodes[0]; |
| 7227 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7228 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7229 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7230 | btrfs_dev_stat_read(device, i)); |
| 7231 | btrfs_mark_buffer_dirty(eb); |
| 7232 | |
| 7233 | out: |
| 7234 | btrfs_free_path(path); |
| 7235 | return ret; |
| 7236 | } |
| 7237 | |
| 7238 | /* |
| 7239 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7240 | */ |
| 7241 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 7242 | struct btrfs_fs_info *fs_info) |
| 7243 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7244 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7245 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7246 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7247 | int ret = 0; |
| 7248 | |
| 7249 | mutex_lock(&fs_devices->device_list_mutex); |
| 7250 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7251 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7252 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7253 | continue; |
| 7254 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7255 | |
| 7256 | /* |
| 7257 | * There is a LOAD-LOAD control dependency between the value of |
| 7258 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7259 | * reading the in-memory counters. Such control dependencies |
| 7260 | * require explicit read memory barriers. |
| 7261 | * |
| 7262 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7263 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7264 | * barrier implied by atomic_xchg in |
| 7265 | * btrfs_dev_stats_read_and_reset |
| 7266 | */ |
| 7267 | smp_rmb(); |
| 7268 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7269 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7270 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7271 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7272 | } |
| 7273 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7274 | |
| 7275 | return ret; |
| 7276 | } |
| 7277 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7278 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7279 | { |
| 7280 | btrfs_dev_stat_inc(dev, index); |
| 7281 | btrfs_dev_stat_print_on_error(dev); |
| 7282 | } |
| 7283 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7284 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7285 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7286 | if (!dev->dev_stats_valid) |
| 7287 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7288 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7289 | "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] | 7290 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7291 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7292 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7293 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7294 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7295 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7296 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7297 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7298 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7299 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7300 | int i; |
| 7301 | |
| 7302 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7303 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7304 | break; |
| 7305 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7306 | return; /* all values == 0, suppress message */ |
| 7307 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7308 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7309 | "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] | 7310 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7311 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7312 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7313 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7314 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7315 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7316 | } |
| 7317 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7318 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7319 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7320 | { |
| 7321 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7322 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7323 | int i; |
| 7324 | |
| 7325 | mutex_lock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7326 | dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7327 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7328 | |
| 7329 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7330 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7331 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7332 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7333 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7334 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7335 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7336 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7337 | if (stats->nr_items > i) |
| 7338 | stats->values[i] = |
| 7339 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7340 | else |
| 7341 | btrfs_dev_stat_reset(dev, i); |
| 7342 | } |
| 7343 | } else { |
| 7344 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7345 | if (stats->nr_items > i) |
| 7346 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7347 | } |
| 7348 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7349 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7350 | return 0; |
| 7351 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7352 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 7353 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7354 | { |
| 7355 | struct buffer_head *bh; |
| 7356 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7357 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7358 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7359 | if (!bdev) |
| 7360 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7361 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7362 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 7363 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7364 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7365 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 7366 | continue; |
| 7367 | |
| 7368 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 7369 | |
| 7370 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 7371 | set_buffer_dirty(bh); |
| 7372 | sync_dirty_buffer(bh); |
| 7373 | brelse(bh); |
| 7374 | } |
| 7375 | |
| 7376 | /* Notify udev that device has changed */ |
| 7377 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 7378 | |
| 7379 | /* Update ctime/mtime for device path for libblkid */ |
| 7380 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7381 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7382 | |
| 7383 | /* |
| 7384 | * Update the size of all devices, which is used for writing out the |
| 7385 | * super blocks. |
| 7386 | */ |
| 7387 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 7388 | { |
| 7389 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7390 | struct btrfs_device *curr, *next; |
| 7391 | |
| 7392 | if (list_empty(&fs_devices->resized_devices)) |
| 7393 | return; |
| 7394 | |
| 7395 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7396 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7397 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 7398 | resized_list) { |
| 7399 | list_del_init(&curr->resized_list); |
| 7400 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 7401 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7402 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7403 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7404 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7405 | |
| 7406 | /* Must be invoked during the transaction commit */ |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7407 | void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans) |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7408 | { |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7409 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7410 | struct extent_map *em; |
| 7411 | struct map_lookup *map; |
| 7412 | struct btrfs_device *dev; |
| 7413 | int i; |
| 7414 | |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7415 | if (list_empty(&trans->pending_chunks)) |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7416 | return; |
| 7417 | |
| 7418 | /* In order to kick the device replace finish process */ |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7419 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7420 | list_for_each_entry(em, &trans->pending_chunks, list) { |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 7421 | map = em->map_lookup; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7422 | |
| 7423 | for (i = 0; i < map->num_stripes; i++) { |
| 7424 | dev = map->stripes[i].dev; |
| 7425 | dev->commit_bytes_used = dev->bytes_used; |
| 7426 | } |
| 7427 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7428 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7429 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7430 | |
| 7431 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7432 | { |
| 7433 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7434 | while (fs_devices) { |
| 7435 | fs_devices->fs_info = fs_info; |
| 7436 | fs_devices = fs_devices->seed; |
| 7437 | } |
| 7438 | } |
| 7439 | |
| 7440 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7441 | { |
| 7442 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7443 | while (fs_devices) { |
| 7444 | fs_devices->fs_info = NULL; |
| 7445 | fs_devices = fs_devices->seed; |
| 7446 | } |
| 7447 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7448 | |
| 7449 | /* |
| 7450 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7451 | */ |
| 7452 | int btrfs_bg_type_to_factor(u64 flags) |
| 7453 | { |
| 7454 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 7455 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7456 | return 2; |
| 7457 | return 1; |
| 7458 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7459 | |
| 7460 | |
| 7461 | static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes) |
| 7462 | { |
| 7463 | int index = btrfs_bg_flags_to_raid_index(type); |
| 7464 | int ncopies = btrfs_raid_array[index].ncopies; |
| 7465 | int data_stripes; |
| 7466 | |
| 7467 | switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 7468 | case BTRFS_BLOCK_GROUP_RAID5: |
| 7469 | data_stripes = num_stripes - 1; |
| 7470 | break; |
| 7471 | case BTRFS_BLOCK_GROUP_RAID6: |
| 7472 | data_stripes = num_stripes - 2; |
| 7473 | break; |
| 7474 | default: |
| 7475 | data_stripes = num_stripes / ncopies; |
| 7476 | break; |
| 7477 | } |
| 7478 | return div_u64(chunk_len, data_stripes); |
| 7479 | } |
| 7480 | |
| 7481 | static int verify_one_dev_extent(struct btrfs_fs_info *fs_info, |
| 7482 | u64 chunk_offset, u64 devid, |
| 7483 | u64 physical_offset, u64 physical_len) |
| 7484 | { |
| 7485 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 7486 | struct extent_map *em; |
| 7487 | struct map_lookup *map; |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7488 | struct btrfs_device *dev; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7489 | u64 stripe_len; |
| 7490 | bool found = false; |
| 7491 | int ret = 0; |
| 7492 | int i; |
| 7493 | |
| 7494 | read_lock(&em_tree->lock); |
| 7495 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 7496 | read_unlock(&em_tree->lock); |
| 7497 | |
| 7498 | if (!em) { |
| 7499 | btrfs_err(fs_info, |
| 7500 | "dev extent physical offset %llu on devid %llu doesn't have corresponding chunk", |
| 7501 | physical_offset, devid); |
| 7502 | ret = -EUCLEAN; |
| 7503 | goto out; |
| 7504 | } |
| 7505 | |
| 7506 | map = em->map_lookup; |
| 7507 | stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes); |
| 7508 | if (physical_len != stripe_len) { |
| 7509 | btrfs_err(fs_info, |
| 7510 | "dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu", |
| 7511 | physical_offset, devid, em->start, physical_len, |
| 7512 | stripe_len); |
| 7513 | ret = -EUCLEAN; |
| 7514 | goto out; |
| 7515 | } |
| 7516 | |
| 7517 | for (i = 0; i < map->num_stripes; i++) { |
| 7518 | if (map->stripes[i].dev->devid == devid && |
| 7519 | map->stripes[i].physical == physical_offset) { |
| 7520 | found = true; |
| 7521 | if (map->verified_stripes >= map->num_stripes) { |
| 7522 | btrfs_err(fs_info, |
| 7523 | "too many dev extents for chunk %llu found", |
| 7524 | em->start); |
| 7525 | ret = -EUCLEAN; |
| 7526 | goto out; |
| 7527 | } |
| 7528 | map->verified_stripes++; |
| 7529 | break; |
| 7530 | } |
| 7531 | } |
| 7532 | if (!found) { |
| 7533 | btrfs_err(fs_info, |
| 7534 | "dev extent physical offset %llu devid %llu has no corresponding chunk", |
| 7535 | physical_offset, devid); |
| 7536 | ret = -EUCLEAN; |
| 7537 | } |
Qu Wenruo | 05a37c4 | 2018-10-05 17:45:55 +0800 | [diff] [blame] | 7538 | |
| 7539 | /* Make sure no dev extent is beyond device bondary */ |
| 7540 | dev = btrfs_find_device(fs_info, devid, NULL, NULL); |
| 7541 | if (!dev) { |
| 7542 | btrfs_err(fs_info, "failed to find devid %llu", devid); |
| 7543 | ret = -EUCLEAN; |
| 7544 | goto out; |
| 7545 | } |
| 7546 | if (physical_offset + physical_len > dev->disk_total_bytes) { |
| 7547 | btrfs_err(fs_info, |
| 7548 | "dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu", |
| 7549 | devid, physical_offset, physical_len, |
| 7550 | dev->disk_total_bytes); |
| 7551 | ret = -EUCLEAN; |
| 7552 | goto out; |
| 7553 | } |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7554 | out: |
| 7555 | free_extent_map(em); |
| 7556 | return ret; |
| 7557 | } |
| 7558 | |
| 7559 | static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info) |
| 7560 | { |
| 7561 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
| 7562 | struct extent_map *em; |
| 7563 | struct rb_node *node; |
| 7564 | int ret = 0; |
| 7565 | |
| 7566 | read_lock(&em_tree->lock); |
Liu Bo | 07e1ce0 | 2018-08-23 03:51:52 +0800 | [diff] [blame] | 7567 | 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] | 7568 | em = rb_entry(node, struct extent_map, rb_node); |
| 7569 | if (em->map_lookup->num_stripes != |
| 7570 | em->map_lookup->verified_stripes) { |
| 7571 | btrfs_err(fs_info, |
| 7572 | "chunk %llu has missing dev extent, have %d expect %d", |
| 7573 | em->start, em->map_lookup->verified_stripes, |
| 7574 | em->map_lookup->num_stripes); |
| 7575 | ret = -EUCLEAN; |
| 7576 | goto out; |
| 7577 | } |
| 7578 | } |
| 7579 | out: |
| 7580 | read_unlock(&em_tree->lock); |
| 7581 | return ret; |
| 7582 | } |
| 7583 | |
| 7584 | /* |
| 7585 | * Ensure that all dev extents are mapped to correct chunk, otherwise |
| 7586 | * later chunk allocation/free would cause unexpected behavior. |
| 7587 | * |
| 7588 | * NOTE: This will iterate through the whole device tree, which should be of |
| 7589 | * the same size level as the chunk tree. This slightly increases mount time. |
| 7590 | */ |
| 7591 | int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info) |
| 7592 | { |
| 7593 | struct btrfs_path *path; |
| 7594 | struct btrfs_root *root = fs_info->dev_root; |
| 7595 | struct btrfs_key key; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7596 | u64 prev_devid = 0; |
| 7597 | u64 prev_dev_ext_end = 0; |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7598 | int ret = 0; |
| 7599 | |
| 7600 | key.objectid = 1; |
| 7601 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 7602 | key.offset = 0; |
| 7603 | |
| 7604 | path = btrfs_alloc_path(); |
| 7605 | if (!path) |
| 7606 | return -ENOMEM; |
| 7607 | |
| 7608 | path->reada = READA_FORWARD; |
| 7609 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7610 | if (ret < 0) |
| 7611 | goto out; |
| 7612 | |
| 7613 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 7614 | ret = btrfs_next_item(root, path); |
| 7615 | if (ret < 0) |
| 7616 | goto out; |
| 7617 | /* No dev extents at all? Not good */ |
| 7618 | if (ret > 0) { |
| 7619 | ret = -EUCLEAN; |
| 7620 | goto out; |
| 7621 | } |
| 7622 | } |
| 7623 | while (1) { |
| 7624 | struct extent_buffer *leaf = path->nodes[0]; |
| 7625 | struct btrfs_dev_extent *dext; |
| 7626 | int slot = path->slots[0]; |
| 7627 | u64 chunk_offset; |
| 7628 | u64 physical_offset; |
| 7629 | u64 physical_len; |
| 7630 | u64 devid; |
| 7631 | |
| 7632 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 7633 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
| 7634 | break; |
| 7635 | devid = key.objectid; |
| 7636 | physical_offset = key.offset; |
| 7637 | |
| 7638 | dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent); |
| 7639 | chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext); |
| 7640 | physical_len = btrfs_dev_extent_length(leaf, dext); |
| 7641 | |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7642 | /* Check if this dev extent overlaps with the previous one */ |
| 7643 | if (devid == prev_devid && physical_offset < prev_dev_ext_end) { |
| 7644 | btrfs_err(fs_info, |
| 7645 | "dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu", |
| 7646 | devid, physical_offset, prev_dev_ext_end); |
| 7647 | ret = -EUCLEAN; |
| 7648 | goto out; |
| 7649 | } |
| 7650 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7651 | ret = verify_one_dev_extent(fs_info, chunk_offset, devid, |
| 7652 | physical_offset, physical_len); |
| 7653 | if (ret < 0) |
| 7654 | goto out; |
Qu Wenruo | 5eb1938 | 2018-10-05 17:45:54 +0800 | [diff] [blame] | 7655 | prev_devid = devid; |
| 7656 | prev_dev_ext_end = physical_offset + physical_len; |
| 7657 | |
Qu Wenruo | cf90d88 | 2018-08-01 10:37:19 +0800 | [diff] [blame] | 7658 | ret = btrfs_next_item(root, path); |
| 7659 | if (ret < 0) |
| 7660 | goto out; |
| 7661 | if (ret > 0) { |
| 7662 | ret = 0; |
| 7663 | break; |
| 7664 | } |
| 7665 | } |
| 7666 | |
| 7667 | /* Ensure all chunks have corresponding dev extents */ |
| 7668 | ret = verify_chunk_dev_extent_mapping(fs_info); |
| 7669 | out: |
| 7670 | btrfs_free_path(path); |
| 7671 | return ret; |
| 7672 | } |
Omar Sandoval | eede2bf | 2016-11-03 10:28:12 -0700 | [diff] [blame] | 7673 | |
| 7674 | /* |
| 7675 | * Check whether the given block group or device is pinned by any inode being |
| 7676 | * used as a swapfile. |
| 7677 | */ |
| 7678 | bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr) |
| 7679 | { |
| 7680 | struct btrfs_swapfile_pin *sp; |
| 7681 | struct rb_node *node; |
| 7682 | |
| 7683 | spin_lock(&fs_info->swapfile_pins_lock); |
| 7684 | node = fs_info->swapfile_pins.rb_node; |
| 7685 | while (node) { |
| 7686 | sp = rb_entry(node, struct btrfs_swapfile_pin, node); |
| 7687 | if (ptr < sp->ptr) |
| 7688 | node = node->rb_left; |
| 7689 | else if (ptr > sp->ptr) |
| 7690 | node = node->rb_right; |
| 7691 | else |
| 7692 | break; |
| 7693 | } |
| 7694 | spin_unlock(&fs_info->swapfile_pins_lock); |
| 7695 | return node != NULL; |
| 7696 | } |