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, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 40 | .raid_name = "raid10", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 41 | .bg_flag = BTRFS_BLOCK_GROUP_RAID10, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 42 | .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 43 | }, |
| 44 | [BTRFS_RAID_RAID1] = { |
| 45 | .sub_stripes = 1, |
| 46 | .dev_stripes = 1, |
| 47 | .devs_max = 2, |
| 48 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 49 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 50 | .devs_increment = 2, |
| 51 | .ncopies = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 52 | .raid_name = "raid1", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 53 | .bg_flag = BTRFS_BLOCK_GROUP_RAID1, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 54 | .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 55 | }, |
| 56 | [BTRFS_RAID_DUP] = { |
| 57 | .sub_stripes = 1, |
| 58 | .dev_stripes = 2, |
| 59 | .devs_max = 1, |
| 60 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 61 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 62 | .devs_increment = 1, |
| 63 | .ncopies = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 64 | .raid_name = "dup", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 65 | .bg_flag = BTRFS_BLOCK_GROUP_DUP, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 66 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 67 | }, |
| 68 | [BTRFS_RAID_RAID0] = { |
| 69 | .sub_stripes = 1, |
| 70 | .dev_stripes = 1, |
| 71 | .devs_max = 0, |
| 72 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 73 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 74 | .devs_increment = 1, |
| 75 | .ncopies = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 76 | .raid_name = "raid0", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 77 | .bg_flag = BTRFS_BLOCK_GROUP_RAID0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 78 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 79 | }, |
| 80 | [BTRFS_RAID_SINGLE] = { |
| 81 | .sub_stripes = 1, |
| 82 | .dev_stripes = 1, |
| 83 | .devs_max = 1, |
| 84 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 85 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 86 | .devs_increment = 1, |
| 87 | .ncopies = 1, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 88 | .raid_name = "single", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 89 | .bg_flag = 0, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 90 | .mindev_error = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 91 | }, |
| 92 | [BTRFS_RAID_RAID5] = { |
| 93 | .sub_stripes = 1, |
| 94 | .dev_stripes = 1, |
| 95 | .devs_max = 0, |
| 96 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 97 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 98 | .devs_increment = 1, |
| 99 | .ncopies = 2, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 100 | .raid_name = "raid5", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 101 | .bg_flag = BTRFS_BLOCK_GROUP_RAID5, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 102 | .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 103 | }, |
| 104 | [BTRFS_RAID_RAID6] = { |
| 105 | .sub_stripes = 1, |
| 106 | .dev_stripes = 1, |
| 107 | .devs_max = 0, |
| 108 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 109 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 110 | .devs_increment = 1, |
| 111 | .ncopies = 3, |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 112 | .raid_name = "raid6", |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 113 | .bg_flag = BTRFS_BLOCK_GROUP_RAID6, |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 114 | .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 115 | }, |
| 116 | }; |
| 117 | |
Anand Jain | ed23467 | 2018-04-25 19:01:42 +0800 | [diff] [blame] | 118 | const char *get_raid_name(enum btrfs_raid_types type) |
| 119 | { |
| 120 | if (type >= BTRFS_NR_RAID_TYPES) |
| 121 | return NULL; |
| 122 | |
| 123 | return btrfs_raid_array[type].raid_name; |
| 124 | } |
| 125 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 126 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 127 | struct btrfs_fs_info *fs_info); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 128 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 129 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 130 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 131 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 132 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 133 | enum btrfs_map_op op, |
| 134 | u64 logical, u64 *length, |
| 135 | struct btrfs_bio **bbio_ret, |
| 136 | int mirror_num, int need_raid_map); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 137 | |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Device locking |
| 140 | * ============== |
| 141 | * |
| 142 | * There are several mutexes that protect manipulation of devices and low-level |
| 143 | * structures like chunks but not block groups, extents or files |
| 144 | * |
| 145 | * uuid_mutex (global lock) |
| 146 | * ------------------------ |
| 147 | * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from |
| 148 | * the SCAN_DEV ioctl registration or from mount either implicitly (the first |
| 149 | * device) or requested by the device= mount option |
| 150 | * |
| 151 | * the mutex can be very coarse and can cover long-running operations |
| 152 | * |
| 153 | * protects: updates to fs_devices counters like missing devices, rw devices, |
| 154 | * seeding, structure cloning, openning/closing devices at mount/umount time |
| 155 | * |
| 156 | * global::fs_devs - add, remove, updates to the global list |
| 157 | * |
| 158 | * does not protect: manipulation of the fs_devices::devices list! |
| 159 | * |
| 160 | * btrfs_device::name - renames (write side), read is RCU |
| 161 | * |
| 162 | * fs_devices::device_list_mutex (per-fs, with RCU) |
| 163 | * ------------------------------------------------ |
| 164 | * protects updates to fs_devices::devices, ie. adding and deleting |
| 165 | * |
| 166 | * simple list traversal with read-only actions can be done with RCU protection |
| 167 | * |
| 168 | * may be used to exclude some operations from running concurrently without any |
| 169 | * modifications to the list (see write_all_supers) |
| 170 | * |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 171 | * balance_mutex |
| 172 | * ------------- |
| 173 | * protects balance structures (status, state) and context accessed from |
| 174 | * several places (internally, ioctl) |
| 175 | * |
| 176 | * chunk_mutex |
| 177 | * ----------- |
| 178 | * protects chunks, adding or removing during allocation, trim or when a new |
| 179 | * device is added/removed |
| 180 | * |
| 181 | * cleaner_mutex |
| 182 | * ------------- |
| 183 | * a big lock that is held by the cleaner thread and prevents running subvolume |
| 184 | * cleaning together with relocation or delayed iputs |
| 185 | * |
| 186 | * |
| 187 | * Lock nesting |
| 188 | * ============ |
| 189 | * |
| 190 | * uuid_mutex |
| 191 | * volume_mutex |
| 192 | * device_list_mutex |
| 193 | * chunk_mutex |
| 194 | * balance_mutex |
Anand Jain | 89595e8 | 2018-04-18 14:59:25 +0800 | [diff] [blame] | 195 | * |
| 196 | * |
| 197 | * Exclusive operations, BTRFS_FS_EXCL_OP |
| 198 | * ====================================== |
| 199 | * |
| 200 | * Maintains the exclusivity of the following operations that apply to the |
| 201 | * whole filesystem and cannot run in parallel. |
| 202 | * |
| 203 | * - Balance (*) |
| 204 | * - Device add |
| 205 | * - Device remove |
| 206 | * - Device replace (*) |
| 207 | * - Resize |
| 208 | * |
| 209 | * The device operations (as above) can be in one of the following states: |
| 210 | * |
| 211 | * - Running state |
| 212 | * - Paused state |
| 213 | * - Completed state |
| 214 | * |
| 215 | * Only device operations marked with (*) can go into the Paused state for the |
| 216 | * following reasons: |
| 217 | * |
| 218 | * - ioctl (only Balance can be Paused through ioctl) |
| 219 | * - filesystem remounted as read-only |
| 220 | * - filesystem unmounted and mounted as read-only |
| 221 | * - system power-cycle and filesystem mounted as read-only |
| 222 | * - filesystem or device errors leading to forced read-only |
| 223 | * |
| 224 | * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations. |
| 225 | * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set. |
| 226 | * A device operation in Paused or Running state can be canceled or resumed |
| 227 | * either by ioctl (Balance only) or when remounted as read-write. |
| 228 | * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or |
| 229 | * completed. |
David Sterba | 9c6b1c4 | 2017-06-16 22:30:00 +0200 | [diff] [blame] | 230 | */ |
| 231 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 232 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 233 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 234 | struct list_head *btrfs_get_fs_uuids(void) |
| 235 | { |
| 236 | return &fs_uuids; |
| 237 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 238 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 239 | /* |
| 240 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 241 | * @fsid: if not NULL, copy the uuid to fs_devices::fsid |
| 242 | * |
| 243 | * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR(). |
| 244 | * The returned struct is not linked onto any lists and can be destroyed with |
| 245 | * kfree() right away. |
| 246 | */ |
| 247 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 248 | { |
| 249 | struct btrfs_fs_devices *fs_devs; |
| 250 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 251 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 252 | if (!fs_devs) |
| 253 | return ERR_PTR(-ENOMEM); |
| 254 | |
| 255 | mutex_init(&fs_devs->device_list_mutex); |
| 256 | |
| 257 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 258 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 259 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 260 | INIT_LIST_HEAD(&fs_devs->fs_list); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 261 | if (fsid) |
| 262 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 263 | |
| 264 | return fs_devs; |
| 265 | } |
| 266 | |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 267 | void btrfs_free_device(struct btrfs_device *device) |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 268 | { |
| 269 | rcu_string_free(device->name); |
| 270 | bio_put(device->flush_bio); |
| 271 | kfree(device); |
| 272 | } |
| 273 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 274 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 275 | { |
| 276 | struct btrfs_device *device; |
| 277 | WARN_ON(fs_devices->opened); |
| 278 | while (!list_empty(&fs_devices->devices)) { |
| 279 | device = list_entry(fs_devices->devices.next, |
| 280 | struct btrfs_device, dev_list); |
| 281 | list_del(&device->dev_list); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 282 | btrfs_free_device(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 283 | } |
| 284 | kfree(fs_devices); |
| 285 | } |
| 286 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 287 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 288 | enum kobject_action action) |
| 289 | { |
| 290 | int ret; |
| 291 | |
| 292 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 293 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 294 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 295 | action, |
| 296 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 297 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 298 | } |
| 299 | |
David Sterba | ffc5a37 | 2018-02-19 17:24:15 +0100 | [diff] [blame] | 300 | void __exit btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 301 | { |
| 302 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 303 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 304 | while (!list_empty(&fs_uuids)) { |
| 305 | fs_devices = list_entry(fs_uuids.next, |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 306 | struct btrfs_fs_devices, fs_list); |
| 307 | list_del(&fs_devices->fs_list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 308 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 309 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 310 | } |
| 311 | |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 312 | /* |
| 313 | * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error. |
| 314 | * 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] | 315 | * btrfs_free_device. |
David Sterba | 48dae9c | 2017-10-30 18:10:25 +0100 | [diff] [blame] | 316 | */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 317 | static struct btrfs_device *__alloc_device(void) |
| 318 | { |
| 319 | struct btrfs_device *dev; |
| 320 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 321 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 322 | if (!dev) |
| 323 | return ERR_PTR(-ENOMEM); |
| 324 | |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 325 | /* |
| 326 | * Preallocate a bio that's always going to be used for flushing device |
| 327 | * barriers and matches the device lifespan |
| 328 | */ |
| 329 | dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL); |
| 330 | if (!dev->flush_bio) { |
| 331 | kfree(dev); |
| 332 | return ERR_PTR(-ENOMEM); |
| 333 | } |
David Sterba | e0ae999 | 2017-06-06 17:06:06 +0200 | [diff] [blame] | 334 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 335 | INIT_LIST_HEAD(&dev->dev_list); |
| 336 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 337 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 338 | |
| 339 | spin_lock_init(&dev->io_lock); |
| 340 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 341 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 342 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 343 | btrfs_device_data_ordered_init(dev); |
Chris Mason | 9bcaaea | 2017-05-04 16:08:08 -0700 | [diff] [blame] | 344 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 345 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 346 | |
| 347 | return dev; |
| 348 | } |
| 349 | |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 350 | /* |
| 351 | * Find a device specified by @devid or @uuid in the list of @fs_devices, or |
| 352 | * return NULL. |
| 353 | * |
| 354 | * If devid and uuid are both specified, the match must be exact, otherwise |
| 355 | * only devid is used. |
| 356 | */ |
| 357 | static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices, |
| 358 | u64 devid, const u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 359 | { |
| 360 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 361 | |
Anand Jain | 636d2c9 | 2018-04-12 10:29:29 +0800 | [diff] [blame] | 362 | list_for_each_entry(dev, &fs_devices->devices, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 363 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 364 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 365 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 366 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 367 | } |
| 368 | return NULL; |
| 369 | } |
| 370 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 371 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 372 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 373 | struct btrfs_fs_devices *fs_devices; |
| 374 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 375 | list_for_each_entry(fs_devices, &fs_uuids, fs_list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 376 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 377 | return fs_devices; |
| 378 | } |
| 379 | return NULL; |
| 380 | } |
| 381 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 382 | static int |
| 383 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 384 | int flush, struct block_device **bdev, |
| 385 | struct buffer_head **bh) |
| 386 | { |
| 387 | int ret; |
| 388 | |
| 389 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 390 | |
| 391 | if (IS_ERR(*bdev)) { |
| 392 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 393 | goto error; |
| 394 | } |
| 395 | |
| 396 | if (flush) |
| 397 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 398 | ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 399 | if (ret) { |
| 400 | blkdev_put(*bdev, flags); |
| 401 | goto error; |
| 402 | } |
| 403 | invalidate_bdev(*bdev); |
| 404 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 405 | if (IS_ERR(*bh)) { |
| 406 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 407 | blkdev_put(*bdev, flags); |
| 408 | goto error; |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | |
| 413 | error: |
| 414 | *bdev = NULL; |
| 415 | *bh = NULL; |
| 416 | return ret; |
| 417 | } |
| 418 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 419 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 420 | struct bio *head, struct bio *tail) |
| 421 | { |
| 422 | |
| 423 | struct bio *old_head; |
| 424 | |
| 425 | old_head = pending_bios->head; |
| 426 | pending_bios->head = head; |
| 427 | if (pending_bios->tail) |
| 428 | tail->bi_next = old_head; |
| 429 | else |
| 430 | pending_bios->tail = tail; |
| 431 | } |
| 432 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 433 | /* |
| 434 | * we try to collect pending bios for a device so we don't get a large |
| 435 | * number of procs sending bios down to the same device. This greatly |
| 436 | * improves the schedulers ability to collect and merge the bios. |
| 437 | * |
| 438 | * But, it also turns into a long list of bios to process and that is sure |
| 439 | * to eventually make the worker thread block. The solution here is to |
| 440 | * make some progress and then put this work struct back at the end of |
| 441 | * the list if the block device is congested. This way, multiple devices |
| 442 | * can make progress from a single worker thread. |
| 443 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 444 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 445 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 446 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 447 | struct bio *pending; |
| 448 | struct backing_dev_info *bdi; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 449 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 450 | struct bio *tail; |
| 451 | struct bio *cur; |
| 452 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 453 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 454 | unsigned long batch_run = 0; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 455 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 456 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 457 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 458 | struct blk_plug plug; |
| 459 | |
| 460 | /* |
| 461 | * this function runs all the bios we've collected for |
| 462 | * a particular device. We don't want to wander off to |
| 463 | * another device without first sending all of these down. |
| 464 | * So, setup a plug here and finish it off before we return |
| 465 | */ |
| 466 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 467 | |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 468 | bdi = device->bdev->bd_bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 469 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 470 | loop: |
| 471 | spin_lock(&device->io_lock); |
| 472 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 473 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 474 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 475 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 476 | /* take all the bios off the list at once and process them |
| 477 | * later on (without the lock held). But, remember the |
| 478 | * tail and other pointers so the bios can be properly reinserted |
| 479 | * into the list if we hit congestion |
| 480 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 481 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 482 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 483 | force_reg = 1; |
| 484 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 485 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 486 | force_reg = 0; |
| 487 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 488 | |
| 489 | pending = pending_bios->head; |
| 490 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 491 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 492 | |
| 493 | /* |
| 494 | * if pending was null this time around, no bios need processing |
| 495 | * at all and we can stop. Otherwise it'll loop back up again |
| 496 | * and do an additional check so no bios are missed. |
| 497 | * |
| 498 | * device->running_pending is used to synchronize with the |
| 499 | * schedule_bio code. |
| 500 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 501 | if (device->pending_sync_bios.head == NULL && |
| 502 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 503 | again = 0; |
| 504 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 505 | } else { |
| 506 | again = 1; |
| 507 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 508 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 509 | |
| 510 | pending_bios->head = NULL; |
| 511 | pending_bios->tail = NULL; |
| 512 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 513 | spin_unlock(&device->io_lock); |
| 514 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 515 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 516 | |
| 517 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 518 | /* we want to work on both lists, but do more bios on the |
| 519 | * sync list than the regular list |
| 520 | */ |
| 521 | if ((num_run > 32 && |
| 522 | pending_bios != &device->pending_sync_bios && |
| 523 | device->pending_sync_bios.head) || |
| 524 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 525 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 526 | spin_lock(&device->io_lock); |
| 527 | requeue_list(pending_bios, pending, tail); |
| 528 | goto loop_lock; |
| 529 | } |
| 530 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 531 | cur = pending; |
| 532 | pending = pending->bi_next; |
| 533 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 534 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 535 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 536 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 537 | /* |
| 538 | * if we're doing the sync list, record that our |
| 539 | * plug has some sync requests on it |
| 540 | * |
| 541 | * If we're doing the regular list and there are |
| 542 | * sync requests sitting around, unplug before |
| 543 | * we add more |
| 544 | */ |
| 545 | if (pending_bios == &device->pending_sync_bios) { |
| 546 | sync_pending = 1; |
| 547 | } else if (sync_pending) { |
| 548 | blk_finish_plug(&plug); |
| 549 | blk_start_plug(&plug); |
| 550 | sync_pending = 0; |
| 551 | } |
| 552 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 553 | btrfsic_submit_bio(cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 554 | num_run++; |
| 555 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 556 | |
| 557 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 558 | |
| 559 | /* |
| 560 | * we made progress, there is more work to do and the bdi |
| 561 | * is now congested. Back off and let other work structs |
| 562 | * run instead |
| 563 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 564 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 565 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 566 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 567 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 568 | ioc = current->io_context; |
| 569 | |
| 570 | /* |
| 571 | * the main goal here is that we don't want to |
| 572 | * block if we're going to be able to submit |
| 573 | * more requests without blocking. |
| 574 | * |
| 575 | * This code does two great things, it pokes into |
| 576 | * the elevator code from a filesystem _and_ |
| 577 | * it makes assumptions about how batching works. |
| 578 | */ |
| 579 | if (ioc && ioc->nr_batch_requests > 0 && |
| 580 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 581 | (last_waited == 0 || |
| 582 | ioc->last_waited == last_waited)) { |
| 583 | /* |
| 584 | * we want to go through our batch of |
| 585 | * requests and stop. So, we copy out |
| 586 | * the ioc->last_waited time and test |
| 587 | * against it before looping |
| 588 | */ |
| 589 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 590 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 591 | continue; |
| 592 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 593 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 594 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 595 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 596 | |
| 597 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 598 | btrfs_queue_work(fs_info->submit_workers, |
| 599 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 600 | goto done; |
| 601 | } |
| 602 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 603 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 604 | cond_resched(); |
| 605 | if (again) |
| 606 | goto loop; |
| 607 | |
| 608 | spin_lock(&device->io_lock); |
| 609 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 610 | goto loop_lock; |
| 611 | spin_unlock(&device->io_lock); |
| 612 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 613 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 614 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 615 | } |
| 616 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 617 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 618 | { |
| 619 | struct btrfs_device *device; |
| 620 | |
| 621 | device = container_of(work, struct btrfs_device, work); |
| 622 | run_scheduled_bios(device); |
| 623 | } |
| 624 | |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 625 | /* |
| 626 | * Search and remove all stale (devices which are not mounted) devices. |
| 627 | * When both inputs are NULL, it will search and release all stale devices. |
| 628 | * path: Optional. When provided will it release all unmounted devices |
| 629 | * matching this path only. |
| 630 | * skip_dev: Optional. Will skip this device when searching for the stale |
| 631 | * devices. |
| 632 | */ |
| 633 | static void btrfs_free_stale_devices(const char *path, |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 634 | struct btrfs_device *skip_device) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 635 | { |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 636 | struct btrfs_fs_devices *fs_devices, *tmp_fs_devices; |
| 637 | struct btrfs_device *device, *tmp_device; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 638 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 639 | 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] | 640 | mutex_lock(&fs_devices->device_list_mutex); |
| 641 | if (fs_devices->opened) { |
| 642 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 643 | continue; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 644 | } |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 645 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 646 | list_for_each_entry_safe(device, tmp_device, |
| 647 | &fs_devices->devices, dev_list) { |
Anand Jain | 522f1b4 | 2018-01-18 22:00:35 +0800 | [diff] [blame] | 648 | int not_found = 0; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 649 | |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 650 | if (skip_device && skip_device == device) |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 651 | continue; |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 652 | if (path && !device->name) |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 653 | continue; |
| 654 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 655 | rcu_read_lock(); |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 656 | if (path) |
Anand Jain | fa6d2ae | 2018-05-29 15:33:08 +0800 | [diff] [blame] | 657 | not_found = strcmp(rcu_str_deref(device->name), |
Anand Jain | d8367db | 2018-01-18 22:00:37 +0800 | [diff] [blame] | 658 | path); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 659 | rcu_read_unlock(); |
Anand Jain | 38cf665 | 2018-01-18 22:00:34 +0800 | [diff] [blame] | 660 | if (not_found) |
| 661 | continue; |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 662 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 663 | /* delete the stale device */ |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 664 | fs_devices->num_devices--; |
| 665 | list_del(&device->dev_list); |
| 666 | btrfs_free_device(device); |
| 667 | |
| 668 | if (fs_devices->num_devices == 0) |
Nikolay Borisov | fd649f1 | 2018-01-30 16:07:37 +0200 | [diff] [blame] | 669 | break; |
Anand Jain | 7bcb816 | 2018-05-29 17:23:20 +0800 | [diff] [blame] | 670 | } |
| 671 | mutex_unlock(&fs_devices->device_list_mutex); |
| 672 | if (fs_devices->num_devices == 0) { |
| 673 | btrfs_sysfs_remove_fsid(fs_devices); |
| 674 | list_del(&fs_devices->fs_list); |
| 675 | free_fs_devices(fs_devices); |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 680 | static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices, |
| 681 | struct btrfs_device *device, fmode_t flags, |
| 682 | void *holder) |
| 683 | { |
| 684 | struct request_queue *q; |
| 685 | struct block_device *bdev; |
| 686 | struct buffer_head *bh; |
| 687 | struct btrfs_super_block *disk_super; |
| 688 | u64 devid; |
| 689 | int ret; |
| 690 | |
| 691 | if (device->bdev) |
| 692 | return -EINVAL; |
| 693 | if (!device->name) |
| 694 | return -EINVAL; |
| 695 | |
| 696 | ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 697 | &bdev, &bh); |
| 698 | if (ret) |
| 699 | return ret; |
| 700 | |
| 701 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 702 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 703 | if (devid != device->devid) |
| 704 | goto error_brelse; |
| 705 | |
| 706 | if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE)) |
| 707 | goto error_brelse; |
| 708 | |
| 709 | device->generation = btrfs_super_generation(disk_super); |
| 710 | |
| 711 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 712 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 713 | fs_devices->seeding = 1; |
| 714 | } else { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 715 | if (bdev_read_only(bdev)) |
| 716 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
| 717 | else |
| 718 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | q = bdev_get_queue(bdev); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 722 | if (!blk_queue_nonrot(q)) |
| 723 | fs_devices->rotating = 1; |
| 724 | |
| 725 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 726 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 727 | device->mode = flags; |
| 728 | |
| 729 | fs_devices->open_devices++; |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 730 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 731 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 732 | fs_devices->rw_devices++; |
Anand Jain | b1b8e38 | 2018-01-22 14:49:37 -0800 | [diff] [blame] | 733 | list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list); |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 734 | } |
| 735 | brelse(bh); |
| 736 | |
| 737 | return 0; |
| 738 | |
| 739 | error_brelse: |
| 740 | brelse(bh); |
| 741 | blkdev_put(bdev, flags); |
| 742 | |
| 743 | return -EINVAL; |
| 744 | } |
| 745 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 746 | /* |
| 747 | * Add new device to list of registered devices |
| 748 | * |
| 749 | * Returns: |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 750 | * device pointer which was just added or updated when successful |
| 751 | * error pointer when failed |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 752 | */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 753 | static noinline struct btrfs_device *device_list_add(const char *path, |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 754 | struct btrfs_super_block *disk_super, |
| 755 | bool *new_device_added) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 756 | { |
| 757 | struct btrfs_device *device; |
| 758 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 759 | struct rcu_string *name; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 760 | u64 found_transid = btrfs_super_generation(disk_super); |
Anand Jain | 3acbcbf | 2018-01-18 22:02:36 +0800 | [diff] [blame] | 761 | u64 devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 762 | |
| 763 | fs_devices = find_fsid(disk_super->fsid); |
| 764 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 765 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 766 | if (IS_ERR(fs_devices)) |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 767 | return ERR_CAST(fs_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 768 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 769 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 770 | list_add(&fs_devices->fs_list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 771 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 772 | device = NULL; |
| 773 | } else { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 774 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 775 | device = find_device(fs_devices, devid, |
| 776 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 777 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 778 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 779 | if (!device) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 780 | if (fs_devices->opened) { |
| 781 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 782 | return ERR_PTR(-EBUSY); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 783 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 784 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 785 | device = btrfs_alloc_device(NULL, &devid, |
| 786 | disk_super->dev_item.uuid); |
| 787 | if (IS_ERR(device)) { |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 788 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 789 | /* we can safely leave the fs_devices entry around */ |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 790 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 791 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 792 | |
| 793 | name = rcu_string_strdup(path, GFP_NOFS); |
| 794 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 795 | btrfs_free_device(device); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 796 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 797 | return ERR_PTR(-ENOMEM); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 798 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 799 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 800 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 801 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 802 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 803 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 804 | device->fs_devices = fs_devices; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 805 | *new_device_added = true; |
Anand Jain | 327f18c | 2018-01-18 22:02:33 +0800 | [diff] [blame] | 806 | |
| 807 | if (disk_super->label[0]) |
| 808 | pr_info("BTRFS: device label %s devid %llu transid %llu %s\n", |
| 809 | disk_super->label, devid, found_transid, path); |
| 810 | else |
| 811 | pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n", |
| 812 | disk_super->fsid, devid, found_transid, path); |
| 813 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 814 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 815 | /* |
| 816 | * When FS is already mounted. |
| 817 | * 1. If you are here and if the device->name is NULL that |
| 818 | * means this device was missing at time of FS mount. |
| 819 | * 2. If you are here and if the device->name is different |
| 820 | * from 'path' that means either |
| 821 | * a. The same device disappeared and reappeared with |
| 822 | * different name. or |
| 823 | * b. The missing-disk-which-was-replaced, has |
| 824 | * reappeared now. |
| 825 | * |
| 826 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 827 | * and unintentional. |
| 828 | * |
| 829 | * Further in case of 1 and 2a above, the disk at 'path' |
| 830 | * would have missed some transaction when it was away and |
| 831 | * in case of 2a the stale bdev has to be updated as well. |
| 832 | * 2b must not be allowed at all time. |
| 833 | */ |
| 834 | |
| 835 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 836 | * For now, we do allow update to btrfs_fs_device through the |
| 837 | * btrfs dev scan cli after FS has been mounted. We're still |
| 838 | * tracking a problem where systems fail mount by subvolume id |
| 839 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 840 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 841 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 842 | /* |
| 843 | * That is if the FS is _not_ mounted and if you |
| 844 | * are here, that means there is more than one |
| 845 | * disk with same uuid and devid.We keep the one |
| 846 | * with larger generation number or the last-in if |
| 847 | * generation are equal. |
| 848 | */ |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 849 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 850 | return ERR_PTR(-EEXIST); |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 851 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 852 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 853 | name = rcu_string_strdup(path, GFP_NOFS); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 854 | if (!name) { |
| 855 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 856 | return ERR_PTR(-ENOMEM); |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 857 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 858 | rcu_string_free(device->name); |
| 859 | rcu_assign_pointer(device->name, name); |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 860 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 861 | fs_devices->missing_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 862 | clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 863 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 866 | /* |
| 867 | * Unmount does not free the btrfs_device struct but would zero |
| 868 | * generation along with most of the other members. So just update |
| 869 | * it back. We need it to pick the disk with largest generation |
| 870 | * (as above). |
| 871 | */ |
| 872 | if (!fs_devices->opened) |
| 873 | device->generation = found_transid; |
| 874 | |
Anand Jain | f2788d2 | 2018-01-18 22:02:34 +0800 | [diff] [blame] | 875 | fs_devices->total_devices = btrfs_super_num_devices(disk_super); |
| 876 | |
Anand Jain | 9c6d173 | 2018-05-29 14:10:20 +0800 | [diff] [blame] | 877 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | e124ece | 2018-01-18 22:02:35 +0800 | [diff] [blame] | 878 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 879 | } |
| 880 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 881 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 882 | { |
| 883 | struct btrfs_fs_devices *fs_devices; |
| 884 | struct btrfs_device *device; |
| 885 | struct btrfs_device *orig_dev; |
| 886 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 887 | fs_devices = alloc_fs_devices(orig->fsid); |
| 888 | if (IS_ERR(fs_devices)) |
| 889 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 890 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 891 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 892 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 893 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 894 | /* 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] | 895 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 896 | struct rcu_string *name; |
| 897 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 898 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 899 | orig_dev->uuid); |
| 900 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 901 | goto error; |
| 902 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 903 | /* |
| 904 | * This is ok to do without rcu read locked because we hold the |
| 905 | * uuid mutex so nothing we touch in here is going to disappear. |
| 906 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 907 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 908 | name = rcu_string_strdup(orig_dev->name->str, |
| 909 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 910 | if (!name) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 911 | btrfs_free_device(device); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 912 | goto error; |
| 913 | } |
| 914 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 915 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 916 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 917 | list_add(&device->dev_list, &fs_devices->devices); |
| 918 | device->fs_devices = fs_devices; |
| 919 | fs_devices->num_devices++; |
| 920 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 921 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 922 | return fs_devices; |
| 923 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 924 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 925 | free_fs_devices(fs_devices); |
| 926 | return ERR_PTR(-ENOMEM); |
| 927 | } |
| 928 | |
Anand Jain | 9b99b11 | 2018-02-27 12:41:59 +0800 | [diff] [blame] | 929 | /* |
| 930 | * After we have read the system tree and know devids belonging to |
| 931 | * this filesystem, remove the device which does not belong there. |
| 932 | */ |
| 933 | 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] | 934 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 935 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 936 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 937 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 938 | mutex_lock(&uuid_mutex); |
| 939 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 940 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 941 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 942 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 943 | &device->dev_state)) { |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 944 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 945 | &device->dev_state) && |
| 946 | (!latest_dev || |
| 947 | device->generation > latest_dev->generation)) { |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 948 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 949 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 950 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 951 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 952 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 953 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 954 | /* |
| 955 | * In the first step, keep the device which has |
| 956 | * the correct fsid and the devid that is used |
| 957 | * for the dev_replace procedure. |
| 958 | * In the second step, the dev_replace state is |
| 959 | * read from the device tree and it is known |
| 960 | * whether the procedure is really active or |
| 961 | * not, which means whether this device is |
| 962 | * used or whether it should be removed. |
| 963 | */ |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 964 | if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 965 | &device->dev_state)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 966 | continue; |
| 967 | } |
| 968 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 969 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 970 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 971 | device->bdev = NULL; |
| 972 | fs_devices->open_devices--; |
| 973 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 974 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 975 | list_del_init(&device->dev_alloc_list); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 976 | clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 977 | if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, |
| 978 | &device->dev_state)) |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 979 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 980 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 981 | list_del_init(&device->dev_list); |
| 982 | fs_devices->num_devices--; |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 983 | btrfs_free_device(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 984 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 985 | |
| 986 | if (fs_devices->seed) { |
| 987 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 988 | goto again; |
| 989 | } |
| 990 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 991 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 992 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 993 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 994 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 995 | |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 996 | static void free_device_rcu(struct rcu_head *head) |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 997 | { |
| 998 | struct btrfs_device *device; |
| 999 | |
| 1000 | device = container_of(head, struct btrfs_device, rcu); |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 1001 | btrfs_free_device(device); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1004 | static void btrfs_close_bdev(struct btrfs_device *device) |
| 1005 | { |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1006 | if (!device->bdev) |
| 1007 | return; |
| 1008 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1009 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1010 | sync_blockdev(device->bdev); |
| 1011 | invalidate_bdev(device->bdev); |
| 1012 | } |
| 1013 | |
David Sterba | 08ffcae | 2017-06-19 16:55:35 +0200 | [diff] [blame] | 1014 | blkdev_put(device->bdev, device->mode); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1017 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1018 | { |
| 1019 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 1020 | struct btrfs_device *new_device; |
| 1021 | struct rcu_string *name; |
| 1022 | |
| 1023 | if (device->bdev) |
| 1024 | fs_devices->open_devices--; |
| 1025 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1026 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1027 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 1028 | list_del_init(&device->dev_alloc_list); |
| 1029 | fs_devices->rw_devices--; |
| 1030 | } |
| 1031 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1032 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1033 | fs_devices->missing_devices--; |
| 1034 | |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1035 | btrfs_close_bdev(device); |
| 1036 | |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1037 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 1038 | device->uuid); |
| 1039 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 1040 | |
| 1041 | /* Safe because we are under uuid_mutex */ |
| 1042 | if (device->name) { |
| 1043 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 1044 | BUG_ON(!name); /* -ENOMEM */ |
| 1045 | rcu_assign_pointer(new_device->name, name); |
| 1046 | } |
| 1047 | |
| 1048 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 1049 | new_device->fs_devices = device->fs_devices; |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1050 | |
| 1051 | call_rcu(&device->rcu, free_device_rcu); |
Anand Jain | f448341a | 2016-06-14 18:55:25 +0800 | [diff] [blame] | 1052 | } |
| 1053 | |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1054 | static int close_fs_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1055 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1056 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1057 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1058 | if (--fs_devices->opened > 0) |
| 1059 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1060 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1061 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 1062 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 959b1c0 | 2018-06-29 08:26:05 +0300 | [diff] [blame] | 1063 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1064 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1065 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1066 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1067 | WARN_ON(fs_devices->open_devices); |
| 1068 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1069 | fs_devices->opened = 0; |
| 1070 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1071 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1072 | return 0; |
| 1073 | } |
| 1074 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1075 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 1076 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1077 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1078 | int ret; |
| 1079 | |
| 1080 | mutex_lock(&uuid_mutex); |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1081 | ret = close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1082 | if (!fs_devices->opened) { |
| 1083 | seed_devices = fs_devices->seed; |
| 1084 | fs_devices->seed = NULL; |
| 1085 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1086 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1087 | |
| 1088 | while (seed_devices) { |
| 1089 | fs_devices = seed_devices; |
| 1090 | seed_devices = fs_devices->seed; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1091 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1092 | free_fs_devices(fs_devices); |
| 1093 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1094 | return ret; |
| 1095 | } |
| 1096 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1097 | static int open_fs_devices(struct btrfs_fs_devices *fs_devices, |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1098 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1099 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1100 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1101 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1102 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1103 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1104 | flags |= FMODE_EXCL; |
| 1105 | |
Anand Jain | f117e29 | 2018-04-12 10:29:26 +0800 | [diff] [blame] | 1106 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 1107 | /* Just open everything we can; ignore failures here */ |
Anand Jain | 0fb08bc | 2017-11-09 23:45:24 +0800 | [diff] [blame] | 1108 | if (btrfs_open_one_device(fs_devices, device, flags, holder)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1109 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1110 | |
Anand Jain | 9f050db | 2017-11-09 23:45:25 +0800 | [diff] [blame] | 1111 | if (!latest_dev || |
| 1112 | device->generation > latest_dev->generation) |
| 1113 | latest_dev = device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1114 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1115 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 1116 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1117 | goto out; |
| 1118 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1119 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 1120 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1121 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 1122 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1123 | return ret; |
| 1124 | } |
| 1125 | |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1126 | static int devid_cmp(void *priv, struct list_head *a, struct list_head *b) |
| 1127 | { |
| 1128 | struct btrfs_device *dev1, *dev2; |
| 1129 | |
| 1130 | dev1 = list_entry(a, struct btrfs_device, dev_list); |
| 1131 | dev2 = list_entry(b, struct btrfs_device, dev_list); |
| 1132 | |
| 1133 | if (dev1->devid < dev2->devid) |
| 1134 | return -1; |
| 1135 | else if (dev1->devid > dev2->devid) |
| 1136 | return 1; |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1140 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1141 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1142 | { |
| 1143 | int ret; |
| 1144 | |
David Sterba | f5194e3 | 2018-06-19 17:09:47 +0200 | [diff] [blame] | 1145 | lockdep_assert_held(&uuid_mutex); |
| 1146 | |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1147 | mutex_lock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1148 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1149 | fs_devices->opened++; |
| 1150 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1151 | } else { |
Anand Jain | f8e10cd | 2018-01-22 14:49:36 -0800 | [diff] [blame] | 1152 | list_sort(NULL, &fs_devices->devices, devid_cmp); |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 1153 | ret = open_fs_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1154 | } |
Anand Jain | 542c590 | 2018-04-12 10:29:34 +0800 | [diff] [blame] | 1155 | mutex_unlock(&fs_devices->device_list_mutex); |
| 1156 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1157 | return ret; |
| 1158 | } |
| 1159 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1160 | static void btrfs_release_disk_super(struct page *page) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1161 | { |
| 1162 | kunmap(page); |
| 1163 | put_page(page); |
| 1164 | } |
| 1165 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1166 | static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 1167 | struct page **page, |
| 1168 | struct btrfs_super_block **disk_super) |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1169 | { |
| 1170 | void *p; |
| 1171 | pgoff_t index; |
| 1172 | |
| 1173 | /* make sure our super fits in the device */ |
| 1174 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1175 | return 1; |
| 1176 | |
| 1177 | /* make sure our super fits in the page */ |
| 1178 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1179 | return 1; |
| 1180 | |
| 1181 | /* make sure our super doesn't straddle pages on disk */ |
| 1182 | index = bytenr >> PAGE_SHIFT; |
| 1183 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1184 | return 1; |
| 1185 | |
| 1186 | /* pull in the page with our super */ |
| 1187 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1188 | index, GFP_KERNEL); |
| 1189 | |
| 1190 | if (IS_ERR_OR_NULL(*page)) |
| 1191 | return 1; |
| 1192 | |
| 1193 | p = kmap(*page); |
| 1194 | |
| 1195 | /* align our pointer to the offset of the super block */ |
| 1196 | *disk_super = p + (bytenr & ~PAGE_MASK); |
| 1197 | |
| 1198 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1199 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1200 | btrfs_release_disk_super(*page); |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
| 1204 | if ((*disk_super)->label[0] && |
| 1205 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1206 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1207 | |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1211 | /* |
| 1212 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1213 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1214 | * is read via pagecache |
| 1215 | */ |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1216 | struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, |
| 1217 | void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1218 | { |
| 1219 | struct btrfs_super_block *disk_super; |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1220 | bool new_device_added = false; |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1221 | struct btrfs_device *device = NULL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1222 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1223 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1224 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1225 | |
David Sterba | 899f930 | 2018-06-19 16:37:36 +0200 | [diff] [blame] | 1226 | lockdep_assert_held(&uuid_mutex); |
| 1227 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1228 | /* |
| 1229 | * we would like to check all the supers, but that would make |
| 1230 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1231 | * So, we need to add a special mount option to scan for |
| 1232 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1233 | */ |
| 1234 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1235 | flags |= FMODE_EXCL; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1236 | |
| 1237 | bdev = blkdev_get_by_path(path, flags, holder); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1238 | if (IS_ERR(bdev)) |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1239 | return ERR_CAST(bdev); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1240 | |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1241 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) { |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1242 | device = ERR_PTR(-EINVAL); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1243 | goto error_bdev_put; |
Anand Jain | 05a5c55 | 2017-12-15 15:40:16 +0800 | [diff] [blame] | 1244 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1245 | |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1246 | device = device_list_add(path, disk_super, &new_device_added); |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1247 | if (!IS_ERR(device)) { |
Anand Jain | 4306a97 | 2018-05-29 12:28:37 +0800 | [diff] [blame] | 1248 | if (new_device_added) |
| 1249 | btrfs_free_stale_devices(path, device); |
| 1250 | } |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1251 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1252 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1253 | |
| 1254 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1255 | blkdev_put(bdev, flags); |
Anand Jain | b6ed73b | 2018-04-12 10:29:24 +0800 | [diff] [blame] | 1256 | |
Gu Jinxiang | 36350e9 | 2018-07-12 14:23:16 +0800 | [diff] [blame] | 1257 | return device; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1258 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1259 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1260 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1261 | struct btrfs_device *device, |
| 1262 | u64 *start, u64 len) |
| 1263 | { |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 1264 | struct btrfs_fs_info *fs_info = device->fs_info; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1265 | struct extent_map *em; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1266 | struct list_head *search_list = &fs_info->pinned_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1267 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1268 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1269 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1270 | if (transaction) |
| 1271 | search_list = &transaction->pending_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1272 | again: |
| 1273 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1274 | struct map_lookup *map; |
| 1275 | int i; |
| 1276 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 1277 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1278 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1279 | u64 end; |
| 1280 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1281 | if (map->stripes[i].dev != device) |
| 1282 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1283 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1284 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1285 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1286 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1287 | /* |
| 1288 | * Make sure that while processing the pinned list we do |
| 1289 | * not override our *start with a lower value, because |
| 1290 | * we can have pinned chunks that fall within this |
| 1291 | * device hole and that have lower physical addresses |
| 1292 | * than the pending chunks we processed before. If we |
| 1293 | * do not take this special care we can end up getting |
| 1294 | * 2 pending chunks that start at the same physical |
| 1295 | * device offsets because the end offset of a pinned |
| 1296 | * chunk can be equal to the start offset of some |
| 1297 | * pending chunk. |
| 1298 | */ |
| 1299 | end = map->stripes[i].physical + em->orig_block_len; |
| 1300 | if (end > *start) { |
| 1301 | *start = end; |
| 1302 | ret = 1; |
| 1303 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1304 | } |
| 1305 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1306 | if (search_list != &fs_info->pinned_chunks) { |
| 1307 | search_list = &fs_info->pinned_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1308 | goto again; |
| 1309 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1310 | |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
| 1314 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1315 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1316 | * find_free_dev_extent_start - find free space in the specified device |
| 1317 | * @device: the device which we search the free space in |
| 1318 | * @num_bytes: the size of the free space that we need |
| 1319 | * @search_start: the position from which to begin the search |
| 1320 | * @start: store the start of the free space. |
| 1321 | * @len: the size of the free space. that we find, or the size |
| 1322 | * 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] | 1323 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1324 | * this uses a pretty simple search, the expectation is that it is |
| 1325 | * called very infrequently and that a given device has a small number |
| 1326 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1327 | * |
| 1328 | * @start is used to store the start of the free space if we find. But if we |
| 1329 | * don't find suitable free space, it will be used to store the start position |
| 1330 | * of the max free space. |
| 1331 | * |
| 1332 | * @len is used to store the size of the free space that we find. |
| 1333 | * But if we don't find suitable free space, it is used to store the size of |
| 1334 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1335 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1336 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
| 1337 | struct btrfs_device *device, u64 num_bytes, |
| 1338 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1339 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1340 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1341 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1342 | struct btrfs_key key; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1343 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1344 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1345 | u64 hole_size; |
| 1346 | u64 max_hole_start; |
| 1347 | u64 max_hole_size; |
| 1348 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1349 | u64 search_end = device->total_bytes; |
| 1350 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1351 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1352 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1353 | |
| 1354 | /* |
| 1355 | * We don't want to overwrite the superblock on the drive nor any area |
| 1356 | * used by the boot loader (grub for example), so we make sure to start |
| 1357 | * at an offset of at least 1MB. |
| 1358 | */ |
David Sterba | 0d0c71b | 2017-06-15 01:30:06 +0200 | [diff] [blame] | 1359 | search_start = max_t(u64, search_start, SZ_1M); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1360 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1361 | path = btrfs_alloc_path(); |
| 1362 | if (!path) |
| 1363 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1364 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1365 | max_hole_start = search_start; |
| 1366 | max_hole_size = 0; |
| 1367 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1368 | again: |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1369 | if (search_start >= search_end || |
| 1370 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1371 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1372 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1375 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1376 | path->search_commit_root = 1; |
| 1377 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1378 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1379 | key.objectid = device->devid; |
| 1380 | key.offset = search_start; |
| 1381 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1382 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1383 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1384 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1385 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1386 | if (ret > 0) { |
| 1387 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1388 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1389 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1390 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1391 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1392 | while (1) { |
| 1393 | l = path->nodes[0]; |
| 1394 | slot = path->slots[0]; |
| 1395 | if (slot >= btrfs_header_nritems(l)) { |
| 1396 | ret = btrfs_next_leaf(root, path); |
| 1397 | if (ret == 0) |
| 1398 | continue; |
| 1399 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1400 | goto out; |
| 1401 | |
| 1402 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1403 | } |
| 1404 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1405 | |
| 1406 | if (key.objectid < device->devid) |
| 1407 | goto next; |
| 1408 | |
| 1409 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1410 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1411 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1412 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1414 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1415 | if (key.offset > search_start) { |
| 1416 | hole_size = key.offset - search_start; |
| 1417 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1418 | /* |
| 1419 | * Have to check before we set max_hole_start, otherwise |
| 1420 | * we could end up sending back this offset anyway. |
| 1421 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1422 | if (contains_pending_extent(transaction, device, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1423 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1424 | hole_size)) { |
| 1425 | if (key.offset >= search_start) { |
| 1426 | hole_size = key.offset - search_start; |
| 1427 | } else { |
| 1428 | WARN_ON_ONCE(1); |
| 1429 | hole_size = 0; |
| 1430 | } |
| 1431 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1432 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1433 | if (hole_size > max_hole_size) { |
| 1434 | max_hole_start = search_start; |
| 1435 | max_hole_size = hole_size; |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | * If this free space is greater than which we need, |
| 1440 | * it must be the max free space that we have found |
| 1441 | * until now, so max_hole_start must point to the start |
| 1442 | * of this free space and the length of this free space |
| 1443 | * is stored in max_hole_size. Thus, we return |
| 1444 | * max_hole_start and max_hole_size and go back to the |
| 1445 | * caller. |
| 1446 | */ |
| 1447 | if (hole_size >= num_bytes) { |
| 1448 | ret = 0; |
| 1449 | goto out; |
| 1450 | } |
| 1451 | } |
| 1452 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1453 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1454 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1455 | dev_extent); |
| 1456 | if (extent_end > search_start) |
| 1457 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1458 | next: |
| 1459 | path->slots[0]++; |
| 1460 | cond_resched(); |
| 1461 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1462 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1463 | /* |
| 1464 | * At this point, search_start should be the end of |
| 1465 | * allocated dev extents, and when shrinking the device, |
| 1466 | * search_end may be smaller than search_start. |
| 1467 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1468 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1469 | hole_size = search_end - search_start; |
| 1470 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1471 | if (contains_pending_extent(transaction, device, &search_start, |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1472 | hole_size)) { |
| 1473 | btrfs_release_path(path); |
| 1474 | goto again; |
| 1475 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1476 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1477 | if (hole_size > max_hole_size) { |
| 1478 | max_hole_start = search_start; |
| 1479 | max_hole_size = hole_size; |
| 1480 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1481 | } |
| 1482 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1483 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1484 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1485 | ret = -ENOSPC; |
| 1486 | else |
| 1487 | ret = 0; |
| 1488 | |
| 1489 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1490 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1491 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1492 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1493 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1494 | return ret; |
| 1495 | } |
| 1496 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1497 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1498 | struct btrfs_device *device, u64 num_bytes, |
| 1499 | u64 *start, u64 *len) |
| 1500 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1501 | /* FIXME use last free of some kind */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1502 | return find_free_dev_extent_start(trans->transaction, device, |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1503 | num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1506 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1507 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1508 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1509 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1510 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1511 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1512 | int ret; |
| 1513 | struct btrfs_path *path; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1514 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1515 | struct btrfs_key found_key; |
| 1516 | struct extent_buffer *leaf = NULL; |
| 1517 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1518 | |
| 1519 | path = btrfs_alloc_path(); |
| 1520 | if (!path) |
| 1521 | return -ENOMEM; |
| 1522 | |
| 1523 | key.objectid = device->devid; |
| 1524 | key.offset = start; |
| 1525 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1526 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1527 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1528 | if (ret > 0) { |
| 1529 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1530 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1531 | if (ret) |
| 1532 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1533 | leaf = path->nodes[0]; |
| 1534 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1535 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1536 | struct btrfs_dev_extent); |
| 1537 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1538 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1539 | key = found_key; |
| 1540 | btrfs_release_path(path); |
| 1541 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1542 | } else if (ret == 0) { |
| 1543 | leaf = path->nodes[0]; |
| 1544 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1545 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1546 | } else { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1547 | btrfs_handle_fs_error(fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1548 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1549 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1550 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1551 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1552 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1553 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1554 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1555 | btrfs_handle_fs_error(fs_info, ret, |
| 1556 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1557 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1558 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1559 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1560 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1561 | btrfs_free_path(path); |
| 1562 | return ret; |
| 1563 | } |
| 1564 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1565 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1566 | struct btrfs_device *device, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1567 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1568 | { |
| 1569 | int ret; |
| 1570 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1571 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 1572 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1573 | struct btrfs_dev_extent *extent; |
| 1574 | struct extent_buffer *leaf; |
| 1575 | struct btrfs_key key; |
| 1576 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1577 | 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] | 1578 | WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1579 | path = btrfs_alloc_path(); |
| 1580 | if (!path) |
| 1581 | return -ENOMEM; |
| 1582 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1583 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1584 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1585 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1586 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1587 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1588 | if (ret) |
| 1589 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1590 | |
| 1591 | leaf = path->nodes[0]; |
| 1592 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1593 | struct btrfs_dev_extent); |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 1594 | btrfs_set_dev_extent_chunk_tree(leaf, extent, |
| 1595 | BTRFS_CHUNK_TREE_OBJECTID); |
Nikolay Borisov | 0ca00af | 2017-08-18 17:58:22 +0300 | [diff] [blame] | 1596 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, |
| 1597 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1598 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1599 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1600 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1601 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1602 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1603 | btrfs_free_path(path); |
| 1604 | return ret; |
| 1605 | } |
| 1606 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1607 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1608 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1609 | struct extent_map_tree *em_tree; |
| 1610 | struct extent_map *em; |
| 1611 | struct rb_node *n; |
| 1612 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1613 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1614 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1615 | read_lock(&em_tree->lock); |
| 1616 | n = rb_last(&em_tree->map); |
| 1617 | if (n) { |
| 1618 | em = rb_entry(n, struct extent_map, rb_node); |
| 1619 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1620 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1621 | read_unlock(&em_tree->lock); |
| 1622 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1623 | return ret; |
| 1624 | } |
| 1625 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1626 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1627 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1628 | { |
| 1629 | int ret; |
| 1630 | struct btrfs_key key; |
| 1631 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1632 | struct btrfs_path *path; |
| 1633 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1634 | path = btrfs_alloc_path(); |
| 1635 | if (!path) |
| 1636 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1637 | |
| 1638 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1639 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1640 | key.offset = (u64)-1; |
| 1641 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1642 | 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] | 1643 | if (ret < 0) |
| 1644 | goto error; |
| 1645 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1646 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1647 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1648 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1649 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1650 | BTRFS_DEV_ITEM_KEY); |
| 1651 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1652 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1653 | } else { |
| 1654 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1655 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1656 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1657 | } |
| 1658 | ret = 0; |
| 1659 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1660 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1661 | return ret; |
| 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * the device information is stored in the chunk root |
| 1666 | * the btrfs_device struct should be fully filled in |
| 1667 | */ |
Anand Jain | c74a0b0 | 2017-11-06 16:36:15 +0800 | [diff] [blame] | 1668 | static int btrfs_add_dev_item(struct btrfs_trans_handle *trans, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1669 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1670 | { |
| 1671 | int ret; |
| 1672 | struct btrfs_path *path; |
| 1673 | struct btrfs_dev_item *dev_item; |
| 1674 | struct extent_buffer *leaf; |
| 1675 | struct btrfs_key key; |
| 1676 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1677 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1678 | path = btrfs_alloc_path(); |
| 1679 | if (!path) |
| 1680 | return -ENOMEM; |
| 1681 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1682 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1683 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1684 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1685 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1686 | ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path, |
| 1687 | &key, sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1688 | if (ret) |
| 1689 | goto out; |
| 1690 | |
| 1691 | leaf = path->nodes[0]; |
| 1692 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1693 | |
| 1694 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1695 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1696 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1697 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1698 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1699 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1700 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1701 | btrfs_device_get_disk_total_bytes(device)); |
| 1702 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1703 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1704 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1705 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1706 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1707 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1708 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1709 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1710 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1711 | ptr = btrfs_device_fsid(dev_item); |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 1712 | write_extent_buffer(leaf, trans->fs_info->fsid, ptr, BTRFS_FSID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1713 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1714 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1715 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1716 | out: |
| 1717 | btrfs_free_path(path); |
| 1718 | return ret; |
| 1719 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1720 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1721 | /* |
| 1722 | * Function to update ctime/mtime for a given device path. |
| 1723 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1724 | */ |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1725 | static void update_dev_time(const char *path_name) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1726 | { |
| 1727 | struct file *filp; |
| 1728 | |
| 1729 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1730 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1731 | return; |
| 1732 | file_update_time(filp); |
| 1733 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1734 | } |
| 1735 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 1736 | static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1737 | struct btrfs_device *device) |
| 1738 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 1739 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1740 | int ret; |
| 1741 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1742 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1743 | struct btrfs_trans_handle *trans; |
| 1744 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1745 | path = btrfs_alloc_path(); |
| 1746 | if (!path) |
| 1747 | return -ENOMEM; |
| 1748 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1749 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1750 | if (IS_ERR(trans)) { |
| 1751 | btrfs_free_path(path); |
| 1752 | return PTR_ERR(trans); |
| 1753 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1754 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1755 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1756 | key.offset = device->devid; |
| 1757 | |
| 1758 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1759 | if (ret) { |
| 1760 | if (ret > 0) |
| 1761 | ret = -ENOENT; |
| 1762 | btrfs_abort_transaction(trans, ret); |
| 1763 | btrfs_end_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1764 | goto out; |
| 1765 | } |
| 1766 | |
| 1767 | ret = btrfs_del_item(trans, root, path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1768 | if (ret) { |
| 1769 | btrfs_abort_transaction(trans, ret); |
| 1770 | btrfs_end_transaction(trans); |
| 1771 | } |
| 1772 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1773 | out: |
| 1774 | btrfs_free_path(path); |
Nikolay Borisov | 5e9f2ad | 2017-10-23 09:58:46 +0300 | [diff] [blame] | 1775 | if (!ret) |
| 1776 | ret = btrfs_commit_transaction(trans); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1777 | return ret; |
| 1778 | } |
| 1779 | |
David Sterba | 3cc31a0 | 2016-02-15 16:00:26 +0100 | [diff] [blame] | 1780 | /* |
| 1781 | * Verify that @num_devices satisfies the RAID profile constraints in the whole |
| 1782 | * filesystem. It's up to the caller to adjust that number regarding eg. device |
| 1783 | * replace. |
| 1784 | */ |
| 1785 | static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info, |
| 1786 | u64 num_devices) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1787 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1788 | u64 all_avail; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1789 | unsigned seq; |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1790 | int i; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1791 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1792 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1793 | seq = read_seqbegin(&fs_info->profiles_lock); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1794 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1795 | all_avail = fs_info->avail_data_alloc_bits | |
| 1796 | fs_info->avail_system_alloc_bits | |
| 1797 | fs_info->avail_metadata_alloc_bits; |
| 1798 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1799 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1800 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Anand Jain | 41a6e89 | 2018-04-25 19:01:43 +0800 | [diff] [blame] | 1801 | if (!(all_avail & btrfs_raid_array[i].bg_flag)) |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1802 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1803 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1804 | if (num_devices < btrfs_raid_array[i].devs_min) { |
Anand Jain | f9fbcaa | 2018-04-25 19:01:44 +0800 | [diff] [blame] | 1805 | int ret = btrfs_raid_array[i].mindev_error; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1806 | |
David Sterba | 418775a | 2016-02-15 16:28:14 +0100 | [diff] [blame] | 1807 | if (ret) |
| 1808 | return ret; |
| 1809 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1813 | } |
| 1814 | |
Omar Sandoval | c9162bd | 2017-08-22 23:46:04 -0700 | [diff] [blame] | 1815 | static struct btrfs_device * btrfs_find_next_active_device( |
| 1816 | struct btrfs_fs_devices *fs_devs, struct btrfs_device *device) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1817 | { |
| 1818 | struct btrfs_device *next_device; |
| 1819 | |
| 1820 | list_for_each_entry(next_device, &fs_devs->devices, dev_list) { |
| 1821 | if (next_device != device && |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1822 | !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state) |
| 1823 | && next_device->bdev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1824 | return next_device; |
| 1825 | } |
| 1826 | |
| 1827 | return NULL; |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | * Helper function to check if the given device is part of s_bdev / latest_bdev |
| 1832 | * and replace it with the provided or the next active device, in the context |
| 1833 | * where this function called, there should be always be another device (or |
| 1834 | * this_dev) which is active. |
| 1835 | */ |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame^] | 1836 | void btrfs_assign_next_active_device(struct btrfs_device *device, |
| 1837 | struct btrfs_device *this_dev) |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1838 | { |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame^] | 1839 | struct btrfs_fs_info *fs_info = device->fs_info; |
Anand Jain | 88acff6 | 2016-05-03 17:44:43 +0800 | [diff] [blame] | 1840 | struct btrfs_device *next_device; |
| 1841 | |
| 1842 | if (this_dev) |
| 1843 | next_device = this_dev; |
| 1844 | else |
| 1845 | next_device = btrfs_find_next_active_device(fs_info->fs_devices, |
| 1846 | device); |
| 1847 | ASSERT(next_device); |
| 1848 | |
| 1849 | if (fs_info->sb->s_bdev && |
| 1850 | (fs_info->sb->s_bdev == device->bdev)) |
| 1851 | fs_info->sb->s_bdev = next_device->bdev; |
| 1852 | |
| 1853 | if (fs_info->fs_devices->latest_bdev == device->bdev) |
| 1854 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1855 | } |
| 1856 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 1857 | int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path, |
| 1858 | u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1859 | { |
| 1860 | struct btrfs_device *device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1861 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1862 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1863 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1864 | int ret = 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1865 | |
| 1866 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1867 | |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1868 | num_devices = fs_devices->num_devices; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 1869 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1870 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1871 | WARN_ON(num_devices < 1); |
| 1872 | num_devices--; |
| 1873 | } |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 1874 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1875 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1876 | ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1877 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1878 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1879 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1880 | ret = btrfs_find_device_by_devspec(fs_info, devid, device_path, |
| 1881 | &device); |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1882 | if (ret) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1883 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1884 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 1885 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1886 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1887 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1888 | } |
| 1889 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1890 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
| 1891 | fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1892 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1893 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1894 | } |
| 1895 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1896 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 1897 | mutex_lock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1898 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1899 | device->fs_devices->rw_devices--; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 1900 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1901 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1902 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1903 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1904 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1905 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1906 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1907 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1908 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1909 | /* |
| 1910 | * TODO: the superblock still includes this device in its num_devices |
| 1911 | * counter although write_all_supers() is not locked out. This |
| 1912 | * could give a filesystem state which requires a degraded mount. |
| 1913 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1914 | ret = btrfs_rm_dev_item(fs_info, device); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1915 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1916 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1917 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 1918 | clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1919 | btrfs_scrub_cancel_dev(fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1920 | |
| 1921 | /* |
| 1922 | * the device list mutex makes sure that we don't change |
| 1923 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1924 | * the device supers. Whoever is writing all supers, should |
| 1925 | * lock the device list mutex before getting the number of |
| 1926 | * devices in the super block (super_copy). Conversely, |
| 1927 | * whoever updates the number of devices in the super block |
| 1928 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1929 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1930 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 1931 | /* |
| 1932 | * In normal cases the cur_devices == fs_devices. But in case |
| 1933 | * of deleting a seed device, the cur_devices should point to |
| 1934 | * its own fs_devices listed under the fs_devices->seed. |
| 1935 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1936 | cur_devices = device->fs_devices; |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1937 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1938 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1939 | |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 1940 | cur_devices->num_devices--; |
| 1941 | cur_devices->total_devices--; |
Anand Jain | b4993e6 | 2018-07-03 17:07:23 +0800 | [diff] [blame] | 1942 | /* Update total_devices of the parent fs_devices if it's seed */ |
| 1943 | if (cur_devices != fs_devices) |
| 1944 | fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1945 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 1946 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 1947 | cur_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1948 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame^] | 1949 | btrfs_assign_next_active_device(device, NULL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1950 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1951 | if (device->bdev) { |
Anand Jain | 41a52a0 | 2018-04-12 10:29:31 +0800 | [diff] [blame] | 1952 | cur_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1953 | /* remove sysfs entry */ |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1954 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1955 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1956 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1957 | num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1; |
| 1958 | btrfs_set_super_num_devices(fs_info->super_copy, num_devices); |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1959 | mutex_unlock(&fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1960 | |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 1961 | /* |
| 1962 | * at this point, the device is zero sized and detached from |
| 1963 | * the devices list. All that's left is to zero out the old |
| 1964 | * supers and free the device. |
| 1965 | */ |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1966 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 1967 | btrfs_scratch_superblocks(device->bdev, device->name->str); |
| 1968 | |
| 1969 | btrfs_close_bdev(device); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 1970 | call_rcu(&device->rcu, free_device_rcu); |
Jeff Mahoney | cea67ab | 2016-09-20 08:50:21 -0400 | [diff] [blame] | 1971 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1972 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1973 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1974 | if (fs_devices->seed == cur_devices) { |
| 1975 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1976 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1977 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1978 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1979 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1980 | cur_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 1981 | close_fs_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1982 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1983 | } |
| 1984 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1985 | out: |
| 1986 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1987 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1988 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1989 | error_undo: |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 1990 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 1991 | mutex_lock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1992 | list_add(&device->dev_alloc_list, |
Anand Jain | b518519 | 2018-04-12 10:29:30 +0800 | [diff] [blame] | 1993 | &fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1994 | device->fs_devices->rw_devices++; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 1995 | mutex_unlock(&fs_info->chunk_mutex); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1996 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1997 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1998 | } |
| 1999 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2000 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2001 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2002 | struct btrfs_fs_devices *fs_devices; |
| 2003 | |
Nikolay Borisov | 68a9db5 | 2018-07-20 19:37:48 +0300 | [diff] [blame] | 2004 | lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2005 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 2006 | /* |
| 2007 | * in case of fs with no seed, srcdev->fs_devices will point |
| 2008 | * to fs_devices of fs_info. However when the dev being replaced is |
| 2009 | * a seed dev it will point to the seed's local fs_devices. In short |
| 2010 | * srcdev will have its correct fs_devices in both the cases. |
| 2011 | */ |
| 2012 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2013 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2014 | list_del_rcu(&srcdev->dev_list); |
David Sterba | 619c47f | 2017-06-19 14:14:22 +0200 | [diff] [blame] | 2015 | list_del(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2016 | fs_devices->num_devices--; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 2017 | if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state)) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2018 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2019 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2020 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2021 | fs_devices->rw_devices--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2022 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2023 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2024 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 2028 | struct btrfs_device *srcdev) |
| 2029 | { |
| 2030 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2031 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2032 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) { |
Anand Jain | 48b3b9d | 2016-04-12 21:36:16 +0800 | [diff] [blame] | 2033 | /* zero out the old super if it is writable */ |
| 2034 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
| 2035 | } |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2036 | |
| 2037 | btrfs_close_bdev(srcdev); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2038 | call_rcu(&srcdev->rcu, free_device_rcu); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2039 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2040 | /* if this is no devs we rather delete the fs_devices */ |
| 2041 | if (!fs_devices->num_devices) { |
| 2042 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2043 | |
Anand Jain | 6dd38f8 | 2017-10-17 06:53:50 +0800 | [diff] [blame] | 2044 | /* |
| 2045 | * On a mounted FS, num_devices can't be zero unless it's a |
| 2046 | * seed. In case of a seed device being replaced, the replace |
| 2047 | * target added to the sprout FS, so there will be no more |
| 2048 | * device left under the seed FS. |
| 2049 | */ |
| 2050 | ASSERT(fs_devices->seeding); |
| 2051 | |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2052 | tmp_fs_devices = fs_info->fs_devices; |
| 2053 | while (tmp_fs_devices) { |
| 2054 | if (tmp_fs_devices->seed == fs_devices) { |
| 2055 | tmp_fs_devices->seed = fs_devices->seed; |
| 2056 | break; |
| 2057 | } |
| 2058 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2059 | } |
| 2060 | fs_devices->seed = NULL; |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 2061 | close_fs_devices(fs_devices); |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2062 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2063 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 2067 | struct btrfs_device *tgtdev) |
| 2068 | { |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2069 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2070 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2071 | WARN_ON(!tgtdev); |
| 2072 | mutex_lock(&fs_devices->device_list_mutex); |
| 2073 | |
| 2074 | btrfs_sysfs_rm_device_link(fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2075 | |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2076 | if (tgtdev->bdev) |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2077 | fs_devices->open_devices--; |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2078 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2079 | fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2080 | |
Nikolay Borisov | d6507cf | 2018-07-20 19:37:50 +0300 | [diff] [blame^] | 2081 | btrfs_assign_next_active_device(tgtdev, NULL); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2082 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2083 | list_del_rcu(&tgtdev->dev_list); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2084 | |
Anand Jain | d9a071f | 2018-04-12 10:29:38 +0800 | [diff] [blame] | 2085 | mutex_unlock(&fs_devices->device_list_mutex); |
Anand Jain | 779bf3fe | 2016-04-18 16:51:23 +0800 | [diff] [blame] | 2086 | |
| 2087 | /* |
| 2088 | * The update_dev_time() with in btrfs_scratch_superblocks() |
| 2089 | * may lead to a call to btrfs_show_devname() which will try |
| 2090 | * to hold device_list_mutex. And here this device |
| 2091 | * is already out of device list, so we don't have to hold |
| 2092 | * the device_list_mutex lock. |
| 2093 | */ |
| 2094 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Anand Jain | 1423881 | 2016-07-22 06:04:53 +0800 | [diff] [blame] | 2095 | |
| 2096 | btrfs_close_bdev(tgtdev); |
David Sterba | f06c596 | 2017-06-06 17:08:23 +0200 | [diff] [blame] | 2097 | call_rcu(&tgtdev->rcu, free_device_rcu); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2100 | static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info, |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2101 | const char *device_path, |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2102 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2103 | { |
| 2104 | int ret = 0; |
| 2105 | struct btrfs_super_block *disk_super; |
| 2106 | u64 devid; |
| 2107 | u8 *dev_uuid; |
| 2108 | struct block_device *bdev; |
| 2109 | struct buffer_head *bh; |
| 2110 | |
| 2111 | *device = NULL; |
| 2112 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2113 | fs_info->bdev_holder, 0, &bdev, &bh); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2114 | if (ret) |
| 2115 | return ret; |
| 2116 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2117 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2118 | dev_uuid = disk_super->dev_item.uuid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2119 | *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2120 | brelse(bh); |
| 2121 | if (!*device) |
| 2122 | ret = -ENOENT; |
| 2123 | blkdev_put(bdev, FMODE_READ); |
| 2124 | return ret; |
| 2125 | } |
| 2126 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2127 | int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info, |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2128 | const char *device_path, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2129 | struct btrfs_device **device) |
| 2130 | { |
| 2131 | *device = NULL; |
| 2132 | if (strcmp(device_path, "missing") == 0) { |
| 2133 | struct list_head *devices; |
| 2134 | struct btrfs_device *tmp; |
| 2135 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2136 | devices = &fs_info->fs_devices->devices; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2137 | list_for_each_entry(tmp, devices, dev_list) { |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2138 | if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 2139 | &tmp->dev_state) && !tmp->bdev) { |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2140 | *device = tmp; |
| 2141 | break; |
| 2142 | } |
| 2143 | } |
| 2144 | |
Anand Jain | d74a625 | 2015-08-14 18:32:56 +0800 | [diff] [blame] | 2145 | if (!*device) |
| 2146 | return BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2147 | |
| 2148 | return 0; |
| 2149 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2150 | return btrfs_find_device_by_path(fs_info, device_path, device); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2154 | /* |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2155 | * Lookup a device given by device id, or the path if the id is 0. |
| 2156 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2157 | int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid, |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2158 | const char *devpath, |
| 2159 | struct btrfs_device **device) |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2160 | { |
| 2161 | int ret; |
| 2162 | |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2163 | if (devid) { |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2164 | ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2165 | *device = btrfs_find_device(fs_info, devid, NULL, NULL); |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2166 | if (!*device) |
| 2167 | ret = -ENOENT; |
| 2168 | } else { |
David Sterba | 5c5c0df | 2016-02-15 16:39:55 +0100 | [diff] [blame] | 2169 | if (!devpath || !devpath[0]) |
Anand Jain | b3d1b15 | 2016-02-13 10:01:37 +0800 | [diff] [blame] | 2170 | return -EINVAL; |
| 2171 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2172 | ret = btrfs_find_device_missing_or_by_path(fs_info, devpath, |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2173 | device); |
| 2174 | } |
| 2175 | return ret; |
| 2176 | } |
| 2177 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2178 | /* |
| 2179 | * does all the dirty work required for changing file system's UUID. |
| 2180 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2181 | static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2182 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2183 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2184 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2185 | struct btrfs_fs_devices *seed_devices; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2186 | struct btrfs_super_block *disk_super = fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2187 | struct btrfs_device *device; |
| 2188 | u64 super_flags; |
| 2189 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2190 | lockdep_assert_held(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2191 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2192 | return -EINVAL; |
| 2193 | |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 2194 | seed_devices = alloc_fs_devices(NULL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2195 | if (IS_ERR(seed_devices)) |
| 2196 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2197 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2198 | old_devices = clone_fs_devices(fs_devices); |
| 2199 | if (IS_ERR(old_devices)) { |
| 2200 | kfree(seed_devices); |
| 2201 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2202 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2203 | |
Anand Jain | c4babc5 | 2018-04-12 10:29:25 +0800 | [diff] [blame] | 2204 | list_add(&old_devices->fs_list, &fs_uuids); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2205 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2206 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2207 | seed_devices->opened = 1; |
| 2208 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2209 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2210 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2211 | |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2212 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2213 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2214 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2215 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2216 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2217 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2218 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2219 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2220 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2221 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2222 | fs_devices->seeding = 0; |
| 2223 | fs_devices->num_devices = 0; |
| 2224 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2225 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2226 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2227 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2228 | |
| 2229 | generate_random_uuid(fs_devices->fsid); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2230 | memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2231 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Anand Jain | 321a4bf | 2018-07-16 22:58:09 +0800 | [diff] [blame] | 2232 | mutex_unlock(&fs_devices->device_list_mutex); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2233 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2234 | super_flags = btrfs_super_flags(disk_super) & |
| 2235 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2236 | btrfs_set_super_flags(disk_super, super_flags); |
| 2237 | |
| 2238 | return 0; |
| 2239 | } |
| 2240 | |
| 2241 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2242 | * Store the expected generation for seed devices in device items. |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2243 | */ |
| 2244 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2245 | struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2246 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2247 | struct btrfs_root *root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2248 | struct btrfs_path *path; |
| 2249 | struct extent_buffer *leaf; |
| 2250 | struct btrfs_dev_item *dev_item; |
| 2251 | struct btrfs_device *device; |
| 2252 | struct btrfs_key key; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2253 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2254 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2255 | u64 devid; |
| 2256 | int ret; |
| 2257 | |
| 2258 | path = btrfs_alloc_path(); |
| 2259 | if (!path) |
| 2260 | return -ENOMEM; |
| 2261 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2262 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2263 | key.offset = 0; |
| 2264 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2265 | |
| 2266 | while (1) { |
| 2267 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2268 | if (ret < 0) |
| 2269 | goto error; |
| 2270 | |
| 2271 | leaf = path->nodes[0]; |
| 2272 | next_slot: |
| 2273 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2274 | ret = btrfs_next_leaf(root, path); |
| 2275 | if (ret > 0) |
| 2276 | break; |
| 2277 | if (ret < 0) |
| 2278 | goto error; |
| 2279 | leaf = path->nodes[0]; |
| 2280 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2281 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2282 | continue; |
| 2283 | } |
| 2284 | |
| 2285 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2286 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2287 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2288 | break; |
| 2289 | |
| 2290 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2291 | struct btrfs_dev_item); |
| 2292 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2293 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2294 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2295 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 2296 | BTRFS_FSID_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2297 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2298 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2299 | |
| 2300 | if (device->fs_devices->seeding) { |
| 2301 | btrfs_set_device_generation(leaf, dev_item, |
| 2302 | device->generation); |
| 2303 | btrfs_mark_buffer_dirty(leaf); |
| 2304 | } |
| 2305 | |
| 2306 | path->slots[0]++; |
| 2307 | goto next_slot; |
| 2308 | } |
| 2309 | ret = 0; |
| 2310 | error: |
| 2311 | btrfs_free_path(path); |
| 2312 | return ret; |
| 2313 | } |
| 2314 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 2315 | 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] | 2316 | { |
Jeff Mahoney | 5112feb | 2016-06-21 20:16:08 -0400 | [diff] [blame] | 2317 | struct btrfs_root *root = fs_info->dev_root; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2318 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2319 | struct btrfs_trans_handle *trans; |
| 2320 | struct btrfs_device *device; |
| 2321 | struct block_device *bdev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2322 | struct super_block *sb = fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2323 | struct rcu_string *name; |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2324 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2325 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2326 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2327 | int ret = 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2328 | bool unlocked = false; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2329 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2330 | if (sb_rdonly(sb) && !fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2331 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2332 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2333 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2334 | fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2335 | if (IS_ERR(bdev)) |
| 2336 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2337 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2338 | if (fs_devices->seeding) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2339 | seeding_dev = 1; |
| 2340 | down_write(&sb->s_umount); |
| 2341 | mutex_lock(&uuid_mutex); |
| 2342 | } |
| 2343 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2344 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2345 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2346 | mutex_lock(&fs_devices->device_list_mutex); |
Anand Jain | 694c51f | 2018-07-03 13:14:51 +0800 | [diff] [blame] | 2347 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2348 | if (device->bdev == bdev) { |
| 2349 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2350 | mutex_unlock( |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2351 | &fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2352 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2353 | } |
| 2354 | } |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2355 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2356 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2357 | device = btrfs_alloc_device(fs_info, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2358 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2359 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2360 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2361 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2362 | } |
| 2363 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2364 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2365 | if (!name) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2366 | ret = -ENOMEM; |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2367 | goto error_free_device; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2368 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2369 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2370 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2371 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2372 | if (IS_ERR(trans)) { |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2373 | ret = PTR_ERR(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2374 | goto error_free_device; |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2377 | q = bdev_get_queue(bdev); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2378 | set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2379 | device->generation = trans->transid; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2380 | device->io_width = fs_info->sectorsize; |
| 2381 | device->io_align = fs_info->sectorsize; |
| 2382 | device->sector_size = fs_info->sectorsize; |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2383 | device->total_bytes = round_down(i_size_read(bdev->bd_inode), |
| 2384 | fs_info->sectorsize); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2385 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2386 | device->commit_total_bytes = device->total_bytes; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2387 | device->fs_info = fs_info; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2388 | device->bdev = bdev; |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 2389 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2390 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2391 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2392 | device->dev_stats_valid = 1; |
David Sterba | 9f6d251 | 2017-06-16 01:48:05 +0200 | [diff] [blame] | 2393 | set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE); |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2394 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2395 | if (seeding_dev) { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2396 | sb->s_flags &= ~SB_RDONLY; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2397 | ret = btrfs_prepare_sprout(fs_info); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2398 | if (ret) { |
| 2399 | btrfs_abort_transaction(trans, ret); |
| 2400 | goto error_trans; |
| 2401 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2402 | } |
| 2403 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2404 | device->fs_devices = fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2405 | |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2406 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2407 | mutex_lock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2408 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
| 2409 | list_add(&device->dev_alloc_list, &fs_devices->alloc_list); |
| 2410 | fs_devices->num_devices++; |
| 2411 | fs_devices->open_devices++; |
| 2412 | fs_devices->rw_devices++; |
| 2413 | fs_devices->total_devices++; |
| 2414 | fs_devices->total_rw_bytes += device->total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2415 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2416 | atomic64_add(device->total_bytes, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2417 | |
Anand Jain | e884f4f | 2017-04-04 18:40:19 +0800 | [diff] [blame] | 2418 | if (!blk_queue_nonrot(q)) |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2419 | fs_devices->rotating = 1; |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2420 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2421 | tmp = btrfs_super_total_bytes(fs_info->super_copy); |
| 2422 | btrfs_set_super_total_bytes(fs_info->super_copy, |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2423 | round_down(tmp + device->total_bytes, fs_info->sectorsize)); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2424 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2425 | tmp = btrfs_super_num_devices(fs_info->super_copy); |
| 2426 | btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2427 | |
| 2428 | /* add sysfs device entry */ |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2429 | btrfs_sysfs_add_device_link(fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2430 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2431 | /* |
| 2432 | * we've got more storage, clear any full flags on the space |
| 2433 | * infos |
| 2434 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2435 | btrfs_clear_space_info_full(fs_info); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2436 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2437 | mutex_unlock(&fs_info->chunk_mutex); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2438 | mutex_unlock(&fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2439 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2440 | if (seeding_dev) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2441 | mutex_lock(&fs_info->chunk_mutex); |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 2442 | ret = init_first_rw_device(trans, fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2443 | mutex_unlock(&fs_info->chunk_mutex); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2444 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2445 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2446 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2447 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2448 | } |
| 2449 | |
Nikolay Borisov | 8e87e85 | 2018-07-20 19:37:47 +0300 | [diff] [blame] | 2450 | ret = btrfs_add_dev_item(trans, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2451 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2452 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2453 | goto error_sysfs; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | if (seeding_dev) { |
| 2457 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2458 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2459 | ret = btrfs_finish_sprout(trans, fs_info); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2460 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2461 | btrfs_abort_transaction(trans, ret); |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2462 | goto error_sysfs; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2463 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2464 | |
| 2465 | /* Sprouting would change fsid of the mounted root, |
| 2466 | * so rename the fsid on the sysfs |
| 2467 | */ |
| 2468 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2469 | fs_info->fsid); |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2470 | if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf)) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2471 | btrfs_warn(fs_info, |
| 2472 | "sysfs: failed to create fsid for sprout"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2473 | } |
| 2474 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2475 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2476 | |
| 2477 | if (seeding_dev) { |
| 2478 | mutex_unlock(&uuid_mutex); |
| 2479 | up_write(&sb->s_umount); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2480 | unlocked = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2481 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2482 | if (ret) /* transaction commit */ |
| 2483 | return ret; |
| 2484 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2485 | ret = btrfs_relocate_sys_chunks(fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2486 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2487 | btrfs_handle_fs_error(fs_info, ret, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 2488 | "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] | 2489 | trans = btrfs_attach_transaction(root); |
| 2490 | if (IS_ERR(trans)) { |
| 2491 | if (PTR_ERR(trans) == -ENOENT) |
| 2492 | return 0; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2493 | ret = PTR_ERR(trans); |
| 2494 | trans = NULL; |
| 2495 | goto error_sysfs; |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2496 | } |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2497 | ret = btrfs_commit_transaction(trans); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2498 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2499 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2500 | /* Update ctime/mtime for libblkid */ |
| 2501 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2502 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2503 | |
Anand Jain | d31c32f | 2017-09-28 14:51:10 +0800 | [diff] [blame] | 2504 | error_sysfs: |
Anand Jain | 5da54bc | 2018-07-03 13:14:50 +0800 | [diff] [blame] | 2505 | btrfs_sysfs_rm_device_link(fs_devices, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2506 | error_trans: |
Anand Jain | 0af2c4b | 2017-09-28 14:51:09 +0800 | [diff] [blame] | 2507 | if (seeding_dev) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 2508 | sb->s_flags |= SB_RDONLY; |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2509 | if (trans) |
| 2510 | btrfs_end_transaction(trans); |
David Sterba | 5c4cf6c | 2017-10-30 19:29:46 +0100 | [diff] [blame] | 2511 | error_free_device: |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 2512 | btrfs_free_device(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2513 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2514 | blkdev_put(bdev, FMODE_EXCL); |
Anand Jain | 7132a26 | 2017-09-28 14:51:11 +0800 | [diff] [blame] | 2515 | if (seeding_dev && !unlocked) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2516 | mutex_unlock(&uuid_mutex); |
| 2517 | up_write(&sb->s_umount); |
| 2518 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2519 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2520 | } |
| 2521 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2522 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2523 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2524 | { |
| 2525 | int ret; |
| 2526 | struct btrfs_path *path; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2527 | struct btrfs_root *root = device->fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2528 | struct btrfs_dev_item *dev_item; |
| 2529 | struct extent_buffer *leaf; |
| 2530 | struct btrfs_key key; |
| 2531 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2532 | path = btrfs_alloc_path(); |
| 2533 | if (!path) |
| 2534 | return -ENOMEM; |
| 2535 | |
| 2536 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2537 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2538 | key.offset = device->devid; |
| 2539 | |
| 2540 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2541 | if (ret < 0) |
| 2542 | goto out; |
| 2543 | |
| 2544 | if (ret > 0) { |
| 2545 | ret = -ENOENT; |
| 2546 | goto out; |
| 2547 | } |
| 2548 | |
| 2549 | leaf = path->nodes[0]; |
| 2550 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2551 | |
| 2552 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2553 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2554 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2555 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2556 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2557 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2558 | btrfs_device_get_disk_total_bytes(device)); |
| 2559 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2560 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2561 | btrfs_mark_buffer_dirty(leaf); |
| 2562 | |
| 2563 | out: |
| 2564 | btrfs_free_path(path); |
| 2565 | return ret; |
| 2566 | } |
| 2567 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2568 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2569 | struct btrfs_device *device, u64 new_size) |
| 2570 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2571 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 2572 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2573 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2574 | u64 old_total; |
| 2575 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2576 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 2577 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2578 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2579 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2580 | new_size = round_down(new_size, fs_info->sectorsize); |
| 2581 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2582 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2583 | old_total = btrfs_super_total_bytes(super_copy); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 2584 | diff = round_down(new_size - device->total_bytes, fs_info->sectorsize); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2585 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2586 | if (new_size <= device->total_bytes || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 2587 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2588 | mutex_unlock(&fs_info->chunk_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2589 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2590 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2591 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2592 | fs_devices = fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2593 | |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 2594 | btrfs_set_super_total_bytes(super_copy, |
| 2595 | round_down(old_total + diff, fs_info->sectorsize)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2596 | device->fs_devices->total_rw_bytes += diff; |
| 2597 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2598 | btrfs_device_set_total_bytes(device, new_size); |
| 2599 | btrfs_device_set_disk_total_bytes(device, new_size); |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 2600 | btrfs_clear_space_info_full(device->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2601 | if (list_empty(&device->resized_list)) |
| 2602 | list_add_tail(&device->resized_list, |
| 2603 | &fs_devices->resized_devices); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2604 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2605 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2606 | return btrfs_update_device(trans, device); |
| 2607 | } |
| 2608 | |
| 2609 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2610 | struct btrfs_fs_info *fs_info, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2611 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2612 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2613 | int ret; |
| 2614 | struct btrfs_path *path; |
| 2615 | struct btrfs_key key; |
| 2616 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2617 | path = btrfs_alloc_path(); |
| 2618 | if (!path) |
| 2619 | return -ENOMEM; |
| 2620 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2621 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2622 | key.offset = chunk_offset; |
| 2623 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2624 | |
| 2625 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2626 | if (ret < 0) |
| 2627 | goto out; |
| 2628 | else if (ret > 0) { /* Logic error or corruption */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2629 | btrfs_handle_fs_error(fs_info, -ENOENT, |
| 2630 | "Failed lookup while freeing chunk."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2631 | ret = -ENOENT; |
| 2632 | goto out; |
| 2633 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2634 | |
| 2635 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2636 | if (ret < 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2637 | btrfs_handle_fs_error(fs_info, ret, |
| 2638 | "Failed to delete chunk item."); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2639 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2640 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2641 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2642 | } |
| 2643 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2644 | 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] | 2645 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2646 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2647 | struct btrfs_disk_key *disk_key; |
| 2648 | struct btrfs_chunk *chunk; |
| 2649 | u8 *ptr; |
| 2650 | int ret = 0; |
| 2651 | u32 num_stripes; |
| 2652 | u32 array_size; |
| 2653 | u32 len = 0; |
| 2654 | u32 cur; |
| 2655 | struct btrfs_key key; |
| 2656 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2657 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2658 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2659 | |
| 2660 | ptr = super_copy->sys_chunk_array; |
| 2661 | cur = 0; |
| 2662 | |
| 2663 | while (cur < array_size) { |
| 2664 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2665 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2666 | |
| 2667 | len = sizeof(*disk_key); |
| 2668 | |
| 2669 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2670 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2671 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2672 | len += btrfs_chunk_item_size(num_stripes); |
| 2673 | } else { |
| 2674 | ret = -EIO; |
| 2675 | break; |
| 2676 | } |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2677 | if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2678 | key.offset == chunk_offset) { |
| 2679 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2680 | array_size -= len; |
| 2681 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2682 | } else { |
| 2683 | ptr += len; |
| 2684 | cur += len; |
| 2685 | } |
| 2686 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2687 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2688 | return ret; |
| 2689 | } |
| 2690 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2691 | static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info, |
| 2692 | u64 logical, u64 length) |
| 2693 | { |
| 2694 | struct extent_map_tree *em_tree; |
| 2695 | struct extent_map *em; |
| 2696 | |
| 2697 | em_tree = &fs_info->mapping_tree.map_tree; |
| 2698 | read_lock(&em_tree->lock); |
| 2699 | em = lookup_extent_mapping(em_tree, logical, length); |
| 2700 | read_unlock(&em_tree->lock); |
| 2701 | |
| 2702 | if (!em) { |
| 2703 | btrfs_crit(fs_info, "unable to find logical %llu length %llu", |
| 2704 | logical, length); |
| 2705 | return ERR_PTR(-EINVAL); |
| 2706 | } |
| 2707 | |
| 2708 | if (em->start > logical || em->start + em->len < logical) { |
| 2709 | btrfs_crit(fs_info, |
| 2710 | "found a bad mapping, wanted %llu-%llu, found %llu-%llu", |
| 2711 | logical, length, em->start, em->start + em->len); |
| 2712 | free_extent_map(em); |
| 2713 | return ERR_PTR(-EINVAL); |
| 2714 | } |
| 2715 | |
| 2716 | /* callers are responsible for dropping em's ref. */ |
| 2717 | return em; |
| 2718 | } |
| 2719 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2720 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2721 | struct btrfs_fs_info *fs_info, u64 chunk_offset) |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2722 | { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2723 | struct extent_map *em; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2724 | struct map_lookup *map; |
| 2725 | u64 dev_extent_len = 0; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2726 | int i, ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2727 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2728 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2729 | em = get_chunk_map(fs_info, chunk_offset, 1); |
| 2730 | if (IS_ERR(em)) { |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2731 | /* |
| 2732 | * 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] | 2733 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2734 | * do anything we still error out. |
| 2735 | */ |
| 2736 | ASSERT(0); |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 2737 | return PTR_ERR(em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2738 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2739 | map = em->map_lookup; |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2740 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | 451a2c1 | 2018-06-20 15:49:07 +0300 | [diff] [blame] | 2741 | check_system_chunk(trans, map->type); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2742 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2743 | |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2744 | /* |
| 2745 | * Take the device list mutex to prevent races with the final phase of |
| 2746 | * a device replace operation that replaces the device object associated |
| 2747 | * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()). |
| 2748 | */ |
| 2749 | mutex_lock(&fs_devices->device_list_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2750 | for (i = 0; i < map->num_stripes; i++) { |
| 2751 | struct btrfs_device *device = map->stripes[i].dev; |
| 2752 | ret = btrfs_free_dev_extent(trans, device, |
| 2753 | map->stripes[i].physical, |
| 2754 | &dev_extent_len); |
| 2755 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2756 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2757 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2758 | goto out; |
| 2759 | } |
| 2760 | |
| 2761 | if (device->bytes_used > 0) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2762 | mutex_lock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2763 | btrfs_device_set_bytes_used(device, |
| 2764 | device->bytes_used - dev_extent_len); |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 2765 | atomic64_add(dev_extent_len, &fs_info->free_chunk_space); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2766 | btrfs_clear_space_info_full(fs_info); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 2767 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | if (map->stripes[i].dev) { |
| 2771 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2772 | if (ret) { |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2773 | mutex_unlock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2774 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2775 | goto out; |
| 2776 | } |
| 2777 | } |
| 2778 | } |
Filipe Manana | 57ba4cb | 2016-05-20 04:34:23 +0100 | [diff] [blame] | 2779 | mutex_unlock(&fs_devices->device_list_mutex); |
| 2780 | |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2781 | ret = btrfs_free_chunk(trans, fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2782 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2783 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2784 | goto out; |
| 2785 | } |
| 2786 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2787 | trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2788 | |
| 2789 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Nikolay Borisov | 408fbf1 | 2017-07-27 14:37:29 +0300 | [diff] [blame] | 2790 | ret = btrfs_del_sys_chunk(fs_info, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2791 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2792 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2793 | goto out; |
| 2794 | } |
| 2795 | } |
| 2796 | |
Nikolay Borisov | 5a98ec0 | 2018-06-20 15:48:56 +0300 | [diff] [blame] | 2797 | ret = btrfs_remove_block_group(trans, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2798 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 2799 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2800 | goto out; |
| 2801 | } |
| 2802 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2803 | out: |
| 2804 | /* once for us */ |
| 2805 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2806 | return ret; |
| 2807 | } |
| 2808 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2809 | 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] | 2810 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2811 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2812 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2813 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2814 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2815 | /* |
| 2816 | * Prevent races with automatic removal of unused block groups. |
| 2817 | * After we relocate and before we remove the chunk with offset |
| 2818 | * chunk_offset, automatic removal of the block group can kick in, |
| 2819 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2820 | * |
| 2821 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2822 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2823 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2824 | * we release the path used to search the chunk/dev tree and before |
| 2825 | * the current task acquires this mutex and calls us. |
| 2826 | */ |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 2827 | lockdep_assert_held(&fs_info->delete_unused_bgs_mutex); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2828 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2829 | ret = btrfs_can_relocate(fs_info, chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2830 | if (ret) |
| 2831 | return -ENOSPC; |
| 2832 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2833 | /* step one, relocate all the extents inside this chunk */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2834 | btrfs_scrub_pause(fs_info); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2835 | ret = btrfs_relocate_block_group(fs_info, chunk_offset); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2836 | btrfs_scrub_continue(fs_info); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2837 | if (ret) |
| 2838 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2839 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 2840 | /* |
| 2841 | * We add the kobjects here (and after forcing data chunk creation) |
| 2842 | * since relocation is the only place we'll create chunks of a new |
| 2843 | * type at runtime. The only place where we'll remove the last |
| 2844 | * chunk of a type is the call immediately below this one. Even |
| 2845 | * so, we're protected against races with the cleaner thread since |
| 2846 | * we're covered by the delete_unused_bgs_mutex. |
| 2847 | */ |
| 2848 | btrfs_add_raid_kobjects(fs_info); |
| 2849 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2850 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 2851 | chunk_offset); |
| 2852 | if (IS_ERR(trans)) { |
| 2853 | ret = PTR_ERR(trans); |
| 2854 | btrfs_handle_fs_error(root->fs_info, ret, NULL); |
| 2855 | return ret; |
| 2856 | } |
Naohiro Aota | 5d8eb6f | 2016-09-02 16:46:32 +0900 | [diff] [blame] | 2857 | |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2858 | /* |
| 2859 | * step two, delete the device extents and the |
| 2860 | * chunk tree entries |
| 2861 | */ |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 2862 | ret = btrfs_remove_chunk(trans, fs_info, chunk_offset); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 2863 | btrfs_end_transaction(trans); |
Chris Mason | 19c4d2f | 2016-10-10 13:43:31 -0700 | [diff] [blame] | 2864 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2865 | } |
| 2866 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2867 | static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2868 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2869 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2870 | struct btrfs_path *path; |
| 2871 | struct extent_buffer *leaf; |
| 2872 | struct btrfs_chunk *chunk; |
| 2873 | struct btrfs_key key; |
| 2874 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2875 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2876 | bool retried = false; |
| 2877 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2878 | int ret; |
| 2879 | |
| 2880 | path = btrfs_alloc_path(); |
| 2881 | if (!path) |
| 2882 | return -ENOMEM; |
| 2883 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2884 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2885 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2886 | key.offset = (u64)-1; |
| 2887 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2888 | |
| 2889 | while (1) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2890 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2891 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2892 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2893 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2894 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2895 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2896 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2897 | |
| 2898 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2899 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2900 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2901 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2902 | if (ret < 0) |
| 2903 | goto error; |
| 2904 | if (ret > 0) |
| 2905 | break; |
| 2906 | |
| 2907 | leaf = path->nodes[0]; |
| 2908 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2909 | |
| 2910 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2911 | struct btrfs_chunk); |
| 2912 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2913 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2914 | |
| 2915 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2916 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2917 | if (ret == -ENOSPC) |
| 2918 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2919 | else |
| 2920 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2921 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2922 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2923 | |
| 2924 | if (found_key.offset == 0) |
| 2925 | break; |
| 2926 | key.offset = found_key.offset - 1; |
| 2927 | } |
| 2928 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2929 | if (failed && !retried) { |
| 2930 | failed = 0; |
| 2931 | retried = true; |
| 2932 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2933 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2934 | ret = -ENOSPC; |
| 2935 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2936 | error: |
| 2937 | btrfs_free_path(path); |
| 2938 | return ret; |
| 2939 | } |
| 2940 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 2941 | /* |
| 2942 | * return 1 : allocate a data chunk successfully, |
| 2943 | * return <0: errors during allocating a data chunk, |
| 2944 | * return 0 : no need to allocate a data chunk. |
| 2945 | */ |
| 2946 | static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info, |
| 2947 | u64 chunk_offset) |
| 2948 | { |
| 2949 | struct btrfs_block_group_cache *cache; |
| 2950 | u64 bytes_used; |
| 2951 | u64 chunk_type; |
| 2952 | |
| 2953 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2954 | ASSERT(cache); |
| 2955 | chunk_type = cache->flags; |
| 2956 | btrfs_put_block_group(cache); |
| 2957 | |
| 2958 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) { |
| 2959 | spin_lock(&fs_info->data_sinfo->lock); |
| 2960 | bytes_used = fs_info->data_sinfo->bytes_used; |
| 2961 | spin_unlock(&fs_info->data_sinfo->lock); |
| 2962 | |
| 2963 | if (!bytes_used) { |
| 2964 | struct btrfs_trans_handle *trans; |
| 2965 | int ret; |
| 2966 | |
| 2967 | trans = btrfs_join_transaction(fs_info->tree_root); |
| 2968 | if (IS_ERR(trans)) |
| 2969 | return PTR_ERR(trans); |
| 2970 | |
Nikolay Borisov | 43a7e99 | 2018-06-20 15:49:15 +0300 | [diff] [blame] | 2971 | ret = btrfs_force_chunk_alloc(trans, |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 2972 | BTRFS_BLOCK_GROUP_DATA); |
| 2973 | btrfs_end_transaction(trans); |
| 2974 | if (ret < 0) |
| 2975 | return ret; |
| 2976 | |
Jeff Mahoney | 75cb379 | 2018-03-20 15:25:26 -0400 | [diff] [blame] | 2977 | btrfs_add_raid_kobjects(fs_info); |
| 2978 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 2979 | return 1; |
| 2980 | } |
| 2981 | } |
| 2982 | return 0; |
| 2983 | } |
| 2984 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2985 | static int insert_balance_item(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2986 | struct btrfs_balance_control *bctl) |
| 2987 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 2988 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2989 | struct btrfs_trans_handle *trans; |
| 2990 | struct btrfs_balance_item *item; |
| 2991 | struct btrfs_disk_balance_args disk_bargs; |
| 2992 | struct btrfs_path *path; |
| 2993 | struct extent_buffer *leaf; |
| 2994 | struct btrfs_key key; |
| 2995 | int ret, err; |
| 2996 | |
| 2997 | path = btrfs_alloc_path(); |
| 2998 | if (!path) |
| 2999 | return -ENOMEM; |
| 3000 | |
| 3001 | trans = btrfs_start_transaction(root, 0); |
| 3002 | if (IS_ERR(trans)) { |
| 3003 | btrfs_free_path(path); |
| 3004 | return PTR_ERR(trans); |
| 3005 | } |
| 3006 | |
| 3007 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3008 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3009 | key.offset = 0; |
| 3010 | |
| 3011 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 3012 | sizeof(*item)); |
| 3013 | if (ret) |
| 3014 | goto out; |
| 3015 | |
| 3016 | leaf = path->nodes[0]; |
| 3017 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3018 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3019 | memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item)); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3020 | |
| 3021 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3022 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3023 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3024 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3025 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3026 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3027 | |
| 3028 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3029 | |
| 3030 | btrfs_mark_buffer_dirty(leaf); |
| 3031 | out: |
| 3032 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3033 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3034 | if (err && !ret) |
| 3035 | ret = err; |
| 3036 | return ret; |
| 3037 | } |
| 3038 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3039 | static int del_balance_item(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3040 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3041 | struct btrfs_root *root = fs_info->tree_root; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3042 | struct btrfs_trans_handle *trans; |
| 3043 | struct btrfs_path *path; |
| 3044 | struct btrfs_key key; |
| 3045 | int ret, err; |
| 3046 | |
| 3047 | path = btrfs_alloc_path(); |
| 3048 | if (!path) |
| 3049 | return -ENOMEM; |
| 3050 | |
| 3051 | trans = btrfs_start_transaction(root, 0); |
| 3052 | if (IS_ERR(trans)) { |
| 3053 | btrfs_free_path(path); |
| 3054 | return PTR_ERR(trans); |
| 3055 | } |
| 3056 | |
| 3057 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3058 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3059 | key.offset = 0; |
| 3060 | |
| 3061 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3062 | if (ret < 0) |
| 3063 | goto out; |
| 3064 | if (ret > 0) { |
| 3065 | ret = -ENOENT; |
| 3066 | goto out; |
| 3067 | } |
| 3068 | |
| 3069 | ret = btrfs_del_item(trans, root, path); |
| 3070 | out: |
| 3071 | btrfs_free_path(path); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3072 | err = btrfs_commit_transaction(trans); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3073 | if (err && !ret) |
| 3074 | ret = err; |
| 3075 | return ret; |
| 3076 | } |
| 3077 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3078 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3079 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3080 | * resume after balance was interrupted. |
| 3081 | */ |
| 3082 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3083 | { |
| 3084 | /* |
| 3085 | * Turn on soft mode for chunk types that were being converted. |
| 3086 | */ |
| 3087 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3088 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3089 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3090 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3091 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3092 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3093 | |
| 3094 | /* |
| 3095 | * Turn on usage filter if is not already used. The idea is |
| 3096 | * that chunks that we have already balanced should be |
| 3097 | * reasonably full. Don't do it for chunks that are being |
| 3098 | * converted - that will keep us from relocating unconverted |
| 3099 | * (albeit full) chunks. |
| 3100 | */ |
| 3101 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3102 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3103 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3104 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3105 | bctl->data.usage = 90; |
| 3106 | } |
| 3107 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3108 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3109 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3110 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3111 | bctl->sys.usage = 90; |
| 3112 | } |
| 3113 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3114 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3115 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3116 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3117 | bctl->meta.usage = 90; |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | /* |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3122 | * Clear the balance status in fs_info and delete the balance item from disk. |
| 3123 | */ |
| 3124 | static void reset_balance_state(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3125 | { |
| 3126 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3127 | int ret; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3128 | |
| 3129 | BUG_ON(!fs_info->balance_ctl); |
| 3130 | |
| 3131 | spin_lock(&fs_info->balance_lock); |
| 3132 | fs_info->balance_ctl = NULL; |
| 3133 | spin_unlock(&fs_info->balance_lock); |
| 3134 | |
| 3135 | kfree(bctl); |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3136 | ret = del_balance_item(fs_info); |
| 3137 | if (ret) |
| 3138 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3141 | /* |
| 3142 | * Balance filters. Return 1 if chunk should be filtered out |
| 3143 | * (should not be balanced). |
| 3144 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3145 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3146 | struct btrfs_balance_args *bargs) |
| 3147 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3148 | chunk_type = chunk_to_extended(chunk_type) & |
| 3149 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3150 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3151 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3152 | return 0; |
| 3153 | |
| 3154 | return 1; |
| 3155 | } |
| 3156 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3157 | 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] | 3158 | struct btrfs_balance_args *bargs) |
| 3159 | { |
| 3160 | struct btrfs_block_group_cache *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3161 | u64 chunk_used; |
| 3162 | u64 user_thresh_min; |
| 3163 | u64 user_thresh_max; |
| 3164 | int ret = 1; |
| 3165 | |
| 3166 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3167 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3168 | |
| 3169 | if (bargs->usage_min == 0) |
| 3170 | user_thresh_min = 0; |
| 3171 | else |
| 3172 | user_thresh_min = div_factor_fine(cache->key.offset, |
| 3173 | bargs->usage_min); |
| 3174 | |
| 3175 | if (bargs->usage_max == 0) |
| 3176 | user_thresh_max = 1; |
| 3177 | else if (bargs->usage_max > 100) |
| 3178 | user_thresh_max = cache->key.offset; |
| 3179 | else |
| 3180 | user_thresh_max = div_factor_fine(cache->key.offset, |
| 3181 | bargs->usage_max); |
| 3182 | |
| 3183 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3184 | ret = 0; |
| 3185 | |
| 3186 | btrfs_put_block_group(cache); |
| 3187 | return ret; |
| 3188 | } |
| 3189 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3190 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3191 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3192 | { |
| 3193 | struct btrfs_block_group_cache *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3194 | u64 chunk_used, user_thresh; |
| 3195 | int ret = 1; |
| 3196 | |
| 3197 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3198 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3199 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3200 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3201 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3202 | else if (bargs->usage > 100) |
| 3203 | user_thresh = cache->key.offset; |
| 3204 | else |
| 3205 | user_thresh = div_factor_fine(cache->key.offset, |
| 3206 | bargs->usage); |
| 3207 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3208 | if (chunk_used < user_thresh) |
| 3209 | ret = 0; |
| 3210 | |
| 3211 | btrfs_put_block_group(cache); |
| 3212 | return ret; |
| 3213 | } |
| 3214 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3215 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3216 | struct btrfs_chunk *chunk, |
| 3217 | struct btrfs_balance_args *bargs) |
| 3218 | { |
| 3219 | struct btrfs_stripe *stripe; |
| 3220 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3221 | int i; |
| 3222 | |
| 3223 | for (i = 0; i < num_stripes; i++) { |
| 3224 | stripe = btrfs_stripe_nr(chunk, i); |
| 3225 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3226 | return 0; |
| 3227 | } |
| 3228 | |
| 3229 | return 1; |
| 3230 | } |
| 3231 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3232 | /* [pstart, pend) */ |
| 3233 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3234 | struct btrfs_chunk *chunk, |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3235 | struct btrfs_balance_args *bargs) |
| 3236 | { |
| 3237 | struct btrfs_stripe *stripe; |
| 3238 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3239 | u64 stripe_offset; |
| 3240 | u64 stripe_length; |
| 3241 | int factor; |
| 3242 | int i; |
| 3243 | |
| 3244 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3245 | return 0; |
| 3246 | |
| 3247 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3248 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3249 | factor = num_stripes / 2; |
| 3250 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3251 | factor = num_stripes - 1; |
| 3252 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3253 | factor = num_stripes - 2; |
| 3254 | } else { |
| 3255 | factor = num_stripes; |
| 3256 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3257 | |
| 3258 | for (i = 0; i < num_stripes; i++) { |
| 3259 | stripe = btrfs_stripe_nr(chunk, i); |
| 3260 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3261 | continue; |
| 3262 | |
| 3263 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3264 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3265 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3266 | |
| 3267 | if (stripe_offset < bargs->pend && |
| 3268 | stripe_offset + stripe_length > bargs->pstart) |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | return 1; |
| 3273 | } |
| 3274 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3275 | /* [vstart, vend) */ |
| 3276 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3277 | struct btrfs_chunk *chunk, |
| 3278 | u64 chunk_offset, |
| 3279 | struct btrfs_balance_args *bargs) |
| 3280 | { |
| 3281 | if (chunk_offset < bargs->vend && |
| 3282 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3283 | /* at least part of the chunk is inside this vrange */ |
| 3284 | return 0; |
| 3285 | |
| 3286 | return 1; |
| 3287 | } |
| 3288 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3289 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3290 | struct btrfs_chunk *chunk, |
| 3291 | struct btrfs_balance_args *bargs) |
| 3292 | { |
| 3293 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3294 | |
| 3295 | if (bargs->stripes_min <= num_stripes |
| 3296 | && num_stripes <= bargs->stripes_max) |
| 3297 | return 0; |
| 3298 | |
| 3299 | return 1; |
| 3300 | } |
| 3301 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3302 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3303 | struct btrfs_balance_args *bargs) |
| 3304 | { |
| 3305 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3306 | return 0; |
| 3307 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3308 | chunk_type = chunk_to_extended(chunk_type) & |
| 3309 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3310 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3311 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3312 | return 1; |
| 3313 | |
| 3314 | return 0; |
| 3315 | } |
| 3316 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3317 | static int should_balance_chunk(struct btrfs_fs_info *fs_info, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3318 | struct extent_buffer *leaf, |
| 3319 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3320 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3321 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3322 | struct btrfs_balance_args *bargs = NULL; |
| 3323 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3324 | |
| 3325 | /* type filter */ |
| 3326 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3327 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
| 3331 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3332 | bargs = &bctl->data; |
| 3333 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3334 | bargs = &bctl->sys; |
| 3335 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3336 | bargs = &bctl->meta; |
| 3337 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3338 | /* profiles filter */ |
| 3339 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3340 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3341 | return 0; |
| 3342 | } |
| 3343 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3344 | /* usage filter */ |
| 3345 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3346 | chunk_usage_filter(fs_info, chunk_offset, bargs)) { |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3347 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3348 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3349 | chunk_usage_range_filter(fs_info, chunk_offset, bargs)) { |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3350 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3353 | /* devid filter */ |
| 3354 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3355 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3356 | return 0; |
| 3357 | } |
| 3358 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3359 | /* drange filter, makes sense only with devid filter */ |
| 3360 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 3361 | chunk_drange_filter(leaf, chunk, bargs)) { |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3362 | return 0; |
| 3363 | } |
| 3364 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3365 | /* vrange filter */ |
| 3366 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3367 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3368 | return 0; |
| 3369 | } |
| 3370 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3371 | /* stripes filter */ |
| 3372 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3373 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3374 | return 0; |
| 3375 | } |
| 3376 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3377 | /* soft profile changing mode */ |
| 3378 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3379 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3380 | return 0; |
| 3381 | } |
| 3382 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3383 | /* |
| 3384 | * limited by count, must be the last filter |
| 3385 | */ |
| 3386 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3387 | if (bargs->limit == 0) |
| 3388 | return 0; |
| 3389 | else |
| 3390 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3391 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3392 | /* |
| 3393 | * Same logic as the 'limit' filter; the minimum cannot be |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 3394 | * determined here because we do not have the global information |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3395 | * about the count of all chunks that satisfy the filters. |
| 3396 | */ |
| 3397 | if (bargs->limit_max == 0) |
| 3398 | return 0; |
| 3399 | else |
| 3400 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3401 | } |
| 3402 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3403 | return 1; |
| 3404 | } |
| 3405 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3406 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3407 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3408 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3409 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3410 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3411 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3412 | struct btrfs_device *device; |
| 3413 | u64 old_size; |
| 3414 | u64 size_to_free; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3415 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3416 | struct btrfs_chunk *chunk; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3417 | struct btrfs_path *path = NULL; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3418 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3419 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3420 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3421 | struct extent_buffer *leaf; |
| 3422 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3423 | int ret; |
| 3424 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3425 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3426 | /* 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] | 3427 | u64 limit_data = bctl->data.limit; |
| 3428 | u64 limit_meta = bctl->meta.limit; |
| 3429 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3430 | u32 count_data = 0; |
| 3431 | u32 count_meta = 0; |
| 3432 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3433 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3434 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3435 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3436 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3437 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3438 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3439 | size_to_free = div_factor(old_size, 1); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3440 | size_to_free = min_t(u64, size_to_free, SZ_1M); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 3441 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3442 | btrfs_device_get_total_bytes(device) - |
| 3443 | btrfs_device_get_bytes_used(device) > size_to_free || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 3444 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3445 | continue; |
| 3446 | |
| 3447 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3448 | if (ret == -ENOSPC) |
| 3449 | break; |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3450 | if (ret) { |
| 3451 | /* btrfs_shrink_device never returns ret > 0 */ |
| 3452 | WARN_ON(ret > 0); |
| 3453 | goto error; |
| 3454 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3455 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3456 | trans = btrfs_start_transaction(dev_root, 0); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3457 | if (IS_ERR(trans)) { |
| 3458 | ret = PTR_ERR(trans); |
| 3459 | btrfs_info_in_rcu(fs_info, |
| 3460 | "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu", |
| 3461 | rcu_str_deref(device->name), ret, |
| 3462 | old_size, old_size - size_to_free); |
| 3463 | goto error; |
| 3464 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3465 | |
| 3466 | ret = btrfs_grow_device(trans, device, old_size); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3467 | if (ret) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3468 | btrfs_end_transaction(trans); |
Liu Bo | 5a488b9 | 2016-07-12 11:24:21 -0700 | [diff] [blame] | 3469 | /* btrfs_grow_device never returns ret > 0 */ |
| 3470 | WARN_ON(ret > 0); |
| 3471 | btrfs_info_in_rcu(fs_info, |
| 3472 | "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu", |
| 3473 | rcu_str_deref(device->name), ret, |
| 3474 | old_size, old_size - size_to_free); |
| 3475 | goto error; |
| 3476 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3477 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 3478 | btrfs_end_transaction(trans); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | /* step two, relocate all the chunks */ |
| 3482 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3483 | if (!path) { |
| 3484 | ret = -ENOMEM; |
| 3485 | goto error; |
| 3486 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3487 | |
| 3488 | /* zero out stat counters */ |
| 3489 | spin_lock(&fs_info->balance_lock); |
| 3490 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3491 | spin_unlock(&fs_info->balance_lock); |
| 3492 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3493 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3494 | /* |
| 3495 | * The single value limit and min/max limits use the same bytes |
| 3496 | * in the |
| 3497 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3498 | bctl->data.limit = limit_data; |
| 3499 | bctl->meta.limit = limit_meta; |
| 3500 | bctl->sys.limit = limit_sys; |
| 3501 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3502 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3503 | key.offset = (u64)-1; |
| 3504 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3505 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3506 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3507 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3508 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3509 | ret = -ECANCELED; |
| 3510 | goto error; |
| 3511 | } |
| 3512 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3513 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3514 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3515 | if (ret < 0) { |
| 3516 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3517 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3518 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3519 | |
| 3520 | /* |
| 3521 | * this shouldn't happen, it means the last relocate |
| 3522 | * failed |
| 3523 | */ |
| 3524 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3525 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3526 | |
| 3527 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3528 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3529 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3530 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3531 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3532 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3533 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3534 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3535 | leaf = path->nodes[0]; |
| 3536 | slot = path->slots[0]; |
| 3537 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3538 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3539 | if (found_key.objectid != key.objectid) { |
| 3540 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3541 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3542 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3543 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3544 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3545 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3546 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3547 | if (!counting) { |
| 3548 | spin_lock(&fs_info->balance_lock); |
| 3549 | bctl->stat.considered++; |
| 3550 | spin_unlock(&fs_info->balance_lock); |
| 3551 | } |
| 3552 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3553 | ret = should_balance_chunk(fs_info, leaf, chunk, |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3554 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3555 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3556 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3557 | if (!ret) { |
| 3558 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3559 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3560 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3561 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3562 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3563 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3564 | spin_lock(&fs_info->balance_lock); |
| 3565 | bctl->stat.expected++; |
| 3566 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3567 | |
| 3568 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3569 | count_data++; |
| 3570 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3571 | count_sys++; |
| 3572 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3573 | count_meta++; |
| 3574 | |
| 3575 | goto loop; |
| 3576 | } |
| 3577 | |
| 3578 | /* |
| 3579 | * Apply limit_min filter, no need to check if the LIMITS |
| 3580 | * filter is used, limit_min is 0 by default |
| 3581 | */ |
| 3582 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3583 | count_data < bctl->data.limit_min) |
| 3584 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3585 | count_meta < bctl->meta.limit_min) |
| 3586 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3587 | count_sys < bctl->sys.limit_min)) { |
| 3588 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3589 | goto loop; |
| 3590 | } |
| 3591 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3592 | if (!chunk_reserved) { |
| 3593 | /* |
| 3594 | * We may be relocating the only data chunk we have, |
| 3595 | * which could potentially end up with losing data's |
| 3596 | * raid profile, so lets allocate an empty one in |
| 3597 | * advance. |
| 3598 | */ |
| 3599 | ret = btrfs_may_alloc_data_chunk(fs_info, |
| 3600 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3601 | if (ret < 0) { |
| 3602 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3603 | goto error; |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 3604 | } else if (ret == 1) { |
| 3605 | chunk_reserved = 1; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3606 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3607 | } |
| 3608 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 3609 | ret = btrfs_relocate_chunk(fs_info, found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3610 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3611 | if (ret && ret != -ENOSPC) |
| 3612 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3613 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3614 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3615 | } else { |
| 3616 | spin_lock(&fs_info->balance_lock); |
| 3617 | bctl->stat.completed++; |
| 3618 | spin_unlock(&fs_info->balance_lock); |
| 3619 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3620 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3621 | if (found_key.offset == 0) |
| 3622 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3623 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3624 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3625 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3626 | if (counting) { |
| 3627 | btrfs_release_path(path); |
| 3628 | counting = false; |
| 3629 | goto again; |
| 3630 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3631 | error: |
| 3632 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3633 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3634 | btrfs_info(fs_info, "%d enospc errors during balance", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3635 | enospc_errors); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3636 | if (!ret) |
| 3637 | ret = -ENOSPC; |
| 3638 | } |
| 3639 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3640 | return ret; |
| 3641 | } |
| 3642 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3643 | /** |
| 3644 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3645 | * @flags: profile to validate |
| 3646 | * @extended: if true @flags is treated as an extended profile |
| 3647 | */ |
| 3648 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3649 | { |
| 3650 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3651 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3652 | |
| 3653 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3654 | |
| 3655 | /* 1) check that all other bits are zeroed */ |
| 3656 | if (flags & ~mask) |
| 3657 | return 0; |
| 3658 | |
| 3659 | /* 2) see if profile is reduced */ |
| 3660 | if (flags == 0) |
| 3661 | return !extended; /* "0" is valid for usual profiles */ |
| 3662 | |
| 3663 | /* true if exactly one bit set */ |
| 3664 | return (flags & (flags - 1)) == 0; |
| 3665 | } |
| 3666 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3667 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3668 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3669 | /* cancel requested || normal exit path */ |
| 3670 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3671 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3672 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3673 | } |
| 3674 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3675 | /* Non-zero return value signifies invalidity */ |
| 3676 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3677 | u64 allowed) |
| 3678 | { |
| 3679 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3680 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3681 | (bctl_arg->target & ~allowed))); |
| 3682 | } |
| 3683 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3684 | /* |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 3685 | * Should be called with balance mutexe held |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3686 | */ |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3687 | int btrfs_balance(struct btrfs_fs_info *fs_info, |
| 3688 | struct btrfs_balance_control *bctl, |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3689 | struct btrfs_ioctl_balance_args *bargs) |
| 3690 | { |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3691 | u64 meta_target, data_target; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3692 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3693 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3694 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3695 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3696 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3697 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3698 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3699 | atomic_read(&fs_info->balance_pause_req) || |
| 3700 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3701 | ret = -EINVAL; |
| 3702 | goto out; |
| 3703 | } |
| 3704 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3705 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3706 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3707 | mixed = 1; |
| 3708 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3709 | /* |
| 3710 | * In case of mixed groups both data and meta should be picked, |
| 3711 | * and identical options should be given for both of them. |
| 3712 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3713 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3714 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3715 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3716 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3717 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3718 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3719 | "balance: mixed groups data and metadata options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3720 | ret = -EINVAL; |
| 3721 | goto out; |
| 3722 | } |
| 3723 | } |
| 3724 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3725 | num_devices = fs_info->fs_devices->num_devices; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 3726 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3727 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3728 | BUG_ON(num_devices < 1); |
| 3729 | num_devices--; |
| 3730 | } |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 3731 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
Austin S. Hemmelgarn | 88be159 | 2016-03-23 14:22:59 -0400 | [diff] [blame] | 3732 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP; |
| 3733 | if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3734 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3735 | if (num_devices > 2) |
| 3736 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3737 | if (num_devices > 3) |
| 3738 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3739 | BTRFS_BLOCK_GROUP_RAID6); |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3740 | if (validate_convert_profile(&bctl->data, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3741 | int index = btrfs_bg_flags_to_raid_index(bctl->data.target); |
| 3742 | |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3743 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3744 | "balance: invalid convert data profile %s", |
| 3745 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3746 | ret = -EINVAL; |
| 3747 | goto out; |
| 3748 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3749 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3750 | int index = btrfs_bg_flags_to_raid_index(bctl->meta.target); |
| 3751 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3752 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3753 | "balance: invalid convert metadata profile %s", |
| 3754 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3755 | ret = -EINVAL; |
| 3756 | goto out; |
| 3757 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3758 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3759 | int index = btrfs_bg_flags_to_raid_index(bctl->sys.target); |
| 3760 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3761 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3762 | "balance: invalid convert system profile %s", |
| 3763 | get_raid_name(index)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3764 | ret = -EINVAL; |
| 3765 | goto out; |
| 3766 | } |
| 3767 | |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3768 | /* allow to reduce meta or sys integrity only if force set */ |
| 3769 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3770 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3771 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3772 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3773 | do { |
| 3774 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3775 | |
| 3776 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3777 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3778 | !(bctl->sys.target & allowed)) || |
| 3779 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3780 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3781 | !(bctl->meta.target & allowed))) { |
| 3782 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3783 | btrfs_info(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3784 | "balance: force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3785 | } else { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 3786 | btrfs_err(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3787 | "balance: reduces metadata integrity, use --force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3788 | ret = -EINVAL; |
| 3789 | goto out; |
| 3790 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3791 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3792 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3793 | |
Adam Borowski | 1450612 | 2017-03-07 23:34:44 +0100 | [diff] [blame] | 3794 | /* if we're not converting, the target field is uninitialized */ |
| 3795 | meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 3796 | bctl->meta.target : fs_info->avail_metadata_alloc_bits; |
| 3797 | data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ? |
| 3798 | bctl->data.target : fs_info->avail_data_alloc_bits; |
| 3799 | if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) < |
| 3800 | btrfs_get_num_tolerated_disk_barrier_failures(data_target)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3801 | int meta_index = btrfs_bg_flags_to_raid_index(meta_target); |
| 3802 | int data_index = btrfs_bg_flags_to_raid_index(data_target); |
| 3803 | |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3804 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3805 | "balance: metadata profile %s has lower redundancy than data profile %s", |
| 3806 | get_raid_name(meta_index), get_raid_name(data_index)); |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 3809 | ret = insert_balance_item(fs_info, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3810 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3811 | goto out; |
| 3812 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3813 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3814 | BUG_ON(ret == -EEXIST); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 3815 | BUG_ON(fs_info->balance_ctl); |
| 3816 | spin_lock(&fs_info->balance_lock); |
| 3817 | fs_info->balance_ctl = bctl; |
| 3818 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3819 | } else { |
| 3820 | BUG_ON(ret != -EEXIST); |
| 3821 | spin_lock(&fs_info->balance_lock); |
| 3822 | update_balance_args(bctl); |
| 3823 | spin_unlock(&fs_info->balance_lock); |
| 3824 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3825 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3826 | ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
| 3827 | set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3828 | mutex_unlock(&fs_info->balance_mutex); |
| 3829 | |
| 3830 | ret = __btrfs_balance(fs_info); |
| 3831 | |
| 3832 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3833 | clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3834 | |
| 3835 | if (bargs) { |
| 3836 | memset(bargs, 0, sizeof(*bargs)); |
David Sterba | 008ef09 | 2018-03-21 02:05:27 +0100 | [diff] [blame] | 3837 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3838 | } |
| 3839 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3840 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3841 | balance_need_close(fs_info)) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3842 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 3843 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3846 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3847 | |
| 3848 | return ret; |
| 3849 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3850 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 3851 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 3852 | else |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3853 | kfree(bctl); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 3854 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
| 3855 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3856 | return ret; |
| 3857 | } |
| 3858 | |
| 3859 | static int balance_kthread(void *data) |
| 3860 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3861 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3862 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3863 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3864 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3865 | if (fs_info->balance_ctl) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3866 | btrfs_info(fs_info, "balance: resuming"); |
David Sterba | 6fcf6e2 | 2018-05-07 17:44:03 +0200 | [diff] [blame] | 3867 | ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3868 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3869 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3870 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3871 | return ret; |
| 3872 | } |
| 3873 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3874 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3875 | { |
| 3876 | struct task_struct *tsk; |
| 3877 | |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 3878 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3879 | if (!fs_info->balance_ctl) { |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 3880 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3881 | return 0; |
| 3882 | } |
David Sterba | 1354e1a | 2018-03-21 02:29:13 +0100 | [diff] [blame] | 3883 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3884 | |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 3885 | if (btrfs_test_opt(fs_info, SKIP_BALANCE)) { |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3886 | btrfs_info(fs_info, "balance: resume skipped"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3887 | return 0; |
| 3888 | } |
| 3889 | |
Anand Jain | 02ee654 | 2018-05-17 15:16:51 +0800 | [diff] [blame] | 3890 | /* |
| 3891 | * A ro->rw remount sequence should continue with the paused balance |
| 3892 | * regardless of who pauses it, system or the user as of now, so set |
| 3893 | * the resume flag. |
| 3894 | */ |
| 3895 | spin_lock(&fs_info->balance_lock); |
| 3896 | fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME; |
| 3897 | spin_unlock(&fs_info->balance_lock); |
| 3898 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3899 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3900 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3901 | } |
| 3902 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3903 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3904 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3905 | struct btrfs_balance_control *bctl; |
| 3906 | struct btrfs_balance_item *item; |
| 3907 | struct btrfs_disk_balance_args disk_bargs; |
| 3908 | struct btrfs_path *path; |
| 3909 | struct extent_buffer *leaf; |
| 3910 | struct btrfs_key key; |
| 3911 | int ret; |
| 3912 | |
| 3913 | path = btrfs_alloc_path(); |
| 3914 | if (!path) |
| 3915 | return -ENOMEM; |
| 3916 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3917 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3918 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3919 | key.offset = 0; |
| 3920 | |
| 3921 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3922 | if (ret < 0) |
| 3923 | goto out; |
| 3924 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3925 | ret = 0; |
| 3926 | goto out; |
| 3927 | } |
| 3928 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3929 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3930 | if (!bctl) { |
| 3931 | ret = -ENOMEM; |
| 3932 | goto out; |
| 3933 | } |
| 3934 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3935 | leaf = path->nodes[0]; |
| 3936 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3937 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3938 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3939 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3940 | |
| 3941 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3942 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3943 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3944 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3945 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3946 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3947 | |
David Sterba | eee95e3 | 2018-03-20 20:07:58 +0100 | [diff] [blame] | 3948 | /* |
| 3949 | * This should never happen, as the paused balance state is recovered |
| 3950 | * during mount without any chance of other exclusive ops to collide. |
| 3951 | * |
| 3952 | * This gives the exclusive op status to balance and keeps in paused |
| 3953 | * state until user intervention (cancel or umount). If the ownership |
| 3954 | * cannot be assigned, show a message but do not fail. The balance |
| 3955 | * is in a paused state and must have fs_info::balance_ctl properly |
| 3956 | * set up. |
| 3957 | */ |
| 3958 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
| 3959 | btrfs_warn(fs_info, |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 3960 | "balance: cannot set exclusive op status, resume manually"); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3961 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3962 | mutex_lock(&fs_info->balance_mutex); |
David Sterba | 833aae1 | 2018-03-21 02:41:30 +0100 | [diff] [blame] | 3963 | BUG_ON(fs_info->balance_ctl); |
| 3964 | spin_lock(&fs_info->balance_lock); |
| 3965 | fs_info->balance_ctl = bctl; |
| 3966 | spin_unlock(&fs_info->balance_lock); |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3967 | mutex_unlock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3968 | out: |
| 3969 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3970 | return ret; |
| 3971 | } |
| 3972 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3973 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3974 | { |
| 3975 | int ret = 0; |
| 3976 | |
| 3977 | mutex_lock(&fs_info->balance_mutex); |
| 3978 | if (!fs_info->balance_ctl) { |
| 3979 | mutex_unlock(&fs_info->balance_mutex); |
| 3980 | return -ENOTCONN; |
| 3981 | } |
| 3982 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3983 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3984 | atomic_inc(&fs_info->balance_pause_req); |
| 3985 | mutex_unlock(&fs_info->balance_mutex); |
| 3986 | |
| 3987 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3988 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3989 | |
| 3990 | mutex_lock(&fs_info->balance_mutex); |
| 3991 | /* we are good with balance_ctl ripped off from under us */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 3992 | BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3993 | atomic_dec(&fs_info->balance_pause_req); |
| 3994 | } else { |
| 3995 | ret = -ENOTCONN; |
| 3996 | } |
| 3997 | |
| 3998 | mutex_unlock(&fs_info->balance_mutex); |
| 3999 | return ret; |
| 4000 | } |
| 4001 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4002 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 4003 | { |
| 4004 | mutex_lock(&fs_info->balance_mutex); |
| 4005 | if (!fs_info->balance_ctl) { |
| 4006 | mutex_unlock(&fs_info->balance_mutex); |
| 4007 | return -ENOTCONN; |
| 4008 | } |
| 4009 | |
David Sterba | cf7d20f | 2018-03-21 01:45:32 +0100 | [diff] [blame] | 4010 | /* |
| 4011 | * A paused balance with the item stored on disk can be resumed at |
| 4012 | * mount time if the mount is read-write. Otherwise it's still paused |
| 4013 | * and we must not allow cancelling as it deletes the item. |
| 4014 | */ |
| 4015 | if (sb_rdonly(fs_info->sb)) { |
| 4016 | mutex_unlock(&fs_info->balance_mutex); |
| 4017 | return -EROFS; |
| 4018 | } |
| 4019 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4020 | atomic_inc(&fs_info->balance_cancel_req); |
| 4021 | /* |
| 4022 | * if we are running just wait and return, balance item is |
| 4023 | * deleted in btrfs_balance in this case |
| 4024 | */ |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4025 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4026 | mutex_unlock(&fs_info->balance_mutex); |
| 4027 | wait_event(fs_info->balance_wait_q, |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4028 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4029 | mutex_lock(&fs_info->balance_mutex); |
| 4030 | } else { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4031 | mutex_unlock(&fs_info->balance_mutex); |
David Sterba | dccdb07 | 2018-03-21 00:20:05 +0100 | [diff] [blame] | 4032 | /* |
| 4033 | * Lock released to allow other waiters to continue, we'll |
| 4034 | * reexamine the status again. |
| 4035 | */ |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4036 | mutex_lock(&fs_info->balance_mutex); |
| 4037 | |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4038 | if (fs_info->balance_ctl) { |
David Sterba | 149196a | 2018-03-20 20:23:09 +0100 | [diff] [blame] | 4039 | reset_balance_state(fs_info); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4040 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
Anand Jain | 6dac13f | 2018-05-16 10:51:26 +0800 | [diff] [blame] | 4041 | btrfs_info(fs_info, "balance: canceled"); |
David Sterba | a17c95d | 2018-03-20 17:28:05 +0100 | [diff] [blame] | 4042 | } |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4043 | } |
| 4044 | |
David Sterba | 3009a62 | 2018-03-21 01:31:04 +0100 | [diff] [blame] | 4045 | BUG_ON(fs_info->balance_ctl || |
| 4046 | test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)); |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 4047 | atomic_dec(&fs_info->balance_cancel_req); |
| 4048 | mutex_unlock(&fs_info->balance_mutex); |
| 4049 | return 0; |
| 4050 | } |
| 4051 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4052 | static int btrfs_uuid_scan_kthread(void *data) |
| 4053 | { |
| 4054 | struct btrfs_fs_info *fs_info = data; |
| 4055 | struct btrfs_root *root = fs_info->tree_root; |
| 4056 | struct btrfs_key key; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4057 | struct btrfs_path *path = NULL; |
| 4058 | int ret = 0; |
| 4059 | struct extent_buffer *eb; |
| 4060 | int slot; |
| 4061 | struct btrfs_root_item root_item; |
| 4062 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4063 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4064 | |
| 4065 | path = btrfs_alloc_path(); |
| 4066 | if (!path) { |
| 4067 | ret = -ENOMEM; |
| 4068 | goto out; |
| 4069 | } |
| 4070 | |
| 4071 | key.objectid = 0; |
| 4072 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4073 | key.offset = 0; |
| 4074 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4075 | while (1) { |
Anand Jain | 7c829b7 | 2018-03-07 17:29:18 +0800 | [diff] [blame] | 4076 | ret = btrfs_search_forward(root, &key, path, |
| 4077 | BTRFS_OLDEST_GENERATION); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4078 | if (ret) { |
| 4079 | if (ret > 0) |
| 4080 | ret = 0; |
| 4081 | break; |
| 4082 | } |
| 4083 | |
| 4084 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 4085 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 4086 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 4087 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 4088 | goto skip; |
| 4089 | |
| 4090 | eb = path->nodes[0]; |
| 4091 | slot = path->slots[0]; |
| 4092 | item_size = btrfs_item_size_nr(eb, slot); |
| 4093 | if (item_size < sizeof(root_item)) |
| 4094 | goto skip; |
| 4095 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4096 | read_extent_buffer(eb, &root_item, |
| 4097 | btrfs_item_ptr_offset(eb, slot), |
| 4098 | (int)sizeof(root_item)); |
| 4099 | if (btrfs_root_refs(&root_item) == 0) |
| 4100 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4101 | |
| 4102 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4103 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4104 | if (trans) |
| 4105 | goto update_tree; |
| 4106 | |
| 4107 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4108 | /* |
| 4109 | * 1 - subvol uuid item |
| 4110 | * 1 - received_subvol uuid item |
| 4111 | */ |
| 4112 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4113 | if (IS_ERR(trans)) { |
| 4114 | ret = PTR_ERR(trans); |
| 4115 | break; |
| 4116 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4117 | continue; |
| 4118 | } else { |
| 4119 | goto skip; |
| 4120 | } |
| 4121 | update_tree: |
| 4122 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4123 | ret = btrfs_uuid_tree_add(trans, root_item.uuid, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4124 | BTRFS_UUID_KEY_SUBVOL, |
| 4125 | key.objectid); |
| 4126 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4127 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4128 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4129 | break; |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Lu Fengqi | cdb345a | 2018-05-29 15:01:53 +0800 | [diff] [blame] | 4134 | ret = btrfs_uuid_tree_add(trans, |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4135 | root_item.received_uuid, |
| 4136 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4137 | key.objectid); |
| 4138 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4139 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4140 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4141 | break; |
| 4142 | } |
| 4143 | } |
| 4144 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4145 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4146 | if (trans) { |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4147 | ret = btrfs_end_transaction(trans); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4148 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4149 | if (ret) |
| 4150 | break; |
| 4151 | } |
| 4152 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4153 | btrfs_release_path(path); |
| 4154 | if (key.offset < (u64)-1) { |
| 4155 | key.offset++; |
| 4156 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4157 | key.offset = 0; |
| 4158 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4159 | } else if (key.objectid < (u64)-1) { |
| 4160 | key.offset = 0; |
| 4161 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4162 | key.objectid++; |
| 4163 | } else { |
| 4164 | break; |
| 4165 | } |
| 4166 | cond_resched(); |
| 4167 | } |
| 4168 | |
| 4169 | out: |
| 4170 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4171 | if (trans && !IS_ERR(trans)) |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4172 | btrfs_end_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4173 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4174 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4175 | else |
Josef Bacik | afcdd12 | 2016-09-02 15:40:02 -0400 | [diff] [blame] | 4176 | set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4177 | up(&fs_info->uuid_tree_rescan_sem); |
| 4178 | return 0; |
| 4179 | } |
| 4180 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4181 | /* |
| 4182 | * Callback for btrfs_uuid_tree_iterate(). |
| 4183 | * returns: |
| 4184 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4185 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4186 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4187 | */ |
| 4188 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4189 | u8 *uuid, u8 type, u64 subid) |
| 4190 | { |
| 4191 | struct btrfs_key key; |
| 4192 | int ret = 0; |
| 4193 | struct btrfs_root *subvol_root; |
| 4194 | |
| 4195 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4196 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4197 | goto out; |
| 4198 | |
| 4199 | key.objectid = subid; |
| 4200 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4201 | key.offset = (u64)-1; |
| 4202 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4203 | if (IS_ERR(subvol_root)) { |
| 4204 | ret = PTR_ERR(subvol_root); |
| 4205 | if (ret == -ENOENT) |
| 4206 | ret = 1; |
| 4207 | goto out; |
| 4208 | } |
| 4209 | |
| 4210 | switch (type) { |
| 4211 | case BTRFS_UUID_KEY_SUBVOL: |
| 4212 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4213 | ret = 1; |
| 4214 | break; |
| 4215 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4216 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4217 | BTRFS_UUID_SIZE)) |
| 4218 | ret = 1; |
| 4219 | break; |
| 4220 | } |
| 4221 | |
| 4222 | out: |
| 4223 | return ret; |
| 4224 | } |
| 4225 | |
| 4226 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4227 | { |
| 4228 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4229 | int ret; |
| 4230 | |
| 4231 | /* |
| 4232 | * 1st step is to iterate through the existing UUID tree and |
| 4233 | * to delete all entries that contain outdated data. |
| 4234 | * 2nd step is to add all missing entries to the UUID tree. |
| 4235 | */ |
| 4236 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4237 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4238 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4239 | up(&fs_info->uuid_tree_rescan_sem); |
| 4240 | return ret; |
| 4241 | } |
| 4242 | return btrfs_uuid_scan_kthread(data); |
| 4243 | } |
| 4244 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4245 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4246 | { |
| 4247 | struct btrfs_trans_handle *trans; |
| 4248 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4249 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4250 | struct task_struct *task; |
| 4251 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4252 | |
| 4253 | /* |
| 4254 | * 1 - root node |
| 4255 | * 1 - root item |
| 4256 | */ |
| 4257 | trans = btrfs_start_transaction(tree_root, 2); |
| 4258 | if (IS_ERR(trans)) |
| 4259 | return PTR_ERR(trans); |
| 4260 | |
| 4261 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4262 | BTRFS_UUID_TREE_OBJECTID); |
| 4263 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4264 | ret = PTR_ERR(uuid_root); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 4265 | btrfs_abort_transaction(trans, ret); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4266 | btrfs_end_transaction(trans); |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4267 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | fs_info->uuid_root = uuid_root; |
| 4271 | |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4272 | ret = btrfs_commit_transaction(trans); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4273 | if (ret) |
| 4274 | return ret; |
| 4275 | |
| 4276 | down(&fs_info->uuid_tree_rescan_sem); |
| 4277 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4278 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4279 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4280 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4281 | up(&fs_info->uuid_tree_rescan_sem); |
| 4282 | return PTR_ERR(task); |
| 4283 | } |
| 4284 | |
| 4285 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4286 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4287 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4288 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4289 | { |
| 4290 | struct task_struct *task; |
| 4291 | |
| 4292 | down(&fs_info->uuid_tree_rescan_sem); |
| 4293 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4294 | if (IS_ERR(task)) { |
| 4295 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4296 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4297 | up(&fs_info->uuid_tree_rescan_sem); |
| 4298 | return PTR_ERR(task); |
| 4299 | } |
| 4300 | |
| 4301 | return 0; |
| 4302 | } |
| 4303 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4304 | /* |
| 4305 | * shrinking a device means finding all of the device extents past |
| 4306 | * the new size, and then following the back refs to the chunks. |
| 4307 | * The chunk relocation code actually frees the device extent |
| 4308 | */ |
| 4309 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4310 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4311 | struct btrfs_fs_info *fs_info = device->fs_info; |
| 4312 | struct btrfs_root *root = fs_info->dev_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4313 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4314 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4315 | struct btrfs_path *path; |
| 4316 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4317 | u64 chunk_offset; |
| 4318 | int ret; |
| 4319 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4320 | int failed = 0; |
| 4321 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4322 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4323 | struct extent_buffer *l; |
| 4324 | struct btrfs_key key; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4325 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4326 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4327 | u64 old_size = btrfs_device_get_total_bytes(device); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4328 | u64 diff; |
| 4329 | |
| 4330 | new_size = round_down(new_size, fs_info->sectorsize); |
Nikolay Borisov | 0e4324a | 2017-07-21 11:28:24 +0300 | [diff] [blame] | 4331 | diff = round_down(old_size - new_size, fs_info->sectorsize); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4332 | |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4333 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4334 | return -EINVAL; |
| 4335 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4336 | path = btrfs_alloc_path(); |
| 4337 | if (!path) |
| 4338 | return -ENOMEM; |
| 4339 | |
Gu Jinxiang | 0338dff | 2018-04-27 16:22:07 +0800 | [diff] [blame] | 4340 | path->reada = READA_BACK; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4341 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4342 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4343 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4344 | btrfs_device_set_total_bytes(device, new_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4345 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4346 | device->fs_devices->total_rw_bytes -= diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4347 | atomic64_sub(diff, &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4348 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4349 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4350 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4351 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4352 | key.objectid = device->devid; |
| 4353 | key.offset = (u64)-1; |
| 4354 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4355 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4356 | do { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4357 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4358 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4359 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4360 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4361 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4362 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4363 | |
| 4364 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4365 | if (ret) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4366 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4367 | if (ret < 0) |
| 4368 | goto done; |
| 4369 | if (ret) { |
| 4370 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4371 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4372 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4373 | } |
| 4374 | |
| 4375 | l = path->nodes[0]; |
| 4376 | slot = path->slots[0]; |
| 4377 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4378 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4379 | if (key.objectid != device->devid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4380 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4381 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4382 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4383 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4384 | |
| 4385 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4386 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4387 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4388 | if (key.offset + length <= new_size) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4389 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4390 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4391 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4392 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4393 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4394 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4395 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4396 | |
Liu Bo | a6f93c7 | 2017-11-15 16:28:11 -0700 | [diff] [blame] | 4397 | /* |
| 4398 | * We may be relocating the only data chunk we have, |
| 4399 | * which could potentially end up with losing data's |
| 4400 | * raid profile, so lets allocate an empty one in |
| 4401 | * advance. |
| 4402 | */ |
| 4403 | ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset); |
| 4404 | if (ret < 0) { |
| 4405 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 4406 | goto done; |
| 4407 | } |
| 4408 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4409 | ret = btrfs_relocate_chunk(fs_info, chunk_offset); |
| 4410 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4411 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4412 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4413 | if (ret == -ENOSPC) |
| 4414 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4415 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4416 | |
| 4417 | if (failed && !retried) { |
| 4418 | failed = 0; |
| 4419 | retried = true; |
| 4420 | goto again; |
| 4421 | } else if (failed && retried) { |
| 4422 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4423 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4424 | } |
| 4425 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4426 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4427 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4428 | if (IS_ERR(trans)) { |
| 4429 | ret = PTR_ERR(trans); |
| 4430 | goto done; |
| 4431 | } |
| 4432 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4433 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4434 | |
| 4435 | /* |
| 4436 | * We checked in the above loop all device extents that were already in |
| 4437 | * the device tree. However before we have updated the device's |
| 4438 | * total_bytes to the new size, we might have had chunk allocations that |
| 4439 | * have not complete yet (new block groups attached to transaction |
| 4440 | * handles), and therefore their device extents were not yet in the |
| 4441 | * device tree and we missed them in the loop above. So if we have any |
| 4442 | * pending chunk using a device extent that overlaps the device range |
| 4443 | * that we can not use anymore, commit the current transaction and |
| 4444 | * repeat the search on the device tree - this way we guarantee we will |
| 4445 | * not have chunks using device extents that end beyond 'new_size'. |
| 4446 | */ |
| 4447 | if (!checked_pending_chunks) { |
| 4448 | u64 start = new_size; |
| 4449 | u64 len = old_size - new_size; |
| 4450 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 4451 | if (contains_pending_extent(trans->transaction, device, |
| 4452 | &start, len)) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4453 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4454 | checked_pending_chunks = true; |
| 4455 | failed = 0; |
| 4456 | retried = false; |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4457 | ret = btrfs_commit_transaction(trans); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4458 | if (ret) |
| 4459 | goto done; |
| 4460 | goto again; |
| 4461 | } |
| 4462 | } |
| 4463 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4464 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4465 | if (list_empty(&device->resized_list)) |
| 4466 | list_add_tail(&device->resized_list, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4467 | &fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4468 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4469 | WARN_ON(diff > old_total); |
Nikolay Borisov | 7dfb8be | 2017-06-16 14:39:20 +0300 | [diff] [blame] | 4470 | btrfs_set_super_total_bytes(super_copy, |
| 4471 | round_down(old_total - diff, fs_info->sectorsize)); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4472 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4473 | |
| 4474 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4475 | ret = btrfs_update_device(trans, device); |
Jeff Mahoney | 3a45bb2 | 2016-09-09 21:39:03 -0400 | [diff] [blame] | 4476 | btrfs_end_transaction(trans); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4477 | done: |
| 4478 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4479 | if (ret) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4480 | mutex_lock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4481 | btrfs_device_set_total_bytes(device, old_size); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4482 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4483 | device->fs_devices->total_rw_bytes += diff; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4484 | atomic64_add(diff, &fs_info->free_chunk_space); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4485 | mutex_unlock(&fs_info->chunk_mutex); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4486 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4487 | return ret; |
| 4488 | } |
| 4489 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4490 | static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4491 | struct btrfs_key *key, |
| 4492 | struct btrfs_chunk *chunk, int item_size) |
| 4493 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4494 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4495 | struct btrfs_disk_key disk_key; |
| 4496 | u32 array_size; |
| 4497 | u8 *ptr; |
| 4498 | |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4499 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4500 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4501 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4502 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4503 | mutex_unlock(&fs_info->chunk_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4504 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4505 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4506 | |
| 4507 | ptr = super_copy->sys_chunk_array + array_size; |
| 4508 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4509 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4510 | ptr += sizeof(disk_key); |
| 4511 | memcpy(ptr, chunk, item_size); |
| 4512 | item_size += sizeof(disk_key); |
| 4513 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 4514 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4515 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4516 | return 0; |
| 4517 | } |
| 4518 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4519 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4520 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4521 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4522 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4523 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4524 | const struct btrfs_device_info *di_a = a; |
| 4525 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4526 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4527 | if (di_a->max_avail > di_b->max_avail) |
| 4528 | return -1; |
| 4529 | if (di_a->max_avail < di_b->max_avail) |
| 4530 | return 1; |
| 4531 | if (di_a->total_avail > di_b->total_avail) |
| 4532 | return -1; |
| 4533 | if (di_a->total_avail < di_b->total_avail) |
| 4534 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4535 | return 0; |
| 4536 | } |
| 4537 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4538 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4539 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4540 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4541 | return; |
| 4542 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4543 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4544 | } |
| 4545 | |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4546 | #define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \ |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4547 | - sizeof(struct btrfs_chunk)) \ |
| 4548 | / sizeof(struct btrfs_stripe) + 1) |
| 4549 | |
| 4550 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4551 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4552 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4553 | / sizeof(struct btrfs_stripe) + 1) |
| 4554 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4555 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4556 | u64 start, u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4557 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4558 | struct btrfs_fs_info *info = trans->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4559 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4560 | struct btrfs_device *device; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4561 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4562 | struct extent_map_tree *em_tree; |
| 4563 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4564 | struct btrfs_device_info *devices_info = NULL; |
| 4565 | u64 total_avail; |
| 4566 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4567 | int data_stripes; /* number of stripes that count for |
| 4568 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4569 | int sub_stripes; /* sub_stripes info for map */ |
| 4570 | int dev_stripes; /* stripes per dev */ |
| 4571 | int devs_max; /* max devs to use */ |
| 4572 | int devs_min; /* min devs needed */ |
| 4573 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4574 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4575 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4576 | u64 max_stripe_size; |
| 4577 | u64 max_chunk_size; |
| 4578 | u64 stripe_size; |
| 4579 | u64 num_bytes; |
| 4580 | int ndevs; |
| 4581 | int i; |
| 4582 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4583 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4584 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4585 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4586 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4587 | if (list_empty(&fs_devices->alloc_list)) { |
| 4588 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4589 | btrfs_debug(info, "%s: no writable device", __func__); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4590 | return -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4591 | } |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4592 | |
Qu Wenruo | 3e72ee8 | 2018-01-30 18:20:45 +0800 | [diff] [blame] | 4593 | index = btrfs_bg_flags_to_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4594 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4595 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4596 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4597 | devs_max = btrfs_raid_array[index].devs_max; |
| 4598 | devs_min = btrfs_raid_array[index].devs_min; |
| 4599 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4600 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4601 | |
| 4602 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4603 | max_stripe_size = SZ_1G; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 4604 | max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4605 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4606 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4607 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4608 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4609 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4610 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4611 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4612 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4613 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4614 | if (!devs_max) |
Qu Wenruo | 062d4d1 | 2018-01-30 18:20:46 +0800 | [diff] [blame] | 4615 | devs_max = BTRFS_MAX_DEVS(info); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4616 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4617 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4618 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4619 | if (!devs_max) |
| 4620 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4621 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4622 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4623 | type); |
| 4624 | BUG_ON(1); |
| 4625 | } |
| 4626 | |
| 4627 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4628 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4629 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4630 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4631 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4632 | GFP_NOFS); |
| 4633 | if (!devices_info) |
| 4634 | return -ENOMEM; |
| 4635 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4636 | /* |
| 4637 | * in the first pass through the devices list, we gather information |
| 4638 | * about the available holes on each device. |
| 4639 | */ |
| 4640 | ndevs = 0; |
Nikolay Borisov | ebcc930 | 2017-06-27 10:02:24 +0300 | [diff] [blame] | 4641 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4642 | u64 max_avail; |
| 4643 | u64 dev_offset; |
| 4644 | |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 4645 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4646 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4647 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4648 | continue; |
| 4649 | } |
| 4650 | |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 4651 | if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 4652 | &device->dev_state) || |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 4653 | test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4654 | continue; |
| 4655 | |
| 4656 | if (device->total_bytes > device->bytes_used) |
| 4657 | total_avail = device->total_bytes - device->bytes_used; |
| 4658 | else |
| 4659 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4660 | |
| 4661 | /* If there is no space on this device, skip it. */ |
| 4662 | if (total_avail == 0) |
| 4663 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4664 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4665 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4666 | max_stripe_size * dev_stripes, |
| 4667 | &dev_offset, &max_avail); |
| 4668 | if (ret && ret != -ENOSPC) |
| 4669 | goto error; |
| 4670 | |
| 4671 | if (ret == 0) |
| 4672 | max_avail = max_stripe_size * dev_stripes; |
| 4673 | |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4674 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) { |
| 4675 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) |
| 4676 | btrfs_debug(info, |
| 4677 | "%s: devid %llu has no free space, have=%llu want=%u", |
| 4678 | __func__, device->devid, max_avail, |
| 4679 | BTRFS_STRIPE_LEN * dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4680 | continue; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4681 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4682 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4683 | if (ndevs == fs_devices->rw_devices) { |
| 4684 | WARN(1, "%s: found more than %llu devices\n", |
| 4685 | __func__, fs_devices->rw_devices); |
| 4686 | break; |
| 4687 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4688 | devices_info[ndevs].dev_offset = dev_offset; |
| 4689 | devices_info[ndevs].max_avail = max_avail; |
| 4690 | devices_info[ndevs].total_avail = total_avail; |
| 4691 | devices_info[ndevs].dev = device; |
| 4692 | ++ndevs; |
| 4693 | } |
| 4694 | |
| 4695 | /* |
| 4696 | * now sort the devices by hole size / available space |
| 4697 | */ |
| 4698 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4699 | btrfs_cmp_device_info, NULL); |
| 4700 | |
| 4701 | /* round down to number of usable stripes */ |
Nikolay Borisov | e5600fd | 2017-06-27 10:02:25 +0300 | [diff] [blame] | 4702 | ndevs = round_down(ndevs, devs_increment); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4703 | |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4704 | if (ndevs < devs_min) { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4705 | ret = -ENOSPC; |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4706 | if (btrfs_test_opt(info, ENOSPC_DEBUG)) { |
| 4707 | btrfs_debug(info, |
| 4708 | "%s: not enough devices with free space: have=%d minimum required=%d", |
Qu Wenruo | ba89b80 | 2018-01-31 13:56:15 +0800 | [diff] [blame] | 4709 | __func__, ndevs, devs_min); |
Qu Wenruo | 4117f20 | 2018-01-22 13:50:54 +0800 | [diff] [blame] | 4710 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4711 | goto error; |
| 4712 | } |
| 4713 | |
Nikolay Borisov | f148ef4 | 2017-06-27 10:02:26 +0300 | [diff] [blame] | 4714 | ndevs = min(ndevs, devs_max); |
| 4715 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4716 | /* |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4717 | * The primary goal is to maximize the number of stripes, so use as |
| 4718 | * many devices as possible, even if the stripes are not maximum sized. |
| 4719 | * |
| 4720 | * The DUP profile stores more than one stripe per device, the |
| 4721 | * max_avail is the total size so we have to adjust. |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4722 | */ |
Hans van Kranenburg | 92e222d | 2018-02-05 17:45:11 +0100 | [diff] [blame] | 4723 | stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4724 | num_stripes = ndevs * dev_stripes; |
| 4725 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4726 | /* |
| 4727 | * this will have to be fixed for RAID1 and RAID10 over |
| 4728 | * more drives |
| 4729 | */ |
| 4730 | data_stripes = num_stripes / ncopies; |
| 4731 | |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4732 | if (type & BTRFS_BLOCK_GROUP_RAID5) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4733 | data_stripes = num_stripes - 1; |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4734 | |
| 4735 | if (type & BTRFS_BLOCK_GROUP_RAID6) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4736 | data_stripes = num_stripes - 2; |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4737 | |
| 4738 | /* |
| 4739 | * Use the number of data stripes to figure out how big this chunk |
| 4740 | * is really going to be in terms of logical address space, |
| 4741 | * and compare that answer with the max chunk size |
| 4742 | */ |
| 4743 | if (stripe_size * data_stripes > max_chunk_size) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4744 | stripe_size = div_u64(max_chunk_size, data_stripes); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4745 | |
| 4746 | /* bump the answer up to a 16MB boundary */ |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4747 | stripe_size = round_up(stripe_size, SZ_16M); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4748 | |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4749 | /* |
| 4750 | * But don't go higher than the limits we found while searching |
| 4751 | * for free extents |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4752 | */ |
Qu Wenruo | 793ff2c | 2018-01-31 14:16:34 +0800 | [diff] [blame] | 4753 | stripe_size = min(devices_info[ndevs - 1].max_avail, |
| 4754 | stripe_size); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4755 | } |
| 4756 | |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4757 | /* align to BTRFS_STRIPE_LEN */ |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4758 | stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4759 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4760 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4761 | if (!map) { |
| 4762 | ret = -ENOMEM; |
| 4763 | goto error; |
| 4764 | } |
| 4765 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4766 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4767 | for (i = 0; i < ndevs; ++i) { |
| 4768 | for (j = 0; j < dev_stripes; ++j) { |
| 4769 | int s = i * dev_stripes + j; |
| 4770 | map->stripes[s].dev = devices_info[i].dev; |
| 4771 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4772 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4773 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4774 | } |
Nikolay Borisov | 500ceed | 2017-07-14 09:55:41 +0300 | [diff] [blame] | 4775 | map->stripe_len = BTRFS_STRIPE_LEN; |
| 4776 | map->io_align = BTRFS_STRIPE_LEN; |
| 4777 | map->io_width = BTRFS_STRIPE_LEN; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4778 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4779 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4780 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4781 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4782 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4783 | trace_btrfs_chunk_alloc(info, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4784 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4785 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4786 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4787 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4788 | ret = -ENOMEM; |
| 4789 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4790 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4791 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 4792 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4793 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4794 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4795 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4796 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4797 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4798 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4799 | em_tree = &info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4800 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4801 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4802 | if (ret) { |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 4803 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4804 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4805 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4806 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4807 | |
Nikolay Borisov | 1efb72a | 2017-08-21 12:43:49 +0300 | [diff] [blame] | 4808 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4809 | refcount_inc(&em->refs); |
| 4810 | write_unlock(&em_tree->lock); |
| 4811 | |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 4812 | ret = btrfs_make_block_group(trans, 0, type, start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4813 | if (ret) |
| 4814 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4815 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4816 | for (i = 0; i < map->num_stripes; i++) { |
| 4817 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4818 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4819 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4820 | |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 4821 | atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space); |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4822 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4823 | free_extent_map(em); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4824 | check_raid56_incompat_flag(info, type); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4825 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4826 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4827 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4828 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4829 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4830 | write_lock(&em_tree->lock); |
| 4831 | remove_extent_mapping(em_tree, em); |
| 4832 | write_unlock(&em_tree->lock); |
| 4833 | |
| 4834 | /* One for our allocation */ |
| 4835 | free_extent_map(em); |
| 4836 | /* One for the tree reference */ |
| 4837 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4838 | /* One for the pending_chunks list reference */ |
| 4839 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4840 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4841 | kfree(devices_info); |
| 4842 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4843 | } |
| 4844 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4845 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4846 | struct btrfs_fs_info *fs_info, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4847 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4848 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 4849 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4850 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4851 | struct btrfs_key key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4852 | struct btrfs_device *device; |
| 4853 | struct btrfs_chunk *chunk; |
| 4854 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4855 | struct extent_map *em; |
| 4856 | struct map_lookup *map; |
| 4857 | size_t item_size; |
| 4858 | u64 dev_offset; |
| 4859 | u64 stripe_size; |
| 4860 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 4861 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4862 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 4863 | em = get_chunk_map(fs_info, chunk_offset, chunk_size); |
| 4864 | if (IS_ERR(em)) |
| 4865 | return PTR_ERR(em); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4866 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 4867 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4868 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4869 | stripe_size = em->orig_block_len; |
| 4870 | |
| 4871 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4872 | if (!chunk) { |
| 4873 | ret = -ENOMEM; |
| 4874 | goto out; |
| 4875 | } |
| 4876 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4877 | /* |
| 4878 | * Take the device list mutex to prevent races with the final phase of |
| 4879 | * a device replace operation that replaces the device object associated |
| 4880 | * with the map's stripes, because the device object's id can change |
| 4881 | * at any time during that final phase of the device replace operation |
| 4882 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 4883 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4884 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4885 | for (i = 0; i < map->num_stripes; i++) { |
| 4886 | device = map->stripes[i].dev; |
| 4887 | dev_offset = map->stripes[i].physical; |
| 4888 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4889 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4890 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4891 | break; |
Nikolay Borisov | b5d9071 | 2017-08-18 17:58:23 +0300 | [diff] [blame] | 4892 | ret = btrfs_alloc_dev_extent(trans, device, chunk_offset, |
| 4893 | dev_offset, stripe_size); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4894 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4895 | break; |
| 4896 | } |
| 4897 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4898 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4899 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4900 | } |
| 4901 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4902 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4903 | for (i = 0; i < map->num_stripes; i++) { |
| 4904 | device = map->stripes[i].dev; |
| 4905 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4906 | |
| 4907 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4908 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4909 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4910 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4911 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4912 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4913 | |
| 4914 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4915 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4916 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4917 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4918 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4919 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4920 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4921 | btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4922 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4923 | |
| 4924 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4925 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4926 | key.offset = chunk_offset; |
| 4927 | |
| 4928 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4929 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4930 | /* |
| 4931 | * TODO: Cleanup of inserted chunk root in case of |
| 4932 | * failure. |
| 4933 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4934 | ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4935 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4936 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4937 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4938 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4939 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4940 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | /* |
| 4944 | * Chunk allocation falls into two parts. The first part does works |
| 4945 | * that make the new allocated chunk useable, but not do any operation |
| 4946 | * that modifies the chunk tree. The second part does the works that |
| 4947 | * require modifying the chunk tree. This division is important for the |
| 4948 | * bootstrap process of adding storage to a seed btrfs. |
| 4949 | */ |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 4950 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4951 | { |
| 4952 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4953 | |
Nikolay Borisov | c216b20 | 2018-06-20 15:49:06 +0300 | [diff] [blame] | 4954 | lockdep_assert_held(&trans->fs_info->chunk_mutex); |
| 4955 | chunk_offset = find_next_chunk(trans->fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4956 | return __btrfs_alloc_chunk(trans, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4957 | } |
| 4958 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4959 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
David Sterba | e4a4dce | 2017-02-10 19:49:01 +0100 | [diff] [blame] | 4960 | struct btrfs_fs_info *fs_info) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4961 | { |
| 4962 | u64 chunk_offset; |
| 4963 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4964 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4965 | int ret; |
| 4966 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4967 | chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 4968 | alloc_profile = btrfs_metadata_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4969 | ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4970 | if (ret) |
| 4971 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4972 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4973 | sys_chunk_offset = find_next_chunk(fs_info); |
Jeff Mahoney | 1b86826 | 2017-05-17 11:38:35 -0400 | [diff] [blame] | 4974 | alloc_profile = btrfs_system_alloc_profile(fs_info); |
David Sterba | 72b468c | 2017-02-10 19:46:27 +0100 | [diff] [blame] | 4975 | ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4976 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4977 | } |
| 4978 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4979 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4980 | { |
| 4981 | int max_errors; |
| 4982 | |
| 4983 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4984 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4985 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4986 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4987 | max_errors = 1; |
| 4988 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4989 | max_errors = 2; |
| 4990 | } else { |
| 4991 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4992 | } |
| 4993 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4994 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4995 | } |
| 4996 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4997 | 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] | 4998 | { |
| 4999 | struct extent_map *em; |
| 5000 | struct map_lookup *map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5001 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5002 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5003 | int i; |
| 5004 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5005 | em = get_chunk_map(fs_info, chunk_offset, 1); |
| 5006 | if (IS_ERR(em)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5007 | return 1; |
| 5008 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5009 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5010 | for (i = 0; i < map->num_stripes; i++) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 5011 | if (test_bit(BTRFS_DEV_STATE_MISSING, |
| 5012 | &map->stripes[i].dev->dev_state)) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5013 | miss_ndevs++; |
| 5014 | continue; |
| 5015 | } |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 5016 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, |
| 5017 | &map->stripes[i].dev->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5018 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5019 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5020 | } |
| 5021 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5022 | |
| 5023 | /* |
| 5024 | * If the number of missing devices is larger than max errors, |
| 5025 | * we can not write the data into that chunk successfully, so |
| 5026 | * set it readonly. |
| 5027 | */ |
| 5028 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 5029 | readonly = 1; |
| 5030 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5031 | free_extent_map(em); |
| 5032 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 5036 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 5037 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 5041 | { |
| 5042 | struct extent_map *em; |
| 5043 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5044 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5045 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5046 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 5047 | if (em) |
| 5048 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5049 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5050 | if (!em) |
| 5051 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5052 | /* once for us */ |
| 5053 | free_extent_map(em); |
| 5054 | /* once for the tree */ |
| 5055 | free_extent_map(em); |
| 5056 | } |
| 5057 | } |
| 5058 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 5059 | 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] | 5060 | { |
| 5061 | struct extent_map *em; |
| 5062 | struct map_lookup *map; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5063 | int ret; |
| 5064 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5065 | em = get_chunk_map(fs_info, logical, len); |
| 5066 | if (IS_ERR(em)) |
| 5067 | /* |
| 5068 | * We could return errors for these cases, but that could get |
| 5069 | * ugly and we'd probably do the same thing which is just not do |
| 5070 | * anything else and exit, so return 1 so the callers don't try |
| 5071 | * to use other copies. |
| 5072 | */ |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5073 | return 1; |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5074 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5075 | map = em->map_lookup; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5076 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 5077 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5078 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5079 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5080 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5081 | ret = 2; |
| 5082 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5083 | /* |
| 5084 | * There could be two corrupted data stripes, we need |
| 5085 | * to loop retry in order to rebuild the correct data. |
Nikolay Borisov | e7e0209 | 2018-06-20 15:48:55 +0300 | [diff] [blame] | 5086 | * |
Liu Bo | 8810f75 | 2018-01-02 13:36:41 -0700 | [diff] [blame] | 5087 | * Fail a stripe at a time on every retry except the |
| 5088 | * stripe under reconstruction. |
| 5089 | */ |
| 5090 | ret = map->num_stripes; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5091 | else |
| 5092 | ret = 1; |
| 5093 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5094 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5095 | btrfs_dev_replace_read_lock(&fs_info->dev_replace); |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5096 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) && |
| 5097 | fs_info->dev_replace.tgtdev) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5098 | ret++; |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5099 | btrfs_dev_replace_read_unlock(&fs_info->dev_replace); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5100 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5101 | return ret; |
| 5102 | } |
| 5103 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5104 | unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5105 | u64 logical) |
| 5106 | { |
| 5107 | struct extent_map *em; |
| 5108 | struct map_lookup *map; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5109 | unsigned long len = fs_info->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5110 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5111 | em = get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5112 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5113 | if (!WARN_ON(IS_ERR(em))) { |
| 5114 | map = em->map_lookup; |
| 5115 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5116 | len = map->stripe_len * nr_data_stripes(map); |
| 5117 | free_extent_map(em); |
| 5118 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5119 | return len; |
| 5120 | } |
| 5121 | |
Nikolay Borisov | e4ff5fb | 2017-07-19 10:48:42 +0300 | [diff] [blame] | 5122 | 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] | 5123 | { |
| 5124 | struct extent_map *em; |
| 5125 | struct map_lookup *map; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5126 | int ret = 0; |
| 5127 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5128 | em = get_chunk_map(fs_info, logical, len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5129 | |
Nikolay Borisov | 69f03f1 | 2017-07-11 16:55:51 +0300 | [diff] [blame] | 5130 | if(!WARN_ON(IS_ERR(em))) { |
| 5131 | map = em->map_lookup; |
| 5132 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
| 5133 | ret = 1; |
| 5134 | free_extent_map(em); |
| 5135 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5136 | return ret; |
| 5137 | } |
| 5138 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5139 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5140 | struct map_lookup *map, int first, |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5141 | int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5142 | { |
| 5143 | int i; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5144 | int num_stripes; |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5145 | int preferred_mirror; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5146 | int tolerance; |
| 5147 | struct btrfs_device *srcdev; |
| 5148 | |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5149 | ASSERT((map->type & |
| 5150 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))); |
| 5151 | |
| 5152 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5153 | num_stripes = map->sub_stripes; |
| 5154 | else |
| 5155 | num_stripes = map->num_stripes; |
| 5156 | |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5157 | preferred_mirror = first + current->pid % num_stripes; |
| 5158 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5159 | if (dev_replace_is_ongoing && |
| 5160 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5161 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5162 | srcdev = fs_info->dev_replace.srcdev; |
| 5163 | else |
| 5164 | srcdev = NULL; |
| 5165 | |
| 5166 | /* |
| 5167 | * try to avoid the drive that is the source drive for a |
| 5168 | * dev-replace procedure, only choose it if no other non-missing |
| 5169 | * mirror is available |
| 5170 | */ |
| 5171 | for (tolerance = 0; tolerance < 2; tolerance++) { |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5172 | if (map->stripes[preferred_mirror].dev->bdev && |
| 5173 | (tolerance || map->stripes[preferred_mirror].dev != srcdev)) |
| 5174 | return preferred_mirror; |
Anand Jain | 99f92a7 | 2018-03-14 16:29:12 +0800 | [diff] [blame] | 5175 | for (i = first; i < first + num_stripes; i++) { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5176 | if (map->stripes[i].dev->bdev && |
| 5177 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5178 | return i; |
| 5179 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5180 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5181 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5182 | /* we couldn't find one that doesn't fail. Just return something |
| 5183 | * and the io error handling code will clean up eventually |
| 5184 | */ |
Anand Jain | 8ba0ae7 | 2018-03-14 16:29:13 +0800 | [diff] [blame] | 5185 | return preferred_mirror; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5186 | } |
| 5187 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5188 | static inline int parity_smaller(u64 a, u64 b) |
| 5189 | { |
| 5190 | return a > b; |
| 5191 | } |
| 5192 | |
| 5193 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5194 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5195 | { |
| 5196 | struct btrfs_bio_stripe s; |
| 5197 | int i; |
| 5198 | u64 l; |
| 5199 | int again = 1; |
| 5200 | |
| 5201 | while (again) { |
| 5202 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5203 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5204 | if (parity_smaller(bbio->raid_map[i], |
| 5205 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5206 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5207 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5208 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5209 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5210 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5211 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5212 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5213 | again = 1; |
| 5214 | } |
| 5215 | } |
| 5216 | } |
| 5217 | } |
| 5218 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5219 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5220 | { |
| 5221 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5222 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5223 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5224 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5225 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5226 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5227 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5228 | /* |
| 5229 | * plus the raid_map, which includes both the tgt dev |
| 5230 | * and the stripes |
| 5231 | */ |
| 5232 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5233 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5234 | |
| 5235 | atomic_set(&bbio->error, 0); |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5236 | refcount_set(&bbio->refs, 1); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5237 | |
| 5238 | return bbio; |
| 5239 | } |
| 5240 | |
| 5241 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5242 | { |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5243 | WARN_ON(!refcount_read(&bbio->refs)); |
| 5244 | refcount_inc(&bbio->refs); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5248 | { |
| 5249 | if (!bbio) |
| 5250 | return; |
Elena Reshetova | 140475a | 2017-03-03 10:55:10 +0200 | [diff] [blame] | 5251 | if (refcount_dec_and_test(&bbio->refs)) |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5252 | kfree(bbio); |
| 5253 | } |
| 5254 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5255 | /* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */ |
| 5256 | /* |
| 5257 | * Please note that, discard won't be sent to target device of device |
| 5258 | * replace. |
| 5259 | */ |
| 5260 | static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info, |
| 5261 | u64 logical, u64 length, |
| 5262 | struct btrfs_bio **bbio_ret) |
| 5263 | { |
| 5264 | struct extent_map *em; |
| 5265 | struct map_lookup *map; |
| 5266 | struct btrfs_bio *bbio; |
| 5267 | u64 offset; |
| 5268 | u64 stripe_nr; |
| 5269 | u64 stripe_nr_end; |
| 5270 | u64 stripe_end_offset; |
| 5271 | u64 stripe_cnt; |
| 5272 | u64 stripe_len; |
| 5273 | u64 stripe_offset; |
| 5274 | u64 num_stripes; |
| 5275 | u32 stripe_index; |
| 5276 | u32 factor = 0; |
| 5277 | u32 sub_stripes = 0; |
| 5278 | u64 stripes_per_dev = 0; |
| 5279 | u32 remaining_stripes = 0; |
| 5280 | u32 last_stripe = 0; |
| 5281 | int ret = 0; |
| 5282 | int i; |
| 5283 | |
| 5284 | /* discard always return a bbio */ |
| 5285 | ASSERT(bbio_ret); |
| 5286 | |
| 5287 | em = get_chunk_map(fs_info, logical, length); |
| 5288 | if (IS_ERR(em)) |
| 5289 | return PTR_ERR(em); |
| 5290 | |
| 5291 | map = em->map_lookup; |
| 5292 | /* we don't discard raid56 yet */ |
| 5293 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
| 5294 | ret = -EOPNOTSUPP; |
| 5295 | goto out; |
| 5296 | } |
| 5297 | |
| 5298 | offset = logical - em->start; |
| 5299 | length = min_t(u64, em->len - offset, length); |
| 5300 | |
| 5301 | stripe_len = map->stripe_len; |
| 5302 | /* |
| 5303 | * stripe_nr counts the total number of stripes we have to stride |
| 5304 | * to get to this block |
| 5305 | */ |
| 5306 | stripe_nr = div64_u64(offset, stripe_len); |
| 5307 | |
| 5308 | /* stripe_offset is the offset of this block in its stripe */ |
| 5309 | stripe_offset = offset - stripe_nr * stripe_len; |
| 5310 | |
| 5311 | stripe_nr_end = round_up(offset + length, map->stripe_len); |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5312 | stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len); |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5313 | stripe_cnt = stripe_nr_end - stripe_nr; |
| 5314 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5315 | (offset + length); |
| 5316 | /* |
| 5317 | * after this, stripe_nr is the number of stripes on this |
| 5318 | * device we have to walk to find the data, and stripe_index is |
| 5319 | * the number of our device in the stripe array |
| 5320 | */ |
| 5321 | num_stripes = 1; |
| 5322 | stripe_index = 0; |
| 5323 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5324 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5325 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5326 | sub_stripes = 1; |
| 5327 | else |
| 5328 | sub_stripes = map->sub_stripes; |
| 5329 | |
| 5330 | factor = map->num_stripes / sub_stripes; |
| 5331 | num_stripes = min_t(u64, map->num_stripes, |
| 5332 | sub_stripes * stripe_cnt); |
| 5333 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
| 5334 | stripe_index *= sub_stripes; |
| 5335 | stripes_per_dev = div_u64_rem(stripe_cnt, factor, |
| 5336 | &remaining_stripes); |
| 5337 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5338 | last_stripe *= sub_stripes; |
| 5339 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5340 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5341 | num_stripes = map->num_stripes; |
| 5342 | } else { |
| 5343 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5344 | &stripe_index); |
| 5345 | } |
| 5346 | |
| 5347 | bbio = alloc_btrfs_bio(num_stripes, 0); |
| 5348 | if (!bbio) { |
| 5349 | ret = -ENOMEM; |
| 5350 | goto out; |
| 5351 | } |
| 5352 | |
| 5353 | for (i = 0; i < num_stripes; i++) { |
| 5354 | bbio->stripes[i].physical = |
| 5355 | map->stripes[stripe_index].physical + |
| 5356 | stripe_offset + stripe_nr * map->stripe_len; |
| 5357 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
| 5358 | |
| 5359 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5360 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5361 | bbio->stripes[i].length = stripes_per_dev * |
| 5362 | map->stripe_len; |
| 5363 | |
| 5364 | if (i / sub_stripes < remaining_stripes) |
| 5365 | bbio->stripes[i].length += |
| 5366 | map->stripe_len; |
| 5367 | |
| 5368 | /* |
| 5369 | * Special for the first stripe and |
| 5370 | * the last stripe: |
| 5371 | * |
| 5372 | * |-------|...|-------| |
| 5373 | * |----------| |
| 5374 | * off end_off |
| 5375 | */ |
| 5376 | if (i < sub_stripes) |
| 5377 | bbio->stripes[i].length -= |
| 5378 | stripe_offset; |
| 5379 | |
| 5380 | if (stripe_index >= last_stripe && |
| 5381 | stripe_index <= (last_stripe + |
| 5382 | sub_stripes - 1)) |
| 5383 | bbio->stripes[i].length -= |
| 5384 | stripe_end_offset; |
| 5385 | |
| 5386 | if (i == sub_stripes - 1) |
| 5387 | stripe_offset = 0; |
| 5388 | } else { |
| 5389 | bbio->stripes[i].length = length; |
| 5390 | } |
| 5391 | |
| 5392 | stripe_index++; |
| 5393 | if (stripe_index == map->num_stripes) { |
| 5394 | stripe_index = 0; |
| 5395 | stripe_nr++; |
| 5396 | } |
| 5397 | } |
| 5398 | |
| 5399 | *bbio_ret = bbio; |
| 5400 | bbio->map_type = map->type; |
| 5401 | bbio->num_stripes = num_stripes; |
| 5402 | out: |
| 5403 | free_extent_map(em); |
| 5404 | return ret; |
| 5405 | } |
| 5406 | |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5407 | /* |
| 5408 | * In dev-replace case, for repair case (that's the only case where the mirror |
| 5409 | * is selected explicitly when calling btrfs_map_block), blocks left of the |
| 5410 | * left cursor can also be read from the target drive. |
| 5411 | * |
| 5412 | * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the |
| 5413 | * array of stripes. |
| 5414 | * For READ, it also needs to be supported using the same mirror number. |
| 5415 | * |
| 5416 | * If the requested block is not left of the left cursor, EIO is returned. This |
| 5417 | * can happen because btrfs_num_copies() returns one more in the dev-replace |
| 5418 | * case. |
| 5419 | */ |
| 5420 | static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info, |
| 5421 | u64 logical, u64 length, |
| 5422 | u64 srcdev_devid, int *mirror_num, |
| 5423 | u64 *physical) |
| 5424 | { |
| 5425 | struct btrfs_bio *bbio = NULL; |
| 5426 | int num_stripes; |
| 5427 | int index_srcdev = 0; |
| 5428 | int found = 0; |
| 5429 | u64 physical_of_found = 0; |
| 5430 | int i; |
| 5431 | int ret = 0; |
| 5432 | |
| 5433 | ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, |
| 5434 | logical, &length, &bbio, 0, 0); |
| 5435 | if (ret) { |
| 5436 | ASSERT(bbio == NULL); |
| 5437 | return ret; |
| 5438 | } |
| 5439 | |
| 5440 | num_stripes = bbio->num_stripes; |
| 5441 | if (*mirror_num > num_stripes) { |
| 5442 | /* |
| 5443 | * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror, |
| 5444 | * that means that the requested area is not left of the left |
| 5445 | * cursor |
| 5446 | */ |
| 5447 | btrfs_put_bbio(bbio); |
| 5448 | return -EIO; |
| 5449 | } |
| 5450 | |
| 5451 | /* |
| 5452 | * process the rest of the function using the mirror_num of the source |
| 5453 | * drive. Therefore look it up first. At the end, patch the device |
| 5454 | * pointer to the one of the target drive. |
| 5455 | */ |
| 5456 | for (i = 0; i < num_stripes; i++) { |
| 5457 | if (bbio->stripes[i].dev->devid != srcdev_devid) |
| 5458 | continue; |
| 5459 | |
| 5460 | /* |
| 5461 | * In case of DUP, in order to keep it simple, only add the |
| 5462 | * mirror with the lowest physical address |
| 5463 | */ |
| 5464 | if (found && |
| 5465 | physical_of_found <= bbio->stripes[i].physical) |
| 5466 | continue; |
| 5467 | |
| 5468 | index_srcdev = i; |
| 5469 | found = 1; |
| 5470 | physical_of_found = bbio->stripes[i].physical; |
| 5471 | } |
| 5472 | |
| 5473 | btrfs_put_bbio(bbio); |
| 5474 | |
| 5475 | ASSERT(found); |
| 5476 | if (!found) |
| 5477 | return -EIO; |
| 5478 | |
| 5479 | *mirror_num = index_srcdev + 1; |
| 5480 | *physical = physical_of_found; |
| 5481 | return ret; |
| 5482 | } |
| 5483 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5484 | static void handle_ops_on_dev_replace(enum btrfs_map_op op, |
| 5485 | struct btrfs_bio **bbio_ret, |
| 5486 | struct btrfs_dev_replace *dev_replace, |
| 5487 | int *num_stripes_ret, int *max_errors_ret) |
| 5488 | { |
| 5489 | struct btrfs_bio *bbio = *bbio_ret; |
| 5490 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5491 | int tgtdev_indexes = 0; |
| 5492 | int num_stripes = *num_stripes_ret; |
| 5493 | int max_errors = *max_errors_ret; |
| 5494 | int i; |
| 5495 | |
| 5496 | if (op == BTRFS_MAP_WRITE) { |
| 5497 | int index_where_to_add; |
| 5498 | |
| 5499 | /* |
| 5500 | * duplicate the write operations while the dev replace |
| 5501 | * procedure is running. Since the copying of the old disk to |
| 5502 | * the new disk takes place at run time while the filesystem is |
| 5503 | * mounted writable, the regular write operations to the old |
| 5504 | * disk have to be duplicated to go to the new disk as well. |
| 5505 | * |
| 5506 | * Note that device->missing is handled by the caller, and that |
| 5507 | * the write to the old disk is already set up in the stripes |
| 5508 | * array. |
| 5509 | */ |
| 5510 | index_where_to_add = num_stripes; |
| 5511 | for (i = 0; i < num_stripes; i++) { |
| 5512 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5513 | /* write to new disk, too */ |
| 5514 | struct btrfs_bio_stripe *new = |
| 5515 | bbio->stripes + index_where_to_add; |
| 5516 | struct btrfs_bio_stripe *old = |
| 5517 | bbio->stripes + i; |
| 5518 | |
| 5519 | new->physical = old->physical; |
| 5520 | new->length = old->length; |
| 5521 | new->dev = dev_replace->tgtdev; |
| 5522 | bbio->tgtdev_map[i] = index_where_to_add; |
| 5523 | index_where_to_add++; |
| 5524 | max_errors++; |
| 5525 | tgtdev_indexes++; |
| 5526 | } |
| 5527 | } |
| 5528 | num_stripes = index_where_to_add; |
| 5529 | } else if (op == BTRFS_MAP_GET_READ_MIRRORS) { |
| 5530 | int index_srcdev = 0; |
| 5531 | int found = 0; |
| 5532 | u64 physical_of_found = 0; |
| 5533 | |
| 5534 | /* |
| 5535 | * During the dev-replace procedure, the target drive can also |
| 5536 | * be used to read data in case it is needed to repair a corrupt |
| 5537 | * block elsewhere. This is possible if the requested area is |
| 5538 | * left of the left cursor. In this area, the target drive is a |
| 5539 | * full copy of the source drive. |
| 5540 | */ |
| 5541 | for (i = 0; i < num_stripes; i++) { |
| 5542 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5543 | /* |
| 5544 | * In case of DUP, in order to keep it simple, |
| 5545 | * only add the mirror with the lowest physical |
| 5546 | * address |
| 5547 | */ |
| 5548 | if (found && |
| 5549 | physical_of_found <= |
| 5550 | bbio->stripes[i].physical) |
| 5551 | continue; |
| 5552 | index_srcdev = i; |
| 5553 | found = 1; |
| 5554 | physical_of_found = bbio->stripes[i].physical; |
| 5555 | } |
| 5556 | } |
| 5557 | if (found) { |
| 5558 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5559 | bbio->stripes + num_stripes; |
| 5560 | |
| 5561 | tgtdev_stripe->physical = physical_of_found; |
| 5562 | tgtdev_stripe->length = |
| 5563 | bbio->stripes[index_srcdev].length; |
| 5564 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5565 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
| 5566 | |
| 5567 | tgtdev_indexes++; |
| 5568 | num_stripes++; |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | *num_stripes_ret = num_stripes; |
| 5573 | *max_errors_ret = max_errors; |
| 5574 | bbio->num_tgtdevs = tgtdev_indexes; |
| 5575 | *bbio_ret = bbio; |
| 5576 | } |
| 5577 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5578 | static bool need_full_stripe(enum btrfs_map_op op) |
| 5579 | { |
| 5580 | return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS); |
| 5581 | } |
| 5582 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5583 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, |
| 5584 | enum btrfs_map_op op, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5585 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5586 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5587 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5588 | { |
| 5589 | struct extent_map *em; |
| 5590 | struct map_lookup *map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5591 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5592 | u64 stripe_offset; |
| 5593 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5594 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5595 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5596 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5597 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5598 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5599 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5600 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5601 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5602 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5603 | int dev_replace_is_ongoing = 0; |
| 5604 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5605 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5606 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5607 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5608 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5609 | if (op == BTRFS_MAP_DISCARD) |
| 5610 | return __btrfs_map_block_for_discard(fs_info, logical, |
| 5611 | *length, bbio_ret); |
| 5612 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5613 | em = get_chunk_map(fs_info, logical, *length); |
| 5614 | if (IS_ERR(em)) |
| 5615 | return PTR_ERR(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5616 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5617 | map = em->map_lookup; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5618 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5619 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5620 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5621 | stripe_nr = offset; |
| 5622 | /* |
| 5623 | * stripe_nr counts the total number of stripes we have to stride |
| 5624 | * to get to this block |
| 5625 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5626 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5627 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5628 | stripe_offset = stripe_nr * stripe_len; |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5629 | if (offset < stripe_offset) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5630 | btrfs_crit(fs_info, |
| 5631 | "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] | 5632 | stripe_offset, offset, em->start, logical, |
| 5633 | stripe_len); |
| 5634 | free_extent_map(em); |
| 5635 | return -EINVAL; |
| 5636 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5637 | |
| 5638 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5639 | stripe_offset = offset - stripe_offset; |
| 5640 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5641 | /* 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] | 5642 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5643 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5644 | raid56_full_stripe_start = offset; |
| 5645 | |
| 5646 | /* allow a write of a full stripe, but make sure we don't |
| 5647 | * allow straddling of stripes |
| 5648 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5649 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5650 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5651 | raid56_full_stripe_start *= full_stripe_len; |
| 5652 | } |
| 5653 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5654 | if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5655 | u64 max_len; |
| 5656 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5657 | For other RAID types and for RAID[56] reads, just allow a single |
| 5658 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5659 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5660 | (op == BTRFS_MAP_WRITE)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5661 | max_len = stripe_len * nr_data_stripes(map) - |
| 5662 | (offset - raid56_full_stripe_start); |
| 5663 | } else { |
| 5664 | /* we limit the length of each bio to what fits in a stripe */ |
| 5665 | max_len = stripe_len - stripe_offset; |
| 5666 | } |
| 5667 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5668 | } else { |
| 5669 | *length = em->len - offset; |
| 5670 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5671 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5672 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5673 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5674 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5675 | goto out; |
| 5676 | |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5677 | btrfs_dev_replace_read_lock(dev_replace); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5678 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5679 | if (!dev_replace_is_ongoing) |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5680 | btrfs_dev_replace_read_unlock(dev_replace); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5681 | else |
| 5682 | btrfs_dev_replace_set_lock_blocking(dev_replace); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5683 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5684 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5685 | !need_full_stripe(op) && dev_replace->tgtdev != NULL) { |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5686 | ret = get_extra_mirror_from_replace(fs_info, logical, *length, |
| 5687 | dev_replace->srcdev->devid, |
| 5688 | &mirror_num, |
| 5689 | &physical_to_patch_in_first_stripe); |
| 5690 | if (ret) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5691 | goto out; |
Liu Bo | 5ab5609 | 2017-03-14 13:33:57 -0700 | [diff] [blame] | 5692 | else |
| 5693 | patch_the_first_stripe_for_dev_replace = 1; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5694 | } else if (mirror_num > map->num_stripes) { |
| 5695 | mirror_num = 0; |
| 5696 | } |
| 5697 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5698 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5699 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5700 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5701 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5702 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5703 | if (!need_full_stripe(op)) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5704 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5705 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5706 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5707 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5708 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5709 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5710 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5711 | stripe_index = find_live_mirror(fs_info, map, 0, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5712 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5713 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5714 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5715 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5716 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5717 | if (need_full_stripe(op)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5718 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5719 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5720 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5721 | } else { |
| 5722 | mirror_num = 1; |
| 5723 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5724 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5725 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5726 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5727 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5728 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5729 | stripe_index *= map->sub_stripes; |
| 5730 | |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5731 | if (need_full_stripe(op)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5732 | num_stripes = map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5733 | else if (mirror_num) |
| 5734 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5735 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5736 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5737 | stripe_index = find_live_mirror(fs_info, map, |
| 5738 | stripe_index, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5739 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5740 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5741 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5742 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5743 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5744 | if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5745 | /* push stripe_nr back to the start of the full stripe */ |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5746 | stripe_nr = div64_u64(raid56_full_stripe_start, |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5747 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5748 | |
| 5749 | /* RAID[56] write or recovery. Return all stripes */ |
| 5750 | num_stripes = map->num_stripes; |
| 5751 | max_errors = nr_parity_stripes(map); |
| 5752 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5753 | *length = map->stripe_len; |
| 5754 | stripe_index = 0; |
| 5755 | stripe_offset = 0; |
| 5756 | } else { |
| 5757 | /* |
| 5758 | * Mirror #0 or #1 means the original data block. |
| 5759 | * Mirror #2 is RAID5 parity block. |
| 5760 | * Mirror #3 is RAID6 Q block. |
| 5761 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5762 | stripe_nr = div_u64_rem(stripe_nr, |
| 5763 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5764 | if (mirror_num > 1) |
| 5765 | stripe_index = nr_data_stripes(map) + |
| 5766 | mirror_num - 2; |
| 5767 | |
| 5768 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5769 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5770 | &stripe_index); |
Anand Jain | de48373 | 2017-10-12 16:43:00 +0800 | [diff] [blame] | 5771 | if (!need_full_stripe(op) && mirror_num <= 1) |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5772 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5773 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5774 | } else { |
| 5775 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5776 | * after this, stripe_nr is the number of stripes on this |
| 5777 | * device we have to walk to find the data, and stripe_index is |
| 5778 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5779 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5780 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5781 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5782 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5783 | } |
Josef Bacik | e042d1e | 2016-04-12 12:54:40 -0400 | [diff] [blame] | 5784 | if (stripe_index >= map->num_stripes) { |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 5785 | btrfs_crit(fs_info, |
| 5786 | "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] | 5787 | stripe_index, map->num_stripes); |
| 5788 | ret = -EINVAL; |
| 5789 | goto out; |
| 5790 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5791 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5792 | num_alloc_stripes = num_stripes; |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5793 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) { |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5794 | if (op == BTRFS_MAP_WRITE) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5795 | num_alloc_stripes <<= 1; |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5796 | if (op == BTRFS_MAP_GET_READ_MIRRORS) |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5797 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5798 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5799 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5800 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5801 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5802 | if (!bbio) { |
| 5803 | ret = -ENOMEM; |
| 5804 | goto out; |
| 5805 | } |
Liu Bo | 6fad823 | 2017-03-14 13:33:59 -0700 | [diff] [blame] | 5806 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5807 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5808 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5809 | /* build raid_map */ |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5810 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map && |
| 5811 | (need_full_stripe(op) || mirror_num > 1)) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5812 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5813 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5814 | |
| 5815 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5816 | sizeof(struct btrfs_bio_stripe) * |
| 5817 | num_alloc_stripes + |
| 5818 | sizeof(int) * tgtdev_indexes); |
| 5819 | |
| 5820 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5821 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5822 | |
| 5823 | /* Fill in the logical address of each stripe */ |
| 5824 | tmp = stripe_nr * nr_data_stripes(map); |
| 5825 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5826 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5827 | em->start + (tmp + i) * map->stripe_len; |
| 5828 | |
| 5829 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5830 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5831 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5832 | RAID6_Q_STRIPE; |
| 5833 | } |
| 5834 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5835 | |
Liu Bo | 0b3d4cd | 2017-03-14 13:33:56 -0700 | [diff] [blame] | 5836 | for (i = 0; i < num_stripes; i++) { |
| 5837 | bbio->stripes[i].physical = |
| 5838 | map->stripes[stripe_index].physical + |
| 5839 | stripe_offset + |
| 5840 | stripe_nr * map->stripe_len; |
| 5841 | bbio->stripes[i].dev = |
| 5842 | map->stripes[stripe_index].dev; |
| 5843 | stripe_index++; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5844 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5845 | |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5846 | if (need_full_stripe(op)) |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5847 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5848 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5849 | if (bbio->raid_map) |
| 5850 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5851 | |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5852 | if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL && |
Liu Bo | 2b19a1f | 2017-03-14 13:34:00 -0700 | [diff] [blame] | 5853 | need_full_stripe(op)) { |
Liu Bo | 73c0f22 | 2017-03-14 13:33:58 -0700 | [diff] [blame] | 5854 | handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes, |
| 5855 | &max_errors); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5856 | } |
| 5857 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5858 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5859 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5860 | bbio->num_stripes = num_stripes; |
| 5861 | bbio->max_errors = max_errors; |
| 5862 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5863 | |
| 5864 | /* |
| 5865 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5866 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5867 | * available as a mirror |
| 5868 | */ |
| 5869 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5870 | WARN_ON(num_stripes > 1); |
| 5871 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5872 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5873 | bbio->mirror_num = map->num_stripes + 1; |
| 5874 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5875 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5876 | if (dev_replace_is_ongoing) { |
| 5877 | btrfs_dev_replace_clear_lock_blocking(dev_replace); |
David Sterba | 7e79cb8 | 2018-03-24 02:11:38 +0100 | [diff] [blame] | 5878 | btrfs_dev_replace_read_unlock(dev_replace); |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5879 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5880 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5881 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5882 | } |
| 5883 | |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5884 | 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] | 5885 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5886 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5887 | { |
Mike Christie | b3d3fa5 | 2016-06-05 14:31:53 -0500 | [diff] [blame] | 5888 | return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5889 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5890 | } |
| 5891 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5892 | /* For Scrub/replace */ |
Christoph Hellwig | cf8cddd | 2016-10-27 09:27:36 +0200 | [diff] [blame] | 5893 | 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] | 5894 | u64 logical, u64 *length, |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 5895 | struct btrfs_bio **bbio_ret) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5896 | { |
David Sterba | 825ad4c | 2017-03-28 14:45:22 +0200 | [diff] [blame] | 5897 | 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] | 5898 | } |
| 5899 | |
Nikolay Borisov | 63a9c7b | 2018-05-04 10:53:05 +0300 | [diff] [blame] | 5900 | int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start, |
| 5901 | u64 physical, u64 **logical, int *naddrs, int *stripe_len) |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5902 | { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5903 | struct extent_map *em; |
| 5904 | struct map_lookup *map; |
| 5905 | u64 *buf; |
| 5906 | u64 bytenr; |
| 5907 | u64 length; |
| 5908 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5909 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5910 | int i, j, nr = 0; |
| 5911 | |
Liu Bo | 592d92e | 2017-03-14 13:33:55 -0700 | [diff] [blame] | 5912 | em = get_chunk_map(fs_info, chunk_start, 1); |
| 5913 | if (IS_ERR(em)) |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5914 | return -EIO; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5915 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5916 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5917 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5918 | rmap_len = map->stripe_len; |
| 5919 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5920 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5921 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5922 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5923 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5924 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5925 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5926 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5927 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5928 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5929 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5930 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5931 | |
| 5932 | for (i = 0; i < map->num_stripes; i++) { |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5933 | if (map->stripes[i].physical > physical || |
| 5934 | map->stripes[i].physical + length <= physical) |
| 5935 | continue; |
| 5936 | |
| 5937 | stripe_nr = physical - map->stripes[i].physical; |
Liu Bo | 42c61ab | 2017-04-03 13:45:24 -0700 | [diff] [blame] | 5938 | stripe_nr = div64_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5939 | |
| 5940 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5941 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5942 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5943 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5944 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5945 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5946 | * Alternatively, just use rmap_len below instead of |
| 5947 | * map->stripe_len */ |
| 5948 | |
| 5949 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5950 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5951 | for (j = 0; j < nr; j++) { |
| 5952 | if (buf[j] == bytenr) |
| 5953 | break; |
| 5954 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5955 | if (j == nr) { |
| 5956 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5957 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5958 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5959 | } |
| 5960 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5961 | *logical = buf; |
| 5962 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5963 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5964 | |
| 5965 | free_extent_map(em); |
| 5966 | return 0; |
| 5967 | } |
| 5968 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5969 | 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] | 5970 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5971 | bio->bi_private = bbio->private; |
| 5972 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5973 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5974 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5975 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5976 | } |
| 5977 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5978 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5979 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5980 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5981 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5982 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 5983 | if (bio->bi_status) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5984 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 5985 | if (bio->bi_status == BLK_STS_IOERR || |
| 5986 | bio->bi_status == BLK_STS_TARGET) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5987 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5988 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 5989 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5990 | |
| 5991 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5992 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5993 | if (dev->bdev) { |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 5994 | if (bio_op(bio) == REQ_OP_WRITE) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 5995 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5996 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5997 | else |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 5998 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5999 | BTRFS_DEV_STAT_READ_ERRS); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 6000 | if (bio->bi_opf & REQ_PREFLUSH) |
Anand Jain | 1cb34c8 | 2017-10-21 01:45:33 +0800 | [diff] [blame] | 6001 | btrfs_dev_stat_inc_and_print(dev, |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6002 | BTRFS_DEV_STAT_FLUSH_ERRS); |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 6003 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6004 | } |
| 6005 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6006 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6007 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6008 | is_orig_bio = 1; |
| 6009 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6010 | btrfs_bio_counter_dec(bbio->fs_info); |
| 6011 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6012 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6013 | if (!is_orig_bio) { |
| 6014 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6015 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6016 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 6017 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6018 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6019 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6020 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 6021 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6022 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 6023 | bio->bi_status = BLK_STS_IOERR; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 6024 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6025 | /* |
| 6026 | * this bio is actually up to date, we didn't |
| 6027 | * go over the max number of errors |
| 6028 | */ |
Anand Jain | 2dbe0c7 | 2017-10-14 08:35:56 +0800 | [diff] [blame] | 6029 | bio->bi_status = BLK_STS_OK; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 6030 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6031 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6032 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 6033 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6034 | bio_put(bio); |
| 6035 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6036 | } |
| 6037 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6038 | /* |
| 6039 | * see run_scheduled_bios for a description of why bios are collected for |
| 6040 | * async submit. |
| 6041 | * |
| 6042 | * This will add one bio to the pending list for a device and make sure |
| 6043 | * the work struct is scheduled. |
| 6044 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6045 | static noinline void btrfs_schedule_bio(struct btrfs_device *device, |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6046 | struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6047 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6048 | struct btrfs_fs_info *fs_info = device->fs_info; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6049 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6050 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6051 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6052 | if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) || |
| 6053 | !device->bdev) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6054 | bio_io_error(bio); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6055 | return; |
| 6056 | } |
| 6057 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6058 | /* don't bother with additional async steps for reads, right now */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6059 | if (bio_op(bio) == REQ_OP_READ) { |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6060 | btrfsic_submit_bio(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6061 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6062 | } |
| 6063 | |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 6064 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6065 | bio->bi_next = NULL; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6066 | |
| 6067 | spin_lock(&device->io_lock); |
Christoph Hellwig | 67f055c | 2016-11-01 07:40:06 -0600 | [diff] [blame] | 6068 | if (op_is_sync(bio->bi_opf)) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6069 | pending_bios = &device->pending_sync_bios; |
| 6070 | else |
| 6071 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6072 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6073 | if (pending_bios->tail) |
| 6074 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6075 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 6076 | pending_bios->tail = bio; |
| 6077 | if (!pending_bios->head) |
| 6078 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6079 | if (device->running_pending) |
| 6080 | should_queue = 0; |
| 6081 | |
| 6082 | spin_unlock(&device->io_lock); |
| 6083 | |
| 6084 | if (should_queue) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6085 | btrfs_queue_work(fs_info->submit_workers, &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6086 | } |
| 6087 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6088 | static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio, |
| 6089 | u64 physical, int dev_nr, int async) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6090 | { |
| 6091 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6092 | struct btrfs_fs_info *fs_info = bbio->fs_info; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6093 | |
| 6094 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6095 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6096 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6097 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6098 | #ifdef DEBUG |
| 6099 | { |
| 6100 | struct rcu_string *name; |
| 6101 | |
| 6102 | rcu_read_lock(); |
| 6103 | name = rcu_dereference(dev->name); |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6104 | btrfs_debug(fs_info, |
| 6105 | "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u", |
| 6106 | bio_op(bio), bio->bi_opf, |
| 6107 | (u64)bio->bi_iter.bi_sector, |
| 6108 | (u_long)dev->bdev->bd_dev, name->str, dev->devid, |
| 6109 | bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6110 | rcu_read_unlock(); |
| 6111 | } |
| 6112 | #endif |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 6113 | bio_set_dev(bio, dev->bdev); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6114 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6115 | btrfs_bio_counter_inc_noblocked(fs_info); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6116 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6117 | if (async) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6118 | btrfs_schedule_bio(dev, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6119 | else |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 6120 | btrfsic_submit_bio(bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6121 | } |
| 6122 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6123 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6124 | { |
| 6125 | atomic_inc(&bbio->error); |
| 6126 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6127 | /* Should be the original bio. */ |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6128 | WARN_ON(bio != bbio->orig_bio); |
| 6129 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6130 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6131 | bio->bi_iter.bi_sector = logical >> 9; |
Anand Jain | 102ed2c | 2017-10-14 08:34:02 +0800 | [diff] [blame] | 6132 | if (atomic_read(&bbio->error) > bbio->max_errors) |
| 6133 | bio->bi_status = BLK_STS_IOERR; |
| 6134 | else |
| 6135 | bio->bi_status = BLK_STS_OK; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6136 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6137 | } |
| 6138 | } |
| 6139 | |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6140 | blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio, |
| 6141 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6142 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6143 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6144 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6145 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6146 | u64 length = 0; |
| 6147 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6148 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6149 | int dev_nr; |
| 6150 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6151 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6152 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6153 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6154 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6155 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6156 | btrfs_bio_counter_inc_blocked(fs_info); |
Liu Bo | bd7d63c | 2017-09-19 17:50:09 -0600 | [diff] [blame] | 6157 | ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6158 | &map_length, &bbio, mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6159 | if (ret) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6160 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6161 | return errno_to_blk_status(ret); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6162 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6163 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6164 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6165 | bbio->orig_bio = first_bio; |
| 6166 | bbio->private = first_bio->bi_private; |
| 6167 | bbio->end_io = first_bio->bi_end_io; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6168 | bbio->fs_info = fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6169 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6170 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6171 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6172 | ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6173 | /* In this case, map_length has been set to the length of |
| 6174 | a single stripe; not the whole write */ |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6175 | if (bio_op(bio) == REQ_OP_WRITE) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6176 | ret = raid56_parity_write(fs_info, bio, bbio, |
| 6177 | map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6178 | } else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6179 | ret = raid56_parity_recover(fs_info, bio, bbio, |
| 6180 | map_length, mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6181 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6182 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6183 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6184 | return errno_to_blk_status(ret); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6185 | } |
| 6186 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6187 | if (map_length < length) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6188 | btrfs_crit(fs_info, |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 6189 | "mapping failed logical %llu bio len %llu len %llu", |
| 6190 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6191 | BUG(); |
| 6192 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6193 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6194 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6195 | dev = bbio->stripes[dev_nr].dev; |
Mike Christie | 37226b2 | 2016-06-05 14:31:52 -0500 | [diff] [blame] | 6196 | if (!dev || !dev->bdev || |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6197 | (bio_op(first_bio) == REQ_OP_WRITE && |
| 6198 | !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6199 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6200 | continue; |
| 6201 | } |
| 6202 | |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6203 | if (dev_nr < total_devs - 1) |
David Sterba | 8b6c1d5 | 2017-06-02 17:48:13 +0200 | [diff] [blame] | 6204 | bio = btrfs_bio_clone(first_bio); |
David Sterba | 3aa8e07 | 2017-06-02 17:38:30 +0200 | [diff] [blame] | 6205 | else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6206 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6207 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6208 | submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, |
| 6209 | dev_nr, async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6210 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6211 | btrfs_bio_counter_dec(fs_info); |
Omar Sandoval | 58efbc9 | 2017-08-22 23:45:59 -0700 | [diff] [blame] | 6212 | return BLK_STS_OK; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6213 | } |
| 6214 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6215 | 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] | 6216 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6217 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6218 | struct btrfs_device *device; |
| 6219 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6220 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6221 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6222 | while (cur_devices) { |
| 6223 | if (!fsid || |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6224 | !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) { |
David Sterba | 35c7010 | 2017-06-15 19:51:51 +0200 | [diff] [blame] | 6225 | device = find_device(cur_devices, devid, uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6226 | if (device) |
| 6227 | return device; |
| 6228 | } |
| 6229 | cur_devices = cur_devices->seed; |
| 6230 | } |
| 6231 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6232 | } |
| 6233 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6234 | 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] | 6235 | u64 devid, u8 *dev_uuid) |
| 6236 | { |
| 6237 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6238 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6239 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6240 | if (IS_ERR(device)) |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6241 | return device; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6242 | |
| 6243 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6244 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6245 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6246 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6247 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6248 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6249 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6250 | return device; |
| 6251 | } |
| 6252 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6253 | /** |
| 6254 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6255 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6256 | * devid is provided (i.e. @devid != NULL). |
| 6257 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6258 | * is generated. |
| 6259 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6260 | * is generated. |
| 6261 | * |
| 6262 | * 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] | 6263 | * 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] | 6264 | * destroyed with btrfs_free_device. |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6265 | */ |
| 6266 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6267 | const u64 *devid, |
| 6268 | const u8 *uuid) |
| 6269 | { |
| 6270 | struct btrfs_device *dev; |
| 6271 | u64 tmp; |
| 6272 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6273 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6274 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6275 | |
| 6276 | dev = __alloc_device(); |
| 6277 | if (IS_ERR(dev)) |
| 6278 | return dev; |
| 6279 | |
| 6280 | if (devid) |
| 6281 | tmp = *devid; |
| 6282 | else { |
| 6283 | int ret; |
| 6284 | |
| 6285 | ret = find_next_devid(fs_info, &tmp); |
| 6286 | if (ret) { |
David Sterba | a425f9d | 2018-03-20 15:47:33 +0100 | [diff] [blame] | 6287 | btrfs_free_device(dev); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6288 | return ERR_PTR(ret); |
| 6289 | } |
| 6290 | } |
| 6291 | dev->devid = tmp; |
| 6292 | |
| 6293 | if (uuid) |
| 6294 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6295 | else |
| 6296 | generate_random_uuid(dev->uuid); |
| 6297 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6298 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6299 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6300 | |
| 6301 | return dev; |
| 6302 | } |
| 6303 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6304 | /* Return -EIO if any error, otherwise return 0. */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6305 | static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6306 | struct extent_buffer *leaf, |
| 6307 | struct btrfs_chunk *chunk, u64 logical) |
| 6308 | { |
| 6309 | u64 length; |
| 6310 | u64 stripe_len; |
| 6311 | u16 num_stripes; |
| 6312 | u16 sub_stripes; |
| 6313 | u64 type; |
Gu Jinxiang | 315409b | 2018-07-04 18:16:39 +0800 | [diff] [blame] | 6314 | u64 features; |
| 6315 | bool mixed = false; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6316 | |
| 6317 | length = btrfs_chunk_length(leaf, chunk); |
| 6318 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6319 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6320 | sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
| 6321 | type = btrfs_chunk_type(leaf, chunk); |
| 6322 | |
| 6323 | if (!num_stripes) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6324 | btrfs_err(fs_info, "invalid chunk num_stripes: %u", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6325 | num_stripes); |
| 6326 | return -EIO; |
| 6327 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6328 | if (!IS_ALIGNED(logical, fs_info->sectorsize)) { |
| 6329 | btrfs_err(fs_info, "invalid chunk logical %llu", logical); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6330 | return -EIO; |
| 6331 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6332 | if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) { |
| 6333 | btrfs_err(fs_info, "invalid chunk sectorsize %u", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6334 | btrfs_chunk_sector_size(leaf, chunk)); |
| 6335 | return -EIO; |
| 6336 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6337 | if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) { |
| 6338 | btrfs_err(fs_info, "invalid chunk length %llu", length); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6339 | return -EIO; |
| 6340 | } |
| 6341 | if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6342 | btrfs_err(fs_info, "invalid chunk stripe length: %llu", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6343 | stripe_len); |
| 6344 | return -EIO; |
| 6345 | } |
| 6346 | if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6347 | type) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6348 | btrfs_err(fs_info, "unrecognized chunk type: %llu", |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6349 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
| 6350 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6351 | btrfs_chunk_type(leaf, chunk)); |
| 6352 | return -EIO; |
| 6353 | } |
Gu Jinxiang | 315409b | 2018-07-04 18:16:39 +0800 | [diff] [blame] | 6354 | |
| 6355 | if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) { |
| 6356 | btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type); |
| 6357 | return -EIO; |
| 6358 | } |
| 6359 | |
| 6360 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 6361 | (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) { |
| 6362 | btrfs_err(fs_info, |
| 6363 | "system chunk with data or metadata type: 0x%llx", type); |
| 6364 | return -EIO; |
| 6365 | } |
| 6366 | |
| 6367 | features = btrfs_super_incompat_flags(fs_info->super_copy); |
| 6368 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 6369 | mixed = true; |
| 6370 | |
| 6371 | if (!mixed) { |
| 6372 | if ((type & BTRFS_BLOCK_GROUP_METADATA) && |
| 6373 | (type & BTRFS_BLOCK_GROUP_DATA)) { |
| 6374 | btrfs_err(fs_info, |
| 6375 | "mixed chunk type in non-mixed mode: 0x%llx", type); |
| 6376 | return -EIO; |
| 6377 | } |
| 6378 | } |
| 6379 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6380 | if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || |
| 6381 | (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || |
| 6382 | (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || |
| 6383 | (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || |
| 6384 | (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || |
| 6385 | ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && |
| 6386 | num_stripes != 1)) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6387 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6388 | "invalid num_stripes:sub_stripes %u:%u for profile %llu", |
| 6389 | num_stripes, sub_stripes, |
| 6390 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 6391 | return -EIO; |
| 6392 | } |
| 6393 | |
| 6394 | return 0; |
| 6395 | } |
| 6396 | |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6397 | static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info, |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6398 | u64 devid, u8 *uuid, bool error) |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6399 | { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6400 | if (error) |
| 6401 | btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6402 | devid, uuid); |
| 6403 | else |
| 6404 | btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing", |
| 6405 | devid, uuid); |
Anand Jain | 5a2b8e6 | 2017-10-09 11:07:45 +0800 | [diff] [blame] | 6406 | } |
| 6407 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6408 | 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] | 6409 | struct extent_buffer *leaf, |
| 6410 | struct btrfs_chunk *chunk) |
| 6411 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6412 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6413 | struct map_lookup *map; |
| 6414 | struct extent_map *em; |
| 6415 | u64 logical; |
| 6416 | u64 length; |
| 6417 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6418 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6419 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6420 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6421 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6422 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6423 | logical = key->offset; |
| 6424 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6425 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6426 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6427 | ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical); |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6428 | if (ret) |
| 6429 | return ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6430 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6431 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6432 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6433 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6434 | |
| 6435 | /* already mapped? */ |
| 6436 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6437 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6438 | return 0; |
| 6439 | } else if (em) { |
| 6440 | free_extent_map(em); |
| 6441 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6442 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6443 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6444 | if (!em) |
| 6445 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6446 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6447 | if (!map) { |
| 6448 | free_extent_map(em); |
| 6449 | return -ENOMEM; |
| 6450 | } |
| 6451 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6452 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6453 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6454 | em->start = logical; |
| 6455 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6456 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6457 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6458 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6459 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6460 | map->num_stripes = num_stripes; |
| 6461 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6462 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6463 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6464 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6465 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6466 | for (i = 0; i < num_stripes; i++) { |
| 6467 | map->stripes[i].physical = |
| 6468 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6469 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6470 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6471 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6472 | BTRFS_UUID_SIZE); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6473 | map->stripes[i].dev = btrfs_find_device(fs_info, devid, |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6474 | uuid, NULL); |
Jeff Mahoney | 3cdde22 | 2016-06-09 21:38:35 -0400 | [diff] [blame] | 6475 | if (!map->stripes[i].dev && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6476 | !btrfs_test_opt(fs_info, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6477 | free_extent_map(em); |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6478 | btrfs_report_missing_device(fs_info, devid, uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6479 | return -ENOENT; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6480 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6481 | if (!map->stripes[i].dev) { |
| 6482 | map->stripes[i].dev = |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6483 | add_missing_dev(fs_info->fs_devices, devid, |
| 6484 | uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6485 | if (IS_ERR(map->stripes[i].dev)) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6486 | free_extent_map(em); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6487 | btrfs_err(fs_info, |
| 6488 | "failed to init missing dev %llu: %ld", |
| 6489 | devid, PTR_ERR(map->stripes[i].dev)); |
| 6490 | return PTR_ERR(map->stripes[i].dev); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6491 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6492 | btrfs_report_missing_device(fs_info, devid, uuid, false); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6493 | } |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6494 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, |
| 6495 | &(map->stripes[i].dev->dev_state)); |
| 6496 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6497 | } |
| 6498 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6499 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6500 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6501 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6502 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6503 | free_extent_map(em); |
| 6504 | |
| 6505 | return 0; |
| 6506 | } |
| 6507 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6508 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6509 | struct btrfs_dev_item *dev_item, |
| 6510 | struct btrfs_device *device) |
| 6511 | { |
| 6512 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6513 | |
| 6514 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6515 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6516 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6517 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6518 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6519 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6520 | device->type = btrfs_device_type(leaf, dev_item); |
| 6521 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6522 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6523 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6524 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6525 | clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6526 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6527 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6528 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6529 | } |
| 6530 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6531 | 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] | 6532 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6533 | { |
| 6534 | struct btrfs_fs_devices *fs_devices; |
| 6535 | int ret; |
| 6536 | |
David Sterba | a32bf9a | 2018-03-16 02:21:22 +0100 | [diff] [blame] | 6537 | lockdep_assert_held(&uuid_mutex); |
David Sterba | 2dfeca9 | 2017-06-14 02:48:07 +0200 | [diff] [blame] | 6538 | ASSERT(fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6539 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6540 | fs_devices = fs_info->fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6541 | while (fs_devices) { |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6542 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6543 | return fs_devices; |
| 6544 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6545 | fs_devices = fs_devices->seed; |
| 6546 | } |
| 6547 | |
| 6548 | fs_devices = find_fsid(fsid); |
| 6549 | if (!fs_devices) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6550 | if (!btrfs_test_opt(fs_info, DEGRADED)) |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6551 | return ERR_PTR(-ENOENT); |
| 6552 | |
| 6553 | fs_devices = alloc_fs_devices(fsid); |
| 6554 | if (IS_ERR(fs_devices)) |
| 6555 | return fs_devices; |
| 6556 | |
| 6557 | fs_devices->seeding = 1; |
| 6558 | fs_devices->opened = 1; |
| 6559 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6560 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6561 | |
| 6562 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6563 | if (IS_ERR(fs_devices)) |
| 6564 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6565 | |
Anand Jain | 897fb57 | 2018-04-12 10:29:28 +0800 | [diff] [blame] | 6566 | ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6567 | if (ret) { |
| 6568 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6569 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6570 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6571 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6572 | |
| 6573 | if (!fs_devices->seeding) { |
Anand Jain | 0226e0e | 2018-04-12 10:29:27 +0800 | [diff] [blame] | 6574 | close_fs_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6575 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6576 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6577 | goto out; |
| 6578 | } |
| 6579 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6580 | fs_devices->seed = fs_info->fs_devices->seed; |
| 6581 | fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6582 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6583 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6584 | } |
| 6585 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6586 | static int read_one_dev(struct btrfs_fs_info *fs_info, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6587 | struct extent_buffer *leaf, |
| 6588 | struct btrfs_dev_item *dev_item) |
| 6589 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6590 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6591 | struct btrfs_device *device; |
| 6592 | u64 devid; |
| 6593 | int ret; |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6594 | u8 fs_uuid[BTRFS_FSID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6595 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6596 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6597 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6598 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6599 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6600 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6601 | BTRFS_FSID_SIZE); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6602 | |
Anand Jain | 44880fd | 2017-07-29 17:50:09 +0800 | [diff] [blame] | 6603 | if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6604 | fs_devices = open_seed_devices(fs_info, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6605 | if (IS_ERR(fs_devices)) |
| 6606 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6607 | } |
| 6608 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6609 | device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6610 | if (!device) { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6611 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6612 | btrfs_report_missing_device(fs_info, devid, |
| 6613 | dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6614 | return -ENOENT; |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6615 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6616 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6617 | device = add_missing_dev(fs_devices, devid, dev_uuid); |
Anand Jain | adfb69a | 2017-10-11 12:46:18 +0800 | [diff] [blame] | 6618 | if (IS_ERR(device)) { |
| 6619 | btrfs_err(fs_info, |
| 6620 | "failed to add missing dev %llu: %ld", |
| 6621 | devid, PTR_ERR(device)); |
| 6622 | return PTR_ERR(device); |
| 6623 | } |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6624 | btrfs_report_missing_device(fs_info, devid, dev_uuid, false); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6625 | } else { |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6626 | if (!device->bdev) { |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6627 | if (!btrfs_test_opt(fs_info, DEGRADED)) { |
| 6628 | btrfs_report_missing_device(fs_info, |
| 6629 | devid, dev_uuid, true); |
Anand Jain | 45dbdbc | 2017-10-09 11:07:44 +0800 | [diff] [blame] | 6630 | return -ENOENT; |
Anand Jain | 2b902df | 2017-10-09 11:07:46 +0800 | [diff] [blame] | 6631 | } |
| 6632 | btrfs_report_missing_device(fs_info, devid, |
| 6633 | dev_uuid, false); |
Qu Wenruo | c550245 | 2017-03-09 09:34:42 +0800 | [diff] [blame] | 6634 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6635 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6636 | if (!device->bdev && |
| 6637 | !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6638 | /* |
| 6639 | * this happens when a device that was properly setup |
| 6640 | * in the device info lists suddenly goes bad. |
| 6641 | * device->bdev is NULL, and so we have to set |
| 6642 | * device->missing to one here |
| 6643 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6644 | device->fs_devices->missing_devices++; |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6645 | set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6646 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6647 | |
| 6648 | /* Move the device to its own fs_devices */ |
| 6649 | if (device->fs_devices != fs_devices) { |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6650 | ASSERT(test_bit(BTRFS_DEV_STATE_MISSING, |
| 6651 | &device->dev_state)); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6652 | |
| 6653 | list_move(&device->dev_list, &fs_devices->devices); |
| 6654 | device->fs_devices->num_devices--; |
| 6655 | fs_devices->num_devices++; |
| 6656 | |
| 6657 | device->fs_devices->missing_devices--; |
| 6658 | fs_devices->missing_devices++; |
| 6659 | |
| 6660 | device->fs_devices = fs_devices; |
| 6661 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6662 | } |
| 6663 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6664 | if (device->fs_devices != fs_info->fs_devices) { |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6665 | BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6666 | if (device->generation != |
| 6667 | btrfs_device_generation(leaf, dev_item)) |
| 6668 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6669 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6670 | |
| 6671 | fill_device_from_item(leaf, dev_item, device); |
Anand Jain | e12c962 | 2017-12-04 12:54:53 +0800 | [diff] [blame] | 6672 | set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state); |
Anand Jain | ebbede4 | 2017-12-04 12:54:52 +0800 | [diff] [blame] | 6673 | if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) && |
Anand Jain | 401e29c | 2017-12-04 12:54:55 +0800 | [diff] [blame] | 6674 | !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6675 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Nikolay Borisov | a5ed45f | 2017-05-11 09:17:46 +0300 | [diff] [blame] | 6676 | atomic64_add(device->total_bytes - device->bytes_used, |
| 6677 | &fs_info->free_chunk_space); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6678 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6679 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6680 | return ret; |
| 6681 | } |
| 6682 | |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6683 | int btrfs_read_sys_array(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6684 | { |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 6685 | struct btrfs_root *root = fs_info->tree_root; |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6686 | struct btrfs_super_block *super_copy = fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6687 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6688 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6689 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6690 | u8 *array_ptr; |
| 6691 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6692 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6693 | u32 num_stripes; |
| 6694 | u32 array_size; |
| 6695 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6696 | u32 cur_offset; |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6697 | u64 type; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6698 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6699 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6700 | ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize); |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6701 | /* |
| 6702 | * This will create extent buffer of nodesize, superblock size is |
| 6703 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6704 | * overallocate but we can keep it as-is, only the first page is used. |
| 6705 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6706 | sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 6707 | if (IS_ERR(sb)) |
| 6708 | return PTR_ERR(sb); |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6709 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6710 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6711 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 6712 | * 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] | 6713 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6714 | * pages up-to-date when the page is larger: extent does not cover the |
| 6715 | * whole page and consequently check_page_uptodate does not find all |
| 6716 | * the page's extents up-to-date (the hole beyond sb), |
| 6717 | * write_extent_buffer then triggers a WARN_ON. |
| 6718 | * |
| 6719 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6720 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6721 | * to silence the warning eg. on PowerPC 64. |
| 6722 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6723 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6724 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6725 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6726 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6727 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6728 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6729 | array_ptr = super_copy->sys_chunk_array; |
| 6730 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6731 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6732 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6733 | while (cur_offset < array_size) { |
| 6734 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6735 | len = sizeof(*disk_key); |
| 6736 | if (cur_offset + len > array_size) |
| 6737 | goto out_short_read; |
| 6738 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6739 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6740 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6741 | array_ptr += len; |
| 6742 | sb_array_offset += len; |
| 6743 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6744 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6745 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6746 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6747 | /* |
| 6748 | * At least one btrfs_chunk with one stripe must be |
| 6749 | * present, exact stripe count check comes afterwards |
| 6750 | */ |
| 6751 | len = btrfs_chunk_item_size(1); |
| 6752 | if (cur_offset + len > array_size) |
| 6753 | goto out_short_read; |
| 6754 | |
| 6755 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 6756 | if (!num_stripes) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6757 | btrfs_err(fs_info, |
| 6758 | "invalid number of stripes %u in sys_array at offset %u", |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 6759 | num_stripes, cur_offset); |
| 6760 | ret = -EIO; |
| 6761 | break; |
| 6762 | } |
| 6763 | |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6764 | type = btrfs_chunk_type(sb, chunk); |
| 6765 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6766 | btrfs_err(fs_info, |
Liu Bo | e06cd3d | 2016-06-03 12:05:15 -0700 | [diff] [blame] | 6767 | "invalid chunk type %llu in sys_array at offset %u", |
| 6768 | type, cur_offset); |
| 6769 | ret = -EIO; |
| 6770 | break; |
| 6771 | } |
| 6772 | |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6773 | len = btrfs_chunk_item_size(num_stripes); |
| 6774 | if (cur_offset + len > array_size) |
| 6775 | goto out_short_read; |
| 6776 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6777 | ret = read_one_chunk(fs_info, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6778 | if (ret) |
| 6779 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6780 | } else { |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6781 | btrfs_err(fs_info, |
| 6782 | "unexpected item type %u in sys_array at offset %u", |
| 6783 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6784 | ret = -EIO; |
| 6785 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6786 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6787 | array_ptr += len; |
| 6788 | sb_array_offset += len; |
| 6789 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6790 | } |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6791 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6792 | free_extent_buffer_stale(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6793 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6794 | |
| 6795 | out_short_read: |
Jeff Mahoney | ab8d0fc | 2016-09-20 10:05:02 -0400 | [diff] [blame] | 6796 | 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] | 6797 | len, cur_offset); |
Liu Bo | d865177 | 2016-06-03 17:41:42 -0700 | [diff] [blame] | 6798 | clear_extent_buffer_uptodate(sb); |
Liu Bo | 1c8b5b6 | 2016-05-13 17:06:59 -0700 | [diff] [blame] | 6799 | free_extent_buffer_stale(sb); |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6800 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6801 | } |
| 6802 | |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6803 | /* |
| 6804 | * Check if all chunks in the fs are OK for read-write degraded mount |
| 6805 | * |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6806 | * If the @failing_dev is specified, it's accounted as missing. |
| 6807 | * |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6808 | * Return true if all chunks meet the minimal RW mount requirements. |
| 6809 | * Return false if any chunk doesn't meet the minimal RW mount requirements. |
| 6810 | */ |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6811 | bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info, |
| 6812 | struct btrfs_device *failing_dev) |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6813 | { |
| 6814 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
| 6815 | struct extent_map *em; |
| 6816 | u64 next_start = 0; |
| 6817 | bool ret = true; |
| 6818 | |
| 6819 | read_lock(&map_tree->map_tree.lock); |
| 6820 | em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1); |
| 6821 | read_unlock(&map_tree->map_tree.lock); |
| 6822 | /* No chunk at all? Return false anyway */ |
| 6823 | if (!em) { |
| 6824 | ret = false; |
| 6825 | goto out; |
| 6826 | } |
| 6827 | while (em) { |
| 6828 | struct map_lookup *map; |
| 6829 | int missing = 0; |
| 6830 | int max_tolerated; |
| 6831 | int i; |
| 6832 | |
| 6833 | map = em->map_lookup; |
| 6834 | max_tolerated = |
| 6835 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 6836 | map->type); |
| 6837 | for (i = 0; i < map->num_stripes; i++) { |
| 6838 | struct btrfs_device *dev = map->stripes[i].dev; |
| 6839 | |
Anand Jain | e6e674b | 2017-12-04 12:54:54 +0800 | [diff] [blame] | 6840 | if (!dev || !dev->bdev || |
| 6841 | test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) || |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6842 | dev->last_flush_error) |
| 6843 | missing++; |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6844 | else if (failing_dev && failing_dev == dev) |
| 6845 | missing++; |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6846 | } |
| 6847 | if (missing > max_tolerated) { |
Anand Jain | 6528b99 | 2017-12-18 17:08:59 +0800 | [diff] [blame] | 6848 | if (!failing_dev) |
| 6849 | btrfs_warn(fs_info, |
Qu Wenruo | 21634a1 | 2017-03-09 09:34:36 +0800 | [diff] [blame] | 6850 | "chunk %llu missing %d devices, max tolerance is %d for writeable mount", |
| 6851 | em->start, missing, max_tolerated); |
| 6852 | free_extent_map(em); |
| 6853 | ret = false; |
| 6854 | goto out; |
| 6855 | } |
| 6856 | next_start = extent_map_end(em); |
| 6857 | free_extent_map(em); |
| 6858 | |
| 6859 | read_lock(&map_tree->map_tree.lock); |
| 6860 | em = lookup_extent_mapping(&map_tree->map_tree, next_start, |
| 6861 | (u64)(-1) - next_start); |
| 6862 | read_unlock(&map_tree->map_tree.lock); |
| 6863 | } |
| 6864 | out: |
| 6865 | return ret; |
| 6866 | } |
| 6867 | |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6868 | int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6869 | { |
Jeff Mahoney | 5b4aace | 2016-06-21 10:40:19 -0400 | [diff] [blame] | 6870 | struct btrfs_root *root = fs_info->chunk_root; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6871 | struct btrfs_path *path; |
| 6872 | struct extent_buffer *leaf; |
| 6873 | struct btrfs_key key; |
| 6874 | struct btrfs_key found_key; |
| 6875 | int ret; |
| 6876 | int slot; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6877 | u64 total_dev = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6878 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6879 | path = btrfs_alloc_path(); |
| 6880 | if (!path) |
| 6881 | return -ENOMEM; |
| 6882 | |
Anand Jain | 3dd0f7a | 2018-04-12 10:29:32 +0800 | [diff] [blame] | 6883 | /* |
| 6884 | * uuid_mutex is needed only if we are mounting a sprout FS |
| 6885 | * otherwise we don't need it. |
| 6886 | */ |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6887 | mutex_lock(&uuid_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 6888 | mutex_lock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6889 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6890 | /* |
| 6891 | * Read all device items, and then all the chunk items. All |
| 6892 | * device items are found before any chunk item (their object id |
| 6893 | * is smaller than the lowest possible object id for a chunk |
| 6894 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6895 | */ |
| 6896 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6897 | key.offset = 0; |
| 6898 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6899 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6900 | if (ret < 0) |
| 6901 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6902 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6903 | leaf = path->nodes[0]; |
| 6904 | slot = path->slots[0]; |
| 6905 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6906 | ret = btrfs_next_leaf(root, path); |
| 6907 | if (ret == 0) |
| 6908 | continue; |
| 6909 | if (ret < 0) |
| 6910 | goto error; |
| 6911 | break; |
| 6912 | } |
| 6913 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6914 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6915 | struct btrfs_dev_item *dev_item; |
| 6916 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6917 | struct btrfs_dev_item); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6918 | ret = read_one_dev(fs_info, leaf, dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6919 | if (ret) |
| 6920 | goto error; |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6921 | total_dev++; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6922 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6923 | struct btrfs_chunk *chunk; |
| 6924 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 6925 | ret = read_one_chunk(fs_info, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6926 | if (ret) |
| 6927 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6928 | } |
| 6929 | path->slots[0]++; |
| 6930 | } |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6931 | |
| 6932 | /* |
| 6933 | * After loading chunk tree, we've got all device information, |
| 6934 | * do another round of validation checks. |
| 6935 | */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6936 | if (total_dev != fs_info->fs_devices->total_devices) { |
| 6937 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6938 | "super_num_devices %llu mismatch with num_devices %llu found here", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6939 | btrfs_super_num_devices(fs_info->super_copy), |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6940 | total_dev); |
| 6941 | ret = -EINVAL; |
| 6942 | goto error; |
| 6943 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6944 | if (btrfs_super_total_bytes(fs_info->super_copy) < |
| 6945 | fs_info->fs_devices->total_rw_bytes) { |
| 6946 | btrfs_err(fs_info, |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6947 | "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 6948 | btrfs_super_total_bytes(fs_info->super_copy), |
| 6949 | fs_info->fs_devices->total_rw_bytes); |
Liu Bo | 99e3ecf | 2016-06-03 12:05:14 -0700 | [diff] [blame] | 6950 | ret = -EINVAL; |
| 6951 | goto error; |
| 6952 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6953 | ret = 0; |
| 6954 | error: |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 6955 | mutex_unlock(&fs_info->chunk_mutex); |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6956 | mutex_unlock(&uuid_mutex); |
| 6957 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6958 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6959 | return ret; |
| 6960 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6961 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6962 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6963 | { |
| 6964 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6965 | struct btrfs_device *device; |
| 6966 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6967 | while (fs_devices) { |
| 6968 | mutex_lock(&fs_devices->device_list_mutex); |
| 6969 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 6970 | device->fs_info = fs_info; |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6971 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6972 | |
| 6973 | fs_devices = fs_devices->seed; |
| 6974 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6975 | } |
| 6976 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6977 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6978 | { |
| 6979 | int i; |
| 6980 | |
| 6981 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6982 | btrfs_dev_stat_reset(dev, i); |
| 6983 | } |
| 6984 | |
| 6985 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6986 | { |
| 6987 | struct btrfs_key key; |
| 6988 | struct btrfs_key found_key; |
| 6989 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6990 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6991 | struct extent_buffer *eb; |
| 6992 | int slot; |
| 6993 | int ret = 0; |
| 6994 | struct btrfs_device *device; |
| 6995 | struct btrfs_path *path = NULL; |
| 6996 | int i; |
| 6997 | |
| 6998 | path = btrfs_alloc_path(); |
| 6999 | if (!path) { |
| 7000 | ret = -ENOMEM; |
| 7001 | goto out; |
| 7002 | } |
| 7003 | |
| 7004 | mutex_lock(&fs_devices->device_list_mutex); |
| 7005 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 7006 | int item_size; |
| 7007 | struct btrfs_dev_stats_item *ptr; |
| 7008 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7009 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7010 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7011 | key.offset = device->devid; |
| 7012 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 7013 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7014 | __btrfs_reset_dev_stats(device); |
| 7015 | device->dev_stats_valid = 1; |
| 7016 | btrfs_release_path(path); |
| 7017 | continue; |
| 7018 | } |
| 7019 | slot = path->slots[0]; |
| 7020 | eb = path->nodes[0]; |
| 7021 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 7022 | item_size = btrfs_item_size_nr(eb, slot); |
| 7023 | |
| 7024 | ptr = btrfs_item_ptr(eb, slot, |
| 7025 | struct btrfs_dev_stats_item); |
| 7026 | |
| 7027 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7028 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 7029 | btrfs_dev_stat_set(device, i, |
| 7030 | btrfs_dev_stats_value(eb, ptr, i)); |
| 7031 | else |
| 7032 | btrfs_dev_stat_reset(device, i); |
| 7033 | } |
| 7034 | |
| 7035 | device->dev_stats_valid = 1; |
| 7036 | btrfs_dev_stat_print_on_load(device); |
| 7037 | btrfs_release_path(path); |
| 7038 | } |
| 7039 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7040 | |
| 7041 | out: |
| 7042 | btrfs_free_path(path); |
| 7043 | return ret < 0 ? ret : 0; |
| 7044 | } |
| 7045 | |
| 7046 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7047 | struct btrfs_device *device) |
| 7048 | { |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7049 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Jeff Mahoney | 6bccf3a | 2016-06-21 21:16:51 -0400 | [diff] [blame] | 7050 | struct btrfs_root *dev_root = fs_info->dev_root; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7051 | struct btrfs_path *path; |
| 7052 | struct btrfs_key key; |
| 7053 | struct extent_buffer *eb; |
| 7054 | struct btrfs_dev_stats_item *ptr; |
| 7055 | int ret; |
| 7056 | int i; |
| 7057 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 7058 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 7059 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7060 | key.offset = device->devid; |
| 7061 | |
| 7062 | path = btrfs_alloc_path(); |
David Sterba | fa25299 | 2017-02-15 09:35:01 +0100 | [diff] [blame] | 7063 | if (!path) |
| 7064 | return -ENOMEM; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7065 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 7066 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7067 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7068 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7069 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7070 | goto out; |
| 7071 | } |
| 7072 | |
| 7073 | if (ret == 0 && |
| 7074 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 7075 | /* need to delete old one and insert a new one */ |
| 7076 | ret = btrfs_del_item(trans, dev_root, path); |
| 7077 | if (ret != 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7078 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7079 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 7080 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7081 | goto out; |
| 7082 | } |
| 7083 | ret = 1; |
| 7084 | } |
| 7085 | |
| 7086 | if (ret == 1) { |
| 7087 | /* need to insert a new item */ |
| 7088 | btrfs_release_path(path); |
| 7089 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 7090 | &key, sizeof(*ptr)); |
| 7091 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7092 | btrfs_warn_in_rcu(fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7093 | "insert dev_stats item for device %s failed %d", |
| 7094 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7095 | goto out; |
| 7096 | } |
| 7097 | } |
| 7098 | |
| 7099 | eb = path->nodes[0]; |
| 7100 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 7101 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7102 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 7103 | btrfs_dev_stat_read(device, i)); |
| 7104 | btrfs_mark_buffer_dirty(eb); |
| 7105 | |
| 7106 | out: |
| 7107 | btrfs_free_path(path); |
| 7108 | return ret; |
| 7109 | } |
| 7110 | |
| 7111 | /* |
| 7112 | * called from commit_transaction. Writes all changed device stats to disk. |
| 7113 | */ |
| 7114 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 7115 | struct btrfs_fs_info *fs_info) |
| 7116 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7117 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7118 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7119 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7120 | int ret = 0; |
| 7121 | |
| 7122 | mutex_lock(&fs_devices->device_list_mutex); |
| 7123 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7124 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
| 7125 | if (!device->dev_stats_valid || stats_cnt == 0) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7126 | continue; |
| 7127 | |
Nikolay Borisov | 9deae96 | 2017-10-24 13:47:37 +0300 | [diff] [blame] | 7128 | |
| 7129 | /* |
| 7130 | * There is a LOAD-LOAD control dependency between the value of |
| 7131 | * dev_stats_ccnt and updating the on-disk values which requires |
| 7132 | * reading the in-memory counters. Such control dependencies |
| 7133 | * require explicit read memory barriers. |
| 7134 | * |
| 7135 | * This memory barriers pairs with smp_mb__before_atomic in |
| 7136 | * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full |
| 7137 | * barrier implied by atomic_xchg in |
| 7138 | * btrfs_dev_stats_read_and_reset |
| 7139 | */ |
| 7140 | smp_rmb(); |
| 7141 | |
Nikolay Borisov | 5495f19 | 2018-07-20 19:37:49 +0300 | [diff] [blame] | 7142 | ret = update_dev_stat_item(trans, device); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7143 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 7144 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7145 | } |
| 7146 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7147 | |
| 7148 | return ret; |
| 7149 | } |
| 7150 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7151 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 7152 | { |
| 7153 | btrfs_dev_stat_inc(dev, index); |
| 7154 | btrfs_dev_stat_print_on_error(dev); |
| 7155 | } |
| 7156 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7157 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7158 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7159 | if (!dev->dev_stats_valid) |
| 7160 | return; |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7161 | btrfs_err_rl_in_rcu(dev->fs_info, |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 7162 | "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] | 7163 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7164 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7165 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7166 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7167 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7168 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 7169 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7170 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7171 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 7172 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 7173 | int i; |
| 7174 | |
| 7175 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7176 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 7177 | break; |
| 7178 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 7179 | return; /* all values == 0, suppress message */ |
| 7180 | |
Jeff Mahoney | fb45625 | 2016-06-22 18:54:56 -0400 | [diff] [blame] | 7181 | btrfs_info_in_rcu(dev->fs_info, |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 7182 | "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] | 7183 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7184 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 7185 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 7186 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 7187 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 7188 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 7189 | } |
| 7190 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 7191 | int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7192 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7193 | { |
| 7194 | struct btrfs_device *dev; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7195 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7196 | int i; |
| 7197 | |
| 7198 | mutex_lock(&fs_devices->device_list_mutex); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7199 | dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7200 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7201 | |
| 7202 | if (!dev) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7203 | btrfs_warn(fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7204 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7205 | } else if (!dev->dev_stats_valid) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 7206 | btrfs_warn(fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 7207 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 7208 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 7209 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 7210 | if (stats->nr_items > i) |
| 7211 | stats->values[i] = |
| 7212 | btrfs_dev_stat_read_and_reset(dev, i); |
| 7213 | else |
| 7214 | btrfs_dev_stat_reset(dev, i); |
| 7215 | } |
| 7216 | } else { |
| 7217 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 7218 | if (stats->nr_items > i) |
| 7219 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 7220 | } |
| 7221 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 7222 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 7223 | return 0; |
| 7224 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7225 | |
David Sterba | da353f6 | 2017-02-14 17:55:53 +0100 | [diff] [blame] | 7226 | void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7227 | { |
| 7228 | struct buffer_head *bh; |
| 7229 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7230 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7231 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7232 | if (!bdev) |
| 7233 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7234 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7235 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 7236 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7237 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 7238 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 7239 | continue; |
| 7240 | |
| 7241 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 7242 | |
| 7243 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 7244 | set_buffer_dirty(bh); |
| 7245 | sync_dirty_buffer(bh); |
| 7246 | brelse(bh); |
| 7247 | } |
| 7248 | |
| 7249 | /* Notify udev that device has changed */ |
| 7250 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 7251 | |
| 7252 | /* Update ctime/mtime for device path for libblkid */ |
| 7253 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 7254 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7255 | |
| 7256 | /* |
| 7257 | * Update the size of all devices, which is used for writing out the |
| 7258 | * super blocks. |
| 7259 | */ |
| 7260 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 7261 | { |
| 7262 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7263 | struct btrfs_device *curr, *next; |
| 7264 | |
| 7265 | if (list_empty(&fs_devices->resized_devices)) |
| 7266 | return; |
| 7267 | |
| 7268 | mutex_lock(&fs_devices->device_list_mutex); |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7269 | mutex_lock(&fs_info->chunk_mutex); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7270 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 7271 | resized_list) { |
| 7272 | list_del_init(&curr->resized_list); |
| 7273 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 7274 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7275 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 7276 | mutex_unlock(&fs_devices->device_list_mutex); |
| 7277 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7278 | |
| 7279 | /* Must be invoked during the transaction commit */ |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7280 | void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans) |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7281 | { |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7282 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7283 | struct extent_map *em; |
| 7284 | struct map_lookup *map; |
| 7285 | struct btrfs_device *dev; |
| 7286 | int i; |
| 7287 | |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7288 | if (list_empty(&trans->pending_chunks)) |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7289 | return; |
| 7290 | |
| 7291 | /* In order to kick the device replace finish process */ |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7292 | mutex_lock(&fs_info->chunk_mutex); |
Nikolay Borisov | e9b919b | 2018-02-07 17:55:49 +0200 | [diff] [blame] | 7293 | list_for_each_entry(em, &trans->pending_chunks, list) { |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 7294 | map = em->map_lookup; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7295 | |
| 7296 | for (i = 0; i < map->num_stripes; i++) { |
| 7297 | dev = map->stripes[i].dev; |
| 7298 | dev->commit_bytes_used = dev->bytes_used; |
| 7299 | } |
| 7300 | } |
David Sterba | 3444136 | 2016-10-04 19:34:27 +0200 | [diff] [blame] | 7301 | mutex_unlock(&fs_info->chunk_mutex); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 7302 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 7303 | |
| 7304 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7305 | { |
| 7306 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7307 | while (fs_devices) { |
| 7308 | fs_devices->fs_info = fs_info; |
| 7309 | fs_devices = fs_devices->seed; |
| 7310 | } |
| 7311 | } |
| 7312 | |
| 7313 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 7314 | { |
| 7315 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 7316 | while (fs_devices) { |
| 7317 | fs_devices->fs_info = NULL; |
| 7318 | fs_devices = fs_devices->seed; |
| 7319 | } |
| 7320 | } |
David Sterba | 46df06b | 2018-07-13 20:46:30 +0200 | [diff] [blame] | 7321 | |
| 7322 | /* |
| 7323 | * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10. |
| 7324 | */ |
| 7325 | int btrfs_bg_type_to_factor(u64 flags) |
| 7326 | { |
| 7327 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 7328 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7329 | return 2; |
| 7330 | return 1; |
| 7331 | } |