Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 23 | #include <linux/random.h> |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 24 | #include <linux/iocontext.h> |
Ben Hutchings | 6f88a44 | 2010-12-29 14:55:03 +0000 | [diff] [blame] | 25 | #include <linux/capability.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 26 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 27 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 28 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 29 | #include <linux/semaphore.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 30 | #include <asm/div64.h> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | #include "ctree.h" |
| 32 | #include "extent_map.h" |
| 33 | #include "disk-io.h" |
| 34 | #include "transaction.h" |
| 35 | #include "print-tree.h" |
| 36 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 37 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 38 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 39 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 40 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 41 | #include "math.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 42 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 43 | #include "sysfs.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 44 | |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 45 | const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
| 46 | [BTRFS_RAID_RAID10] = { |
| 47 | .sub_stripes = 2, |
| 48 | .dev_stripes = 1, |
| 49 | .devs_max = 0, /* 0 == as many as possible */ |
| 50 | .devs_min = 4, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 51 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 52 | .devs_increment = 2, |
| 53 | .ncopies = 2, |
| 54 | }, |
| 55 | [BTRFS_RAID_RAID1] = { |
| 56 | .sub_stripes = 1, |
| 57 | .dev_stripes = 1, |
| 58 | .devs_max = 2, |
| 59 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 60 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 61 | .devs_increment = 2, |
| 62 | .ncopies = 2, |
| 63 | }, |
| 64 | [BTRFS_RAID_DUP] = { |
| 65 | .sub_stripes = 1, |
| 66 | .dev_stripes = 2, |
| 67 | .devs_max = 1, |
| 68 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 69 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 70 | .devs_increment = 1, |
| 71 | .ncopies = 2, |
| 72 | }, |
| 73 | [BTRFS_RAID_RAID0] = { |
| 74 | .sub_stripes = 1, |
| 75 | .dev_stripes = 1, |
| 76 | .devs_max = 0, |
| 77 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 78 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 79 | .devs_increment = 1, |
| 80 | .ncopies = 1, |
| 81 | }, |
| 82 | [BTRFS_RAID_SINGLE] = { |
| 83 | .sub_stripes = 1, |
| 84 | .dev_stripes = 1, |
| 85 | .devs_max = 1, |
| 86 | .devs_min = 1, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 87 | .tolerated_failures = 0, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 88 | .devs_increment = 1, |
| 89 | .ncopies = 1, |
| 90 | }, |
| 91 | [BTRFS_RAID_RAID5] = { |
| 92 | .sub_stripes = 1, |
| 93 | .dev_stripes = 1, |
| 94 | .devs_max = 0, |
| 95 | .devs_min = 2, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 96 | .tolerated_failures = 1, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 97 | .devs_increment = 1, |
| 98 | .ncopies = 2, |
| 99 | }, |
| 100 | [BTRFS_RAID_RAID6] = { |
| 101 | .sub_stripes = 1, |
| 102 | .dev_stripes = 1, |
| 103 | .devs_max = 0, |
| 104 | .devs_min = 3, |
Zhao Lei | 8789f4f | 2015-09-15 21:08:07 +0800 | [diff] [blame] | 105 | .tolerated_failures = 2, |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 106 | .devs_increment = 1, |
| 107 | .ncopies = 3, |
| 108 | }, |
| 109 | }; |
| 110 | |
Colin Ian King | fb75d85 | 2016-01-19 00:05:28 +0000 | [diff] [blame] | 111 | const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = { |
Zhao Lei | af90204 | 2015-09-15 21:08:06 +0800 | [diff] [blame] | 112 | [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10, |
| 113 | [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1, |
| 114 | [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP, |
| 115 | [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0, |
| 116 | [BTRFS_RAID_SINGLE] = 0, |
| 117 | [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5, |
| 118 | [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6, |
| 119 | }; |
| 120 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 121 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 122 | struct btrfs_root *root, |
| 123 | struct btrfs_device *device); |
| 124 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 125 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 126 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 127 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
David Sterba | 87ad58c | 2015-11-27 18:49:39 +0100 | [diff] [blame] | 128 | static void btrfs_close_one_device(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 129 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 130 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 131 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 132 | struct list_head *btrfs_get_fs_uuids(void) |
| 133 | { |
| 134 | return &fs_uuids; |
| 135 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 136 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 137 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 138 | { |
| 139 | struct btrfs_fs_devices *fs_devs; |
| 140 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 141 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 142 | if (!fs_devs) |
| 143 | return ERR_PTR(-ENOMEM); |
| 144 | |
| 145 | mutex_init(&fs_devs->device_list_mutex); |
| 146 | |
| 147 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 148 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 149 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 150 | INIT_LIST_HEAD(&fs_devs->list); |
| 151 | |
| 152 | return fs_devs; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 157 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 158 | * generated. |
| 159 | * |
| 160 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 161 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 162 | * can be destroyed with kfree() right away. |
| 163 | */ |
| 164 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 165 | { |
| 166 | struct btrfs_fs_devices *fs_devs; |
| 167 | |
| 168 | fs_devs = __alloc_fs_devices(); |
| 169 | if (IS_ERR(fs_devs)) |
| 170 | return fs_devs; |
| 171 | |
| 172 | if (fsid) |
| 173 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 174 | else |
| 175 | generate_random_uuid(fs_devs->fsid); |
| 176 | |
| 177 | return fs_devs; |
| 178 | } |
| 179 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 180 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 181 | { |
| 182 | struct btrfs_device *device; |
| 183 | WARN_ON(fs_devices->opened); |
| 184 | while (!list_empty(&fs_devices->devices)) { |
| 185 | device = list_entry(fs_devices->devices.next, |
| 186 | struct btrfs_device, dev_list); |
| 187 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 188 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 189 | kfree(device); |
| 190 | } |
| 191 | kfree(fs_devices); |
| 192 | } |
| 193 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 194 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 195 | enum kobject_action action) |
| 196 | { |
| 197 | int ret; |
| 198 | |
| 199 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 200 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 201 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 202 | action, |
| 203 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 204 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 205 | } |
| 206 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 207 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 208 | { |
| 209 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 210 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 211 | while (!list_empty(&fs_uuids)) { |
| 212 | fs_devices = list_entry(fs_uuids.next, |
| 213 | struct btrfs_fs_devices, list); |
| 214 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 215 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 216 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 219 | static struct btrfs_device *__alloc_device(void) |
| 220 | { |
| 221 | struct btrfs_device *dev; |
| 222 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 223 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 224 | if (!dev) |
| 225 | return ERR_PTR(-ENOMEM); |
| 226 | |
| 227 | INIT_LIST_HEAD(&dev->dev_list); |
| 228 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 229 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 230 | |
| 231 | spin_lock_init(&dev->io_lock); |
| 232 | |
| 233 | spin_lock_init(&dev->reada_lock); |
| 234 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 235 | atomic_set(&dev->dev_stats_ccnt, 0); |
Sebastian Andrzej Siewior | 546bed6 | 2016-01-15 14:37:15 +0100 | [diff] [blame] | 236 | btrfs_device_data_ordered_init(dev); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 237 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
| 238 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 239 | |
| 240 | return dev; |
| 241 | } |
| 242 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 243 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 244 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 245 | { |
| 246 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 247 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 248 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 249 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 250 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 251 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 252 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 253 | } |
| 254 | return NULL; |
| 255 | } |
| 256 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 257 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 258 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 259 | struct btrfs_fs_devices *fs_devices; |
| 260 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 261 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 262 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 263 | return fs_devices; |
| 264 | } |
| 265 | return NULL; |
| 266 | } |
| 267 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 268 | static int |
| 269 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 270 | int flush, struct block_device **bdev, |
| 271 | struct buffer_head **bh) |
| 272 | { |
| 273 | int ret; |
| 274 | |
| 275 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 276 | |
| 277 | if (IS_ERR(*bdev)) { |
| 278 | ret = PTR_ERR(*bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 279 | goto error; |
| 280 | } |
| 281 | |
| 282 | if (flush) |
| 283 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 284 | ret = set_blocksize(*bdev, 4096); |
| 285 | if (ret) { |
| 286 | blkdev_put(*bdev, flags); |
| 287 | goto error; |
| 288 | } |
| 289 | invalidate_bdev(*bdev); |
| 290 | *bh = btrfs_read_dev_super(*bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 291 | if (IS_ERR(*bh)) { |
| 292 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 293 | blkdev_put(*bdev, flags); |
| 294 | goto error; |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | |
| 299 | error: |
| 300 | *bdev = NULL; |
| 301 | *bh = NULL; |
| 302 | return ret; |
| 303 | } |
| 304 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 305 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 306 | struct bio *head, struct bio *tail) |
| 307 | { |
| 308 | |
| 309 | struct bio *old_head; |
| 310 | |
| 311 | old_head = pending_bios->head; |
| 312 | pending_bios->head = head; |
| 313 | if (pending_bios->tail) |
| 314 | tail->bi_next = old_head; |
| 315 | else |
| 316 | pending_bios->tail = tail; |
| 317 | } |
| 318 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 319 | /* |
| 320 | * we try to collect pending bios for a device so we don't get a large |
| 321 | * number of procs sending bios down to the same device. This greatly |
| 322 | * improves the schedulers ability to collect and merge the bios. |
| 323 | * |
| 324 | * But, it also turns into a long list of bios to process and that is sure |
| 325 | * to eventually make the worker thread block. The solution here is to |
| 326 | * make some progress and then put this work struct back at the end of |
| 327 | * the list if the block device is congested. This way, multiple devices |
| 328 | * can make progress from a single worker thread. |
| 329 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 330 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 331 | { |
| 332 | struct bio *pending; |
| 333 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 334 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 335 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 336 | struct bio *tail; |
| 337 | struct bio *cur; |
| 338 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 339 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 340 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 341 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 342 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 343 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 344 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 345 | struct blk_plug plug; |
| 346 | |
| 347 | /* |
| 348 | * this function runs all the bios we've collected for |
| 349 | * a particular device. We don't want to wander off to |
| 350 | * another device without first sending all of these down. |
| 351 | * So, setup a plug here and finish it off before we return |
| 352 | */ |
| 353 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 354 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 355 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 356 | fs_info = device->dev_root->fs_info; |
| 357 | limit = btrfs_async_submit_limit(fs_info); |
| 358 | limit = limit * 2 / 3; |
| 359 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 360 | loop: |
| 361 | spin_lock(&device->io_lock); |
| 362 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 363 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 364 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 365 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 366 | /* take all the bios off the list at once and process them |
| 367 | * later on (without the lock held). But, remember the |
| 368 | * tail and other pointers so the bios can be properly reinserted |
| 369 | * into the list if we hit congestion |
| 370 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 371 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 372 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 373 | force_reg = 1; |
| 374 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 375 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 376 | force_reg = 0; |
| 377 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 378 | |
| 379 | pending = pending_bios->head; |
| 380 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 381 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 382 | |
| 383 | /* |
| 384 | * if pending was null this time around, no bios need processing |
| 385 | * at all and we can stop. Otherwise it'll loop back up again |
| 386 | * and do an additional check so no bios are missed. |
| 387 | * |
| 388 | * device->running_pending is used to synchronize with the |
| 389 | * schedule_bio code. |
| 390 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 391 | if (device->pending_sync_bios.head == NULL && |
| 392 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 393 | again = 0; |
| 394 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 395 | } else { |
| 396 | again = 1; |
| 397 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 398 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 399 | |
| 400 | pending_bios->head = NULL; |
| 401 | pending_bios->tail = NULL; |
| 402 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 403 | spin_unlock(&device->io_lock); |
| 404 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 405 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 406 | |
| 407 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 408 | /* we want to work on both lists, but do more bios on the |
| 409 | * sync list than the regular list |
| 410 | */ |
| 411 | if ((num_run > 32 && |
| 412 | pending_bios != &device->pending_sync_bios && |
| 413 | device->pending_sync_bios.head) || |
| 414 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 415 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 416 | spin_lock(&device->io_lock); |
| 417 | requeue_list(pending_bios, pending, tail); |
| 418 | goto loop_lock; |
| 419 | } |
| 420 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 421 | cur = pending; |
| 422 | pending = pending->bi_next; |
| 423 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 424 | |
David Sterba | ee86395 | 2015-02-16 19:41:40 +0100 | [diff] [blame] | 425 | /* |
| 426 | * atomic_dec_return implies a barrier for waitqueue_active |
| 427 | */ |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 428 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 429 | waitqueue_active(&fs_info->async_submit_wait)) |
| 430 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 431 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 432 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 433 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 434 | /* |
| 435 | * if we're doing the sync list, record that our |
| 436 | * plug has some sync requests on it |
| 437 | * |
| 438 | * If we're doing the regular list and there are |
| 439 | * sync requests sitting around, unplug before |
| 440 | * we add more |
| 441 | */ |
| 442 | if (pending_bios == &device->pending_sync_bios) { |
| 443 | sync_pending = 1; |
| 444 | } else if (sync_pending) { |
| 445 | blk_finish_plug(&plug); |
| 446 | blk_start_plug(&plug); |
| 447 | sync_pending = 0; |
| 448 | } |
| 449 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 450 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 451 | num_run++; |
| 452 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 453 | |
| 454 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * we made progress, there is more work to do and the bdi |
| 458 | * is now congested. Back off and let other work structs |
| 459 | * run instead |
| 460 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 461 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 462 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 463 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 464 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 465 | ioc = current->io_context; |
| 466 | |
| 467 | /* |
| 468 | * the main goal here is that we don't want to |
| 469 | * block if we're going to be able to submit |
| 470 | * more requests without blocking. |
| 471 | * |
| 472 | * This code does two great things, it pokes into |
| 473 | * the elevator code from a filesystem _and_ |
| 474 | * it makes assumptions about how batching works. |
| 475 | */ |
| 476 | if (ioc && ioc->nr_batch_requests > 0 && |
| 477 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 478 | (last_waited == 0 || |
| 479 | ioc->last_waited == last_waited)) { |
| 480 | /* |
| 481 | * we want to go through our batch of |
| 482 | * requests and stop. So, we copy out |
| 483 | * the ioc->last_waited time and test |
| 484 | * against it before looping |
| 485 | */ |
| 486 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 487 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 488 | continue; |
| 489 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 490 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 491 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 492 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 493 | |
| 494 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 495 | btrfs_queue_work(fs_info->submit_workers, |
| 496 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 497 | goto done; |
| 498 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 499 | /* unplug every 64 requests just for good measure */ |
| 500 | if (batch_run % 64 == 0) { |
| 501 | blk_finish_plug(&plug); |
| 502 | blk_start_plug(&plug); |
| 503 | sync_pending = 0; |
| 504 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 505 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 506 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 507 | cond_resched(); |
| 508 | if (again) |
| 509 | goto loop; |
| 510 | |
| 511 | spin_lock(&device->io_lock); |
| 512 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 513 | goto loop_lock; |
| 514 | spin_unlock(&device->io_lock); |
| 515 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 516 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 517 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 520 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 521 | { |
| 522 | struct btrfs_device *device; |
| 523 | |
| 524 | device = container_of(work, struct btrfs_device, work); |
| 525 | run_scheduled_bios(device); |
| 526 | } |
| 527 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 528 | |
| 529 | void btrfs_free_stale_device(struct btrfs_device *cur_dev) |
| 530 | { |
| 531 | struct btrfs_fs_devices *fs_devs; |
| 532 | struct btrfs_device *dev; |
| 533 | |
| 534 | if (!cur_dev->name) |
| 535 | return; |
| 536 | |
| 537 | list_for_each_entry(fs_devs, &fs_uuids, list) { |
| 538 | int del = 1; |
| 539 | |
| 540 | if (fs_devs->opened) |
| 541 | continue; |
| 542 | if (fs_devs->seeding) |
| 543 | continue; |
| 544 | |
| 545 | list_for_each_entry(dev, &fs_devs->devices, dev_list) { |
| 546 | |
| 547 | if (dev == cur_dev) |
| 548 | continue; |
| 549 | if (!dev->name) |
| 550 | continue; |
| 551 | |
| 552 | /* |
| 553 | * Todo: This won't be enough. What if the same device |
| 554 | * comes back (with new uuid and) with its mapper path? |
| 555 | * But for now, this does help as mostly an admin will |
| 556 | * either use mapper or non mapper path throughout. |
| 557 | */ |
| 558 | rcu_read_lock(); |
| 559 | del = strcmp(rcu_str_deref(dev->name), |
| 560 | rcu_str_deref(cur_dev->name)); |
| 561 | rcu_read_unlock(); |
| 562 | if (!del) |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | if (!del) { |
| 567 | /* delete the stale device */ |
| 568 | if (fs_devs->num_devices == 1) { |
| 569 | btrfs_sysfs_remove_fsid(fs_devs); |
| 570 | list_del(&fs_devs->list); |
| 571 | free_fs_devices(fs_devs); |
| 572 | } else { |
| 573 | fs_devs->num_devices--; |
| 574 | list_del(&dev->dev_list); |
| 575 | rcu_string_free(dev->name); |
| 576 | kfree(dev); |
| 577 | } |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 583 | /* |
| 584 | * Add new device to list of registered devices |
| 585 | * |
| 586 | * Returns: |
| 587 | * 1 - first time device is seen |
| 588 | * 0 - device already known |
| 589 | * < 0 - error |
| 590 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 591 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 592 | struct btrfs_super_block *disk_super, |
| 593 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 594 | { |
| 595 | struct btrfs_device *device; |
| 596 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 597 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 598 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 599 | u64 found_transid = btrfs_super_generation(disk_super); |
| 600 | |
| 601 | fs_devices = find_fsid(disk_super->fsid); |
| 602 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 603 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 604 | if (IS_ERR(fs_devices)) |
| 605 | return PTR_ERR(fs_devices); |
| 606 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 607 | list_add(&fs_devices->list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 608 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 609 | device = NULL; |
| 610 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 611 | device = __find_device(&fs_devices->devices, devid, |
| 612 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 613 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 614 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 615 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 616 | if (fs_devices->opened) |
| 617 | return -EBUSY; |
| 618 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 619 | device = btrfs_alloc_device(NULL, &devid, |
| 620 | disk_super->dev_item.uuid); |
| 621 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 622 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 623 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 624 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 625 | |
| 626 | name = rcu_string_strdup(path, GFP_NOFS); |
| 627 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 628 | kfree(device); |
| 629 | return -ENOMEM; |
| 630 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 631 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 632 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 633 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 634 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 635 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 636 | mutex_unlock(&fs_devices->device_list_mutex); |
| 637 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 638 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 639 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 640 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 641 | /* |
| 642 | * When FS is already mounted. |
| 643 | * 1. If you are here and if the device->name is NULL that |
| 644 | * means this device was missing at time of FS mount. |
| 645 | * 2. If you are here and if the device->name is different |
| 646 | * from 'path' that means either |
| 647 | * a. The same device disappeared and reappeared with |
| 648 | * different name. or |
| 649 | * b. The missing-disk-which-was-replaced, has |
| 650 | * reappeared now. |
| 651 | * |
| 652 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 653 | * and unintentional. |
| 654 | * |
| 655 | * Further in case of 1 and 2a above, the disk at 'path' |
| 656 | * would have missed some transaction when it was away and |
| 657 | * in case of 2a the stale bdev has to be updated as well. |
| 658 | * 2b must not be allowed at all time. |
| 659 | */ |
| 660 | |
| 661 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 662 | * For now, we do allow update to btrfs_fs_device through the |
| 663 | * btrfs dev scan cli after FS has been mounted. We're still |
| 664 | * tracking a problem where systems fail mount by subvolume id |
| 665 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 666 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 667 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 668 | /* |
| 669 | * That is if the FS is _not_ mounted and if you |
| 670 | * are here, that means there is more than one |
| 671 | * disk with same uuid and devid.We keep the one |
| 672 | * with larger generation number or the last-in if |
| 673 | * generation are equal. |
| 674 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 675 | return -EEXIST; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 676 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 677 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 678 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 679 | if (!name) |
| 680 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 681 | rcu_string_free(device->name); |
| 682 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 683 | if (device->missing) { |
| 684 | fs_devices->missing_devices--; |
| 685 | device->missing = 0; |
| 686 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 687 | } |
| 688 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 689 | /* |
| 690 | * Unmount does not free the btrfs_device struct but would zero |
| 691 | * generation along with most of the other members. So just update |
| 692 | * it back. We need it to pick the disk with largest generation |
| 693 | * (as above). |
| 694 | */ |
| 695 | if (!fs_devices->opened) |
| 696 | device->generation = found_transid; |
| 697 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 698 | /* |
| 699 | * if there is new btrfs on an already registered device, |
| 700 | * then remove the stale device entry. |
| 701 | */ |
| 702 | btrfs_free_stale_device(device); |
| 703 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 704 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 705 | |
| 706 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 707 | } |
| 708 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 709 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 710 | { |
| 711 | struct btrfs_fs_devices *fs_devices; |
| 712 | struct btrfs_device *device; |
| 713 | struct btrfs_device *orig_dev; |
| 714 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 715 | fs_devices = alloc_fs_devices(orig->fsid); |
| 716 | if (IS_ERR(fs_devices)) |
| 717 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 718 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 719 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 720 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 721 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 722 | /* 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] | 723 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 724 | struct rcu_string *name; |
| 725 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 726 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 727 | orig_dev->uuid); |
| 728 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 729 | goto error; |
| 730 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 731 | /* |
| 732 | * This is ok to do without rcu read locked because we hold the |
| 733 | * uuid mutex so nothing we touch in here is going to disappear. |
| 734 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 735 | if (orig_dev->name) { |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 736 | name = rcu_string_strdup(orig_dev->name->str, |
| 737 | GFP_KERNEL); |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 738 | if (!name) { |
| 739 | kfree(device); |
| 740 | goto error; |
| 741 | } |
| 742 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 743 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 744 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 745 | list_add(&device->dev_list, &fs_devices->devices); |
| 746 | device->fs_devices = fs_devices; |
| 747 | fs_devices->num_devices++; |
| 748 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 749 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 750 | return fs_devices; |
| 751 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 752 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 753 | free_fs_devices(fs_devices); |
| 754 | return ERR_PTR(-ENOMEM); |
| 755 | } |
| 756 | |
Eric Sandeen | 9eaed21 | 2014-08-01 18:12:35 -0500 | [diff] [blame] | 757 | void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 758 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 759 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 760 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 761 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 762 | mutex_lock(&uuid_mutex); |
| 763 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 764 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 765 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 766 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 767 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 768 | (!latest_dev || |
| 769 | device->generation > latest_dev->generation)) { |
| 770 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 771 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 772 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 773 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 774 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 775 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 776 | /* |
| 777 | * In the first step, keep the device which has |
| 778 | * the correct fsid and the devid that is used |
| 779 | * for the dev_replace procedure. |
| 780 | * In the second step, the dev_replace state is |
| 781 | * read from the device tree and it is known |
| 782 | * whether the procedure is really active or |
| 783 | * not, which means whether this device is |
| 784 | * used or whether it should be removed. |
| 785 | */ |
| 786 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 787 | continue; |
| 788 | } |
| 789 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 790 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 791 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 792 | device->bdev = NULL; |
| 793 | fs_devices->open_devices--; |
| 794 | } |
| 795 | if (device->writeable) { |
| 796 | list_del_init(&device->dev_alloc_list); |
| 797 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 798 | if (!device->is_tgtdev_for_dev_replace) |
| 799 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 800 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 801 | list_del_init(&device->dev_list); |
| 802 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 803 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 804 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 805 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 806 | |
| 807 | if (fs_devices->seed) { |
| 808 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 809 | goto again; |
| 810 | } |
| 811 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 812 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 813 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 814 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 815 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 816 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 817 | static void __free_device(struct work_struct *work) |
| 818 | { |
| 819 | struct btrfs_device *device; |
| 820 | |
| 821 | device = container_of(work, struct btrfs_device, rcu_work); |
| 822 | |
| 823 | if (device->bdev) |
| 824 | blkdev_put(device->bdev, device->mode); |
| 825 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 826 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 827 | kfree(device); |
| 828 | } |
| 829 | |
| 830 | static void free_device(struct rcu_head *head) |
| 831 | { |
| 832 | struct btrfs_device *device; |
| 833 | |
| 834 | device = container_of(head, struct btrfs_device, rcu); |
| 835 | |
| 836 | INIT_WORK(&device->rcu_work, __free_device); |
| 837 | schedule_work(&device->rcu_work); |
| 838 | } |
| 839 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 840 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 841 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 842 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 843 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 844 | if (--fs_devices->opened > 0) |
| 845 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 846 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 847 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 848 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Anand Jain | f190aa4 | 2015-08-14 18:33:05 +0800 | [diff] [blame] | 849 | btrfs_close_one_device(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 850 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 851 | mutex_unlock(&fs_devices->device_list_mutex); |
| 852 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 853 | WARN_ON(fs_devices->open_devices); |
| 854 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 855 | fs_devices->opened = 0; |
| 856 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 857 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 861 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 862 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 863 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 864 | int ret; |
| 865 | |
| 866 | mutex_lock(&uuid_mutex); |
| 867 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 868 | if (!fs_devices->opened) { |
| 869 | seed_devices = fs_devices->seed; |
| 870 | fs_devices->seed = NULL; |
| 871 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 872 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 873 | |
| 874 | while (seed_devices) { |
| 875 | fs_devices = seed_devices; |
| 876 | seed_devices = fs_devices->seed; |
| 877 | __btrfs_close_devices(fs_devices); |
| 878 | free_fs_devices(fs_devices); |
| 879 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 880 | /* |
| 881 | * Wait for rcu kworkers under __btrfs_close_devices |
| 882 | * to finish all blkdev_puts so device is really |
| 883 | * free when umount is done. |
| 884 | */ |
| 885 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 886 | return ret; |
| 887 | } |
| 888 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 889 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 890 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 891 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 892 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 893 | struct block_device *bdev; |
| 894 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 895 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 896 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 897 | struct buffer_head *bh; |
| 898 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 899 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 900 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 901 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 902 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 903 | flags |= FMODE_EXCL; |
| 904 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 905 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 906 | if (device->bdev) |
| 907 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 908 | if (!device->name) |
| 909 | continue; |
| 910 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 911 | /* Just open everything we can; ignore failures here */ |
| 912 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 913 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 914 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 915 | |
| 916 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 917 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 918 | if (devid != device->devid) |
| 919 | goto error_brelse; |
| 920 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 921 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 922 | BTRFS_UUID_SIZE)) |
| 923 | goto error_brelse; |
| 924 | |
| 925 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 926 | if (!latest_dev || |
| 927 | device->generation > latest_dev->generation) |
| 928 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 929 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 930 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 931 | device->writeable = 0; |
| 932 | } else { |
| 933 | device->writeable = !bdev_read_only(bdev); |
| 934 | seeding = 0; |
| 935 | } |
| 936 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 937 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 938 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 939 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 940 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 941 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 942 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 943 | device->mode = flags; |
| 944 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 945 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 946 | fs_devices->rotating = 1; |
| 947 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 948 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 949 | if (device->writeable && |
| 950 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 951 | fs_devices->rw_devices++; |
| 952 | list_add(&device->dev_alloc_list, |
| 953 | &fs_devices->alloc_list); |
| 954 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 955 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 956 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 957 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 958 | error_brelse: |
| 959 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 960 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 961 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 962 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 963 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 964 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 965 | goto out; |
| 966 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 967 | fs_devices->seeding = seeding; |
| 968 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 969 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 970 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 971 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 976 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 977 | { |
| 978 | int ret; |
| 979 | |
| 980 | mutex_lock(&uuid_mutex); |
| 981 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 982 | fs_devices->opened++; |
| 983 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 984 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 985 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 986 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 987 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 988 | return ret; |
| 989 | } |
| 990 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 991 | void btrfs_release_disk_super(struct page *page) |
| 992 | { |
| 993 | kunmap(page); |
| 994 | put_page(page); |
| 995 | } |
| 996 | |
| 997 | int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr, |
| 998 | struct page **page, struct btrfs_super_block **disk_super) |
| 999 | { |
| 1000 | void *p; |
| 1001 | pgoff_t index; |
| 1002 | |
| 1003 | /* make sure our super fits in the device */ |
| 1004 | if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1005 | return 1; |
| 1006 | |
| 1007 | /* make sure our super fits in the page */ |
| 1008 | if (sizeof(**disk_super) > PAGE_SIZE) |
| 1009 | return 1; |
| 1010 | |
| 1011 | /* make sure our super doesn't straddle pages on disk */ |
| 1012 | index = bytenr >> PAGE_SHIFT; |
| 1013 | if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index) |
| 1014 | return 1; |
| 1015 | |
| 1016 | /* pull in the page with our super */ |
| 1017 | *page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1018 | index, GFP_KERNEL); |
| 1019 | |
| 1020 | if (IS_ERR_OR_NULL(*page)) |
| 1021 | return 1; |
| 1022 | |
| 1023 | p = kmap(*page); |
| 1024 | |
| 1025 | /* align our pointer to the offset of the super block */ |
| 1026 | *disk_super = p + (bytenr & ~PAGE_MASK); |
| 1027 | |
| 1028 | if (btrfs_super_bytenr(*disk_super) != bytenr || |
| 1029 | btrfs_super_magic(*disk_super) != BTRFS_MAGIC) { |
| 1030 | btrfs_release_disk_super(*page); |
| 1031 | return 1; |
| 1032 | } |
| 1033 | |
| 1034 | if ((*disk_super)->label[0] && |
| 1035 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1]) |
| 1036 | (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1041 | /* |
| 1042 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1043 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1044 | * is read via pagecache |
| 1045 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1046 | int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1047 | struct btrfs_fs_devices **fs_devices_ret) |
| 1048 | { |
| 1049 | struct btrfs_super_block *disk_super; |
| 1050 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1051 | struct page *page; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1052 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1053 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1054 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1055 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1056 | u64 bytenr; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1057 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1058 | /* |
| 1059 | * we would like to check all the supers, but that would make |
| 1060 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1061 | * So, we need to add a special mount option to scan for |
| 1062 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1063 | */ |
| 1064 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1065 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 1066 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1067 | |
| 1068 | bdev = blkdev_get_by_path(path, flags, holder); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1069 | if (IS_ERR(bdev)) { |
| 1070 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1071 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1074 | if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1075 | goto error_bdev_put; |
| 1076 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1077 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1078 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1079 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1080 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1081 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 1082 | if (ret > 0) { |
| 1083 | if (disk_super->label[0]) { |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 1084 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 1085 | } else { |
| 1086 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 1087 | } |
| 1088 | |
| 1089 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 1090 | ret = 0; |
| 1091 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1092 | if (!ret && fs_devices_ret) |
| 1093 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1094 | |
Anand Jain | 6cf86a00 | 2016-02-13 10:01:29 +0800 | [diff] [blame] | 1095 | btrfs_release_disk_super(page); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1096 | |
| 1097 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1098 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1099 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1100 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1101 | return ret; |
| 1102 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1103 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1104 | /* helper to account the used device space in the range */ |
| 1105 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 1106 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1107 | { |
| 1108 | struct btrfs_key key; |
| 1109 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1110 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1111 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1112 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1113 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1114 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1115 | struct extent_buffer *l; |
| 1116 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1117 | *length = 0; |
| 1118 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1119 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1120 | return 0; |
| 1121 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1122 | path = btrfs_alloc_path(); |
| 1123 | if (!path) |
| 1124 | return -ENOMEM; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1125 | path->reada = READA_FORWARD; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1126 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1127 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1128 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1129 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1130 | |
| 1131 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1132 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1133 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1134 | if (ret > 0) { |
| 1135 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1136 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1137 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1138 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1139 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1140 | while (1) { |
| 1141 | l = path->nodes[0]; |
| 1142 | slot = path->slots[0]; |
| 1143 | if (slot >= btrfs_header_nritems(l)) { |
| 1144 | ret = btrfs_next_leaf(root, path); |
| 1145 | if (ret == 0) |
| 1146 | continue; |
| 1147 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1148 | goto out; |
| 1149 | |
| 1150 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1151 | } |
| 1152 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1153 | |
| 1154 | if (key.objectid < device->devid) |
| 1155 | goto next; |
| 1156 | |
| 1157 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1158 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1159 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1160 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1161 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1162 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1163 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1164 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1165 | dev_extent); |
| 1166 | if (key.offset <= start && extent_end > end) { |
| 1167 | *length = end - start + 1; |
| 1168 | break; |
| 1169 | } else if (key.offset <= start && extent_end > start) |
| 1170 | *length += extent_end - start; |
| 1171 | else if (key.offset > start && extent_end <= end) |
| 1172 | *length += extent_end - key.offset; |
| 1173 | else if (key.offset > start && key.offset <= end) { |
| 1174 | *length += end - key.offset + 1; |
| 1175 | break; |
| 1176 | } else if (key.offset > end) |
| 1177 | break; |
| 1178 | |
| 1179 | next: |
| 1180 | path->slots[0]++; |
| 1181 | } |
| 1182 | ret = 0; |
| 1183 | out: |
| 1184 | btrfs_free_path(path); |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1188 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1189 | struct btrfs_device *device, |
| 1190 | u64 *start, u64 len) |
| 1191 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1192 | struct btrfs_fs_info *fs_info = device->dev_root->fs_info; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1193 | struct extent_map *em; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1194 | struct list_head *search_list = &fs_info->pinned_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1195 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1196 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1197 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1198 | if (transaction) |
| 1199 | search_list = &transaction->pending_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1200 | again: |
| 1201 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1202 | struct map_lookup *map; |
| 1203 | int i; |
| 1204 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 1205 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1206 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1207 | u64 end; |
| 1208 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1209 | if (map->stripes[i].dev != device) |
| 1210 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1211 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1212 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1213 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1214 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1215 | /* |
| 1216 | * Make sure that while processing the pinned list we do |
| 1217 | * not override our *start with a lower value, because |
| 1218 | * we can have pinned chunks that fall within this |
| 1219 | * device hole and that have lower physical addresses |
| 1220 | * than the pending chunks we processed before. If we |
| 1221 | * do not take this special care we can end up getting |
| 1222 | * 2 pending chunks that start at the same physical |
| 1223 | * device offsets because the end offset of a pinned |
| 1224 | * chunk can be equal to the start offset of some |
| 1225 | * pending chunk. |
| 1226 | */ |
| 1227 | end = map->stripes[i].physical + em->orig_block_len; |
| 1228 | if (end > *start) { |
| 1229 | *start = end; |
| 1230 | ret = 1; |
| 1231 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1232 | } |
| 1233 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1234 | if (search_list != &fs_info->pinned_chunks) { |
| 1235 | search_list = &fs_info->pinned_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1236 | goto again; |
| 1237 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1238 | |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
| 1242 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1243 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1244 | * find_free_dev_extent_start - find free space in the specified device |
| 1245 | * @device: the device which we search the free space in |
| 1246 | * @num_bytes: the size of the free space that we need |
| 1247 | * @search_start: the position from which to begin the search |
| 1248 | * @start: store the start of the free space. |
| 1249 | * @len: the size of the free space. that we find, or the size |
| 1250 | * 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] | 1251 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1252 | * this uses a pretty simple search, the expectation is that it is |
| 1253 | * called very infrequently and that a given device has a small number |
| 1254 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1255 | * |
| 1256 | * @start is used to store the start of the free space if we find. But if we |
| 1257 | * don't find suitable free space, it will be used to store the start position |
| 1258 | * of the max free space. |
| 1259 | * |
| 1260 | * @len is used to store the size of the free space that we find. |
| 1261 | * But if we don't find suitable free space, it is used to store the size of |
| 1262 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1263 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1264 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
| 1265 | struct btrfs_device *device, u64 num_bytes, |
| 1266 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1267 | { |
| 1268 | struct btrfs_key key; |
| 1269 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1270 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1271 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1272 | u64 hole_size; |
| 1273 | u64 max_hole_start; |
| 1274 | u64 max_hole_size; |
| 1275 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1276 | u64 search_end = device->total_bytes; |
| 1277 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1278 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1279 | struct extent_buffer *l; |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1280 | u64 min_search_start; |
| 1281 | |
| 1282 | /* |
| 1283 | * We don't want to overwrite the superblock on the drive nor any area |
| 1284 | * used by the boot loader (grub for example), so we make sure to start |
| 1285 | * at an offset of at least 1MB. |
| 1286 | */ |
| 1287 | min_search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
| 1288 | search_start = max(search_start, min_search_start); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1289 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1290 | path = btrfs_alloc_path(); |
| 1291 | if (!path) |
| 1292 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1293 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1294 | max_hole_start = search_start; |
| 1295 | max_hole_size = 0; |
| 1296 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1297 | again: |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1298 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1299 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1300 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 1303 | path->reada = READA_FORWARD; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1304 | path->search_commit_root = 1; |
| 1305 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1306 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1307 | key.objectid = device->devid; |
| 1308 | key.offset = search_start; |
| 1309 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1310 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1311 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1312 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1313 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1314 | if (ret > 0) { |
| 1315 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1316 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1317 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1318 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1319 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1320 | while (1) { |
| 1321 | l = path->nodes[0]; |
| 1322 | slot = path->slots[0]; |
| 1323 | if (slot >= btrfs_header_nritems(l)) { |
| 1324 | ret = btrfs_next_leaf(root, path); |
| 1325 | if (ret == 0) |
| 1326 | continue; |
| 1327 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1328 | goto out; |
| 1329 | |
| 1330 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1331 | } |
| 1332 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1333 | |
| 1334 | if (key.objectid < device->devid) |
| 1335 | goto next; |
| 1336 | |
| 1337 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1338 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1339 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1340 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1341 | goto next; |
| 1342 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1343 | if (key.offset > search_start) { |
| 1344 | hole_size = key.offset - search_start; |
| 1345 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1346 | /* |
| 1347 | * Have to check before we set max_hole_start, otherwise |
| 1348 | * we could end up sending back this offset anyway. |
| 1349 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1350 | if (contains_pending_extent(transaction, device, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1351 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1352 | hole_size)) { |
| 1353 | if (key.offset >= search_start) { |
| 1354 | hole_size = key.offset - search_start; |
| 1355 | } else { |
| 1356 | WARN_ON_ONCE(1); |
| 1357 | hole_size = 0; |
| 1358 | } |
| 1359 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1360 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1361 | if (hole_size > max_hole_size) { |
| 1362 | max_hole_start = search_start; |
| 1363 | max_hole_size = hole_size; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * If this free space is greater than which we need, |
| 1368 | * it must be the max free space that we have found |
| 1369 | * until now, so max_hole_start must point to the start |
| 1370 | * of this free space and the length of this free space |
| 1371 | * is stored in max_hole_size. Thus, we return |
| 1372 | * max_hole_start and max_hole_size and go back to the |
| 1373 | * caller. |
| 1374 | */ |
| 1375 | if (hole_size >= num_bytes) { |
| 1376 | ret = 0; |
| 1377 | goto out; |
| 1378 | } |
| 1379 | } |
| 1380 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1381 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1382 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1383 | dev_extent); |
| 1384 | if (extent_end > search_start) |
| 1385 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1386 | next: |
| 1387 | path->slots[0]++; |
| 1388 | cond_resched(); |
| 1389 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1390 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1391 | /* |
| 1392 | * At this point, search_start should be the end of |
| 1393 | * allocated dev extents, and when shrinking the device, |
| 1394 | * search_end may be smaller than search_start. |
| 1395 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1396 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1397 | hole_size = search_end - search_start; |
| 1398 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1399 | if (contains_pending_extent(transaction, device, &search_start, |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1400 | hole_size)) { |
| 1401 | btrfs_release_path(path); |
| 1402 | goto again; |
| 1403 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1404 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1405 | if (hole_size > max_hole_size) { |
| 1406 | max_hole_start = search_start; |
| 1407 | max_hole_size = hole_size; |
| 1408 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1409 | } |
| 1410 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1411 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1412 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1413 | ret = -ENOSPC; |
| 1414 | else |
| 1415 | ret = 0; |
| 1416 | |
| 1417 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1418 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1419 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1420 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1421 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1422 | return ret; |
| 1423 | } |
| 1424 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1425 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1426 | struct btrfs_device *device, u64 num_bytes, |
| 1427 | u64 *start, u64 *len) |
| 1428 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1429 | /* FIXME use last free of some kind */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1430 | return find_free_dev_extent_start(trans->transaction, device, |
Filipe Manana | 8cdc7c5 | 2016-01-06 22:42:35 +0000 | [diff] [blame] | 1431 | num_bytes, 0, start, len); |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1432 | } |
| 1433 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1434 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1435 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1436 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1437 | { |
| 1438 | int ret; |
| 1439 | struct btrfs_path *path; |
| 1440 | struct btrfs_root *root = device->dev_root; |
| 1441 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1442 | struct btrfs_key found_key; |
| 1443 | struct extent_buffer *leaf = NULL; |
| 1444 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1445 | |
| 1446 | path = btrfs_alloc_path(); |
| 1447 | if (!path) |
| 1448 | return -ENOMEM; |
| 1449 | |
| 1450 | key.objectid = device->devid; |
| 1451 | key.offset = start; |
| 1452 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1453 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1454 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1455 | if (ret > 0) { |
| 1456 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1457 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1458 | if (ret) |
| 1459 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1460 | leaf = path->nodes[0]; |
| 1461 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1462 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1463 | struct btrfs_dev_extent); |
| 1464 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1465 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1466 | key = found_key; |
| 1467 | btrfs_release_path(path); |
| 1468 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1469 | } else if (ret == 0) { |
| 1470 | leaf = path->nodes[0]; |
| 1471 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1472 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1473 | } else { |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 1474 | btrfs_std_error(root->fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1475 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1476 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1477 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1478 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1479 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1480 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1481 | if (ret) { |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 1482 | btrfs_std_error(root->fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1483 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1484 | } else { |
Josef Bacik | 3204d33 | 2015-09-24 10:46:10 -0400 | [diff] [blame] | 1485 | set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1486 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1487 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1488 | btrfs_free_path(path); |
| 1489 | return ret; |
| 1490 | } |
| 1491 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1492 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1493 | struct btrfs_device *device, |
| 1494 | u64 chunk_tree, u64 chunk_objectid, |
| 1495 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1496 | { |
| 1497 | int ret; |
| 1498 | struct btrfs_path *path; |
| 1499 | struct btrfs_root *root = device->dev_root; |
| 1500 | struct btrfs_dev_extent *extent; |
| 1501 | struct extent_buffer *leaf; |
| 1502 | struct btrfs_key key; |
| 1503 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1504 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1505 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1506 | path = btrfs_alloc_path(); |
| 1507 | if (!path) |
| 1508 | return -ENOMEM; |
| 1509 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1510 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1511 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1512 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1513 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1514 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1515 | if (ret) |
| 1516 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1517 | |
| 1518 | leaf = path->nodes[0]; |
| 1519 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1520 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1521 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1522 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1523 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1524 | |
| 1525 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1526 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1527 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1528 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1529 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1530 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1531 | btrfs_free_path(path); |
| 1532 | return ret; |
| 1533 | } |
| 1534 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1535 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1536 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1537 | struct extent_map_tree *em_tree; |
| 1538 | struct extent_map *em; |
| 1539 | struct rb_node *n; |
| 1540 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1541 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1542 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1543 | read_lock(&em_tree->lock); |
| 1544 | n = rb_last(&em_tree->map); |
| 1545 | if (n) { |
| 1546 | em = rb_entry(n, struct extent_map, rb_node); |
| 1547 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1548 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1549 | read_unlock(&em_tree->lock); |
| 1550 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1551 | return ret; |
| 1552 | } |
| 1553 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1554 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1555 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1556 | { |
| 1557 | int ret; |
| 1558 | struct btrfs_key key; |
| 1559 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1560 | struct btrfs_path *path; |
| 1561 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1562 | path = btrfs_alloc_path(); |
| 1563 | if (!path) |
| 1564 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1565 | |
| 1566 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1567 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1568 | key.offset = (u64)-1; |
| 1569 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1570 | 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] | 1571 | if (ret < 0) |
| 1572 | goto error; |
| 1573 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1574 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1575 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1576 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1577 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1578 | BTRFS_DEV_ITEM_KEY); |
| 1579 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1580 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1581 | } else { |
| 1582 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1583 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1584 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1585 | } |
| 1586 | ret = 0; |
| 1587 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1588 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * the device information is stored in the chunk root |
| 1594 | * the btrfs_device struct should be fully filled in |
| 1595 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1596 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1597 | struct btrfs_root *root, |
| 1598 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1599 | { |
| 1600 | int ret; |
| 1601 | struct btrfs_path *path; |
| 1602 | struct btrfs_dev_item *dev_item; |
| 1603 | struct extent_buffer *leaf; |
| 1604 | struct btrfs_key key; |
| 1605 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1606 | |
| 1607 | root = root->fs_info->chunk_root; |
| 1608 | |
| 1609 | path = btrfs_alloc_path(); |
| 1610 | if (!path) |
| 1611 | return -ENOMEM; |
| 1612 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1613 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1614 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1615 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1616 | |
| 1617 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1618 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1619 | if (ret) |
| 1620 | goto out; |
| 1621 | |
| 1622 | leaf = path->nodes[0]; |
| 1623 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1624 | |
| 1625 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1626 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1627 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1628 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1629 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1630 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1631 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1632 | btrfs_device_get_disk_total_bytes(device)); |
| 1633 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1634 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1635 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1636 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1637 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1638 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1639 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1640 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1641 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1642 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1643 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1644 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1645 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1646 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1647 | out: |
| 1648 | btrfs_free_path(path); |
| 1649 | return ret; |
| 1650 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1651 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1652 | /* |
| 1653 | * Function to update ctime/mtime for a given device path. |
| 1654 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1655 | */ |
| 1656 | static void update_dev_time(char *path_name) |
| 1657 | { |
| 1658 | struct file *filp; |
| 1659 | |
| 1660 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1661 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1662 | return; |
| 1663 | file_update_time(filp); |
| 1664 | filp_close(filp, NULL); |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1665 | } |
| 1666 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1667 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1668 | struct btrfs_device *device) |
| 1669 | { |
| 1670 | int ret; |
| 1671 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1672 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1673 | struct btrfs_trans_handle *trans; |
| 1674 | |
| 1675 | root = root->fs_info->chunk_root; |
| 1676 | |
| 1677 | path = btrfs_alloc_path(); |
| 1678 | if (!path) |
| 1679 | return -ENOMEM; |
| 1680 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1681 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1682 | if (IS_ERR(trans)) { |
| 1683 | btrfs_free_path(path); |
| 1684 | return PTR_ERR(trans); |
| 1685 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1686 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1687 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1688 | key.offset = device->devid; |
| 1689 | |
| 1690 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1691 | if (ret < 0) |
| 1692 | goto out; |
| 1693 | |
| 1694 | if (ret > 0) { |
| 1695 | ret = -ENOENT; |
| 1696 | goto out; |
| 1697 | } |
| 1698 | |
| 1699 | ret = btrfs_del_item(trans, root, path); |
| 1700 | if (ret) |
| 1701 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1702 | out: |
| 1703 | btrfs_free_path(path); |
| 1704 | btrfs_commit_transaction(trans, root); |
| 1705 | return ret; |
| 1706 | } |
| 1707 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1708 | static int __check_raid_min_devices(struct btrfs_fs_info *fs_info) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1709 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1710 | u64 all_avail; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1711 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1712 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1713 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1714 | num_devices = fs_info->fs_devices->num_devices; |
| 1715 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); |
| 1716 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1717 | WARN_ON(num_devices < 1); |
| 1718 | num_devices--; |
| 1719 | } |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1720 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1721 | |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1722 | do { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1723 | seq = read_seqbegin(&fs_info->profiles_lock); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1724 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1725 | all_avail = fs_info->avail_data_alloc_bits | |
| 1726 | fs_info->avail_system_alloc_bits | |
| 1727 | fs_info->avail_metadata_alloc_bits; |
| 1728 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1729 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1730 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1731 | return BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1732 | } |
| 1733 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1734 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1735 | return BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1736 | } |
| 1737 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1738 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1739 | fs_info->fs_devices->rw_devices <= 2) { |
| 1740 | return BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1741 | } |
| 1742 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1743 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1744 | fs_info->fs_devices->rw_devices <= 3) { |
| 1745 | return BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
| 1746 | } |
| 1747 | |
| 1748 | return 0; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1749 | } |
| 1750 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame^] | 1751 | int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1752 | { |
| 1753 | struct btrfs_device *device; |
| 1754 | struct btrfs_device *next_device; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1755 | struct btrfs_fs_devices *cur_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1756 | u64 num_devices; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1757 | int ret = 0; |
| 1758 | bool clear_super = false; |
Anand Jain | 42b6742 | 2016-02-13 10:01:38 +0800 | [diff] [blame] | 1759 | char *dev_name = NULL; |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1760 | |
| 1761 | mutex_lock(&uuid_mutex); |
| 1762 | |
Anand Jain | bd45ffb | 2016-02-13 10:01:34 +0800 | [diff] [blame] | 1763 | ret = __check_raid_min_devices(root->fs_info); |
Anand Jain | f1fa7f2 | 2016-02-13 10:01:33 +0800 | [diff] [blame] | 1764 | if (ret) |
| 1765 | goto out; |
| 1766 | |
Anand Jain | 6b526ed | 2016-02-13 10:01:39 +0800 | [diff] [blame^] | 1767 | ret = btrfs_find_device_by_user_input(root, devid, device_path, |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1768 | &device); |
| 1769 | if (ret) |
| 1770 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1771 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1772 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1773 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1774 | goto out; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1775 | } |
| 1776 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1777 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1778 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1779 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1783 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1784 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1785 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1786 | unlock_chunks(root); |
Anand Jain | 42b6742 | 2016-02-13 10:01:38 +0800 | [diff] [blame] | 1787 | dev_name = kstrdup(device->name->str, GFP_KERNEL); |
| 1788 | if (!dev_name) { |
| 1789 | ret = -ENOMEM; |
| 1790 | goto error_undo; |
| 1791 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1792 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1793 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1794 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1795 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1796 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1797 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1798 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1799 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1800 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1801 | /* |
| 1802 | * TODO: the superblock still includes this device in its num_devices |
| 1803 | * counter although write_all_supers() is not locked out. This |
| 1804 | * could give a filesystem state which requires a degraded mount. |
| 1805 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1806 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1807 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1808 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1809 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1810 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1811 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1812 | |
| 1813 | /* |
| 1814 | * the device list mutex makes sure that we don't change |
| 1815 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1816 | * the device supers. Whoever is writing all supers, should |
| 1817 | * lock the device list mutex before getting the number of |
| 1818 | * devices in the super block (super_copy). Conversely, |
| 1819 | * whoever updates the number of devices in the super block |
| 1820 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1821 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1822 | |
| 1823 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1824 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1825 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1826 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1827 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1828 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1829 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1830 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1831 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1832 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1833 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1834 | struct btrfs_device, dev_list); |
| 1835 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1836 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1837 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1838 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1839 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1840 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1841 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1842 | /* remove sysfs entry */ |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 1843 | btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1844 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1845 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1846 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1847 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1848 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1849 | btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1850 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1851 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1852 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1853 | struct btrfs_fs_devices *fs_devices; |
| 1854 | fs_devices = root->fs_info->fs_devices; |
| 1855 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1856 | if (fs_devices->seed == cur_devices) { |
| 1857 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1858 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1859 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1860 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1861 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1862 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1863 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1864 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1865 | } |
| 1866 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1867 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1868 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1869 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1870 | /* |
| 1871 | * at this point, the device is zero sized. We want to |
| 1872 | * remove it from the devices list and zero out the old super |
| 1873 | */ |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1874 | if (clear_super) { |
Anand Jain | 42b6742 | 2016-02-13 10:01:38 +0800 | [diff] [blame] | 1875 | struct block_device *bdev; |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1876 | |
Anand Jain | 42b6742 | 2016-02-13 10:01:38 +0800 | [diff] [blame] | 1877 | bdev = blkdev_get_by_path(dev_name, FMODE_READ | FMODE_EXCL, |
| 1878 | root->fs_info->bdev_holder); |
| 1879 | if (!IS_ERR(bdev)) { |
| 1880 | btrfs_scratch_superblocks(bdev, dev_name); |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1881 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1882 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1883 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1884 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1885 | out: |
Anand Jain | 42b6742 | 2016-02-13 10:01:38 +0800 | [diff] [blame] | 1886 | kfree(dev_name); |
| 1887 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1888 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1889 | return ret; |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1890 | |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1891 | error_undo: |
| 1892 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1893 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1894 | list_add(&device->dev_alloc_list, |
| 1895 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1896 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1897 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1898 | } |
Anand Jain | 24fc572 | 2016-02-13 10:01:36 +0800 | [diff] [blame] | 1899 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1900 | } |
| 1901 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1902 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1903 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1904 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1905 | struct btrfs_fs_devices *fs_devices; |
| 1906 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1907 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1908 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1909 | /* |
| 1910 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1911 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1912 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1913 | * srcdev will have its correct fs_devices in both the cases. |
| 1914 | */ |
| 1915 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1916 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1917 | list_del_rcu(&srcdev->dev_list); |
| 1918 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1919 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1920 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1921 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1922 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1923 | if (srcdev->writeable) { |
| 1924 | fs_devices->rw_devices--; |
| 1925 | /* zero out the old super if it is writable */ |
David Sterba | 31388ab | 2015-11-19 11:35:17 +0100 | [diff] [blame] | 1926 | btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1927 | } |
| 1928 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1929 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1930 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 1934 | struct btrfs_device *srcdev) |
| 1935 | { |
| 1936 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1937 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1938 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1939 | |
| 1940 | /* |
| 1941 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 1942 | * zero |
| 1943 | */ |
| 1944 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 1945 | |
| 1946 | /* if this is no devs we rather delete the fs_devices */ |
| 1947 | if (!fs_devices->num_devices) { |
| 1948 | struct btrfs_fs_devices *tmp_fs_devices; |
| 1949 | |
| 1950 | tmp_fs_devices = fs_info->fs_devices; |
| 1951 | while (tmp_fs_devices) { |
| 1952 | if (tmp_fs_devices->seed == fs_devices) { |
| 1953 | tmp_fs_devices->seed = fs_devices->seed; |
| 1954 | break; |
| 1955 | } |
| 1956 | tmp_fs_devices = tmp_fs_devices->seed; |
| 1957 | } |
| 1958 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 1959 | __btrfs_close_devices(fs_devices); |
| 1960 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1961 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1965 | struct btrfs_device *tgtdev) |
| 1966 | { |
| 1967 | struct btrfs_device *next_device; |
| 1968 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1969 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1970 | WARN_ON(!tgtdev); |
| 1971 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 1972 | |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 1973 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 1974 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1975 | if (tgtdev->bdev) { |
David Sterba | 31388ab | 2015-11-19 11:35:17 +0100 | [diff] [blame] | 1976 | btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1977 | fs_info->fs_devices->open_devices--; |
| 1978 | } |
| 1979 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1980 | |
| 1981 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1982 | struct btrfs_device, dev_list); |
| 1983 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1984 | fs_info->sb->s_bdev = next_device->bdev; |
| 1985 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1986 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1987 | list_del_rcu(&tgtdev->dev_list); |
| 1988 | |
| 1989 | call_rcu(&tgtdev->rcu, free_device); |
| 1990 | |
| 1991 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1992 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1993 | } |
| 1994 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1995 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1996 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1997 | { |
| 1998 | int ret = 0; |
| 1999 | struct btrfs_super_block *disk_super; |
| 2000 | u64 devid; |
| 2001 | u8 *dev_uuid; |
| 2002 | struct block_device *bdev; |
| 2003 | struct buffer_head *bh; |
| 2004 | |
| 2005 | *device = NULL; |
| 2006 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 2007 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 2008 | if (ret) |
| 2009 | return ret; |
| 2010 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2011 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2012 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2013 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2014 | disk_super->fsid); |
| 2015 | brelse(bh); |
| 2016 | if (!*device) |
| 2017 | ret = -ENOENT; |
| 2018 | blkdev_put(bdev, FMODE_READ); |
| 2019 | return ret; |
| 2020 | } |
| 2021 | |
| 2022 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 2023 | char *device_path, |
| 2024 | struct btrfs_device **device) |
| 2025 | { |
| 2026 | *device = NULL; |
| 2027 | if (strcmp(device_path, "missing") == 0) { |
| 2028 | struct list_head *devices; |
| 2029 | struct btrfs_device *tmp; |
| 2030 | |
| 2031 | devices = &root->fs_info->fs_devices->devices; |
| 2032 | /* |
| 2033 | * It is safe to read the devices since the volume_mutex |
| 2034 | * is held by the caller. |
| 2035 | */ |
| 2036 | list_for_each_entry(tmp, devices, dev_list) { |
| 2037 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 2038 | *device = tmp; |
| 2039 | break; |
| 2040 | } |
| 2041 | } |
| 2042 | |
Anand Jain | d74a625 | 2015-08-14 18:32:56 +0800 | [diff] [blame] | 2043 | if (!*device) |
| 2044 | return BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2045 | |
| 2046 | return 0; |
| 2047 | } else { |
| 2048 | return btrfs_find_device_by_path(root, device_path, device); |
| 2049 | } |
| 2050 | } |
| 2051 | |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2052 | int btrfs_find_device_by_user_input(struct btrfs_root *root, u64 srcdevid, |
| 2053 | char *srcdev_name, |
| 2054 | struct btrfs_device **device) |
| 2055 | { |
| 2056 | int ret; |
| 2057 | |
| 2058 | if (srcdevid) { |
| 2059 | ret = 0; |
| 2060 | *device = btrfs_find_device(root->fs_info, srcdevid, NULL, |
| 2061 | NULL); |
| 2062 | if (!*device) |
| 2063 | ret = -ENOENT; |
| 2064 | } else { |
Anand Jain | b3d1b15 | 2016-02-13 10:01:37 +0800 | [diff] [blame] | 2065 | if (!srcdev_name || !srcdev_name[0]) |
| 2066 | return -EINVAL; |
| 2067 | |
Anand Jain | 24e0474 | 2016-02-13 10:01:35 +0800 | [diff] [blame] | 2068 | ret = btrfs_find_device_missing_or_by_path(root, srcdev_name, |
| 2069 | device); |
| 2070 | } |
| 2071 | return ret; |
| 2072 | } |
| 2073 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2074 | /* |
| 2075 | * does all the dirty work required for changing file system's UUID. |
| 2076 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2077 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2078 | { |
| 2079 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2080 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2081 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2082 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2083 | struct btrfs_device *device; |
| 2084 | u64 super_flags; |
| 2085 | |
| 2086 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2087 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2088 | return -EINVAL; |
| 2089 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2090 | seed_devices = __alloc_fs_devices(); |
| 2091 | if (IS_ERR(seed_devices)) |
| 2092 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2093 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2094 | old_devices = clone_fs_devices(fs_devices); |
| 2095 | if (IS_ERR(old_devices)) { |
| 2096 | kfree(seed_devices); |
| 2097 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2098 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2099 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2100 | list_add(&old_devices->list, &fs_uuids); |
| 2101 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2102 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2103 | seed_devices->opened = 1; |
| 2104 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2105 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2106 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2107 | |
| 2108 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2109 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2110 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2111 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2112 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2113 | |
| 2114 | lock_chunks(root); |
| 2115 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 2116 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2117 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2118 | fs_devices->seeding = 0; |
| 2119 | fs_devices->num_devices = 0; |
| 2120 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2121 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2122 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2123 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2124 | |
| 2125 | generate_random_uuid(fs_devices->fsid); |
| 2126 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2127 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2128 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2129 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2130 | super_flags = btrfs_super_flags(disk_super) & |
| 2131 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2132 | btrfs_set_super_flags(disk_super, super_flags); |
| 2133 | |
| 2134 | return 0; |
| 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * strore the expected generation for seed devices in device items. |
| 2139 | */ |
| 2140 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2141 | struct btrfs_root *root) |
| 2142 | { |
| 2143 | struct btrfs_path *path; |
| 2144 | struct extent_buffer *leaf; |
| 2145 | struct btrfs_dev_item *dev_item; |
| 2146 | struct btrfs_device *device; |
| 2147 | struct btrfs_key key; |
| 2148 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2149 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2150 | u64 devid; |
| 2151 | int ret; |
| 2152 | |
| 2153 | path = btrfs_alloc_path(); |
| 2154 | if (!path) |
| 2155 | return -ENOMEM; |
| 2156 | |
| 2157 | root = root->fs_info->chunk_root; |
| 2158 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2159 | key.offset = 0; |
| 2160 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2161 | |
| 2162 | while (1) { |
| 2163 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2164 | if (ret < 0) |
| 2165 | goto error; |
| 2166 | |
| 2167 | leaf = path->nodes[0]; |
| 2168 | next_slot: |
| 2169 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2170 | ret = btrfs_next_leaf(root, path); |
| 2171 | if (ret > 0) |
| 2172 | break; |
| 2173 | if (ret < 0) |
| 2174 | goto error; |
| 2175 | leaf = path->nodes[0]; |
| 2176 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2177 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2178 | continue; |
| 2179 | } |
| 2180 | |
| 2181 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2182 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2183 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2184 | break; |
| 2185 | |
| 2186 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2187 | struct btrfs_dev_item); |
| 2188 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2189 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2190 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2191 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2192 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2193 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2194 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2195 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2196 | |
| 2197 | if (device->fs_devices->seeding) { |
| 2198 | btrfs_set_device_generation(leaf, dev_item, |
| 2199 | device->generation); |
| 2200 | btrfs_mark_buffer_dirty(leaf); |
| 2201 | } |
| 2202 | |
| 2203 | path->slots[0]++; |
| 2204 | goto next_slot; |
| 2205 | } |
| 2206 | ret = 0; |
| 2207 | error: |
| 2208 | btrfs_free_path(path); |
| 2209 | return ret; |
| 2210 | } |
| 2211 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2212 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2213 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2214 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2215 | struct btrfs_trans_handle *trans; |
| 2216 | struct btrfs_device *device; |
| 2217 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2218 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2219 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2220 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2221 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2222 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2223 | int ret = 0; |
| 2224 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2225 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2226 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2227 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2228 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2229 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2230 | if (IS_ERR(bdev)) |
| 2231 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2232 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2233 | if (root->fs_info->fs_devices->seeding) { |
| 2234 | seeding_dev = 1; |
| 2235 | down_write(&sb->s_umount); |
| 2236 | mutex_lock(&uuid_mutex); |
| 2237 | } |
| 2238 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2239 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2240 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2241 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2242 | |
| 2243 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2244 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2245 | if (device->bdev == bdev) { |
| 2246 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2247 | mutex_unlock( |
| 2248 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2249 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2250 | } |
| 2251 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2252 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2253 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2254 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2255 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2256 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2257 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2258 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2259 | } |
| 2260 | |
David Sterba | 78f2c9e | 2016-02-11 14:25:38 +0100 | [diff] [blame] | 2261 | name = rcu_string_strdup(device_path, GFP_KERNEL); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2262 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2263 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2264 | ret = -ENOMEM; |
| 2265 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2266 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2267 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2268 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2269 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2270 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2271 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2272 | kfree(device); |
| 2273 | ret = PTR_ERR(trans); |
| 2274 | goto error; |
| 2275 | } |
| 2276 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2277 | q = bdev_get_queue(bdev); |
| 2278 | if (blk_queue_discard(q)) |
| 2279 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2280 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2281 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2282 | device->io_width = root->sectorsize; |
| 2283 | device->io_align = root->sectorsize; |
| 2284 | device->sector_size = root->sectorsize; |
| 2285 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2286 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2287 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2288 | device->dev_root = root->fs_info->dev_root; |
| 2289 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2290 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2291 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2292 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2293 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2294 | set_blocksize(device->bdev, 4096); |
| 2295 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2296 | if (seeding_dev) { |
| 2297 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2298 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2299 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2303 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2304 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2305 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2306 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2307 | list_add(&device->dev_alloc_list, |
| 2308 | &root->fs_info->fs_devices->alloc_list); |
| 2309 | root->fs_info->fs_devices->num_devices++; |
| 2310 | root->fs_info->fs_devices->open_devices++; |
| 2311 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2312 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2313 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2314 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2315 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2316 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2317 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2318 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2319 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2320 | root->fs_info->fs_devices->rotating = 1; |
| 2321 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2322 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2323 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2324 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2325 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2326 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2327 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2328 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2329 | |
| 2330 | /* add sysfs device entry */ |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 2331 | btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2332 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2333 | /* |
| 2334 | * we've got more storage, clear any full flags on the space |
| 2335 | * infos |
| 2336 | */ |
| 2337 | btrfs_clear_space_info_full(root->fs_info); |
| 2338 | |
| 2339 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2340 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2341 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2342 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2343 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2344 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2345 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2346 | if (ret) { |
| 2347 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2348 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2349 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | ret = btrfs_add_device(trans, root, device); |
| 2353 | if (ret) { |
| 2354 | btrfs_abort_transaction(trans, root, ret); |
| 2355 | goto error_trans; |
| 2356 | } |
| 2357 | |
| 2358 | if (seeding_dev) { |
| 2359 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2360 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2361 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2362 | if (ret) { |
| 2363 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2364 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2365 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2366 | |
| 2367 | /* Sprouting would change fsid of the mounted root, |
| 2368 | * so rename the fsid on the sysfs |
| 2369 | */ |
| 2370 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2371 | root->fs_info->fsid); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 2372 | if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj, |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 2373 | fsid_buf)) |
David Sterba | f14d104 | 2015-10-08 11:37:06 +0200 | [diff] [blame] | 2374 | btrfs_warn(root->fs_info, |
| 2375 | "sysfs: failed to create fsid for sprout"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2376 | } |
| 2377 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2378 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2379 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2380 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2381 | |
| 2382 | if (seeding_dev) { |
| 2383 | mutex_unlock(&uuid_mutex); |
| 2384 | up_write(&sb->s_umount); |
| 2385 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2386 | if (ret) /* transaction commit */ |
| 2387 | return ret; |
| 2388 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2389 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2390 | if (ret < 0) |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 2391 | btrfs_std_error(root->fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2392 | "Failed to relocate sys chunks after " |
| 2393 | "device initialization. This can be fixed " |
| 2394 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2395 | trans = btrfs_attach_transaction(root); |
| 2396 | if (IS_ERR(trans)) { |
| 2397 | if (PTR_ERR(trans) == -ENOENT) |
| 2398 | return 0; |
| 2399 | return PTR_ERR(trans); |
| 2400 | } |
| 2401 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2402 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2403 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2404 | /* Update ctime/mtime for libblkid */ |
| 2405 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2406 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2407 | |
| 2408 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2409 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2410 | rcu_string_free(device->name); |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 2411 | btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2412 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2413 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2414 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2415 | if (seeding_dev) { |
| 2416 | mutex_unlock(&uuid_mutex); |
| 2417 | up_write(&sb->s_umount); |
| 2418 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2419 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2420 | } |
| 2421 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2422 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2423 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2424 | struct btrfs_device **device_out) |
| 2425 | { |
| 2426 | struct request_queue *q; |
| 2427 | struct btrfs_device *device; |
| 2428 | struct block_device *bdev; |
| 2429 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2430 | struct list_head *devices; |
| 2431 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2432 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2433 | int ret = 0; |
| 2434 | |
| 2435 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2436 | if (fs_info->fs_devices->seeding) { |
| 2437 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2438 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2439 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2440 | |
| 2441 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2442 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2443 | if (IS_ERR(bdev)) { |
| 2444 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2445 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2446 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2447 | |
| 2448 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2449 | |
| 2450 | devices = &fs_info->fs_devices->devices; |
| 2451 | list_for_each_entry(device, devices, dev_list) { |
| 2452 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2453 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2454 | ret = -EEXIST; |
| 2455 | goto error; |
| 2456 | } |
| 2457 | } |
| 2458 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2459 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2460 | if (i_size_read(bdev->bd_inode) < |
| 2461 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2462 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2463 | ret = -EINVAL; |
| 2464 | goto error; |
| 2465 | } |
| 2466 | |
| 2467 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2468 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2469 | if (IS_ERR(device)) { |
| 2470 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2471 | goto error; |
| 2472 | } |
| 2473 | |
| 2474 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2475 | if (!name) { |
| 2476 | kfree(device); |
| 2477 | ret = -ENOMEM; |
| 2478 | goto error; |
| 2479 | } |
| 2480 | rcu_assign_pointer(device->name, name); |
| 2481 | |
| 2482 | q = bdev_get_queue(bdev); |
| 2483 | if (blk_queue_discard(q)) |
| 2484 | device->can_discard = 1; |
| 2485 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2486 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2487 | device->generation = 0; |
| 2488 | device->io_width = root->sectorsize; |
| 2489 | device->io_align = root->sectorsize; |
| 2490 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2491 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2492 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2493 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2494 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2495 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2496 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2497 | device->dev_root = fs_info->dev_root; |
| 2498 | device->bdev = bdev; |
| 2499 | device->in_fs_metadata = 1; |
| 2500 | device->is_tgtdev_for_dev_replace = 1; |
| 2501 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2502 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2503 | set_blocksize(device->bdev, 4096); |
| 2504 | device->fs_devices = fs_info->fs_devices; |
| 2505 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2506 | fs_info->fs_devices->num_devices++; |
| 2507 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2508 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2509 | |
| 2510 | *device_out = device; |
| 2511 | return ret; |
| 2512 | |
| 2513 | error: |
| 2514 | blkdev_put(bdev, FMODE_EXCL); |
| 2515 | return ret; |
| 2516 | } |
| 2517 | |
| 2518 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2519 | struct btrfs_device *tgtdev) |
| 2520 | { |
| 2521 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2522 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2523 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2524 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2525 | tgtdev->dev_root = fs_info->dev_root; |
| 2526 | tgtdev->in_fs_metadata = 1; |
| 2527 | } |
| 2528 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2529 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2530 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2531 | { |
| 2532 | int ret; |
| 2533 | struct btrfs_path *path; |
| 2534 | struct btrfs_root *root; |
| 2535 | struct btrfs_dev_item *dev_item; |
| 2536 | struct extent_buffer *leaf; |
| 2537 | struct btrfs_key key; |
| 2538 | |
| 2539 | root = device->dev_root->fs_info->chunk_root; |
| 2540 | |
| 2541 | path = btrfs_alloc_path(); |
| 2542 | if (!path) |
| 2543 | return -ENOMEM; |
| 2544 | |
| 2545 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2546 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2547 | key.offset = device->devid; |
| 2548 | |
| 2549 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2550 | if (ret < 0) |
| 2551 | goto out; |
| 2552 | |
| 2553 | if (ret > 0) { |
| 2554 | ret = -ENOENT; |
| 2555 | goto out; |
| 2556 | } |
| 2557 | |
| 2558 | leaf = path->nodes[0]; |
| 2559 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2560 | |
| 2561 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2562 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2563 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2564 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2565 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2566 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2567 | btrfs_device_get_disk_total_bytes(device)); |
| 2568 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2569 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2570 | btrfs_mark_buffer_dirty(leaf); |
| 2571 | |
| 2572 | out: |
| 2573 | btrfs_free_path(path); |
| 2574 | return ret; |
| 2575 | } |
| 2576 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2577 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2578 | struct btrfs_device *device, u64 new_size) |
| 2579 | { |
| 2580 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2581 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2582 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2583 | u64 old_total; |
| 2584 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2585 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2586 | if (!device->writeable) |
| 2587 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2588 | |
| 2589 | lock_chunks(device->dev_root); |
| 2590 | old_total = btrfs_super_total_bytes(super_copy); |
| 2591 | diff = new_size - device->total_bytes; |
| 2592 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2593 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2594 | device->is_tgtdev_for_dev_replace) { |
| 2595 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2596 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2597 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2598 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2599 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2600 | |
| 2601 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2602 | device->fs_devices->total_rw_bytes += diff; |
| 2603 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2604 | btrfs_device_set_total_bytes(device, new_size); |
| 2605 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2606 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2607 | if (list_empty(&device->resized_list)) |
| 2608 | list_add_tail(&device->resized_list, |
| 2609 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2610 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2611 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2612 | return btrfs_update_device(trans, device); |
| 2613 | } |
| 2614 | |
| 2615 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2616 | struct btrfs_root *root, u64 chunk_objectid, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2617 | u64 chunk_offset) |
| 2618 | { |
| 2619 | int ret; |
| 2620 | struct btrfs_path *path; |
| 2621 | struct btrfs_key key; |
| 2622 | |
| 2623 | root = root->fs_info->chunk_root; |
| 2624 | path = btrfs_alloc_path(); |
| 2625 | if (!path) |
| 2626 | return -ENOMEM; |
| 2627 | |
| 2628 | key.objectid = chunk_objectid; |
| 2629 | key.offset = chunk_offset; |
| 2630 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2631 | |
| 2632 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2633 | if (ret < 0) |
| 2634 | goto out; |
| 2635 | else if (ret > 0) { /* Logic error or corruption */ |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 2636 | btrfs_std_error(root->fs_info, -ENOENT, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2637 | "Failed lookup while freeing chunk."); |
| 2638 | ret = -ENOENT; |
| 2639 | goto out; |
| 2640 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2641 | |
| 2642 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2643 | if (ret < 0) |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 2644 | btrfs_std_error(root->fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2645 | "Failed to delete chunk item."); |
| 2646 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2647 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2648 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2649 | } |
| 2650 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2651 | static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64 |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2652 | chunk_offset) |
| 2653 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2654 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2655 | struct btrfs_disk_key *disk_key; |
| 2656 | struct btrfs_chunk *chunk; |
| 2657 | u8 *ptr; |
| 2658 | int ret = 0; |
| 2659 | u32 num_stripes; |
| 2660 | u32 array_size; |
| 2661 | u32 len = 0; |
| 2662 | u32 cur; |
| 2663 | struct btrfs_key key; |
| 2664 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2665 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2666 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2667 | |
| 2668 | ptr = super_copy->sys_chunk_array; |
| 2669 | cur = 0; |
| 2670 | |
| 2671 | while (cur < array_size) { |
| 2672 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2673 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2674 | |
| 2675 | len = sizeof(*disk_key); |
| 2676 | |
| 2677 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2678 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2679 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2680 | len += btrfs_chunk_item_size(num_stripes); |
| 2681 | } else { |
| 2682 | ret = -EIO; |
| 2683 | break; |
| 2684 | } |
| 2685 | if (key.objectid == chunk_objectid && |
| 2686 | key.offset == chunk_offset) { |
| 2687 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2688 | array_size -= len; |
| 2689 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2690 | } else { |
| 2691 | ptr += len; |
| 2692 | cur += len; |
| 2693 | } |
| 2694 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2695 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2696 | return ret; |
| 2697 | } |
| 2698 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2699 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2700 | struct btrfs_root *root, u64 chunk_offset) |
| 2701 | { |
| 2702 | struct extent_map_tree *em_tree; |
| 2703 | struct extent_map *em; |
| 2704 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2705 | struct map_lookup *map; |
| 2706 | u64 dev_extent_len = 0; |
| 2707 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2708 | int i, ret = 0; |
| 2709 | |
| 2710 | /* Just in case */ |
| 2711 | root = root->fs_info->chunk_root; |
| 2712 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2713 | |
| 2714 | read_lock(&em_tree->lock); |
| 2715 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2716 | read_unlock(&em_tree->lock); |
| 2717 | |
| 2718 | if (!em || em->start > chunk_offset || |
| 2719 | em->start + em->len < chunk_offset) { |
| 2720 | /* |
| 2721 | * 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] | 2722 | * user having built with ASSERT enabled, so if ASSERT doesn't |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2723 | * do anything we still error out. |
| 2724 | */ |
| 2725 | ASSERT(0); |
| 2726 | if (em) |
| 2727 | free_extent_map(em); |
| 2728 | return -EINVAL; |
| 2729 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 2730 | map = em->map_lookup; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2731 | lock_chunks(root->fs_info->chunk_root); |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 2732 | check_system_chunk(trans, extent_root, map->type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2733 | unlock_chunks(root->fs_info->chunk_root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2734 | |
| 2735 | for (i = 0; i < map->num_stripes; i++) { |
| 2736 | struct btrfs_device *device = map->stripes[i].dev; |
| 2737 | ret = btrfs_free_dev_extent(trans, device, |
| 2738 | map->stripes[i].physical, |
| 2739 | &dev_extent_len); |
| 2740 | if (ret) { |
| 2741 | btrfs_abort_transaction(trans, root, ret); |
| 2742 | goto out; |
| 2743 | } |
| 2744 | |
| 2745 | if (device->bytes_used > 0) { |
| 2746 | lock_chunks(root); |
| 2747 | btrfs_device_set_bytes_used(device, |
| 2748 | device->bytes_used - dev_extent_len); |
| 2749 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2750 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2751 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2752 | btrfs_clear_space_info_full(root->fs_info); |
| 2753 | unlock_chunks(root); |
| 2754 | } |
| 2755 | |
| 2756 | if (map->stripes[i].dev) { |
| 2757 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2758 | if (ret) { |
| 2759 | btrfs_abort_transaction(trans, root, ret); |
| 2760 | goto out; |
| 2761 | } |
| 2762 | } |
| 2763 | } |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2764 | ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2765 | if (ret) { |
| 2766 | btrfs_abort_transaction(trans, root, ret); |
| 2767 | goto out; |
| 2768 | } |
| 2769 | |
| 2770 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2771 | |
| 2772 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2773 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2774 | if (ret) { |
| 2775 | btrfs_abort_transaction(trans, root, ret); |
| 2776 | goto out; |
| 2777 | } |
| 2778 | } |
| 2779 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2780 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2781 | if (ret) { |
| 2782 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2783 | goto out; |
| 2784 | } |
| 2785 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2786 | out: |
| 2787 | /* once for us */ |
| 2788 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2789 | return ret; |
| 2790 | } |
| 2791 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 2792 | static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2793 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2794 | struct btrfs_root *extent_root; |
| 2795 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2796 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2797 | |
| 2798 | root = root->fs_info->chunk_root; |
| 2799 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2800 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2801 | /* |
| 2802 | * Prevent races with automatic removal of unused block groups. |
| 2803 | * After we relocate and before we remove the chunk with offset |
| 2804 | * chunk_offset, automatic removal of the block group can kick in, |
| 2805 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2806 | * |
| 2807 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2808 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2809 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2810 | * we release the path used to search the chunk/dev tree and before |
| 2811 | * the current task acquires this mutex and calls us. |
| 2812 | */ |
| 2813 | ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex)); |
| 2814 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2815 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2816 | if (ret) |
| 2817 | return -ENOSPC; |
| 2818 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2819 | /* step one, relocate all the extents inside this chunk */ |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2820 | btrfs_scrub_pause(root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2821 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2822 | btrfs_scrub_continue(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2823 | if (ret) |
| 2824 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2825 | |
Filipe Manana | 7fd0118 | 2015-11-13 23:57:17 +0000 | [diff] [blame] | 2826 | trans = btrfs_start_trans_remove_block_group(root->fs_info, |
| 2827 | chunk_offset); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2828 | if (IS_ERR(trans)) { |
| 2829 | ret = PTR_ERR(trans); |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 2830 | btrfs_std_error(root->fs_info, ret, NULL); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2831 | return ret; |
| 2832 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2833 | |
| 2834 | /* |
| 2835 | * step two, delete the device extents and the |
| 2836 | * chunk tree entries |
| 2837 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2838 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2839 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2840 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2841 | } |
| 2842 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2843 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2844 | { |
| 2845 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2846 | struct btrfs_path *path; |
| 2847 | struct extent_buffer *leaf; |
| 2848 | struct btrfs_chunk *chunk; |
| 2849 | struct btrfs_key key; |
| 2850 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2851 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2852 | bool retried = false; |
| 2853 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2854 | int ret; |
| 2855 | |
| 2856 | path = btrfs_alloc_path(); |
| 2857 | if (!path) |
| 2858 | return -ENOMEM; |
| 2859 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2860 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2861 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2862 | key.offset = (u64)-1; |
| 2863 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2864 | |
| 2865 | while (1) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2866 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2867 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2868 | if (ret < 0) { |
| 2869 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2870 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2871 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2872 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2873 | |
| 2874 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2875 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2876 | if (ret) |
| 2877 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2878 | if (ret < 0) |
| 2879 | goto error; |
| 2880 | if (ret > 0) |
| 2881 | break; |
| 2882 | |
| 2883 | leaf = path->nodes[0]; |
| 2884 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2885 | |
| 2886 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2887 | struct btrfs_chunk); |
| 2888 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2889 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2890 | |
| 2891 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2892 | ret = btrfs_relocate_chunk(chunk_root, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2893 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2894 | if (ret == -ENOSPC) |
| 2895 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2896 | else |
| 2897 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2898 | } |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2899 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2900 | |
| 2901 | if (found_key.offset == 0) |
| 2902 | break; |
| 2903 | key.offset = found_key.offset - 1; |
| 2904 | } |
| 2905 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2906 | if (failed && !retried) { |
| 2907 | failed = 0; |
| 2908 | retried = true; |
| 2909 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2910 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2911 | ret = -ENOSPC; |
| 2912 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2913 | error: |
| 2914 | btrfs_free_path(path); |
| 2915 | return ret; |
| 2916 | } |
| 2917 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2918 | static int insert_balance_item(struct btrfs_root *root, |
| 2919 | struct btrfs_balance_control *bctl) |
| 2920 | { |
| 2921 | struct btrfs_trans_handle *trans; |
| 2922 | struct btrfs_balance_item *item; |
| 2923 | struct btrfs_disk_balance_args disk_bargs; |
| 2924 | struct btrfs_path *path; |
| 2925 | struct extent_buffer *leaf; |
| 2926 | struct btrfs_key key; |
| 2927 | int ret, err; |
| 2928 | |
| 2929 | path = btrfs_alloc_path(); |
| 2930 | if (!path) |
| 2931 | return -ENOMEM; |
| 2932 | |
| 2933 | trans = btrfs_start_transaction(root, 0); |
| 2934 | if (IS_ERR(trans)) { |
| 2935 | btrfs_free_path(path); |
| 2936 | return PTR_ERR(trans); |
| 2937 | } |
| 2938 | |
| 2939 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 2940 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2941 | key.offset = 0; |
| 2942 | |
| 2943 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2944 | sizeof(*item)); |
| 2945 | if (ret) |
| 2946 | goto out; |
| 2947 | |
| 2948 | leaf = path->nodes[0]; |
| 2949 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2950 | |
| 2951 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2952 | |
| 2953 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2954 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2955 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2956 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2957 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2958 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2959 | |
| 2960 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2961 | |
| 2962 | btrfs_mark_buffer_dirty(leaf); |
| 2963 | out: |
| 2964 | btrfs_free_path(path); |
| 2965 | err = btrfs_commit_transaction(trans, root); |
| 2966 | if (err && !ret) |
| 2967 | ret = err; |
| 2968 | return ret; |
| 2969 | } |
| 2970 | |
| 2971 | static int del_balance_item(struct btrfs_root *root) |
| 2972 | { |
| 2973 | struct btrfs_trans_handle *trans; |
| 2974 | struct btrfs_path *path; |
| 2975 | struct btrfs_key key; |
| 2976 | int ret, err; |
| 2977 | |
| 2978 | path = btrfs_alloc_path(); |
| 2979 | if (!path) |
| 2980 | return -ENOMEM; |
| 2981 | |
| 2982 | trans = btrfs_start_transaction(root, 0); |
| 2983 | if (IS_ERR(trans)) { |
| 2984 | btrfs_free_path(path); |
| 2985 | return PTR_ERR(trans); |
| 2986 | } |
| 2987 | |
| 2988 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 2989 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2990 | key.offset = 0; |
| 2991 | |
| 2992 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2993 | if (ret < 0) |
| 2994 | goto out; |
| 2995 | if (ret > 0) { |
| 2996 | ret = -ENOENT; |
| 2997 | goto out; |
| 2998 | } |
| 2999 | |
| 3000 | ret = btrfs_del_item(trans, root, path); |
| 3001 | out: |
| 3002 | btrfs_free_path(path); |
| 3003 | err = btrfs_commit_transaction(trans, root); |
| 3004 | if (err && !ret) |
| 3005 | ret = err; |
| 3006 | return ret; |
| 3007 | } |
| 3008 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3009 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3010 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3011 | * resume after balance was interrupted. |
| 3012 | */ |
| 3013 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3014 | { |
| 3015 | /* |
| 3016 | * Turn on soft mode for chunk types that were being converted. |
| 3017 | */ |
| 3018 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3019 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3020 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3021 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3022 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3023 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3024 | |
| 3025 | /* |
| 3026 | * Turn on usage filter if is not already used. The idea is |
| 3027 | * that chunks that we have already balanced should be |
| 3028 | * reasonably full. Don't do it for chunks that are being |
| 3029 | * converted - that will keep us from relocating unconverted |
| 3030 | * (albeit full) chunks. |
| 3031 | */ |
| 3032 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3033 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3034 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3035 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3036 | bctl->data.usage = 90; |
| 3037 | } |
| 3038 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3039 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3040 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3041 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3042 | bctl->sys.usage = 90; |
| 3043 | } |
| 3044 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3045 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3046 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3047 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3048 | bctl->meta.usage = 90; |
| 3049 | } |
| 3050 | } |
| 3051 | |
| 3052 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3053 | * Should be called with both balance and volume mutexes held to |
| 3054 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 3055 | * restriper. Same goes for unset_balance_control. |
| 3056 | */ |
| 3057 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 3058 | { |
| 3059 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 3060 | |
| 3061 | BUG_ON(fs_info->balance_ctl); |
| 3062 | |
| 3063 | spin_lock(&fs_info->balance_lock); |
| 3064 | fs_info->balance_ctl = bctl; |
| 3065 | spin_unlock(&fs_info->balance_lock); |
| 3066 | } |
| 3067 | |
| 3068 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 3069 | { |
| 3070 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3071 | |
| 3072 | BUG_ON(!fs_info->balance_ctl); |
| 3073 | |
| 3074 | spin_lock(&fs_info->balance_lock); |
| 3075 | fs_info->balance_ctl = NULL; |
| 3076 | spin_unlock(&fs_info->balance_lock); |
| 3077 | |
| 3078 | kfree(bctl); |
| 3079 | } |
| 3080 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3081 | /* |
| 3082 | * Balance filters. Return 1 if chunk should be filtered out |
| 3083 | * (should not be balanced). |
| 3084 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3085 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3086 | struct btrfs_balance_args *bargs) |
| 3087 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3088 | chunk_type = chunk_to_extended(chunk_type) & |
| 3089 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3090 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3091 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3092 | return 0; |
| 3093 | |
| 3094 | return 1; |
| 3095 | } |
| 3096 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3097 | 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] | 3098 | struct btrfs_balance_args *bargs) |
| 3099 | { |
| 3100 | struct btrfs_block_group_cache *cache; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3101 | u64 chunk_used; |
| 3102 | u64 user_thresh_min; |
| 3103 | u64 user_thresh_max; |
| 3104 | int ret = 1; |
| 3105 | |
| 3106 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3107 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3108 | |
| 3109 | if (bargs->usage_min == 0) |
| 3110 | user_thresh_min = 0; |
| 3111 | else |
| 3112 | user_thresh_min = div_factor_fine(cache->key.offset, |
| 3113 | bargs->usage_min); |
| 3114 | |
| 3115 | if (bargs->usage_max == 0) |
| 3116 | user_thresh_max = 1; |
| 3117 | else if (bargs->usage_max > 100) |
| 3118 | user_thresh_max = cache->key.offset; |
| 3119 | else |
| 3120 | user_thresh_max = div_factor_fine(cache->key.offset, |
| 3121 | bargs->usage_max); |
| 3122 | |
| 3123 | if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max) |
| 3124 | ret = 0; |
| 3125 | |
| 3126 | btrfs_put_block_group(cache); |
| 3127 | return ret; |
| 3128 | } |
| 3129 | |
Holger Hoffstätte | dba72cb | 2015-11-17 12:29:32 +0100 | [diff] [blame] | 3130 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3131 | u64 chunk_offset, struct btrfs_balance_args *bargs) |
| 3132 | { |
| 3133 | struct btrfs_block_group_cache *cache; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3134 | u64 chunk_used, user_thresh; |
| 3135 | int ret = 1; |
| 3136 | |
| 3137 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3138 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3139 | |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3140 | if (bargs->usage_min == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3141 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3142 | else if (bargs->usage > 100) |
| 3143 | user_thresh = cache->key.offset; |
| 3144 | else |
| 3145 | user_thresh = div_factor_fine(cache->key.offset, |
| 3146 | bargs->usage); |
| 3147 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3148 | if (chunk_used < user_thresh) |
| 3149 | ret = 0; |
| 3150 | |
| 3151 | btrfs_put_block_group(cache); |
| 3152 | return ret; |
| 3153 | } |
| 3154 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3155 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3156 | struct btrfs_chunk *chunk, |
| 3157 | struct btrfs_balance_args *bargs) |
| 3158 | { |
| 3159 | struct btrfs_stripe *stripe; |
| 3160 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3161 | int i; |
| 3162 | |
| 3163 | for (i = 0; i < num_stripes; i++) { |
| 3164 | stripe = btrfs_stripe_nr(chunk, i); |
| 3165 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
| 3169 | return 1; |
| 3170 | } |
| 3171 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3172 | /* [pstart, pend) */ |
| 3173 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3174 | struct btrfs_chunk *chunk, |
| 3175 | u64 chunk_offset, |
| 3176 | struct btrfs_balance_args *bargs) |
| 3177 | { |
| 3178 | struct btrfs_stripe *stripe; |
| 3179 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3180 | u64 stripe_offset; |
| 3181 | u64 stripe_length; |
| 3182 | int factor; |
| 3183 | int i; |
| 3184 | |
| 3185 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3186 | return 0; |
| 3187 | |
| 3188 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3189 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3190 | factor = num_stripes / 2; |
| 3191 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3192 | factor = num_stripes - 1; |
| 3193 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3194 | factor = num_stripes - 2; |
| 3195 | } else { |
| 3196 | factor = num_stripes; |
| 3197 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3198 | |
| 3199 | for (i = 0; i < num_stripes; i++) { |
| 3200 | stripe = btrfs_stripe_nr(chunk, i); |
| 3201 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3202 | continue; |
| 3203 | |
| 3204 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3205 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3206 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3207 | |
| 3208 | if (stripe_offset < bargs->pend && |
| 3209 | stripe_offset + stripe_length > bargs->pstart) |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | return 1; |
| 3214 | } |
| 3215 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3216 | /* [vstart, vend) */ |
| 3217 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3218 | struct btrfs_chunk *chunk, |
| 3219 | u64 chunk_offset, |
| 3220 | struct btrfs_balance_args *bargs) |
| 3221 | { |
| 3222 | if (chunk_offset < bargs->vend && |
| 3223 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3224 | /* at least part of the chunk is inside this vrange */ |
| 3225 | return 0; |
| 3226 | |
| 3227 | return 1; |
| 3228 | } |
| 3229 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3230 | static int chunk_stripes_range_filter(struct extent_buffer *leaf, |
| 3231 | struct btrfs_chunk *chunk, |
| 3232 | struct btrfs_balance_args *bargs) |
| 3233 | { |
| 3234 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3235 | |
| 3236 | if (bargs->stripes_min <= num_stripes |
| 3237 | && num_stripes <= bargs->stripes_max) |
| 3238 | return 0; |
| 3239 | |
| 3240 | return 1; |
| 3241 | } |
| 3242 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3243 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3244 | struct btrfs_balance_args *bargs) |
| 3245 | { |
| 3246 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3247 | return 0; |
| 3248 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3249 | chunk_type = chunk_to_extended(chunk_type) & |
| 3250 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3251 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3252 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3253 | return 1; |
| 3254 | |
| 3255 | return 0; |
| 3256 | } |
| 3257 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3258 | static int should_balance_chunk(struct btrfs_root *root, |
| 3259 | struct extent_buffer *leaf, |
| 3260 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3261 | { |
| 3262 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3263 | struct btrfs_balance_args *bargs = NULL; |
| 3264 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3265 | |
| 3266 | /* type filter */ |
| 3267 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3268 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3273 | bargs = &bctl->data; |
| 3274 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3275 | bargs = &bctl->sys; |
| 3276 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3277 | bargs = &bctl->meta; |
| 3278 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3279 | /* profiles filter */ |
| 3280 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3281 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3282 | return 0; |
| 3283 | } |
| 3284 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3285 | /* usage filter */ |
| 3286 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3287 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3288 | return 0; |
David Sterba | bc30946 | 2015-10-20 18:22:13 +0200 | [diff] [blame] | 3289 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && |
| 3290 | chunk_usage_range_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3291 | return 0; |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3292 | } |
| 3293 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3294 | /* devid filter */ |
| 3295 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3296 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3297 | return 0; |
| 3298 | } |
| 3299 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3300 | /* drange filter, makes sense only with devid filter */ |
| 3301 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3302 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3303 | return 0; |
| 3304 | } |
| 3305 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3306 | /* vrange filter */ |
| 3307 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3308 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3309 | return 0; |
| 3310 | } |
| 3311 | |
GabrÃel Arthúr Pétursson | dee32d0 | 2015-09-28 22:32:41 +0000 | [diff] [blame] | 3312 | /* stripes filter */ |
| 3313 | if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) && |
| 3314 | chunk_stripes_range_filter(leaf, chunk, bargs)) { |
| 3315 | return 0; |
| 3316 | } |
| 3317 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3318 | /* soft profile changing mode */ |
| 3319 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3320 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3321 | return 0; |
| 3322 | } |
| 3323 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3324 | /* |
| 3325 | * limited by count, must be the last filter |
| 3326 | */ |
| 3327 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3328 | if (bargs->limit == 0) |
| 3329 | return 0; |
| 3330 | else |
| 3331 | bargs->limit--; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3332 | } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) { |
| 3333 | /* |
| 3334 | * Same logic as the 'limit' filter; the minimum cannot be |
| 3335 | * determined here because we do not have the global informatoin |
| 3336 | * about the count of all chunks that satisfy the filters. |
| 3337 | */ |
| 3338 | if (bargs->limit_max == 0) |
| 3339 | return 0; |
| 3340 | else |
| 3341 | bargs->limit_max--; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3342 | } |
| 3343 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3344 | return 1; |
| 3345 | } |
| 3346 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3347 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3348 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3349 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3350 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3351 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3352 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3353 | struct btrfs_device *device; |
| 3354 | u64 old_size; |
| 3355 | u64 size_to_free; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3356 | u64 chunk_type; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3357 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3358 | struct btrfs_path *path; |
| 3359 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3360 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3361 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3362 | struct extent_buffer *leaf; |
| 3363 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3364 | int ret; |
| 3365 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3366 | bool counting = true; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3367 | /* 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] | 3368 | u64 limit_data = bctl->data.limit; |
| 3369 | u64 limit_meta = bctl->meta.limit; |
| 3370 | u64 limit_sys = bctl->sys.limit; |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3371 | u32 count_data = 0; |
| 3372 | u32 count_meta = 0; |
| 3373 | u32 count_sys = 0; |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3374 | int chunk_reserved = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3375 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3376 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3377 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3378 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3379 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3380 | size_to_free = div_factor(old_size, 1); |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 3381 | size_to_free = min_t(u64, size_to_free, SZ_1M); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3382 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3383 | btrfs_device_get_total_bytes(device) - |
| 3384 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3385 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3386 | continue; |
| 3387 | |
| 3388 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3389 | if (ret == -ENOSPC) |
| 3390 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3391 | BUG_ON(ret); |
| 3392 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3393 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3394 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3395 | |
| 3396 | ret = btrfs_grow_device(trans, device, old_size); |
| 3397 | BUG_ON(ret); |
| 3398 | |
| 3399 | btrfs_end_transaction(trans, dev_root); |
| 3400 | } |
| 3401 | |
| 3402 | /* step two, relocate all the chunks */ |
| 3403 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3404 | if (!path) { |
| 3405 | ret = -ENOMEM; |
| 3406 | goto error; |
| 3407 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3408 | |
| 3409 | /* zero out stat counters */ |
| 3410 | spin_lock(&fs_info->balance_lock); |
| 3411 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3412 | spin_unlock(&fs_info->balance_lock); |
| 3413 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3414 | if (!counting) { |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3415 | /* |
| 3416 | * The single value limit and min/max limits use the same bytes |
| 3417 | * in the |
| 3418 | */ |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3419 | bctl->data.limit = limit_data; |
| 3420 | bctl->meta.limit = limit_meta; |
| 3421 | bctl->sys.limit = limit_sys; |
| 3422 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3423 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3424 | key.offset = (u64)-1; |
| 3425 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3426 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3427 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3428 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3429 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3430 | ret = -ECANCELED; |
| 3431 | goto error; |
| 3432 | } |
| 3433 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3434 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3435 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3436 | if (ret < 0) { |
| 3437 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3438 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3439 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3440 | |
| 3441 | /* |
| 3442 | * this shouldn't happen, it means the last relocate |
| 3443 | * failed |
| 3444 | */ |
| 3445 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3446 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3447 | |
| 3448 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3449 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3450 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3451 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3452 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3453 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3454 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3455 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3456 | leaf = path->nodes[0]; |
| 3457 | slot = path->slots[0]; |
| 3458 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3459 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3460 | if (found_key.objectid != key.objectid) { |
| 3461 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3462 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3463 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3464 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3465 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3466 | chunk_type = btrfs_chunk_type(leaf, chunk); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3467 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3468 | if (!counting) { |
| 3469 | spin_lock(&fs_info->balance_lock); |
| 3470 | bctl->stat.considered++; |
| 3471 | spin_unlock(&fs_info->balance_lock); |
| 3472 | } |
| 3473 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3474 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3475 | found_key.offset); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3476 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3477 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3478 | if (!ret) { |
| 3479 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3480 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3481 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3482 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3483 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3484 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3485 | spin_lock(&fs_info->balance_lock); |
| 3486 | bctl->stat.expected++; |
| 3487 | spin_unlock(&fs_info->balance_lock); |
David Sterba | 12907fc | 2015-10-10 17:16:50 +0200 | [diff] [blame] | 3488 | |
| 3489 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3490 | count_data++; |
| 3491 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3492 | count_sys++; |
| 3493 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3494 | count_meta++; |
| 3495 | |
| 3496 | goto loop; |
| 3497 | } |
| 3498 | |
| 3499 | /* |
| 3500 | * Apply limit_min filter, no need to check if the LIMITS |
| 3501 | * filter is used, limit_min is 0 by default |
| 3502 | */ |
| 3503 | if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) && |
| 3504 | count_data < bctl->data.limit_min) |
| 3505 | || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) && |
| 3506 | count_meta < bctl->meta.limit_min) |
| 3507 | || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 3508 | count_sys < bctl->sys.limit_min)) { |
| 3509 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3510 | goto loop; |
| 3511 | } |
| 3512 | |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3513 | if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) && !chunk_reserved) { |
| 3514 | trans = btrfs_start_transaction(chunk_root, 0); |
| 3515 | if (IS_ERR(trans)) { |
| 3516 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3517 | ret = PTR_ERR(trans); |
| 3518 | goto error; |
| 3519 | } |
| 3520 | |
| 3521 | ret = btrfs_force_chunk_alloc(trans, chunk_root, |
| 3522 | BTRFS_BLOCK_GROUP_DATA); |
Filipe Manana | 8a7d656 | 2015-12-10 11:08:59 +0000 | [diff] [blame] | 3523 | btrfs_end_transaction(trans, chunk_root); |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3524 | if (ret < 0) { |
| 3525 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
| 3526 | goto error; |
| 3527 | } |
Zhao Lei | 2c9fe83 | 2015-11-09 11:51:32 +0800 | [diff] [blame] | 3528 | chunk_reserved = 1; |
| 3529 | } |
| 3530 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3531 | ret = btrfs_relocate_chunk(chunk_root, |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3532 | found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3533 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3534 | if (ret && ret != -ENOSPC) |
| 3535 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3536 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3537 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3538 | } else { |
| 3539 | spin_lock(&fs_info->balance_lock); |
| 3540 | bctl->stat.completed++; |
| 3541 | spin_unlock(&fs_info->balance_lock); |
| 3542 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3543 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3544 | if (found_key.offset == 0) |
| 3545 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3546 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3547 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3548 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3549 | if (counting) { |
| 3550 | btrfs_release_path(path); |
| 3551 | counting = false; |
| 3552 | goto again; |
| 3553 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3554 | error: |
| 3555 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3556 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3557 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3558 | enospc_errors); |
| 3559 | if (!ret) |
| 3560 | ret = -ENOSPC; |
| 3561 | } |
| 3562 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3563 | return ret; |
| 3564 | } |
| 3565 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3566 | /** |
| 3567 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3568 | * @flags: profile to validate |
| 3569 | * @extended: if true @flags is treated as an extended profile |
| 3570 | */ |
| 3571 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3572 | { |
| 3573 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3574 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3575 | |
| 3576 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3577 | |
| 3578 | /* 1) check that all other bits are zeroed */ |
| 3579 | if (flags & ~mask) |
| 3580 | return 0; |
| 3581 | |
| 3582 | /* 2) see if profile is reduced */ |
| 3583 | if (flags == 0) |
| 3584 | return !extended; /* "0" is valid for usual profiles */ |
| 3585 | |
| 3586 | /* true if exactly one bit set */ |
| 3587 | return (flags & (flags - 1)) == 0; |
| 3588 | } |
| 3589 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3590 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3591 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3592 | /* cancel requested || normal exit path */ |
| 3593 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3594 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3595 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3596 | } |
| 3597 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3598 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3599 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3600 | int ret; |
| 3601 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3602 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3603 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3604 | if (ret) |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame] | 3605 | btrfs_std_error(fs_info, ret, NULL); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3606 | |
| 3607 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3608 | } |
| 3609 | |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3610 | /* Non-zero return value signifies invalidity */ |
| 3611 | static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg, |
| 3612 | u64 allowed) |
| 3613 | { |
| 3614 | return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3615 | (!alloc_profile_is_valid(bctl_arg->target, 1) || |
| 3616 | (bctl_arg->target & ~allowed))); |
| 3617 | } |
| 3618 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3619 | /* |
| 3620 | * Should be called with both balance and volume mutexes held |
| 3621 | */ |
| 3622 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3623 | struct btrfs_ioctl_balance_args *bargs) |
| 3624 | { |
| 3625 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3626 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3627 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3628 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3629 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3630 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3631 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3632 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3633 | atomic_read(&fs_info->balance_pause_req) || |
| 3634 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3635 | ret = -EINVAL; |
| 3636 | goto out; |
| 3637 | } |
| 3638 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3639 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3640 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3641 | mixed = 1; |
| 3642 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3643 | /* |
| 3644 | * In case of mixed groups both data and meta should be picked, |
| 3645 | * and identical options should be given for both of them. |
| 3646 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3647 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3648 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3649 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3650 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3651 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3652 | btrfs_err(fs_info, "with mixed groups data and " |
| 3653 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3654 | ret = -EINVAL; |
| 3655 | goto out; |
| 3656 | } |
| 3657 | } |
| 3658 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3659 | num_devices = fs_info->fs_devices->num_devices; |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 3660 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3661 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3662 | BUG_ON(num_devices < 1); |
| 3663 | num_devices--; |
| 3664 | } |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 3665 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3666 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3667 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3668 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3669 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3670 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3671 | if (num_devices > 2) |
| 3672 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3673 | if (num_devices > 3) |
| 3674 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3675 | BTRFS_BLOCK_GROUP_RAID6); |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3676 | if (validate_convert_profile(&bctl->data, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3677 | btrfs_err(fs_info, "unable to start balance with target " |
| 3678 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3679 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3680 | ret = -EINVAL; |
| 3681 | goto out; |
| 3682 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3683 | if (validate_convert_profile(&bctl->meta, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3684 | btrfs_err(fs_info, |
| 3685 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3686 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3687 | ret = -EINVAL; |
| 3688 | goto out; |
| 3689 | } |
Alexandru Moise | bdcd3c9 | 2015-09-22 20:02:25 +0000 | [diff] [blame] | 3690 | if (validate_convert_profile(&bctl->sys, allowed)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3691 | btrfs_err(fs_info, |
| 3692 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3693 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3694 | ret = -EINVAL; |
| 3695 | goto out; |
| 3696 | } |
| 3697 | |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3698 | /* allow to reduce meta or sys integrity only if force set */ |
| 3699 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3700 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3701 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3702 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3703 | do { |
| 3704 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3705 | |
| 3706 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3707 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3708 | !(bctl->sys.target & allowed)) || |
| 3709 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3710 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3711 | !(bctl->meta.target & allowed))) { |
| 3712 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3713 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3714 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3715 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3716 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3717 | ret = -EINVAL; |
| 3718 | goto out; |
| 3719 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3720 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3721 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3722 | |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3723 | if (btrfs_get_num_tolerated_disk_barrier_failures(bctl->meta.target) < |
| 3724 | btrfs_get_num_tolerated_disk_barrier_failures(bctl->data.target)) { |
| 3725 | btrfs_warn(fs_info, |
Filipe Manana | fedc004 | 2016-01-15 10:56:15 +0000 | [diff] [blame] | 3726 | "metadata profile 0x%llx has lower redundancy than data profile 0x%llx", |
Sam Tygier | ee592d0 | 2016-01-06 08:46:12 +0000 | [diff] [blame] | 3727 | bctl->meta.target, bctl->data.target); |
| 3728 | } |
| 3729 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3730 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
Zhao Lei | 943c6e9 | 2015-08-19 15:54:15 +0800 | [diff] [blame] | 3731 | fs_info->num_tolerated_disk_barrier_failures = min( |
| 3732 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info), |
| 3733 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 3734 | bctl->sys.target)); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3735 | } |
| 3736 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3737 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3738 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3739 | goto out; |
| 3740 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3741 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3742 | BUG_ON(ret == -EEXIST); |
| 3743 | set_balance_control(bctl); |
| 3744 | } else { |
| 3745 | BUG_ON(ret != -EEXIST); |
| 3746 | spin_lock(&fs_info->balance_lock); |
| 3747 | update_balance_args(bctl); |
| 3748 | spin_unlock(&fs_info->balance_lock); |
| 3749 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3750 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3751 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3752 | mutex_unlock(&fs_info->balance_mutex); |
| 3753 | |
| 3754 | ret = __btrfs_balance(fs_info); |
| 3755 | |
| 3756 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3757 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3758 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3759 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3760 | fs_info->num_tolerated_disk_barrier_failures = |
| 3761 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3762 | } |
| 3763 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3764 | if (bargs) { |
| 3765 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3766 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3769 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3770 | balance_need_close(fs_info)) { |
| 3771 | __cancel_balance(fs_info); |
| 3772 | } |
| 3773 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3774 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3775 | |
| 3776 | return ret; |
| 3777 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3778 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3779 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3780 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3781 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3782 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3783 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3784 | return ret; |
| 3785 | } |
| 3786 | |
| 3787 | static int balance_kthread(void *data) |
| 3788 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3789 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3790 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3791 | |
| 3792 | mutex_lock(&fs_info->volume_mutex); |
| 3793 | mutex_lock(&fs_info->balance_mutex); |
| 3794 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3795 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3796 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3797 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3798 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3799 | |
| 3800 | mutex_unlock(&fs_info->balance_mutex); |
| 3801 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3802 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3803 | return ret; |
| 3804 | } |
| 3805 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3806 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3807 | { |
| 3808 | struct task_struct *tsk; |
| 3809 | |
| 3810 | spin_lock(&fs_info->balance_lock); |
| 3811 | if (!fs_info->balance_ctl) { |
| 3812 | spin_unlock(&fs_info->balance_lock); |
| 3813 | return 0; |
| 3814 | } |
| 3815 | spin_unlock(&fs_info->balance_lock); |
| 3816 | |
| 3817 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3818 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3819 | return 0; |
| 3820 | } |
| 3821 | |
| 3822 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3823 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3824 | } |
| 3825 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3826 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3827 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3828 | struct btrfs_balance_control *bctl; |
| 3829 | struct btrfs_balance_item *item; |
| 3830 | struct btrfs_disk_balance_args disk_bargs; |
| 3831 | struct btrfs_path *path; |
| 3832 | struct extent_buffer *leaf; |
| 3833 | struct btrfs_key key; |
| 3834 | int ret; |
| 3835 | |
| 3836 | path = btrfs_alloc_path(); |
| 3837 | if (!path) |
| 3838 | return -ENOMEM; |
| 3839 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3840 | key.objectid = BTRFS_BALANCE_OBJECTID; |
David Sterba | c479cb4 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 3841 | key.type = BTRFS_TEMPORARY_ITEM_KEY; |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3842 | key.offset = 0; |
| 3843 | |
| 3844 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3845 | if (ret < 0) |
| 3846 | goto out; |
| 3847 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3848 | ret = 0; |
| 3849 | goto out; |
| 3850 | } |
| 3851 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3852 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3853 | if (!bctl) { |
| 3854 | ret = -ENOMEM; |
| 3855 | goto out; |
| 3856 | } |
| 3857 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3858 | leaf = path->nodes[0]; |
| 3859 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3860 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3861 | bctl->fs_info = fs_info; |
| 3862 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3863 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3864 | |
| 3865 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3866 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3867 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3868 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3869 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3870 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3871 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3872 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3873 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3874 | mutex_lock(&fs_info->volume_mutex); |
| 3875 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3876 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3877 | set_balance_control(bctl); |
| 3878 | |
| 3879 | mutex_unlock(&fs_info->balance_mutex); |
| 3880 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3881 | out: |
| 3882 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3883 | return ret; |
| 3884 | } |
| 3885 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3886 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3887 | { |
| 3888 | int ret = 0; |
| 3889 | |
| 3890 | mutex_lock(&fs_info->balance_mutex); |
| 3891 | if (!fs_info->balance_ctl) { |
| 3892 | mutex_unlock(&fs_info->balance_mutex); |
| 3893 | return -ENOTCONN; |
| 3894 | } |
| 3895 | |
| 3896 | if (atomic_read(&fs_info->balance_running)) { |
| 3897 | atomic_inc(&fs_info->balance_pause_req); |
| 3898 | mutex_unlock(&fs_info->balance_mutex); |
| 3899 | |
| 3900 | wait_event(fs_info->balance_wait_q, |
| 3901 | atomic_read(&fs_info->balance_running) == 0); |
| 3902 | |
| 3903 | mutex_lock(&fs_info->balance_mutex); |
| 3904 | /* we are good with balance_ctl ripped off from under us */ |
| 3905 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3906 | atomic_dec(&fs_info->balance_pause_req); |
| 3907 | } else { |
| 3908 | ret = -ENOTCONN; |
| 3909 | } |
| 3910 | |
| 3911 | mutex_unlock(&fs_info->balance_mutex); |
| 3912 | return ret; |
| 3913 | } |
| 3914 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3915 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3916 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3917 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3918 | return -EROFS; |
| 3919 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3920 | mutex_lock(&fs_info->balance_mutex); |
| 3921 | if (!fs_info->balance_ctl) { |
| 3922 | mutex_unlock(&fs_info->balance_mutex); |
| 3923 | return -ENOTCONN; |
| 3924 | } |
| 3925 | |
| 3926 | atomic_inc(&fs_info->balance_cancel_req); |
| 3927 | /* |
| 3928 | * if we are running just wait and return, balance item is |
| 3929 | * deleted in btrfs_balance in this case |
| 3930 | */ |
| 3931 | if (atomic_read(&fs_info->balance_running)) { |
| 3932 | mutex_unlock(&fs_info->balance_mutex); |
| 3933 | wait_event(fs_info->balance_wait_q, |
| 3934 | atomic_read(&fs_info->balance_running) == 0); |
| 3935 | mutex_lock(&fs_info->balance_mutex); |
| 3936 | } else { |
| 3937 | /* __cancel_balance needs volume_mutex */ |
| 3938 | mutex_unlock(&fs_info->balance_mutex); |
| 3939 | mutex_lock(&fs_info->volume_mutex); |
| 3940 | mutex_lock(&fs_info->balance_mutex); |
| 3941 | |
| 3942 | if (fs_info->balance_ctl) |
| 3943 | __cancel_balance(fs_info); |
| 3944 | |
| 3945 | mutex_unlock(&fs_info->volume_mutex); |
| 3946 | } |
| 3947 | |
| 3948 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3949 | atomic_dec(&fs_info->balance_cancel_req); |
| 3950 | mutex_unlock(&fs_info->balance_mutex); |
| 3951 | return 0; |
| 3952 | } |
| 3953 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3954 | static int btrfs_uuid_scan_kthread(void *data) |
| 3955 | { |
| 3956 | struct btrfs_fs_info *fs_info = data; |
| 3957 | struct btrfs_root *root = fs_info->tree_root; |
| 3958 | struct btrfs_key key; |
| 3959 | struct btrfs_key max_key; |
| 3960 | struct btrfs_path *path = NULL; |
| 3961 | int ret = 0; |
| 3962 | struct extent_buffer *eb; |
| 3963 | int slot; |
| 3964 | struct btrfs_root_item root_item; |
| 3965 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3966 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3967 | |
| 3968 | path = btrfs_alloc_path(); |
| 3969 | if (!path) { |
| 3970 | ret = -ENOMEM; |
| 3971 | goto out; |
| 3972 | } |
| 3973 | |
| 3974 | key.objectid = 0; |
| 3975 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3976 | key.offset = 0; |
| 3977 | |
| 3978 | max_key.objectid = (u64)-1; |
| 3979 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3980 | max_key.offset = (u64)-1; |
| 3981 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3982 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3983 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3984 | if (ret) { |
| 3985 | if (ret > 0) |
| 3986 | ret = 0; |
| 3987 | break; |
| 3988 | } |
| 3989 | |
| 3990 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3991 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3992 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3993 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3994 | goto skip; |
| 3995 | |
| 3996 | eb = path->nodes[0]; |
| 3997 | slot = path->slots[0]; |
| 3998 | item_size = btrfs_item_size_nr(eb, slot); |
| 3999 | if (item_size < sizeof(root_item)) |
| 4000 | goto skip; |
| 4001 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4002 | read_extent_buffer(eb, &root_item, |
| 4003 | btrfs_item_ptr_offset(eb, slot), |
| 4004 | (int)sizeof(root_item)); |
| 4005 | if (btrfs_root_refs(&root_item) == 0) |
| 4006 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4007 | |
| 4008 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 4009 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 4010 | if (trans) |
| 4011 | goto update_tree; |
| 4012 | |
| 4013 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4014 | /* |
| 4015 | * 1 - subvol uuid item |
| 4016 | * 1 - received_subvol uuid item |
| 4017 | */ |
| 4018 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 4019 | if (IS_ERR(trans)) { |
| 4020 | ret = PTR_ERR(trans); |
| 4021 | break; |
| 4022 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4023 | continue; |
| 4024 | } else { |
| 4025 | goto skip; |
| 4026 | } |
| 4027 | update_tree: |
| 4028 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4029 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 4030 | root_item.uuid, |
| 4031 | BTRFS_UUID_KEY_SUBVOL, |
| 4032 | key.objectid); |
| 4033 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4034 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4035 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4036 | break; |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4041 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 4042 | root_item.received_uuid, |
| 4043 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 4044 | key.objectid); |
| 4045 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4046 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4047 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4048 | break; |
| 4049 | } |
| 4050 | } |
| 4051 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4052 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4053 | if (trans) { |
| 4054 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4055 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4056 | if (ret) |
| 4057 | break; |
| 4058 | } |
| 4059 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4060 | btrfs_release_path(path); |
| 4061 | if (key.offset < (u64)-1) { |
| 4062 | key.offset++; |
| 4063 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 4064 | key.offset = 0; |
| 4065 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4066 | } else if (key.objectid < (u64)-1) { |
| 4067 | key.offset = 0; |
| 4068 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4069 | key.objectid++; |
| 4070 | } else { |
| 4071 | break; |
| 4072 | } |
| 4073 | cond_resched(); |
| 4074 | } |
| 4075 | |
| 4076 | out: |
| 4077 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4078 | if (trans && !IS_ERR(trans)) |
| 4079 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4080 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4081 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4082 | else |
| 4083 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4084 | up(&fs_info->uuid_tree_rescan_sem); |
| 4085 | return 0; |
| 4086 | } |
| 4087 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4088 | /* |
| 4089 | * Callback for btrfs_uuid_tree_iterate(). |
| 4090 | * returns: |
| 4091 | * 0 check succeeded, the entry is not outdated. |
Adam Buchbinder | bb7ab3b | 2016-03-04 11:23:12 -0800 | [diff] [blame] | 4092 | * < 0 if an error occurred. |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4093 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4094 | */ |
| 4095 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4096 | u8 *uuid, u8 type, u64 subid) |
| 4097 | { |
| 4098 | struct btrfs_key key; |
| 4099 | int ret = 0; |
| 4100 | struct btrfs_root *subvol_root; |
| 4101 | |
| 4102 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4103 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4104 | goto out; |
| 4105 | |
| 4106 | key.objectid = subid; |
| 4107 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4108 | key.offset = (u64)-1; |
| 4109 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4110 | if (IS_ERR(subvol_root)) { |
| 4111 | ret = PTR_ERR(subvol_root); |
| 4112 | if (ret == -ENOENT) |
| 4113 | ret = 1; |
| 4114 | goto out; |
| 4115 | } |
| 4116 | |
| 4117 | switch (type) { |
| 4118 | case BTRFS_UUID_KEY_SUBVOL: |
| 4119 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4120 | ret = 1; |
| 4121 | break; |
| 4122 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4123 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4124 | BTRFS_UUID_SIZE)) |
| 4125 | ret = 1; |
| 4126 | break; |
| 4127 | } |
| 4128 | |
| 4129 | out: |
| 4130 | return ret; |
| 4131 | } |
| 4132 | |
| 4133 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4134 | { |
| 4135 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4136 | int ret; |
| 4137 | |
| 4138 | /* |
| 4139 | * 1st step is to iterate through the existing UUID tree and |
| 4140 | * to delete all entries that contain outdated data. |
| 4141 | * 2nd step is to add all missing entries to the UUID tree. |
| 4142 | */ |
| 4143 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4144 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4145 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4146 | up(&fs_info->uuid_tree_rescan_sem); |
| 4147 | return ret; |
| 4148 | } |
| 4149 | return btrfs_uuid_scan_kthread(data); |
| 4150 | } |
| 4151 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4152 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4153 | { |
| 4154 | struct btrfs_trans_handle *trans; |
| 4155 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4156 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4157 | struct task_struct *task; |
| 4158 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4159 | |
| 4160 | /* |
| 4161 | * 1 - root node |
| 4162 | * 1 - root item |
| 4163 | */ |
| 4164 | trans = btrfs_start_transaction(tree_root, 2); |
| 4165 | if (IS_ERR(trans)) |
| 4166 | return PTR_ERR(trans); |
| 4167 | |
| 4168 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4169 | BTRFS_UUID_TREE_OBJECTID); |
| 4170 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4171 | ret = PTR_ERR(uuid_root); |
| 4172 | btrfs_abort_transaction(trans, tree_root, ret); |
| 4173 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | fs_info->uuid_root = uuid_root; |
| 4177 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4178 | ret = btrfs_commit_transaction(trans, tree_root); |
| 4179 | if (ret) |
| 4180 | return ret; |
| 4181 | |
| 4182 | down(&fs_info->uuid_tree_rescan_sem); |
| 4183 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4184 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4185 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4186 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4187 | up(&fs_info->uuid_tree_rescan_sem); |
| 4188 | return PTR_ERR(task); |
| 4189 | } |
| 4190 | |
| 4191 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4192 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4193 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4194 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4195 | { |
| 4196 | struct task_struct *task; |
| 4197 | |
| 4198 | down(&fs_info->uuid_tree_rescan_sem); |
| 4199 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4200 | if (IS_ERR(task)) { |
| 4201 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4202 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4203 | up(&fs_info->uuid_tree_rescan_sem); |
| 4204 | return PTR_ERR(task); |
| 4205 | } |
| 4206 | |
| 4207 | return 0; |
| 4208 | } |
| 4209 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4210 | /* |
| 4211 | * shrinking a device means finding all of the device extents past |
| 4212 | * the new size, and then following the back refs to the chunks. |
| 4213 | * The chunk relocation code actually frees the device extent |
| 4214 | */ |
| 4215 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4216 | { |
| 4217 | struct btrfs_trans_handle *trans; |
| 4218 | struct btrfs_root *root = device->dev_root; |
| 4219 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4220 | struct btrfs_path *path; |
| 4221 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4222 | u64 chunk_offset; |
| 4223 | int ret; |
| 4224 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4225 | int failed = 0; |
| 4226 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4227 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4228 | struct extent_buffer *l; |
| 4229 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4230 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4231 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4232 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 4233 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4234 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4235 | if (device->is_tgtdev_for_dev_replace) |
| 4236 | return -EINVAL; |
| 4237 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4238 | path = btrfs_alloc_path(); |
| 4239 | if (!path) |
| 4240 | return -ENOMEM; |
| 4241 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 4242 | path->reada = READA_FORWARD; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4243 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4244 | lock_chunks(root); |
| 4245 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4246 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4247 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4248 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4249 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4250 | root->fs_info->free_chunk_space -= diff; |
| 4251 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4252 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4253 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4254 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4255 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4256 | key.objectid = device->devid; |
| 4257 | key.offset = (u64)-1; |
| 4258 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4259 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4260 | do { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4261 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4262 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4263 | if (ret < 0) { |
| 4264 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4265 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4266 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4267 | |
| 4268 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4269 | if (ret) |
| 4270 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4271 | if (ret < 0) |
| 4272 | goto done; |
| 4273 | if (ret) { |
| 4274 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4275 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4276 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | l = path->nodes[0]; |
| 4280 | slot = path->slots[0]; |
| 4281 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4282 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4283 | if (key.objectid != device->devid) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4284 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4285 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4286 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4287 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4288 | |
| 4289 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4290 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4291 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4292 | if (key.offset + length <= new_size) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4293 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4294 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4295 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4296 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4297 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4298 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4299 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4300 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 4301 | ret = btrfs_relocate_chunk(root, chunk_offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4302 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4303 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4304 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4305 | if (ret == -ENOSPC) |
| 4306 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4307 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4308 | |
| 4309 | if (failed && !retried) { |
| 4310 | failed = 0; |
| 4311 | retried = true; |
| 4312 | goto again; |
| 4313 | } else if (failed && retried) { |
| 4314 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4315 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4316 | } |
| 4317 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4318 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4319 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4320 | if (IS_ERR(trans)) { |
| 4321 | ret = PTR_ERR(trans); |
| 4322 | goto done; |
| 4323 | } |
| 4324 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4325 | lock_chunks(root); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4326 | |
| 4327 | /* |
| 4328 | * We checked in the above loop all device extents that were already in |
| 4329 | * the device tree. However before we have updated the device's |
| 4330 | * total_bytes to the new size, we might have had chunk allocations that |
| 4331 | * have not complete yet (new block groups attached to transaction |
| 4332 | * handles), and therefore their device extents were not yet in the |
| 4333 | * device tree and we missed them in the loop above. So if we have any |
| 4334 | * pending chunk using a device extent that overlaps the device range |
| 4335 | * that we can not use anymore, commit the current transaction and |
| 4336 | * repeat the search on the device tree - this way we guarantee we will |
| 4337 | * not have chunks using device extents that end beyond 'new_size'. |
| 4338 | */ |
| 4339 | if (!checked_pending_chunks) { |
| 4340 | u64 start = new_size; |
| 4341 | u64 len = old_size - new_size; |
| 4342 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 4343 | if (contains_pending_extent(trans->transaction, device, |
| 4344 | &start, len)) { |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4345 | unlock_chunks(root); |
| 4346 | checked_pending_chunks = true; |
| 4347 | failed = 0; |
| 4348 | retried = false; |
| 4349 | ret = btrfs_commit_transaction(trans, root); |
| 4350 | if (ret) |
| 4351 | goto done; |
| 4352 | goto again; |
| 4353 | } |
| 4354 | } |
| 4355 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4356 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4357 | if (list_empty(&device->resized_list)) |
| 4358 | list_add_tail(&device->resized_list, |
| 4359 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4360 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4361 | WARN_ON(diff > old_total); |
| 4362 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4363 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4364 | |
| 4365 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4366 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4367 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4368 | done: |
| 4369 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4370 | if (ret) { |
| 4371 | lock_chunks(root); |
| 4372 | btrfs_device_set_total_bytes(device, old_size); |
| 4373 | if (device->writeable) |
| 4374 | device->fs_devices->total_rw_bytes += diff; |
| 4375 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4376 | root->fs_info->free_chunk_space += diff; |
| 4377 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4378 | unlock_chunks(root); |
| 4379 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4380 | return ret; |
| 4381 | } |
| 4382 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4383 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4384 | struct btrfs_key *key, |
| 4385 | struct btrfs_chunk *chunk, int item_size) |
| 4386 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4387 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4388 | struct btrfs_disk_key disk_key; |
| 4389 | u32 array_size; |
| 4390 | u8 *ptr; |
| 4391 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4392 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4393 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4394 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4395 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4396 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4397 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4398 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4399 | |
| 4400 | ptr = super_copy->sys_chunk_array + array_size; |
| 4401 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4402 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4403 | ptr += sizeof(disk_key); |
| 4404 | memcpy(ptr, chunk, item_size); |
| 4405 | item_size += sizeof(disk_key); |
| 4406 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4407 | unlock_chunks(root); |
| 4408 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4409 | return 0; |
| 4410 | } |
| 4411 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4412 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4413 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4414 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4415 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4416 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4417 | const struct btrfs_device_info *di_a = a; |
| 4418 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4419 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4420 | if (di_a->max_avail > di_b->max_avail) |
| 4421 | return -1; |
| 4422 | if (di_a->max_avail < di_b->max_avail) |
| 4423 | return 1; |
| 4424 | if (di_a->total_avail > di_b->total_avail) |
| 4425 | return -1; |
| 4426 | if (di_a->total_avail < di_b->total_avail) |
| 4427 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4428 | return 0; |
| 4429 | } |
| 4430 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4431 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4432 | { |
| 4433 | /* TODO allow them to set a preferred stripe size */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4434 | return SZ_64K; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4438 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4439 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4440 | return; |
| 4441 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4442 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4443 | } |
| 4444 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4445 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4446 | - sizeof(struct btrfs_item) \ |
| 4447 | - sizeof(struct btrfs_chunk)) \ |
| 4448 | / sizeof(struct btrfs_stripe) + 1) |
| 4449 | |
| 4450 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4451 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4452 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4453 | / sizeof(struct btrfs_stripe) + 1) |
| 4454 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4455 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4456 | struct btrfs_root *extent_root, u64 start, |
| 4457 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4458 | { |
| 4459 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4460 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4461 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4462 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4463 | struct extent_map_tree *em_tree; |
| 4464 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4465 | struct btrfs_device_info *devices_info = NULL; |
| 4466 | u64 total_avail; |
| 4467 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4468 | int data_stripes; /* number of stripes that count for |
| 4469 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4470 | int sub_stripes; /* sub_stripes info for map */ |
| 4471 | int dev_stripes; /* stripes per dev */ |
| 4472 | int devs_max; /* max devs to use */ |
| 4473 | int devs_min; /* min devs needed */ |
| 4474 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4475 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4476 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4477 | u64 max_stripe_size; |
| 4478 | u64 max_chunk_size; |
| 4479 | u64 stripe_size; |
| 4480 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4481 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4482 | int ndevs; |
| 4483 | int i; |
| 4484 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4485 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4486 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4487 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4488 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4489 | if (list_empty(&fs_devices->alloc_list)) |
| 4490 | return -ENOSPC; |
| 4491 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4492 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4493 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4494 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4495 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4496 | devs_max = btrfs_raid_array[index].devs_max; |
| 4497 | devs_min = btrfs_raid_array[index].devs_min; |
| 4498 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4499 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4500 | |
| 4501 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4502 | max_stripe_size = SZ_1G; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4503 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4504 | if (!devs_max) |
| 4505 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4506 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4507 | /* for larger filesystems, use larger metadata chunks */ |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4508 | if (fs_devices->total_rw_bytes > 50ULL * SZ_1G) |
| 4509 | max_stripe_size = SZ_1G; |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4510 | else |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4511 | max_stripe_size = SZ_256M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4512 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4513 | if (!devs_max) |
| 4514 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4515 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 4516 | max_stripe_size = SZ_32M; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4517 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4518 | if (!devs_max) |
| 4519 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4520 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4521 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4522 | type); |
| 4523 | BUG_ON(1); |
| 4524 | } |
| 4525 | |
| 4526 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4527 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4528 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4529 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4530 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4531 | GFP_NOFS); |
| 4532 | if (!devices_info) |
| 4533 | return -ENOMEM; |
| 4534 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4535 | cur = fs_devices->alloc_list.next; |
| 4536 | |
| 4537 | /* |
| 4538 | * in the first pass through the devices list, we gather information |
| 4539 | * about the available holes on each device. |
| 4540 | */ |
| 4541 | ndevs = 0; |
| 4542 | while (cur != &fs_devices->alloc_list) { |
| 4543 | struct btrfs_device *device; |
| 4544 | u64 max_avail; |
| 4545 | u64 dev_offset; |
| 4546 | |
| 4547 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4548 | |
| 4549 | cur = cur->next; |
| 4550 | |
| 4551 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4552 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4553 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4554 | continue; |
| 4555 | } |
| 4556 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4557 | if (!device->in_fs_metadata || |
| 4558 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4559 | continue; |
| 4560 | |
| 4561 | if (device->total_bytes > device->bytes_used) |
| 4562 | total_avail = device->total_bytes - device->bytes_used; |
| 4563 | else |
| 4564 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4565 | |
| 4566 | /* If there is no space on this device, skip it. */ |
| 4567 | if (total_avail == 0) |
| 4568 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4569 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4570 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4571 | max_stripe_size * dev_stripes, |
| 4572 | &dev_offset, &max_avail); |
| 4573 | if (ret && ret != -ENOSPC) |
| 4574 | goto error; |
| 4575 | |
| 4576 | if (ret == 0) |
| 4577 | max_avail = max_stripe_size * dev_stripes; |
| 4578 | |
| 4579 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4580 | continue; |
| 4581 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4582 | if (ndevs == fs_devices->rw_devices) { |
| 4583 | WARN(1, "%s: found more than %llu devices\n", |
| 4584 | __func__, fs_devices->rw_devices); |
| 4585 | break; |
| 4586 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4587 | devices_info[ndevs].dev_offset = dev_offset; |
| 4588 | devices_info[ndevs].max_avail = max_avail; |
| 4589 | devices_info[ndevs].total_avail = total_avail; |
| 4590 | devices_info[ndevs].dev = device; |
| 4591 | ++ndevs; |
| 4592 | } |
| 4593 | |
| 4594 | /* |
| 4595 | * now sort the devices by hole size / available space |
| 4596 | */ |
| 4597 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4598 | btrfs_cmp_device_info, NULL); |
| 4599 | |
| 4600 | /* round down to number of usable stripes */ |
| 4601 | ndevs -= ndevs % devs_increment; |
| 4602 | |
| 4603 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4604 | ret = -ENOSPC; |
| 4605 | goto error; |
| 4606 | } |
| 4607 | |
| 4608 | if (devs_max && ndevs > devs_max) |
| 4609 | ndevs = devs_max; |
| 4610 | /* |
| 4611 | * the primary goal is to maximize the number of stripes, so use as many |
| 4612 | * devices as possible, even if the stripes are not maximum sized. |
| 4613 | */ |
| 4614 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4615 | num_stripes = ndevs * dev_stripes; |
| 4616 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4617 | /* |
| 4618 | * this will have to be fixed for RAID1 and RAID10 over |
| 4619 | * more drives |
| 4620 | */ |
| 4621 | data_stripes = num_stripes / ncopies; |
| 4622 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4623 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4624 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4625 | btrfs_super_stripesize(info->super_copy)); |
| 4626 | data_stripes = num_stripes - 1; |
| 4627 | } |
| 4628 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4629 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4630 | btrfs_super_stripesize(info->super_copy)); |
| 4631 | data_stripes = num_stripes - 2; |
| 4632 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4633 | |
| 4634 | /* |
| 4635 | * Use the number of data stripes to figure out how big this chunk |
| 4636 | * is really going to be in terms of logical address space, |
| 4637 | * and compare that answer with the max chunk size |
| 4638 | */ |
| 4639 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4640 | u64 mask = (1ULL << 24) - 1; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4641 | |
| 4642 | stripe_size = div_u64(max_chunk_size, data_stripes); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4643 | |
| 4644 | /* bump the answer up to a 16MB boundary */ |
| 4645 | stripe_size = (stripe_size + mask) & ~mask; |
| 4646 | |
| 4647 | /* but don't go higher than the limits we found |
| 4648 | * while searching for free extents |
| 4649 | */ |
| 4650 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4651 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4652 | } |
| 4653 | |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4654 | stripe_size = div_u64(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4655 | |
| 4656 | /* align to BTRFS_STRIPE_LEN */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4657 | stripe_size = div_u64(stripe_size, raid_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4658 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4659 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4660 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4661 | if (!map) { |
| 4662 | ret = -ENOMEM; |
| 4663 | goto error; |
| 4664 | } |
| 4665 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4666 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4667 | for (i = 0; i < ndevs; ++i) { |
| 4668 | for (j = 0; j < dev_stripes; ++j) { |
| 4669 | int s = i * dev_stripes + j; |
| 4670 | map->stripes[s].dev = devices_info[i].dev; |
| 4671 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4672 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4673 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4674 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4675 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4676 | map->stripe_len = raid_stripe_len; |
| 4677 | map->io_align = raid_stripe_len; |
| 4678 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4679 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4680 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4681 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4682 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4683 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4684 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4685 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4686 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4687 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4688 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4689 | ret = -ENOMEM; |
| 4690 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4691 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4692 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 4693 | em->map_lookup = map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4694 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4695 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4696 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4697 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4698 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4699 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4700 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4701 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4702 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4703 | if (!ret) { |
| 4704 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4705 | atomic_inc(&em->refs); |
| 4706 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4707 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4708 | if (ret) { |
| 4709 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4710 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4711 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4712 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4713 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4714 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4715 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4716 | if (ret) |
| 4717 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4718 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4719 | for (i = 0; i < map->num_stripes; i++) { |
| 4720 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4721 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4722 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4723 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4724 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4725 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4726 | map->num_stripes); |
| 4727 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4728 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4729 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4730 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4731 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4732 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4733 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4734 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4735 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4736 | write_lock(&em_tree->lock); |
| 4737 | remove_extent_mapping(em_tree, em); |
| 4738 | write_unlock(&em_tree->lock); |
| 4739 | |
| 4740 | /* One for our allocation */ |
| 4741 | free_extent_map(em); |
| 4742 | /* One for the tree reference */ |
| 4743 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4744 | /* One for the pending_chunks list reference */ |
| 4745 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4746 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4747 | kfree(devices_info); |
| 4748 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4749 | } |
| 4750 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4751 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4752 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4753 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4754 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4755 | struct btrfs_key key; |
| 4756 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4757 | struct btrfs_device *device; |
| 4758 | struct btrfs_chunk *chunk; |
| 4759 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4760 | struct extent_map_tree *em_tree; |
| 4761 | struct extent_map *em; |
| 4762 | struct map_lookup *map; |
| 4763 | size_t item_size; |
| 4764 | u64 dev_offset; |
| 4765 | u64 stripe_size; |
| 4766 | int i = 0; |
Chris Mason | 140e639 | 2015-12-23 13:30:51 -0800 | [diff] [blame] | 4767 | int ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4768 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4769 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4770 | read_lock(&em_tree->lock); |
| 4771 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4772 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4773 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4774 | if (!em) { |
| 4775 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4776 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4777 | return -EINVAL; |
| 4778 | } |
| 4779 | |
| 4780 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4781 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4782 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4783 | chunk_size, em->start, em->len); |
| 4784 | free_extent_map(em); |
| 4785 | return -EINVAL; |
| 4786 | } |
| 4787 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 4788 | map = em->map_lookup; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4789 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4790 | stripe_size = em->orig_block_len; |
| 4791 | |
| 4792 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4793 | if (!chunk) { |
| 4794 | ret = -ENOMEM; |
| 4795 | goto out; |
| 4796 | } |
| 4797 | |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4798 | /* |
| 4799 | * Take the device list mutex to prevent races with the final phase of |
| 4800 | * a device replace operation that replaces the device object associated |
| 4801 | * with the map's stripes, because the device object's id can change |
| 4802 | * at any time during that final phase of the device replace operation |
| 4803 | * (dev-replace.c:btrfs_dev_replace_finishing()). |
| 4804 | */ |
| 4805 | mutex_lock(&chunk_root->fs_info->fs_devices->device_list_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4806 | for (i = 0; i < map->num_stripes; i++) { |
| 4807 | device = map->stripes[i].dev; |
| 4808 | dev_offset = map->stripes[i].physical; |
| 4809 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4810 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4811 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4812 | break; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4813 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4814 | chunk_root->root_key.objectid, |
| 4815 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4816 | chunk_offset, dev_offset, |
| 4817 | stripe_size); |
| 4818 | if (ret) |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4819 | break; |
| 4820 | } |
| 4821 | if (ret) { |
| 4822 | mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex); |
| 4823 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4824 | } |
| 4825 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4826 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4827 | for (i = 0; i < map->num_stripes; i++) { |
| 4828 | device = map->stripes[i].dev; |
| 4829 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4830 | |
| 4831 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4832 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4833 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4834 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4835 | } |
Filipe Manana | 50460e3 | 2015-11-20 10:42:47 +0000 | [diff] [blame] | 4836 | mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4837 | |
| 4838 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4839 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4840 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4841 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4842 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4843 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4844 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4845 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4846 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4847 | |
| 4848 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4849 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4850 | key.offset = chunk_offset; |
| 4851 | |
| 4852 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4853 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4854 | /* |
| 4855 | * TODO: Cleanup of inserted chunk root in case of |
| 4856 | * failure. |
| 4857 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4858 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4859 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4860 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4861 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4862 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4863 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4864 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4865 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | /* |
| 4869 | * Chunk allocation falls into two parts. The first part does works |
| 4870 | * that make the new allocated chunk useable, but not do any operation |
| 4871 | * that modifies the chunk tree. The second part does the works that |
| 4872 | * require modifying the chunk tree. This division is important for the |
| 4873 | * bootstrap process of adding storage to a seed btrfs. |
| 4874 | */ |
| 4875 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4876 | struct btrfs_root *extent_root, u64 type) |
| 4877 | { |
| 4878 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4879 | |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 4880 | ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex)); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4881 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4882 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4883 | } |
| 4884 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4885 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4886 | struct btrfs_root *root, |
| 4887 | struct btrfs_device *device) |
| 4888 | { |
| 4889 | u64 chunk_offset; |
| 4890 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4891 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4892 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4893 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4894 | int ret; |
| 4895 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4896 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4897 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4898 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4899 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4900 | if (ret) |
| 4901 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4902 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4903 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4904 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4905 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4906 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4907 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4908 | } |
| 4909 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4910 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4911 | { |
| 4912 | int max_errors; |
| 4913 | |
| 4914 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4915 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4916 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4917 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4918 | max_errors = 1; |
| 4919 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4920 | max_errors = 2; |
| 4921 | } else { |
| 4922 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4923 | } |
| 4924 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4925 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4929 | { |
| 4930 | struct extent_map *em; |
| 4931 | struct map_lookup *map; |
| 4932 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4933 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4934 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4935 | int i; |
| 4936 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4937 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4938 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4939 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4940 | if (!em) |
| 4941 | return 1; |
| 4942 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 4943 | map = em->map_lookup; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4944 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4945 | if (map->stripes[i].dev->missing) { |
| 4946 | miss_ndevs++; |
| 4947 | continue; |
| 4948 | } |
| 4949 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4950 | if (!map->stripes[i].dev->writeable) { |
| 4951 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4952 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4953 | } |
| 4954 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4955 | |
| 4956 | /* |
| 4957 | * If the number of missing devices is larger than max errors, |
| 4958 | * we can not write the data into that chunk successfully, so |
| 4959 | * set it readonly. |
| 4960 | */ |
| 4961 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4962 | readonly = 1; |
| 4963 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4964 | free_extent_map(em); |
| 4965 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4969 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4970 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4974 | { |
| 4975 | struct extent_map *em; |
| 4976 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4977 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4978 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4979 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4980 | if (em) |
| 4981 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4982 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4983 | if (!em) |
| 4984 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4985 | /* once for us */ |
| 4986 | free_extent_map(em); |
| 4987 | /* once for the tree */ |
| 4988 | free_extent_map(em); |
| 4989 | } |
| 4990 | } |
| 4991 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4992 | 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] | 4993 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4994 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4995 | struct extent_map *em; |
| 4996 | struct map_lookup *map; |
| 4997 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4998 | int ret; |
| 4999 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5000 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5001 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5002 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5003 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5004 | /* |
| 5005 | * We could return errors for these cases, but that could get ugly and |
| 5006 | * we'd probably do the same thing which is just not do anything else |
| 5007 | * and exit, so return 1 so the callers don't try to use other copies. |
| 5008 | */ |
| 5009 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5010 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5011 | logical+len); |
| 5012 | return 1; |
| 5013 | } |
| 5014 | |
| 5015 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 5016 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5017 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5018 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 5019 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 5020 | return 1; |
| 5021 | } |
| 5022 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5023 | map = em->map_lookup; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5024 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 5025 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5026 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5027 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5028 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 5029 | ret = 2; |
| 5030 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5031 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5032 | else |
| 5033 | ret = 1; |
| 5034 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5035 | |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5036 | btrfs_dev_replace_lock(&fs_info->dev_replace, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5037 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 5038 | ret++; |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5039 | btrfs_dev_replace_unlock(&fs_info->dev_replace, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5040 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5041 | return ret; |
| 5042 | } |
| 5043 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5044 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 5045 | struct btrfs_mapping_tree *map_tree, |
| 5046 | u64 logical) |
| 5047 | { |
| 5048 | struct extent_map *em; |
| 5049 | struct map_lookup *map; |
| 5050 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5051 | unsigned long len = root->sectorsize; |
| 5052 | |
| 5053 | read_lock(&em_tree->lock); |
| 5054 | em = lookup_extent_mapping(em_tree, logical, len); |
| 5055 | read_unlock(&em_tree->lock); |
| 5056 | BUG_ON(!em); |
| 5057 | |
| 5058 | BUG_ON(em->start > logical || em->start + em->len < logical); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5059 | map = em->map_lookup; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5060 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5061 | len = map->stripe_len * nr_data_stripes(map); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5062 | free_extent_map(em); |
| 5063 | return len; |
| 5064 | } |
| 5065 | |
| 5066 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 5067 | u64 logical, u64 len, int mirror_num) |
| 5068 | { |
| 5069 | struct extent_map *em; |
| 5070 | struct map_lookup *map; |
| 5071 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5072 | int ret = 0; |
| 5073 | |
| 5074 | read_lock(&em_tree->lock); |
| 5075 | em = lookup_extent_mapping(em_tree, logical, len); |
| 5076 | read_unlock(&em_tree->lock); |
| 5077 | BUG_ON(!em); |
| 5078 | |
| 5079 | BUG_ON(em->start > logical || em->start + em->len < logical); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5080 | map = em->map_lookup; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5081 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5082 | ret = 1; |
| 5083 | free_extent_map(em); |
| 5084 | return ret; |
| 5085 | } |
| 5086 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5087 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 5088 | struct map_lookup *map, int first, int num, |
| 5089 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5090 | { |
| 5091 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5092 | int tolerance; |
| 5093 | struct btrfs_device *srcdev; |
| 5094 | |
| 5095 | if (dev_replace_is_ongoing && |
| 5096 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5097 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5098 | srcdev = fs_info->dev_replace.srcdev; |
| 5099 | else |
| 5100 | srcdev = NULL; |
| 5101 | |
| 5102 | /* |
| 5103 | * try to avoid the drive that is the source drive for a |
| 5104 | * dev-replace procedure, only choose it if no other non-missing |
| 5105 | * mirror is available |
| 5106 | */ |
| 5107 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 5108 | if (map->stripes[optimal].dev->bdev && |
| 5109 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 5110 | return optimal; |
| 5111 | for (i = first; i < first + num; i++) { |
| 5112 | if (map->stripes[i].dev->bdev && |
| 5113 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5114 | return i; |
| 5115 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5116 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5117 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5118 | /* we couldn't find one that doesn't fail. Just return something |
| 5119 | * and the io error handling code will clean up eventually |
| 5120 | */ |
| 5121 | return optimal; |
| 5122 | } |
| 5123 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5124 | static inline int parity_smaller(u64 a, u64 b) |
| 5125 | { |
| 5126 | return a > b; |
| 5127 | } |
| 5128 | |
| 5129 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5130 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5131 | { |
| 5132 | struct btrfs_bio_stripe s; |
| 5133 | int i; |
| 5134 | u64 l; |
| 5135 | int again = 1; |
| 5136 | |
| 5137 | while (again) { |
| 5138 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5139 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5140 | if (parity_smaller(bbio->raid_map[i], |
| 5141 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5142 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5143 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5144 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5145 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5146 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5147 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5148 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5149 | again = 1; |
| 5150 | } |
| 5151 | } |
| 5152 | } |
| 5153 | } |
| 5154 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5155 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5156 | { |
| 5157 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5158 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5159 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5160 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5161 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5162 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5163 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5164 | /* |
| 5165 | * plus the raid_map, which includes both the tgt dev |
| 5166 | * and the stripes |
| 5167 | */ |
| 5168 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5169 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5170 | |
| 5171 | atomic_set(&bbio->error, 0); |
| 5172 | atomic_set(&bbio->refs, 1); |
| 5173 | |
| 5174 | return bbio; |
| 5175 | } |
| 5176 | |
| 5177 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5178 | { |
| 5179 | WARN_ON(!atomic_read(&bbio->refs)); |
| 5180 | atomic_inc(&bbio->refs); |
| 5181 | } |
| 5182 | |
| 5183 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5184 | { |
| 5185 | if (!bbio) |
| 5186 | return; |
| 5187 | if (atomic_dec_and_test(&bbio->refs)) |
| 5188 | kfree(bbio); |
| 5189 | } |
| 5190 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5191 | static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5192 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5193 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5194 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5195 | { |
| 5196 | struct extent_map *em; |
| 5197 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5198 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5199 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5200 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5201 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5202 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5203 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5204 | u64 stripe_nr_orig; |
| 5205 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5206 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5207 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5208 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5209 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5210 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5211 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5212 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5213 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5214 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5215 | int dev_replace_is_ongoing = 0; |
| 5216 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5217 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5218 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5219 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5220 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5221 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5222 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5223 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5224 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5225 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5226 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5227 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5228 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5229 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5230 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5231 | if (em->start > logical || em->start + em->len < logical) { |
| 5232 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5233 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5234 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 5235 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5236 | return -EINVAL; |
| 5237 | } |
| 5238 | |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5239 | map = em->map_lookup; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5240 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5241 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5242 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5243 | stripe_nr = offset; |
| 5244 | /* |
| 5245 | * stripe_nr counts the total number of stripes we have to stride |
| 5246 | * to get to this block |
| 5247 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5248 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5249 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5250 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5251 | BUG_ON(offset < stripe_offset); |
| 5252 | |
| 5253 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5254 | stripe_offset = offset - stripe_offset; |
| 5255 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5256 | /* 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] | 5257 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5258 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5259 | raid56_full_stripe_start = offset; |
| 5260 | |
| 5261 | /* allow a write of a full stripe, but make sure we don't |
| 5262 | * allow straddling of stripes |
| 5263 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5264 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5265 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5266 | raid56_full_stripe_start *= full_stripe_len; |
| 5267 | } |
| 5268 | |
| 5269 | if (rw & REQ_DISCARD) { |
| 5270 | /* we don't discard raid56 yet */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5271 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5272 | ret = -EOPNOTSUPP; |
| 5273 | goto out; |
| 5274 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5275 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5276 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5277 | u64 max_len; |
| 5278 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5279 | For other RAID types and for RAID[56] reads, just allow a single |
| 5280 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5281 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5282 | (rw & REQ_WRITE)) { |
| 5283 | max_len = stripe_len * nr_data_stripes(map) - |
| 5284 | (offset - raid56_full_stripe_start); |
| 5285 | } else { |
| 5286 | /* we limit the length of each bio to what fits in a stripe */ |
| 5287 | max_len = stripe_len - stripe_offset; |
| 5288 | } |
| 5289 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5290 | } else { |
| 5291 | *length = em->len - offset; |
| 5292 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5293 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5294 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5295 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5296 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5297 | goto out; |
| 5298 | |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5299 | btrfs_dev_replace_lock(dev_replace, 0); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5300 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5301 | if (!dev_replace_is_ongoing) |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5302 | btrfs_dev_replace_unlock(dev_replace, 0); |
| 5303 | else |
| 5304 | btrfs_dev_replace_set_lock_blocking(dev_replace); |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5305 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5306 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5307 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5308 | dev_replace->tgtdev != NULL) { |
| 5309 | /* |
| 5310 | * in dev-replace case, for repair case (that's the only |
| 5311 | * case where the mirror is selected explicitly when |
| 5312 | * calling btrfs_map_block), blocks left of the left cursor |
| 5313 | * can also be read from the target drive. |
| 5314 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5315 | * the last one to the array of stripes. For READ, it also |
| 5316 | * needs to be supported using the same mirror number. |
| 5317 | * If the requested block is not left of the left cursor, |
| 5318 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5319 | * returns one more in the dev-replace case. |
| 5320 | */ |
| 5321 | u64 tmp_length = *length; |
| 5322 | struct btrfs_bio *tmp_bbio = NULL; |
| 5323 | int tmp_num_stripes; |
| 5324 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5325 | int index_srcdev = 0; |
| 5326 | int found = 0; |
| 5327 | u64 physical_of_found = 0; |
| 5328 | |
| 5329 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5330 | logical, &tmp_length, &tmp_bbio, 0, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5331 | if (ret) { |
| 5332 | WARN_ON(tmp_bbio != NULL); |
| 5333 | goto out; |
| 5334 | } |
| 5335 | |
| 5336 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5337 | if (mirror_num > tmp_num_stripes) { |
| 5338 | /* |
| 5339 | * REQ_GET_READ_MIRRORS does not contain this |
| 5340 | * mirror, that means that the requested area |
| 5341 | * is not left of the left cursor |
| 5342 | */ |
| 5343 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5344 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5345 | goto out; |
| 5346 | } |
| 5347 | |
| 5348 | /* |
| 5349 | * process the rest of the function using the mirror_num |
| 5350 | * of the source drive. Therefore look it up first. |
| 5351 | * At the end, patch the device pointer to the one of the |
| 5352 | * target drive. |
| 5353 | */ |
| 5354 | for (i = 0; i < tmp_num_stripes; i++) { |
Zhao Lei | 94a97df | 2015-12-09 21:03:49 +0800 | [diff] [blame] | 5355 | if (tmp_bbio->stripes[i].dev->devid != srcdev_devid) |
| 5356 | continue; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5357 | |
Zhao Lei | 94a97df | 2015-12-09 21:03:49 +0800 | [diff] [blame] | 5358 | /* |
| 5359 | * In case of DUP, in order to keep it simple, only add |
| 5360 | * the mirror with the lowest physical address |
| 5361 | */ |
| 5362 | if (found && |
| 5363 | physical_of_found <= tmp_bbio->stripes[i].physical) |
| 5364 | continue; |
| 5365 | |
| 5366 | index_srcdev = i; |
| 5367 | found = 1; |
| 5368 | physical_of_found = tmp_bbio->stripes[i].physical; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5369 | } |
| 5370 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5371 | btrfs_put_bbio(tmp_bbio); |
Zhao Lei | 94a97df | 2015-12-09 21:03:49 +0800 | [diff] [blame] | 5372 | |
| 5373 | if (!found) { |
| 5374 | WARN_ON(1); |
| 5375 | ret = -EIO; |
| 5376 | goto out; |
| 5377 | } |
| 5378 | |
| 5379 | mirror_num = index_srcdev + 1; |
| 5380 | patch_the_first_stripe_for_dev_replace = 1; |
| 5381 | physical_to_patch_in_first_stripe = physical_of_found; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5382 | } else if (mirror_num > map->num_stripes) { |
| 5383 | mirror_num = 0; |
| 5384 | } |
| 5385 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5386 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5387 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5388 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5389 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5390 | stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5391 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5392 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5393 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5394 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5395 | if (rw & REQ_DISCARD) |
| 5396 | num_stripes = min_t(u64, map->num_stripes, |
| 5397 | stripe_nr_end - stripe_nr_orig); |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5398 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5399 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5400 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5401 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5402 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5403 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5404 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5405 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5406 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5407 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5408 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5409 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5410 | current->pid % map->num_stripes, |
| 5411 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5412 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5413 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5414 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5415 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5416 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5417 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5418 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5419 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5420 | } else { |
| 5421 | mirror_num = 1; |
| 5422 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5423 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5424 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5425 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5426 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5427 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5428 | stripe_index *= map->sub_stripes; |
| 5429 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5430 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5431 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5432 | else if (rw & REQ_DISCARD) |
| 5433 | num_stripes = min_t(u64, map->sub_stripes * |
| 5434 | (stripe_nr_end - stripe_nr_orig), |
| 5435 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5436 | else if (mirror_num) |
| 5437 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5438 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5439 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5440 | stripe_index = find_live_mirror(fs_info, map, |
| 5441 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5442 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5443 | current->pid % map->sub_stripes, |
| 5444 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5445 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5446 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5447 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5448 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5449 | if (need_raid_map && |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5450 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5451 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5452 | /* push stripe_nr back to the start of the full stripe */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5453 | stripe_nr = div_u64(raid56_full_stripe_start, |
| 5454 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5455 | |
| 5456 | /* RAID[56] write or recovery. Return all stripes */ |
| 5457 | num_stripes = map->num_stripes; |
| 5458 | max_errors = nr_parity_stripes(map); |
| 5459 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5460 | *length = map->stripe_len; |
| 5461 | stripe_index = 0; |
| 5462 | stripe_offset = 0; |
| 5463 | } else { |
| 5464 | /* |
| 5465 | * Mirror #0 or #1 means the original data block. |
| 5466 | * Mirror #2 is RAID5 parity block. |
| 5467 | * Mirror #3 is RAID6 Q block. |
| 5468 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5469 | stripe_nr = div_u64_rem(stripe_nr, |
| 5470 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5471 | if (mirror_num > 1) |
| 5472 | stripe_index = nr_data_stripes(map) + |
| 5473 | mirror_num - 2; |
| 5474 | |
| 5475 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5476 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5477 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5478 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5479 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5480 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5481 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5482 | } else { |
| 5483 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5484 | * after this, stripe_nr is the number of stripes on this |
| 5485 | * device we have to walk to find the data, and stripe_index is |
| 5486 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5487 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5488 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5489 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5490 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5491 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5492 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5493 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5494 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5495 | if (dev_replace_is_ongoing) { |
| 5496 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5497 | num_alloc_stripes <<= 1; |
| 5498 | if (rw & REQ_GET_READ_MIRRORS) |
| 5499 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5500 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5501 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5502 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5503 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5504 | if (!bbio) { |
| 5505 | ret = -ENOMEM; |
| 5506 | goto out; |
| 5507 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5508 | if (dev_replace_is_ongoing) |
| 5509 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5510 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5511 | /* build raid_map */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5512 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5513 | need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5514 | mirror_num > 1)) { |
| 5515 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5516 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5517 | |
| 5518 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5519 | sizeof(struct btrfs_bio_stripe) * |
| 5520 | num_alloc_stripes + |
| 5521 | sizeof(int) * tgtdev_indexes); |
| 5522 | |
| 5523 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5524 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5525 | |
| 5526 | /* Fill in the logical address of each stripe */ |
| 5527 | tmp = stripe_nr * nr_data_stripes(map); |
| 5528 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5529 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5530 | em->start + (tmp + i) * map->stripe_len; |
| 5531 | |
| 5532 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5533 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5534 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5535 | RAID6_Q_STRIPE; |
| 5536 | } |
| 5537 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5538 | if (rw & REQ_DISCARD) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5539 | u32 factor = 0; |
| 5540 | u32 sub_stripes = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5541 | u64 stripes_per_dev = 0; |
| 5542 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5543 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5544 | |
| 5545 | if (map->type & |
| 5546 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5547 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5548 | sub_stripes = 1; |
| 5549 | else |
| 5550 | sub_stripes = map->sub_stripes; |
| 5551 | |
| 5552 | factor = map->num_stripes / sub_stripes; |
| 5553 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5554 | stripe_nr_orig, |
| 5555 | factor, |
| 5556 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5557 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5558 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5559 | } |
| 5560 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5561 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5562 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5563 | map->stripes[stripe_index].physical + |
| 5564 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5565 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5566 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5567 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5568 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5569 | bbio->stripes[i].length = stripes_per_dev * |
| 5570 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5571 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5572 | if (i / sub_stripes < remaining_stripes) |
| 5573 | bbio->stripes[i].length += |
| 5574 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5575 | |
| 5576 | /* |
| 5577 | * Special for the first stripe and |
| 5578 | * the last stripe: |
| 5579 | * |
| 5580 | * |-------|...|-------| |
| 5581 | * |----------| |
| 5582 | * off end_off |
| 5583 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5584 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5585 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5586 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5587 | |
| 5588 | if (stripe_index >= last_stripe && |
| 5589 | stripe_index <= (last_stripe + |
| 5590 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5591 | bbio->stripes[i].length -= |
| 5592 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5593 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5594 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5595 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5596 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5597 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5598 | |
| 5599 | stripe_index++; |
| 5600 | if (stripe_index == map->num_stripes) { |
| 5601 | /* This could only happen for RAID0/10 */ |
| 5602 | stripe_index = 0; |
| 5603 | stripe_nr++; |
| 5604 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5605 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5606 | } else { |
| 5607 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5608 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5609 | map->stripes[stripe_index].physical + |
| 5610 | stripe_offset + |
| 5611 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5612 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5613 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5614 | stripe_index++; |
| 5615 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5616 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5617 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5618 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5619 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5620 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5621 | if (bbio->raid_map) |
| 5622 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5623 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5624 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5625 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5626 | dev_replace->tgtdev != NULL) { |
| 5627 | int index_where_to_add; |
| 5628 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5629 | |
| 5630 | /* |
| 5631 | * duplicate the write operations while the dev replace |
| 5632 | * procedure is running. Since the copying of the old disk |
| 5633 | * to the new disk takes place at run time while the |
| 5634 | * filesystem is mounted writable, the regular write |
| 5635 | * operations to the old disk have to be duplicated to go |
| 5636 | * to the new disk as well. |
| 5637 | * Note that device->missing is handled by the caller, and |
| 5638 | * that the write to the old disk is already set up in the |
| 5639 | * stripes array. |
| 5640 | */ |
| 5641 | index_where_to_add = num_stripes; |
| 5642 | for (i = 0; i < num_stripes; i++) { |
| 5643 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5644 | /* write to new disk, too */ |
| 5645 | struct btrfs_bio_stripe *new = |
| 5646 | bbio->stripes + index_where_to_add; |
| 5647 | struct btrfs_bio_stripe *old = |
| 5648 | bbio->stripes + i; |
| 5649 | |
| 5650 | new->physical = old->physical; |
| 5651 | new->length = old->length; |
| 5652 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5653 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5654 | index_where_to_add++; |
| 5655 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5656 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5657 | } |
| 5658 | } |
| 5659 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5660 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5661 | dev_replace->tgtdev != NULL) { |
| 5662 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5663 | int index_srcdev = 0; |
| 5664 | int found = 0; |
| 5665 | u64 physical_of_found = 0; |
| 5666 | |
| 5667 | /* |
| 5668 | * During the dev-replace procedure, the target drive can |
| 5669 | * also be used to read data in case it is needed to repair |
| 5670 | * a corrupt block elsewhere. This is possible if the |
| 5671 | * requested area is left of the left cursor. In this area, |
| 5672 | * the target drive is a full copy of the source drive. |
| 5673 | */ |
| 5674 | for (i = 0; i < num_stripes; i++) { |
| 5675 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5676 | /* |
| 5677 | * In case of DUP, in order to keep it |
| 5678 | * simple, only add the mirror with the |
| 5679 | * lowest physical address |
| 5680 | */ |
| 5681 | if (found && |
| 5682 | physical_of_found <= |
| 5683 | bbio->stripes[i].physical) |
| 5684 | continue; |
| 5685 | index_srcdev = i; |
| 5686 | found = 1; |
| 5687 | physical_of_found = bbio->stripes[i].physical; |
| 5688 | } |
| 5689 | } |
| 5690 | if (found) { |
David Sterba | 258ece0 | 2015-02-24 19:45:15 +0100 | [diff] [blame] | 5691 | if (physical_of_found + map->stripe_len <= |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5692 | dev_replace->cursor_left) { |
| 5693 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5694 | bbio->stripes + num_stripes; |
| 5695 | |
| 5696 | tgtdev_stripe->physical = physical_of_found; |
| 5697 | tgtdev_stripe->length = |
| 5698 | bbio->stripes[index_srcdev].length; |
| 5699 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5700 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5701 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5702 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5703 | num_stripes++; |
| 5704 | } |
| 5705 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5706 | } |
| 5707 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5708 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5709 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5710 | bbio->num_stripes = num_stripes; |
| 5711 | bbio->max_errors = max_errors; |
| 5712 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5713 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5714 | |
| 5715 | /* |
| 5716 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5717 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5718 | * available as a mirror |
| 5719 | */ |
| 5720 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5721 | WARN_ON(num_stripes > 1); |
| 5722 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5723 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5724 | bbio->mirror_num = map->num_stripes + 1; |
| 5725 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5726 | out: |
Liu Bo | 73beece | 2015-07-17 16:49:19 +0800 | [diff] [blame] | 5727 | if (dev_replace_is_ongoing) { |
| 5728 | btrfs_dev_replace_clear_lock_blocking(dev_replace); |
| 5729 | btrfs_dev_replace_unlock(dev_replace, 0); |
| 5730 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5731 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5732 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5733 | } |
| 5734 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5735 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5736 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5737 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5738 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5739 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5740 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5741 | } |
| 5742 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5743 | /* For Scrub/replace */ |
| 5744 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5745 | u64 logical, u64 *length, |
| 5746 | struct btrfs_bio **bbio_ret, int mirror_num, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5747 | int need_raid_map) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5748 | { |
| 5749 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5750 | mirror_num, need_raid_map); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5751 | } |
| 5752 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5753 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5754 | u64 chunk_start, u64 physical, u64 devid, |
| 5755 | u64 **logical, int *naddrs, int *stripe_len) |
| 5756 | { |
| 5757 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5758 | struct extent_map *em; |
| 5759 | struct map_lookup *map; |
| 5760 | u64 *buf; |
| 5761 | u64 bytenr; |
| 5762 | u64 length; |
| 5763 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5764 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5765 | int i, j, nr = 0; |
| 5766 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5767 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5768 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5769 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5770 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5771 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5772 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5773 | chunk_start); |
| 5774 | return -EIO; |
| 5775 | } |
| 5776 | |
| 5777 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5778 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5779 | em->start, chunk_start); |
| 5780 | free_extent_map(em); |
| 5781 | return -EIO; |
| 5782 | } |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 5783 | map = em->map_lookup; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5784 | |
| 5785 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5786 | rmap_len = map->stripe_len; |
| 5787 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5788 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5789 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5790 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5791 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5792 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5793 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5794 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5795 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5796 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5797 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5798 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5799 | |
| 5800 | for (i = 0; i < map->num_stripes; i++) { |
| 5801 | if (devid && map->stripes[i].dev->devid != devid) |
| 5802 | continue; |
| 5803 | if (map->stripes[i].physical > physical || |
| 5804 | map->stripes[i].physical + length <= physical) |
| 5805 | continue; |
| 5806 | |
| 5807 | stripe_nr = physical - map->stripes[i].physical; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5808 | stripe_nr = div_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5809 | |
| 5810 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5811 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5812 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5813 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5814 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5815 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5816 | * Alternatively, just use rmap_len below instead of |
| 5817 | * map->stripe_len */ |
| 5818 | |
| 5819 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5820 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5821 | for (j = 0; j < nr; j++) { |
| 5822 | if (buf[j] == bytenr) |
| 5823 | break; |
| 5824 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5825 | if (j == nr) { |
| 5826 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5827 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5828 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5829 | } |
| 5830 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5831 | *logical = buf; |
| 5832 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5833 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5834 | |
| 5835 | free_extent_map(em); |
| 5836 | return 0; |
| 5837 | } |
| 5838 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5839 | 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] | 5840 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5841 | bio->bi_private = bbio->private; |
| 5842 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5843 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5844 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5845 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5846 | } |
| 5847 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5848 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5849 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5850 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5851 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5852 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5853 | if (bio->bi_error) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5854 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5855 | if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5856 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5857 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 5858 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5859 | |
| 5860 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5861 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5862 | if (dev->bdev) { |
| 5863 | if (bio->bi_rw & WRITE) |
| 5864 | btrfs_dev_stat_inc(dev, |
| 5865 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5866 | else |
| 5867 | btrfs_dev_stat_inc(dev, |
| 5868 | BTRFS_DEV_STAT_READ_ERRS); |
| 5869 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5870 | btrfs_dev_stat_inc(dev, |
| 5871 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5872 | btrfs_dev_stat_print_on_error(dev); |
| 5873 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5874 | } |
| 5875 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5876 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5877 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5878 | is_orig_bio = 1; |
| 5879 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5880 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5881 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5882 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5883 | if (!is_orig_bio) { |
| 5884 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5885 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5886 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5887 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5888 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5889 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5890 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5891 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5892 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5893 | bio->bi_error = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5894 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5895 | /* |
| 5896 | * this bio is actually up to date, we didn't |
| 5897 | * go over the max number of errors |
| 5898 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5899 | bio->bi_error = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5900 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5901 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5902 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5903 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5904 | bio_put(bio); |
| 5905 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5906 | } |
| 5907 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5908 | /* |
| 5909 | * see run_scheduled_bios for a description of why bios are collected for |
| 5910 | * async submit. |
| 5911 | * |
| 5912 | * This will add one bio to the pending list for a device and make sure |
| 5913 | * the work struct is scheduled. |
| 5914 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5915 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5916 | struct btrfs_device *device, |
| 5917 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5918 | { |
| 5919 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5920 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5921 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5922 | if (device->missing || !device->bdev) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5923 | bio_io_error(bio); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5924 | return; |
| 5925 | } |
| 5926 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5927 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5928 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5929 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5930 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5931 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5932 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5933 | } |
| 5934 | |
| 5935 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5936 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5937 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5938 | * made progress against dirty pages when we've really just put it |
| 5939 | * on a queue for later |
| 5940 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5941 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5942 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5943 | bio->bi_next = NULL; |
| 5944 | bio->bi_rw |= rw; |
| 5945 | |
| 5946 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5947 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5948 | pending_bios = &device->pending_sync_bios; |
| 5949 | else |
| 5950 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5951 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5952 | if (pending_bios->tail) |
| 5953 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5954 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5955 | pending_bios->tail = bio; |
| 5956 | if (!pending_bios->head) |
| 5957 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5958 | if (device->running_pending) |
| 5959 | should_queue = 0; |
| 5960 | |
| 5961 | spin_unlock(&device->io_lock); |
| 5962 | |
| 5963 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5964 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5965 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5966 | } |
| 5967 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5968 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5969 | struct bio *bio, u64 physical, int dev_nr, |
| 5970 | int rw, int async) |
| 5971 | { |
| 5972 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5973 | |
| 5974 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5975 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5976 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5977 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5978 | #ifdef DEBUG |
| 5979 | { |
| 5980 | struct rcu_string *name; |
| 5981 | |
| 5982 | rcu_read_lock(); |
| 5983 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5984 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5985 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5986 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5987 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5988 | rcu_read_unlock(); |
| 5989 | } |
| 5990 | #endif |
| 5991 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5992 | |
| 5993 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5994 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5995 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5996 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5997 | else |
| 5998 | btrfsic_submit_bio(rw, bio); |
| 5999 | } |
| 6000 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6001 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 6002 | { |
| 6003 | atomic_inc(&bbio->error); |
| 6004 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 6005 | /* Shoud be the original bio. */ |
| 6006 | WARN_ON(bio != bbio->orig_bio); |
| 6007 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6008 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6009 | bio->bi_iter.bi_sector = logical >> 9; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6010 | bio->bi_error = -EIO; |
| 6011 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6012 | } |
| 6013 | } |
| 6014 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6015 | int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio, |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 6016 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6017 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6018 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6019 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6020 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6021 | u64 length = 0; |
| 6022 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6023 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6024 | int dev_nr; |
| 6025 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6026 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6027 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6028 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6029 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6030 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6031 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6032 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6033 | mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6034 | if (ret) { |
| 6035 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6036 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6037 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6038 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6039 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6040 | bbio->orig_bio = first_bio; |
| 6041 | bbio->private = first_bio->bi_private; |
| 6042 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6043 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6044 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6045 | |
Zhao Lei | ad1ba2a | 2015-12-15 18:18:09 +0800 | [diff] [blame] | 6046 | if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
| 6047 | ((rw & WRITE) || (mirror_num > 1))) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6048 | /* In this case, map_length has been set to the length of |
| 6049 | a single stripe; not the whole write */ |
| 6050 | if (rw & WRITE) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6051 | ret = raid56_parity_write(root, bio, bbio, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6052 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6053 | ret = raid56_parity_recover(root, bio, bbio, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6054 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6055 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6056 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6057 | btrfs_bio_counter_dec(root->fs_info); |
| 6058 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6059 | } |
| 6060 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6061 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6062 | btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6063 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6064 | BUG(); |
| 6065 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6066 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6067 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6068 | dev = bbio->stripes[dev_nr].dev; |
| 6069 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 6070 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6071 | continue; |
| 6072 | } |
| 6073 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6074 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6075 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6076 | BUG_ON(!bio); /* -ENOMEM */ |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 6077 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6078 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6079 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6080 | submit_stripe_bio(root, bbio, bio, |
| 6081 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 6082 | async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6083 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6084 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6085 | return 0; |
| 6086 | } |
| 6087 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6088 | 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] | 6089 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6090 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6091 | struct btrfs_device *device; |
| 6092 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6093 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6094 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6095 | while (cur_devices) { |
| 6096 | if (!fsid || |
| 6097 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 6098 | device = __find_device(&cur_devices->devices, |
| 6099 | devid, uuid); |
| 6100 | if (device) |
| 6101 | return device; |
| 6102 | } |
| 6103 | cur_devices = cur_devices->seed; |
| 6104 | } |
| 6105 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6106 | } |
| 6107 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6108 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6109 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6110 | u64 devid, u8 *dev_uuid) |
| 6111 | { |
| 6112 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6113 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6114 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6115 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 6116 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6117 | |
| 6118 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6119 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6120 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6121 | |
| 6122 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6123 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6124 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6125 | return device; |
| 6126 | } |
| 6127 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6128 | /** |
| 6129 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6130 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6131 | * devid is provided (i.e. @devid != NULL). |
| 6132 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6133 | * is generated. |
| 6134 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6135 | * is generated. |
| 6136 | * |
| 6137 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 6138 | * on error. Returned struct is not linked onto any lists and can be |
| 6139 | * destroyed with kfree() right away. |
| 6140 | */ |
| 6141 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6142 | const u64 *devid, |
| 6143 | const u8 *uuid) |
| 6144 | { |
| 6145 | struct btrfs_device *dev; |
| 6146 | u64 tmp; |
| 6147 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6148 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6149 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6150 | |
| 6151 | dev = __alloc_device(); |
| 6152 | if (IS_ERR(dev)) |
| 6153 | return dev; |
| 6154 | |
| 6155 | if (devid) |
| 6156 | tmp = *devid; |
| 6157 | else { |
| 6158 | int ret; |
| 6159 | |
| 6160 | ret = find_next_devid(fs_info, &tmp); |
| 6161 | if (ret) { |
| 6162 | kfree(dev); |
| 6163 | return ERR_PTR(ret); |
| 6164 | } |
| 6165 | } |
| 6166 | dev->devid = tmp; |
| 6167 | |
| 6168 | if (uuid) |
| 6169 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6170 | else |
| 6171 | generate_random_uuid(dev->uuid); |
| 6172 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6173 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6174 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6175 | |
| 6176 | return dev; |
| 6177 | } |
| 6178 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6179 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 6180 | struct extent_buffer *leaf, |
| 6181 | struct btrfs_chunk *chunk) |
| 6182 | { |
| 6183 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6184 | struct map_lookup *map; |
| 6185 | struct extent_map *em; |
| 6186 | u64 logical; |
| 6187 | u64 length; |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6188 | u64 stripe_len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6189 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6190 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6191 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6192 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6193 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6194 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6195 | logical = key->offset; |
| 6196 | length = btrfs_chunk_length(leaf, chunk); |
Qu Wenruo | f04b772 | 2015-12-15 09:14:37 +0800 | [diff] [blame] | 6197 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6198 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6199 | /* Validation check */ |
| 6200 | if (!num_stripes) { |
| 6201 | btrfs_err(root->fs_info, "invalid chunk num_stripes: %u", |
| 6202 | num_stripes); |
| 6203 | return -EIO; |
| 6204 | } |
| 6205 | if (!IS_ALIGNED(logical, root->sectorsize)) { |
| 6206 | btrfs_err(root->fs_info, |
| 6207 | "invalid chunk logical %llu", logical); |
| 6208 | return -EIO; |
| 6209 | } |
| 6210 | if (!length || !IS_ALIGNED(length, root->sectorsize)) { |
| 6211 | btrfs_err(root->fs_info, |
| 6212 | "invalid chunk length %llu", length); |
| 6213 | return -EIO; |
| 6214 | } |
| 6215 | if (!is_power_of_2(stripe_len)) { |
| 6216 | btrfs_err(root->fs_info, "invalid chunk stripe length: %llu", |
| 6217 | stripe_len); |
| 6218 | return -EIO; |
| 6219 | } |
| 6220 | if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6221 | btrfs_chunk_type(leaf, chunk)) { |
| 6222 | btrfs_err(root->fs_info, "unrecognized chunk type: %llu", |
| 6223 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
| 6224 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 6225 | btrfs_chunk_type(leaf, chunk)); |
| 6226 | return -EIO; |
| 6227 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6228 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6229 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6230 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6231 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6232 | |
| 6233 | /* already mapped? */ |
| 6234 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6235 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6236 | return 0; |
| 6237 | } else if (em) { |
| 6238 | free_extent_map(em); |
| 6239 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6240 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6241 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6242 | if (!em) |
| 6243 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6244 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6245 | if (!map) { |
| 6246 | free_extent_map(em); |
| 6247 | return -ENOMEM; |
| 6248 | } |
| 6249 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6250 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6251 | em->map_lookup = map; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6252 | em->start = logical; |
| 6253 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6254 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6255 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6256 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6257 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6258 | map->num_stripes = num_stripes; |
| 6259 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6260 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6261 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6262 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6263 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6264 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6265 | for (i = 0; i < num_stripes; i++) { |
| 6266 | map->stripes[i].physical = |
| 6267 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6268 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6269 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6270 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6271 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6272 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6273 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6274 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6275 | free_extent_map(em); |
| 6276 | return -EIO; |
| 6277 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6278 | if (!map->stripes[i].dev) { |
| 6279 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6280 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6281 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6282 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6283 | free_extent_map(em); |
| 6284 | return -EIO; |
| 6285 | } |
Anand Jain | 816fceb | 2015-04-27 12:46:18 +0800 | [diff] [blame] | 6286 | btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing", |
| 6287 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6288 | } |
| 6289 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6290 | } |
| 6291 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6292 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6293 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6294 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6295 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6296 | free_extent_map(em); |
| 6297 | |
| 6298 | return 0; |
| 6299 | } |
| 6300 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6301 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6302 | struct btrfs_dev_item *dev_item, |
| 6303 | struct btrfs_device *device) |
| 6304 | { |
| 6305 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6306 | |
| 6307 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6308 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6309 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6310 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6311 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6312 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6313 | device->type = btrfs_device_type(leaf, dev_item); |
| 6314 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6315 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6316 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6317 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6318 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6319 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6320 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6321 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6322 | } |
| 6323 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6324 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6325 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6326 | { |
| 6327 | struct btrfs_fs_devices *fs_devices; |
| 6328 | int ret; |
| 6329 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6330 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6331 | |
| 6332 | fs_devices = root->fs_info->fs_devices->seed; |
| 6333 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6334 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6335 | return fs_devices; |
| 6336 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6337 | fs_devices = fs_devices->seed; |
| 6338 | } |
| 6339 | |
| 6340 | fs_devices = find_fsid(fsid); |
| 6341 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6342 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6343 | return ERR_PTR(-ENOENT); |
| 6344 | |
| 6345 | fs_devices = alloc_fs_devices(fsid); |
| 6346 | if (IS_ERR(fs_devices)) |
| 6347 | return fs_devices; |
| 6348 | |
| 6349 | fs_devices->seeding = 1; |
| 6350 | fs_devices->opened = 1; |
| 6351 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6352 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6353 | |
| 6354 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6355 | if (IS_ERR(fs_devices)) |
| 6356 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6357 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6358 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6359 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6360 | if (ret) { |
| 6361 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6362 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6363 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6364 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6365 | |
| 6366 | if (!fs_devices->seeding) { |
| 6367 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6368 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6369 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6370 | goto out; |
| 6371 | } |
| 6372 | |
| 6373 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6374 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6375 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6376 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6377 | } |
| 6378 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6379 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6380 | struct extent_buffer *leaf, |
| 6381 | struct btrfs_dev_item *dev_item) |
| 6382 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6383 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6384 | struct btrfs_device *device; |
| 6385 | u64 devid; |
| 6386 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6387 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6388 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6389 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6390 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6391 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6392 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6393 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6394 | BTRFS_UUID_SIZE); |
| 6395 | |
| 6396 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6397 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6398 | if (IS_ERR(fs_devices)) |
| 6399 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6400 | } |
| 6401 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6402 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6403 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6404 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6405 | return -EIO; |
| 6406 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6407 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6408 | if (!device) |
| 6409 | return -ENOMEM; |
Anand Jain | 33b97e4 | 2015-05-08 04:34:35 +0800 | [diff] [blame] | 6410 | btrfs_warn(root->fs_info, "devid %llu uuid %pU missing", |
| 6411 | devid, dev_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6412 | } else { |
| 6413 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6414 | return -EIO; |
| 6415 | |
| 6416 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6417 | /* |
| 6418 | * this happens when a device that was properly setup |
| 6419 | * in the device info lists suddenly goes bad. |
| 6420 | * device->bdev is NULL, and so we have to set |
| 6421 | * device->missing to one here |
| 6422 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6423 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6424 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6425 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6426 | |
| 6427 | /* Move the device to its own fs_devices */ |
| 6428 | if (device->fs_devices != fs_devices) { |
| 6429 | ASSERT(device->missing); |
| 6430 | |
| 6431 | list_move(&device->dev_list, &fs_devices->devices); |
| 6432 | device->fs_devices->num_devices--; |
| 6433 | fs_devices->num_devices++; |
| 6434 | |
| 6435 | device->fs_devices->missing_devices--; |
| 6436 | fs_devices->missing_devices++; |
| 6437 | |
| 6438 | device->fs_devices = fs_devices; |
| 6439 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6443 | BUG_ON(device->writeable); |
| 6444 | if (device->generation != |
| 6445 | btrfs_device_generation(leaf, dev_item)) |
| 6446 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6447 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6448 | |
| 6449 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6450 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6451 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6452 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6453 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6454 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6455 | device->bytes_used; |
| 6456 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6457 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6458 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6459 | return ret; |
| 6460 | } |
| 6461 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6462 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6463 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6464 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6465 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6466 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6467 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6468 | u8 *array_ptr; |
| 6469 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6470 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6471 | u32 num_stripes; |
| 6472 | u32 array_size; |
| 6473 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6474 | u32 cur_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6475 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6476 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6477 | ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize); |
| 6478 | /* |
| 6479 | * This will create extent buffer of nodesize, superblock size is |
| 6480 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6481 | * overallocate but we can keep it as-is, only the first page is used. |
| 6482 | */ |
| 6483 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6484 | if (!sb) |
| 6485 | return -ENOMEM; |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6486 | set_extent_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6487 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6488 | /* |
| 6489 | * The sb extent buffer is artifical and just used to read the system array. |
David Sterba | 4db8c52 | 2015-12-03 13:06:46 +0100 | [diff] [blame] | 6490 | * set_extent_buffer_uptodate() call does not properly mark all it's |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6491 | * pages up-to-date when the page is larger: extent does not cover the |
| 6492 | * whole page and consequently check_page_uptodate does not find all |
| 6493 | * the page's extents up-to-date (the hole beyond sb), |
| 6494 | * write_extent_buffer then triggers a WARN_ON. |
| 6495 | * |
| 6496 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6497 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6498 | * to silence the warning eg. on PowerPC 64. |
| 6499 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 6500 | if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6501 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6502 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6503 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6504 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6505 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6506 | array_ptr = super_copy->sys_chunk_array; |
| 6507 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6508 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6509 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6510 | while (cur_offset < array_size) { |
| 6511 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6512 | len = sizeof(*disk_key); |
| 6513 | if (cur_offset + len > array_size) |
| 6514 | goto out_short_read; |
| 6515 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6516 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6517 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6518 | array_ptr += len; |
| 6519 | sb_array_offset += len; |
| 6520 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6521 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6522 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6523 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6524 | /* |
| 6525 | * At least one btrfs_chunk with one stripe must be |
| 6526 | * present, exact stripe count check comes afterwards |
| 6527 | */ |
| 6528 | len = btrfs_chunk_item_size(1); |
| 6529 | if (cur_offset + len > array_size) |
| 6530 | goto out_short_read; |
| 6531 | |
| 6532 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
David Sterba | f5cdedd | 2015-11-30 17:27:06 +0100 | [diff] [blame] | 6533 | if (!num_stripes) { |
| 6534 | printk(KERN_ERR |
| 6535 | "BTRFS: invalid number of stripes %u in sys_array at offset %u\n", |
| 6536 | num_stripes, cur_offset); |
| 6537 | ret = -EIO; |
| 6538 | break; |
| 6539 | } |
| 6540 | |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6541 | len = btrfs_chunk_item_size(num_stripes); |
| 6542 | if (cur_offset + len > array_size) |
| 6543 | goto out_short_read; |
| 6544 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6545 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6546 | if (ret) |
| 6547 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6548 | } else { |
David Sterba | 93a3d46 | 2015-11-30 17:27:09 +0100 | [diff] [blame] | 6549 | printk(KERN_ERR |
| 6550 | "BTRFS: unexpected item type %u in sys_array at offset %u\n", |
| 6551 | (u32)key.type, cur_offset); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6552 | ret = -EIO; |
| 6553 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6554 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6555 | array_ptr += len; |
| 6556 | sb_array_offset += len; |
| 6557 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6558 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6559 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6560 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6561 | |
| 6562 | out_short_read: |
| 6563 | printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n", |
| 6564 | len, cur_offset); |
| 6565 | free_extent_buffer(sb); |
| 6566 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6567 | } |
| 6568 | |
| 6569 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6570 | { |
| 6571 | struct btrfs_path *path; |
| 6572 | struct extent_buffer *leaf; |
| 6573 | struct btrfs_key key; |
| 6574 | struct btrfs_key found_key; |
| 6575 | int ret; |
| 6576 | int slot; |
| 6577 | |
| 6578 | root = root->fs_info->chunk_root; |
| 6579 | |
| 6580 | path = btrfs_alloc_path(); |
| 6581 | if (!path) |
| 6582 | return -ENOMEM; |
| 6583 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6584 | mutex_lock(&uuid_mutex); |
| 6585 | lock_chunks(root); |
| 6586 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6587 | /* |
| 6588 | * Read all device items, and then all the chunk items. All |
| 6589 | * device items are found before any chunk item (their object id |
| 6590 | * is smaller than the lowest possible object id for a chunk |
| 6591 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6592 | */ |
| 6593 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6594 | key.offset = 0; |
| 6595 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6596 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6597 | if (ret < 0) |
| 6598 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6599 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6600 | leaf = path->nodes[0]; |
| 6601 | slot = path->slots[0]; |
| 6602 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6603 | ret = btrfs_next_leaf(root, path); |
| 6604 | if (ret == 0) |
| 6605 | continue; |
| 6606 | if (ret < 0) |
| 6607 | goto error; |
| 6608 | break; |
| 6609 | } |
| 6610 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6611 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6612 | struct btrfs_dev_item *dev_item; |
| 6613 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6614 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6615 | ret = read_one_dev(root, leaf, dev_item); |
| 6616 | if (ret) |
| 6617 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6618 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6619 | struct btrfs_chunk *chunk; |
| 6620 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6621 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6622 | if (ret) |
| 6623 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6624 | } |
| 6625 | path->slots[0]++; |
| 6626 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6627 | ret = 0; |
| 6628 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6629 | unlock_chunks(root); |
| 6630 | mutex_unlock(&uuid_mutex); |
| 6631 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6632 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6633 | return ret; |
| 6634 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6635 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6636 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6637 | { |
| 6638 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6639 | struct btrfs_device *device; |
| 6640 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6641 | while (fs_devices) { |
| 6642 | mutex_lock(&fs_devices->device_list_mutex); |
| 6643 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6644 | device->dev_root = fs_info->dev_root; |
| 6645 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6646 | |
| 6647 | fs_devices = fs_devices->seed; |
| 6648 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6651 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6652 | { |
| 6653 | int i; |
| 6654 | |
| 6655 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6656 | btrfs_dev_stat_reset(dev, i); |
| 6657 | } |
| 6658 | |
| 6659 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6660 | { |
| 6661 | struct btrfs_key key; |
| 6662 | struct btrfs_key found_key; |
| 6663 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6664 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6665 | struct extent_buffer *eb; |
| 6666 | int slot; |
| 6667 | int ret = 0; |
| 6668 | struct btrfs_device *device; |
| 6669 | struct btrfs_path *path = NULL; |
| 6670 | int i; |
| 6671 | |
| 6672 | path = btrfs_alloc_path(); |
| 6673 | if (!path) { |
| 6674 | ret = -ENOMEM; |
| 6675 | goto out; |
| 6676 | } |
| 6677 | |
| 6678 | mutex_lock(&fs_devices->device_list_mutex); |
| 6679 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6680 | int item_size; |
| 6681 | struct btrfs_dev_stats_item *ptr; |
| 6682 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 6683 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 6684 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6685 | key.offset = device->devid; |
| 6686 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6687 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6688 | __btrfs_reset_dev_stats(device); |
| 6689 | device->dev_stats_valid = 1; |
| 6690 | btrfs_release_path(path); |
| 6691 | continue; |
| 6692 | } |
| 6693 | slot = path->slots[0]; |
| 6694 | eb = path->nodes[0]; |
| 6695 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6696 | item_size = btrfs_item_size_nr(eb, slot); |
| 6697 | |
| 6698 | ptr = btrfs_item_ptr(eb, slot, |
| 6699 | struct btrfs_dev_stats_item); |
| 6700 | |
| 6701 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6702 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6703 | btrfs_dev_stat_set(device, i, |
| 6704 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6705 | else |
| 6706 | btrfs_dev_stat_reset(device, i); |
| 6707 | } |
| 6708 | |
| 6709 | device->dev_stats_valid = 1; |
| 6710 | btrfs_dev_stat_print_on_load(device); |
| 6711 | btrfs_release_path(path); |
| 6712 | } |
| 6713 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6714 | |
| 6715 | out: |
| 6716 | btrfs_free_path(path); |
| 6717 | return ret < 0 ? ret : 0; |
| 6718 | } |
| 6719 | |
| 6720 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6721 | struct btrfs_root *dev_root, |
| 6722 | struct btrfs_device *device) |
| 6723 | { |
| 6724 | struct btrfs_path *path; |
| 6725 | struct btrfs_key key; |
| 6726 | struct extent_buffer *eb; |
| 6727 | struct btrfs_dev_stats_item *ptr; |
| 6728 | int ret; |
| 6729 | int i; |
| 6730 | |
David Sterba | 242e295 | 2016-01-25 17:51:31 +0100 | [diff] [blame] | 6731 | key.objectid = BTRFS_DEV_STATS_OBJECTID; |
| 6732 | key.type = BTRFS_PERSISTENT_ITEM_KEY; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6733 | key.offset = device->devid; |
| 6734 | |
| 6735 | path = btrfs_alloc_path(); |
| 6736 | BUG_ON(!path); |
| 6737 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6738 | if (ret < 0) { |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 6739 | btrfs_warn_in_rcu(dev_root->fs_info, |
| 6740 | "error %d while searching for dev_stats item for device %s", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6741 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6742 | goto out; |
| 6743 | } |
| 6744 | |
| 6745 | if (ret == 0 && |
| 6746 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6747 | /* need to delete old one and insert a new one */ |
| 6748 | ret = btrfs_del_item(trans, dev_root, path); |
| 6749 | if (ret != 0) { |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 6750 | btrfs_warn_in_rcu(dev_root->fs_info, |
| 6751 | "delete too small dev_stats item for device %s failed %d", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6752 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6753 | goto out; |
| 6754 | } |
| 6755 | ret = 1; |
| 6756 | } |
| 6757 | |
| 6758 | if (ret == 1) { |
| 6759 | /* need to insert a new item */ |
| 6760 | btrfs_release_path(path); |
| 6761 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6762 | &key, sizeof(*ptr)); |
| 6763 | if (ret < 0) { |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 6764 | btrfs_warn_in_rcu(dev_root->fs_info, |
| 6765 | "insert dev_stats item for device %s failed %d", |
| 6766 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6767 | goto out; |
| 6768 | } |
| 6769 | } |
| 6770 | |
| 6771 | eb = path->nodes[0]; |
| 6772 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6773 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6774 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6775 | btrfs_dev_stat_read(device, i)); |
| 6776 | btrfs_mark_buffer_dirty(eb); |
| 6777 | |
| 6778 | out: |
| 6779 | btrfs_free_path(path); |
| 6780 | return ret; |
| 6781 | } |
| 6782 | |
| 6783 | /* |
| 6784 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6785 | */ |
| 6786 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6787 | struct btrfs_fs_info *fs_info) |
| 6788 | { |
| 6789 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6790 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6791 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6792 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6793 | int ret = 0; |
| 6794 | |
| 6795 | mutex_lock(&fs_devices->device_list_mutex); |
| 6796 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6797 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6798 | continue; |
| 6799 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6800 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6801 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6802 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6803 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6804 | } |
| 6805 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6806 | |
| 6807 | return ret; |
| 6808 | } |
| 6809 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6810 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6811 | { |
| 6812 | btrfs_dev_stat_inc(dev, index); |
| 6813 | btrfs_dev_stat_print_on_error(dev); |
| 6814 | } |
| 6815 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6816 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6817 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6818 | if (!dev->dev_stats_valid) |
| 6819 | return; |
David Sterba | b14af3b | 2015-10-08 10:43:10 +0200 | [diff] [blame] | 6820 | btrfs_err_rl_in_rcu(dev->dev_root->fs_info, |
| 6821 | "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] | 6822 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6823 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6824 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6825 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6826 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6827 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6828 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6829 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6830 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6831 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6832 | int i; |
| 6833 | |
| 6834 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6835 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6836 | break; |
| 6837 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6838 | return; /* all values == 0, suppress message */ |
| 6839 | |
David Sterba | ecaeb14 | 2015-10-08 09:01:03 +0200 | [diff] [blame] | 6840 | btrfs_info_in_rcu(dev->dev_root->fs_info, |
| 6841 | "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] | 6842 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6843 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6844 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6845 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6846 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6847 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6848 | } |
| 6849 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6850 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6851 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6852 | { |
| 6853 | struct btrfs_device *dev; |
| 6854 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6855 | int i; |
| 6856 | |
| 6857 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6858 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6859 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6860 | |
| 6861 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6862 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6863 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6864 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6865 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6866 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6867 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6868 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6869 | if (stats->nr_items > i) |
| 6870 | stats->values[i] = |
| 6871 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6872 | else |
| 6873 | btrfs_dev_stat_reset(dev, i); |
| 6874 | } |
| 6875 | } else { |
| 6876 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6877 | if (stats->nr_items > i) |
| 6878 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6879 | } |
| 6880 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6881 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6882 | return 0; |
| 6883 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6884 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 6885 | void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path) |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6886 | { |
| 6887 | struct buffer_head *bh; |
| 6888 | struct btrfs_super_block *disk_super; |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 6889 | int copy_num; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6890 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 6891 | if (!bdev) |
| 6892 | return; |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6893 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 6894 | for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; |
| 6895 | copy_num++) { |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6896 | |
Anand Jain | 12b1c26 | 2015-08-14 18:32:59 +0800 | [diff] [blame] | 6897 | if (btrfs_read_dev_one_super(bdev, copy_num, &bh)) |
| 6898 | continue; |
| 6899 | |
| 6900 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6901 | |
| 6902 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6903 | set_buffer_dirty(bh); |
| 6904 | sync_dirty_buffer(bh); |
| 6905 | brelse(bh); |
| 6906 | } |
| 6907 | |
| 6908 | /* Notify udev that device has changed */ |
| 6909 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
| 6910 | |
| 6911 | /* Update ctime/mtime for device path for libblkid */ |
| 6912 | update_dev_time(device_path); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6913 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6914 | |
| 6915 | /* |
| 6916 | * Update the size of all devices, which is used for writing out the |
| 6917 | * super blocks. |
| 6918 | */ |
| 6919 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6920 | { |
| 6921 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6922 | struct btrfs_device *curr, *next; |
| 6923 | |
| 6924 | if (list_empty(&fs_devices->resized_devices)) |
| 6925 | return; |
| 6926 | |
| 6927 | mutex_lock(&fs_devices->device_list_mutex); |
| 6928 | lock_chunks(fs_info->dev_root); |
| 6929 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6930 | resized_list) { |
| 6931 | list_del_init(&curr->resized_list); |
| 6932 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6933 | } |
| 6934 | unlock_chunks(fs_info->dev_root); |
| 6935 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6936 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6937 | |
| 6938 | /* Must be invoked during the transaction commit */ |
| 6939 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6940 | struct btrfs_transaction *transaction) |
| 6941 | { |
| 6942 | struct extent_map *em; |
| 6943 | struct map_lookup *map; |
| 6944 | struct btrfs_device *dev; |
| 6945 | int i; |
| 6946 | |
| 6947 | if (list_empty(&transaction->pending_chunks)) |
| 6948 | return; |
| 6949 | |
| 6950 | /* In order to kick the device replace finish process */ |
| 6951 | lock_chunks(root); |
| 6952 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
Jeff Mahoney | 95617d6 | 2015-06-03 10:55:48 -0400 | [diff] [blame] | 6953 | map = em->map_lookup; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6954 | |
| 6955 | for (i = 0; i < map->num_stripes; i++) { |
| 6956 | dev = map->stripes[i].dev; |
| 6957 | dev->commit_bytes_used = dev->bytes_used; |
| 6958 | } |
| 6959 | } |
| 6960 | unlock_chunks(root); |
| 6961 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 6962 | |
| 6963 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6964 | { |
| 6965 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6966 | while (fs_devices) { |
| 6967 | fs_devices->fs_info = fs_info; |
| 6968 | fs_devices = fs_devices->seed; |
| 6969 | } |
| 6970 | } |
| 6971 | |
| 6972 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6973 | { |
| 6974 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6975 | while (fs_devices) { |
| 6976 | fs_devices->fs_info = NULL; |
| 6977 | fs_devices = fs_devices->seed; |
| 6978 | } |
| 6979 | } |
Anand Jain | f190aa4 | 2015-08-14 18:33:05 +0800 | [diff] [blame] | 6980 | |
David Sterba | 87ad58c | 2015-11-27 18:49:39 +0100 | [diff] [blame] | 6981 | static void btrfs_close_one_device(struct btrfs_device *device) |
Anand Jain | f190aa4 | 2015-08-14 18:33:05 +0800 | [diff] [blame] | 6982 | { |
| 6983 | struct btrfs_fs_devices *fs_devices = device->fs_devices; |
| 6984 | struct btrfs_device *new_device; |
| 6985 | struct rcu_string *name; |
| 6986 | |
| 6987 | if (device->bdev) |
| 6988 | fs_devices->open_devices--; |
| 6989 | |
| 6990 | if (device->writeable && |
| 6991 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
| 6992 | list_del_init(&device->dev_alloc_list); |
| 6993 | fs_devices->rw_devices--; |
| 6994 | } |
| 6995 | |
| 6996 | if (device->missing) |
| 6997 | fs_devices->missing_devices--; |
| 6998 | |
| 6999 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 7000 | device->uuid); |
| 7001 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
| 7002 | |
| 7003 | /* Safe because we are under uuid_mutex */ |
| 7004 | if (device->name) { |
| 7005 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
| 7006 | BUG_ON(!name); /* -ENOMEM */ |
| 7007 | rcu_assign_pointer(new_device->name, name); |
| 7008 | } |
| 7009 | |
| 7010 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
| 7011 | new_device->fs_devices = device->fs_devices; |
| 7012 | |
| 7013 | call_rcu(&device->rcu, free_device); |
| 7014 | } |