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 | |
| 111 | const u64 const btrfs_raid_group[BTRFS_NR_RAID_TYPES] = { |
| 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); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 128 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 129 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 130 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 131 | struct list_head *btrfs_get_fs_uuids(void) |
| 132 | { |
| 133 | return &fs_uuids; |
| 134 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 135 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 136 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 137 | { |
| 138 | struct btrfs_fs_devices *fs_devs; |
| 139 | |
| 140 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 141 | if (!fs_devs) |
| 142 | return ERR_PTR(-ENOMEM); |
| 143 | |
| 144 | mutex_init(&fs_devs->device_list_mutex); |
| 145 | |
| 146 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 147 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 148 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 149 | INIT_LIST_HEAD(&fs_devs->list); |
| 150 | |
| 151 | return fs_devs; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 156 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 157 | * generated. |
| 158 | * |
| 159 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 160 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 161 | * can be destroyed with kfree() right away. |
| 162 | */ |
| 163 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 164 | { |
| 165 | struct btrfs_fs_devices *fs_devs; |
| 166 | |
| 167 | fs_devs = __alloc_fs_devices(); |
| 168 | if (IS_ERR(fs_devs)) |
| 169 | return fs_devs; |
| 170 | |
| 171 | if (fsid) |
| 172 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 173 | else |
| 174 | generate_random_uuid(fs_devs->fsid); |
| 175 | |
| 176 | return fs_devs; |
| 177 | } |
| 178 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 179 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 180 | { |
| 181 | struct btrfs_device *device; |
| 182 | WARN_ON(fs_devices->opened); |
| 183 | while (!list_empty(&fs_devices->devices)) { |
| 184 | device = list_entry(fs_devices->devices.next, |
| 185 | struct btrfs_device, dev_list); |
| 186 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 187 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 188 | kfree(device); |
| 189 | } |
| 190 | kfree(fs_devices); |
| 191 | } |
| 192 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 193 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 194 | enum kobject_action action) |
| 195 | { |
| 196 | int ret; |
| 197 | |
| 198 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 199 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 200 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 201 | action, |
| 202 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 203 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 204 | } |
| 205 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 206 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 207 | { |
| 208 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 209 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 210 | while (!list_empty(&fs_uuids)) { |
| 211 | fs_devices = list_entry(fs_uuids.next, |
| 212 | struct btrfs_fs_devices, list); |
| 213 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 214 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 215 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 218 | static struct btrfs_device *__alloc_device(void) |
| 219 | { |
| 220 | struct btrfs_device *dev; |
| 221 | |
| 222 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 223 | if (!dev) |
| 224 | return ERR_PTR(-ENOMEM); |
| 225 | |
| 226 | INIT_LIST_HEAD(&dev->dev_list); |
| 227 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 228 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 229 | |
| 230 | spin_lock_init(&dev->io_lock); |
| 231 | |
| 232 | spin_lock_init(&dev->reada_lock); |
| 233 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 234 | atomic_set(&dev->dev_stats_ccnt, 0); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 235 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 236 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 237 | |
| 238 | return dev; |
| 239 | } |
| 240 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 241 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 242 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 243 | { |
| 244 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 245 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 246 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 247 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 248 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 249 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 250 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 251 | } |
| 252 | return NULL; |
| 253 | } |
| 254 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 255 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 256 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 257 | struct btrfs_fs_devices *fs_devices; |
| 258 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 259 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 260 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 261 | return fs_devices; |
| 262 | } |
| 263 | return NULL; |
| 264 | } |
| 265 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 266 | static int |
| 267 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 268 | int flush, struct block_device **bdev, |
| 269 | struct buffer_head **bh) |
| 270 | { |
| 271 | int ret; |
| 272 | |
| 273 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 274 | |
| 275 | if (IS_ERR(*bdev)) { |
| 276 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 277 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 278 | goto error; |
| 279 | } |
| 280 | |
| 281 | if (flush) |
| 282 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 283 | ret = set_blocksize(*bdev, 4096); |
| 284 | if (ret) { |
| 285 | blkdev_put(*bdev, flags); |
| 286 | goto error; |
| 287 | } |
| 288 | invalidate_bdev(*bdev); |
| 289 | *bh = btrfs_read_dev_super(*bdev); |
| 290 | if (!*bh) { |
| 291 | ret = -EINVAL; |
| 292 | blkdev_put(*bdev, flags); |
| 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | |
| 298 | error: |
| 299 | *bdev = NULL; |
| 300 | *bh = NULL; |
| 301 | return ret; |
| 302 | } |
| 303 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 304 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 305 | struct bio *head, struct bio *tail) |
| 306 | { |
| 307 | |
| 308 | struct bio *old_head; |
| 309 | |
| 310 | old_head = pending_bios->head; |
| 311 | pending_bios->head = head; |
| 312 | if (pending_bios->tail) |
| 313 | tail->bi_next = old_head; |
| 314 | else |
| 315 | pending_bios->tail = tail; |
| 316 | } |
| 317 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 318 | /* |
| 319 | * we try to collect pending bios for a device so we don't get a large |
| 320 | * number of procs sending bios down to the same device. This greatly |
| 321 | * improves the schedulers ability to collect and merge the bios. |
| 322 | * |
| 323 | * But, it also turns into a long list of bios to process and that is sure |
| 324 | * to eventually make the worker thread block. The solution here is to |
| 325 | * make some progress and then put this work struct back at the end of |
| 326 | * the list if the block device is congested. This way, multiple devices |
| 327 | * can make progress from a single worker thread. |
| 328 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 329 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 330 | { |
| 331 | struct bio *pending; |
| 332 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 333 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 334 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 335 | struct bio *tail; |
| 336 | struct bio *cur; |
| 337 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 338 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 339 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 340 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 341 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 342 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 343 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 344 | struct blk_plug plug; |
| 345 | |
| 346 | /* |
| 347 | * this function runs all the bios we've collected for |
| 348 | * a particular device. We don't want to wander off to |
| 349 | * another device without first sending all of these down. |
| 350 | * So, setup a plug here and finish it off before we return |
| 351 | */ |
| 352 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 353 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 354 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 355 | fs_info = device->dev_root->fs_info; |
| 356 | limit = btrfs_async_submit_limit(fs_info); |
| 357 | limit = limit * 2 / 3; |
| 358 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 359 | loop: |
| 360 | spin_lock(&device->io_lock); |
| 361 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 362 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 363 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 364 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 365 | /* take all the bios off the list at once and process them |
| 366 | * later on (without the lock held). But, remember the |
| 367 | * tail and other pointers so the bios can be properly reinserted |
| 368 | * into the list if we hit congestion |
| 369 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 370 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 371 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 372 | force_reg = 1; |
| 373 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 374 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 375 | force_reg = 0; |
| 376 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 377 | |
| 378 | pending = pending_bios->head; |
| 379 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 380 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * if pending was null this time around, no bios need processing |
| 384 | * at all and we can stop. Otherwise it'll loop back up again |
| 385 | * and do an additional check so no bios are missed. |
| 386 | * |
| 387 | * device->running_pending is used to synchronize with the |
| 388 | * schedule_bio code. |
| 389 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 390 | if (device->pending_sync_bios.head == NULL && |
| 391 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 392 | again = 0; |
| 393 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 394 | } else { |
| 395 | again = 1; |
| 396 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 397 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 398 | |
| 399 | pending_bios->head = NULL; |
| 400 | pending_bios->tail = NULL; |
| 401 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 402 | spin_unlock(&device->io_lock); |
| 403 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 404 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 405 | |
| 406 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 407 | /* we want to work on both lists, but do more bios on the |
| 408 | * sync list than the regular list |
| 409 | */ |
| 410 | if ((num_run > 32 && |
| 411 | pending_bios != &device->pending_sync_bios && |
| 412 | device->pending_sync_bios.head) || |
| 413 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 414 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 415 | spin_lock(&device->io_lock); |
| 416 | requeue_list(pending_bios, pending, tail); |
| 417 | goto loop_lock; |
| 418 | } |
| 419 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 420 | cur = pending; |
| 421 | pending = pending->bi_next; |
| 422 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 423 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 424 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 425 | waitqueue_active(&fs_info->async_submit_wait)) |
| 426 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 427 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 428 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 429 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 430 | /* |
| 431 | * if we're doing the sync list, record that our |
| 432 | * plug has some sync requests on it |
| 433 | * |
| 434 | * If we're doing the regular list and there are |
| 435 | * sync requests sitting around, unplug before |
| 436 | * we add more |
| 437 | */ |
| 438 | if (pending_bios == &device->pending_sync_bios) { |
| 439 | sync_pending = 1; |
| 440 | } else if (sync_pending) { |
| 441 | blk_finish_plug(&plug); |
| 442 | blk_start_plug(&plug); |
| 443 | sync_pending = 0; |
| 444 | } |
| 445 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 446 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 447 | num_run++; |
| 448 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 449 | |
| 450 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 451 | |
| 452 | /* |
| 453 | * we made progress, there is more work to do and the bdi |
| 454 | * is now congested. Back off and let other work structs |
| 455 | * run instead |
| 456 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 457 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 458 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 459 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 460 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 461 | ioc = current->io_context; |
| 462 | |
| 463 | /* |
| 464 | * the main goal here is that we don't want to |
| 465 | * block if we're going to be able to submit |
| 466 | * more requests without blocking. |
| 467 | * |
| 468 | * This code does two great things, it pokes into |
| 469 | * the elevator code from a filesystem _and_ |
| 470 | * it makes assumptions about how batching works. |
| 471 | */ |
| 472 | if (ioc && ioc->nr_batch_requests > 0 && |
| 473 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 474 | (last_waited == 0 || |
| 475 | ioc->last_waited == last_waited)) { |
| 476 | /* |
| 477 | * we want to go through our batch of |
| 478 | * requests and stop. So, we copy out |
| 479 | * the ioc->last_waited time and test |
| 480 | * against it before looping |
| 481 | */ |
| 482 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 483 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 484 | continue; |
| 485 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 486 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 487 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 488 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 489 | |
| 490 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 491 | btrfs_queue_work(fs_info->submit_workers, |
| 492 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 493 | goto done; |
| 494 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 495 | /* unplug every 64 requests just for good measure */ |
| 496 | if (batch_run % 64 == 0) { |
| 497 | blk_finish_plug(&plug); |
| 498 | blk_start_plug(&plug); |
| 499 | sync_pending = 0; |
| 500 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 501 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 502 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 503 | cond_resched(); |
| 504 | if (again) |
| 505 | goto loop; |
| 506 | |
| 507 | spin_lock(&device->io_lock); |
| 508 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 509 | goto loop_lock; |
| 510 | spin_unlock(&device->io_lock); |
| 511 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 512 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 513 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 514 | } |
| 515 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 516 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 517 | { |
| 518 | struct btrfs_device *device; |
| 519 | |
| 520 | device = container_of(work, struct btrfs_device, work); |
| 521 | run_scheduled_bios(device); |
| 522 | } |
| 523 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 524 | |
| 525 | void btrfs_free_stale_device(struct btrfs_device *cur_dev) |
| 526 | { |
| 527 | struct btrfs_fs_devices *fs_devs; |
| 528 | struct btrfs_device *dev; |
| 529 | |
| 530 | if (!cur_dev->name) |
| 531 | return; |
| 532 | |
| 533 | list_for_each_entry(fs_devs, &fs_uuids, list) { |
| 534 | int del = 1; |
| 535 | |
| 536 | if (fs_devs->opened) |
| 537 | continue; |
| 538 | if (fs_devs->seeding) |
| 539 | continue; |
| 540 | |
| 541 | list_for_each_entry(dev, &fs_devs->devices, dev_list) { |
| 542 | |
| 543 | if (dev == cur_dev) |
| 544 | continue; |
| 545 | if (!dev->name) |
| 546 | continue; |
| 547 | |
| 548 | /* |
| 549 | * Todo: This won't be enough. What if the same device |
| 550 | * comes back (with new uuid and) with its mapper path? |
| 551 | * But for now, this does help as mostly an admin will |
| 552 | * either use mapper or non mapper path throughout. |
| 553 | */ |
| 554 | rcu_read_lock(); |
| 555 | del = strcmp(rcu_str_deref(dev->name), |
| 556 | rcu_str_deref(cur_dev->name)); |
| 557 | rcu_read_unlock(); |
| 558 | if (!del) |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | if (!del) { |
| 563 | /* delete the stale device */ |
| 564 | if (fs_devs->num_devices == 1) { |
| 565 | btrfs_sysfs_remove_fsid(fs_devs); |
| 566 | list_del(&fs_devs->list); |
| 567 | free_fs_devices(fs_devs); |
| 568 | } else { |
| 569 | fs_devs->num_devices--; |
| 570 | list_del(&dev->dev_list); |
| 571 | rcu_string_free(dev->name); |
| 572 | kfree(dev); |
| 573 | } |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 579 | /* |
| 580 | * Add new device to list of registered devices |
| 581 | * |
| 582 | * Returns: |
| 583 | * 1 - first time device is seen |
| 584 | * 0 - device already known |
| 585 | * < 0 - error |
| 586 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 587 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 588 | struct btrfs_super_block *disk_super, |
| 589 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 590 | { |
| 591 | struct btrfs_device *device; |
| 592 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 593 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 594 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 595 | u64 found_transid = btrfs_super_generation(disk_super); |
| 596 | |
| 597 | fs_devices = find_fsid(disk_super->fsid); |
| 598 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 599 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 600 | if (IS_ERR(fs_devices)) |
| 601 | return PTR_ERR(fs_devices); |
| 602 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 603 | list_add(&fs_devices->list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 604 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 605 | device = NULL; |
| 606 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 607 | device = __find_device(&fs_devices->devices, devid, |
| 608 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 609 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 610 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 611 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 612 | if (fs_devices->opened) |
| 613 | return -EBUSY; |
| 614 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 615 | device = btrfs_alloc_device(NULL, &devid, |
| 616 | disk_super->dev_item.uuid); |
| 617 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 618 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 619 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 620 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 621 | |
| 622 | name = rcu_string_strdup(path, GFP_NOFS); |
| 623 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 624 | kfree(device); |
| 625 | return -ENOMEM; |
| 626 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 627 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 628 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 629 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 630 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 631 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 632 | mutex_unlock(&fs_devices->device_list_mutex); |
| 633 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 634 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 635 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 636 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 637 | /* |
| 638 | * When FS is already mounted. |
| 639 | * 1. If you are here and if the device->name is NULL that |
| 640 | * means this device was missing at time of FS mount. |
| 641 | * 2. If you are here and if the device->name is different |
| 642 | * from 'path' that means either |
| 643 | * a. The same device disappeared and reappeared with |
| 644 | * different name. or |
| 645 | * b. The missing-disk-which-was-replaced, has |
| 646 | * reappeared now. |
| 647 | * |
| 648 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 649 | * and unintentional. |
| 650 | * |
| 651 | * Further in case of 1 and 2a above, the disk at 'path' |
| 652 | * would have missed some transaction when it was away and |
| 653 | * in case of 2a the stale bdev has to be updated as well. |
| 654 | * 2b must not be allowed at all time. |
| 655 | */ |
| 656 | |
| 657 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 658 | * For now, we do allow update to btrfs_fs_device through the |
| 659 | * btrfs dev scan cli after FS has been mounted. We're still |
| 660 | * tracking a problem where systems fail mount by subvolume id |
| 661 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 662 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 663 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 664 | /* |
| 665 | * That is if the FS is _not_ mounted and if you |
| 666 | * are here, that means there is more than one |
| 667 | * disk with same uuid and devid.We keep the one |
| 668 | * with larger generation number or the last-in if |
| 669 | * generation are equal. |
| 670 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 671 | return -EEXIST; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 672 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 673 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 674 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 675 | if (!name) |
| 676 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 677 | rcu_string_free(device->name); |
| 678 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 679 | if (device->missing) { |
| 680 | fs_devices->missing_devices--; |
| 681 | device->missing = 0; |
| 682 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 683 | } |
| 684 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 685 | /* |
| 686 | * Unmount does not free the btrfs_device struct but would zero |
| 687 | * generation along with most of the other members. So just update |
| 688 | * it back. We need it to pick the disk with largest generation |
| 689 | * (as above). |
| 690 | */ |
| 691 | if (!fs_devices->opened) |
| 692 | device->generation = found_transid; |
| 693 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 694 | /* |
| 695 | * if there is new btrfs on an already registered device, |
| 696 | * then remove the stale device entry. |
| 697 | */ |
| 698 | btrfs_free_stale_device(device); |
| 699 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 700 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 701 | |
| 702 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 703 | } |
| 704 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 705 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 706 | { |
| 707 | struct btrfs_fs_devices *fs_devices; |
| 708 | struct btrfs_device *device; |
| 709 | struct btrfs_device *orig_dev; |
| 710 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 711 | fs_devices = alloc_fs_devices(orig->fsid); |
| 712 | if (IS_ERR(fs_devices)) |
| 713 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 714 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 715 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 716 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 717 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 718 | /* 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] | 719 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 720 | struct rcu_string *name; |
| 721 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 722 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 723 | orig_dev->uuid); |
| 724 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 725 | goto error; |
| 726 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 727 | /* |
| 728 | * This is ok to do without rcu read locked because we hold the |
| 729 | * uuid mutex so nothing we touch in here is going to disappear. |
| 730 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 731 | if (orig_dev->name) { |
| 732 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 733 | if (!name) { |
| 734 | kfree(device); |
| 735 | goto error; |
| 736 | } |
| 737 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 738 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 739 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 740 | list_add(&device->dev_list, &fs_devices->devices); |
| 741 | device->fs_devices = fs_devices; |
| 742 | fs_devices->num_devices++; |
| 743 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 744 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 745 | return fs_devices; |
| 746 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 747 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 748 | free_fs_devices(fs_devices); |
| 749 | return ERR_PTR(-ENOMEM); |
| 750 | } |
| 751 | |
Eric Sandeen | 9eaed21 | 2014-08-01 18:12:35 -0500 | [diff] [blame] | 752 | 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] | 753 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 754 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 755 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 756 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 757 | mutex_lock(&uuid_mutex); |
| 758 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 759 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 760 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 761 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 762 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 763 | (!latest_dev || |
| 764 | device->generation > latest_dev->generation)) { |
| 765 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 766 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 767 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 768 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 769 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 770 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 771 | /* |
| 772 | * In the first step, keep the device which has |
| 773 | * the correct fsid and the devid that is used |
| 774 | * for the dev_replace procedure. |
| 775 | * In the second step, the dev_replace state is |
| 776 | * read from the device tree and it is known |
| 777 | * whether the procedure is really active or |
| 778 | * not, which means whether this device is |
| 779 | * used or whether it should be removed. |
| 780 | */ |
| 781 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 782 | continue; |
| 783 | } |
| 784 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 785 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 786 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 787 | device->bdev = NULL; |
| 788 | fs_devices->open_devices--; |
| 789 | } |
| 790 | if (device->writeable) { |
| 791 | list_del_init(&device->dev_alloc_list); |
| 792 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 793 | if (!device->is_tgtdev_for_dev_replace) |
| 794 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 795 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 796 | list_del_init(&device->dev_list); |
| 797 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 798 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 799 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 800 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 801 | |
| 802 | if (fs_devices->seed) { |
| 803 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 804 | goto again; |
| 805 | } |
| 806 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 807 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 808 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 809 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 810 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 811 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 812 | static void __free_device(struct work_struct *work) |
| 813 | { |
| 814 | struct btrfs_device *device; |
| 815 | |
| 816 | device = container_of(work, struct btrfs_device, rcu_work); |
| 817 | |
| 818 | if (device->bdev) |
| 819 | blkdev_put(device->bdev, device->mode); |
| 820 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 821 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 822 | kfree(device); |
| 823 | } |
| 824 | |
| 825 | static void free_device(struct rcu_head *head) |
| 826 | { |
| 827 | struct btrfs_device *device; |
| 828 | |
| 829 | device = container_of(head, struct btrfs_device, rcu); |
| 830 | |
| 831 | INIT_WORK(&device->rcu_work, __free_device); |
| 832 | schedule_work(&device->rcu_work); |
| 833 | } |
| 834 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 835 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 836 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 837 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 838 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 839 | if (--fs_devices->opened > 0) |
| 840 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 841 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 842 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 843 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 844 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 845 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 846 | |
| 847 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 848 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 849 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 850 | if (device->writeable && |
| 851 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 852 | list_del_init(&device->dev_alloc_list); |
| 853 | fs_devices->rw_devices--; |
| 854 | } |
| 855 | |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 856 | if (device->missing) |
| 857 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 858 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 859 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 860 | device->uuid); |
| 861 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 862 | |
| 863 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 864 | if (device->name) { |
| 865 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 866 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 867 | rcu_assign_pointer(new_device->name, name); |
| 868 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 869 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 870 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 871 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 872 | |
| 873 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 874 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 875 | mutex_unlock(&fs_devices->device_list_mutex); |
| 876 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 877 | WARN_ON(fs_devices->open_devices); |
| 878 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 879 | fs_devices->opened = 0; |
| 880 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 881 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 882 | return 0; |
| 883 | } |
| 884 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 885 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 886 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 887 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 888 | int ret; |
| 889 | |
| 890 | mutex_lock(&uuid_mutex); |
| 891 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 892 | if (!fs_devices->opened) { |
| 893 | seed_devices = fs_devices->seed; |
| 894 | fs_devices->seed = NULL; |
| 895 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 896 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 897 | |
| 898 | while (seed_devices) { |
| 899 | fs_devices = seed_devices; |
| 900 | seed_devices = fs_devices->seed; |
| 901 | __btrfs_close_devices(fs_devices); |
| 902 | free_fs_devices(fs_devices); |
| 903 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 904 | /* |
| 905 | * Wait for rcu kworkers under __btrfs_close_devices |
| 906 | * to finish all blkdev_puts so device is really |
| 907 | * free when umount is done. |
| 908 | */ |
| 909 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 910 | return ret; |
| 911 | } |
| 912 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 913 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 914 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 915 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 916 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 917 | struct block_device *bdev; |
| 918 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 919 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 920 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 921 | struct buffer_head *bh; |
| 922 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 923 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 924 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 925 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 926 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 927 | flags |= FMODE_EXCL; |
| 928 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 929 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 930 | if (device->bdev) |
| 931 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 932 | if (!device->name) |
| 933 | continue; |
| 934 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 935 | /* Just open everything we can; ignore failures here */ |
| 936 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 937 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 938 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 939 | |
| 940 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 941 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 942 | if (devid != device->devid) |
| 943 | goto error_brelse; |
| 944 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 945 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 946 | BTRFS_UUID_SIZE)) |
| 947 | goto error_brelse; |
| 948 | |
| 949 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 950 | if (!latest_dev || |
| 951 | device->generation > latest_dev->generation) |
| 952 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 953 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 954 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 955 | device->writeable = 0; |
| 956 | } else { |
| 957 | device->writeable = !bdev_read_only(bdev); |
| 958 | seeding = 0; |
| 959 | } |
| 960 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 961 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 962 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 963 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 964 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 965 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 966 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 967 | device->mode = flags; |
| 968 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 969 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 970 | fs_devices->rotating = 1; |
| 971 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 972 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 973 | if (device->writeable && |
| 974 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 975 | fs_devices->rw_devices++; |
| 976 | list_add(&device->dev_alloc_list, |
| 977 | &fs_devices->alloc_list); |
| 978 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 979 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 980 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 981 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 982 | error_brelse: |
| 983 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 984 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 985 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 986 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 987 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 988 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 989 | goto out; |
| 990 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 991 | fs_devices->seeding = seeding; |
| 992 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 993 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 994 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 995 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 996 | return ret; |
| 997 | } |
| 998 | |
| 999 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1000 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1001 | { |
| 1002 | int ret; |
| 1003 | |
| 1004 | mutex_lock(&uuid_mutex); |
| 1005 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1006 | fs_devices->opened++; |
| 1007 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1008 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1009 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1010 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1011 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1012 | return ret; |
| 1013 | } |
| 1014 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1015 | /* |
| 1016 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1017 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1018 | * is read via pagecache |
| 1019 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1020 | 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] | 1021 | struct btrfs_fs_devices **fs_devices_ret) |
| 1022 | { |
| 1023 | struct btrfs_super_block *disk_super; |
| 1024 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1025 | struct page *page; |
| 1026 | void *p; |
| 1027 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1028 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1029 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1030 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1031 | u64 bytenr; |
| 1032 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1033 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1034 | /* |
| 1035 | * we would like to check all the supers, but that would make |
| 1036 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1037 | * So, we need to add a special mount option to scan for |
| 1038 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1039 | */ |
| 1040 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1041 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 1042 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1043 | |
| 1044 | bdev = blkdev_get_by_path(path, flags, holder); |
| 1045 | |
| 1046 | if (IS_ERR(bdev)) { |
| 1047 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1048 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* make sure our super fits in the device */ |
| 1052 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1053 | goto error_bdev_put; |
| 1054 | |
| 1055 | /* make sure our super fits in the page */ |
| 1056 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 1057 | goto error_bdev_put; |
| 1058 | |
| 1059 | /* make sure our super doesn't straddle pages on disk */ |
| 1060 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 1061 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 1062 | goto error_bdev_put; |
| 1063 | |
| 1064 | /* pull in the page with our super */ |
| 1065 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1066 | index, GFP_NOFS); |
| 1067 | |
| 1068 | if (IS_ERR_OR_NULL(page)) |
| 1069 | goto error_bdev_put; |
| 1070 | |
| 1071 | p = kmap(page); |
| 1072 | |
| 1073 | /* align our pointer to the offset of the super block */ |
| 1074 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 1075 | |
| 1076 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1077 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1078 | goto error_unmap; |
| 1079 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1080 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1081 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1082 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1083 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1084 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 1085 | if (ret > 0) { |
| 1086 | if (disk_super->label[0]) { |
| 1087 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 1088 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1089 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 1090 | } else { |
| 1091 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 1092 | } |
| 1093 | |
| 1094 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 1095 | ret = 0; |
| 1096 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1097 | if (!ret && fs_devices_ret) |
| 1098 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1099 | |
| 1100 | error_unmap: |
| 1101 | kunmap(page); |
| 1102 | page_cache_release(page); |
| 1103 | |
| 1104 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1105 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1106 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1107 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1108 | return ret; |
| 1109 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1110 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1111 | /* helper to account the used device space in the range */ |
| 1112 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 1113 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1114 | { |
| 1115 | struct btrfs_key key; |
| 1116 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1117 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1118 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1119 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1120 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1121 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1122 | struct extent_buffer *l; |
| 1123 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1124 | *length = 0; |
| 1125 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1126 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1127 | return 0; |
| 1128 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1129 | path = btrfs_alloc_path(); |
| 1130 | if (!path) |
| 1131 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1132 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1133 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1134 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1135 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1136 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1137 | |
| 1138 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1139 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1140 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1141 | if (ret > 0) { |
| 1142 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1143 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1144 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1145 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1146 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1147 | while (1) { |
| 1148 | l = path->nodes[0]; |
| 1149 | slot = path->slots[0]; |
| 1150 | if (slot >= btrfs_header_nritems(l)) { |
| 1151 | ret = btrfs_next_leaf(root, path); |
| 1152 | if (ret == 0) |
| 1153 | continue; |
| 1154 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1155 | goto out; |
| 1156 | |
| 1157 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1158 | } |
| 1159 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1160 | |
| 1161 | if (key.objectid < device->devid) |
| 1162 | goto next; |
| 1163 | |
| 1164 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1165 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1166 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1167 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1168 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1169 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1170 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1171 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1172 | dev_extent); |
| 1173 | if (key.offset <= start && extent_end > end) { |
| 1174 | *length = end - start + 1; |
| 1175 | break; |
| 1176 | } else if (key.offset <= start && extent_end > start) |
| 1177 | *length += extent_end - start; |
| 1178 | else if (key.offset > start && extent_end <= end) |
| 1179 | *length += extent_end - key.offset; |
| 1180 | else if (key.offset > start && key.offset <= end) { |
| 1181 | *length += end - key.offset + 1; |
| 1182 | break; |
| 1183 | } else if (key.offset > end) |
| 1184 | break; |
| 1185 | |
| 1186 | next: |
| 1187 | path->slots[0]++; |
| 1188 | } |
| 1189 | ret = 0; |
| 1190 | out: |
| 1191 | btrfs_free_path(path); |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1195 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1196 | struct btrfs_device *device, |
| 1197 | u64 *start, u64 len) |
| 1198 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1199 | struct btrfs_fs_info *fs_info = device->dev_root->fs_info; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1200 | struct extent_map *em; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1201 | struct list_head *search_list = &fs_info->pinned_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1202 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1203 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1204 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1205 | if (transaction) |
| 1206 | search_list = &transaction->pending_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1207 | again: |
| 1208 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1209 | struct map_lookup *map; |
| 1210 | int i; |
| 1211 | |
| 1212 | map = (struct map_lookup *)em->bdev; |
| 1213 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1214 | u64 end; |
| 1215 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1216 | if (map->stripes[i].dev != device) |
| 1217 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1218 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1219 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1220 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1221 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1222 | /* |
| 1223 | * Make sure that while processing the pinned list we do |
| 1224 | * not override our *start with a lower value, because |
| 1225 | * we can have pinned chunks that fall within this |
| 1226 | * device hole and that have lower physical addresses |
| 1227 | * than the pending chunks we processed before. If we |
| 1228 | * do not take this special care we can end up getting |
| 1229 | * 2 pending chunks that start at the same physical |
| 1230 | * device offsets because the end offset of a pinned |
| 1231 | * chunk can be equal to the start offset of some |
| 1232 | * pending chunk. |
| 1233 | */ |
| 1234 | end = map->stripes[i].physical + em->orig_block_len; |
| 1235 | if (end > *start) { |
| 1236 | *start = end; |
| 1237 | ret = 1; |
| 1238 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1239 | } |
| 1240 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1241 | if (search_list != &fs_info->pinned_chunks) { |
| 1242 | search_list = &fs_info->pinned_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1243 | goto again; |
| 1244 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1245 | |
| 1246 | return ret; |
| 1247 | } |
| 1248 | |
| 1249 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1250 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1251 | * find_free_dev_extent_start - find free space in the specified device |
| 1252 | * @device: the device which we search the free space in |
| 1253 | * @num_bytes: the size of the free space that we need |
| 1254 | * @search_start: the position from which to begin the search |
| 1255 | * @start: store the start of the free space. |
| 1256 | * @len: the size of the free space. that we find, or the size |
| 1257 | * 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] | 1258 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1259 | * this uses a pretty simple search, the expectation is that it is |
| 1260 | * called very infrequently and that a given device has a small number |
| 1261 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1262 | * |
| 1263 | * @start is used to store the start of the free space if we find. But if we |
| 1264 | * don't find suitable free space, it will be used to store the start position |
| 1265 | * of the max free space. |
| 1266 | * |
| 1267 | * @len is used to store the size of the free space that we find. |
| 1268 | * But if we don't find suitable free space, it is used to store the size of |
| 1269 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1270 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1271 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
| 1272 | struct btrfs_device *device, u64 num_bytes, |
| 1273 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1274 | { |
| 1275 | struct btrfs_key key; |
| 1276 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1277 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1278 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1279 | u64 hole_size; |
| 1280 | u64 max_hole_start; |
| 1281 | u64 max_hole_size; |
| 1282 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1283 | u64 search_end = device->total_bytes; |
| 1284 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1285 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1286 | struct extent_buffer *l; |
| 1287 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1288 | path = btrfs_alloc_path(); |
| 1289 | if (!path) |
| 1290 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1291 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1292 | max_hole_start = search_start; |
| 1293 | max_hole_size = 0; |
| 1294 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1295 | again: |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1296 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1297 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1298 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1301 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1302 | path->search_commit_root = 1; |
| 1303 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1304 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1305 | key.objectid = device->devid; |
| 1306 | key.offset = search_start; |
| 1307 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1308 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1309 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1310 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1311 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1312 | if (ret > 0) { |
| 1313 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1314 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1315 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1316 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1317 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1318 | while (1) { |
| 1319 | l = path->nodes[0]; |
| 1320 | slot = path->slots[0]; |
| 1321 | if (slot >= btrfs_header_nritems(l)) { |
| 1322 | ret = btrfs_next_leaf(root, path); |
| 1323 | if (ret == 0) |
| 1324 | continue; |
| 1325 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1326 | goto out; |
| 1327 | |
| 1328 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1329 | } |
| 1330 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1331 | |
| 1332 | if (key.objectid < device->devid) |
| 1333 | goto next; |
| 1334 | |
| 1335 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1336 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1337 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1338 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1339 | goto next; |
| 1340 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1341 | if (key.offset > search_start) { |
| 1342 | hole_size = key.offset - search_start; |
| 1343 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1344 | /* |
| 1345 | * Have to check before we set max_hole_start, otherwise |
| 1346 | * we could end up sending back this offset anyway. |
| 1347 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1348 | if (contains_pending_extent(transaction, device, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1349 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1350 | hole_size)) { |
| 1351 | if (key.offset >= search_start) { |
| 1352 | hole_size = key.offset - search_start; |
| 1353 | } else { |
| 1354 | WARN_ON_ONCE(1); |
| 1355 | hole_size = 0; |
| 1356 | } |
| 1357 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1358 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1359 | if (hole_size > max_hole_size) { |
| 1360 | max_hole_start = search_start; |
| 1361 | max_hole_size = hole_size; |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * If this free space is greater than which we need, |
| 1366 | * it must be the max free space that we have found |
| 1367 | * until now, so max_hole_start must point to the start |
| 1368 | * of this free space and the length of this free space |
| 1369 | * is stored in max_hole_size. Thus, we return |
| 1370 | * max_hole_start and max_hole_size and go back to the |
| 1371 | * caller. |
| 1372 | */ |
| 1373 | if (hole_size >= num_bytes) { |
| 1374 | ret = 0; |
| 1375 | goto out; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1379 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1380 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1381 | dev_extent); |
| 1382 | if (extent_end > search_start) |
| 1383 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1384 | next: |
| 1385 | path->slots[0]++; |
| 1386 | cond_resched(); |
| 1387 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1388 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1389 | /* |
| 1390 | * At this point, search_start should be the end of |
| 1391 | * allocated dev extents, and when shrinking the device, |
| 1392 | * search_end may be smaller than search_start. |
| 1393 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1394 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1395 | hole_size = search_end - search_start; |
| 1396 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1397 | if (contains_pending_extent(transaction, device, &search_start, |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1398 | hole_size)) { |
| 1399 | btrfs_release_path(path); |
| 1400 | goto again; |
| 1401 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1402 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1403 | if (hole_size > max_hole_size) { |
| 1404 | max_hole_start = search_start; |
| 1405 | max_hole_size = hole_size; |
| 1406 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1409 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1410 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1411 | ret = -ENOSPC; |
| 1412 | else |
| 1413 | ret = 0; |
| 1414 | |
| 1415 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1416 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1417 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1418 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1419 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1420 | return ret; |
| 1421 | } |
| 1422 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1423 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1424 | struct btrfs_device *device, u64 num_bytes, |
| 1425 | u64 *start, u64 *len) |
| 1426 | { |
| 1427 | struct btrfs_root *root = device->dev_root; |
| 1428 | u64 search_start; |
| 1429 | |
| 1430 | /* FIXME use last free of some kind */ |
| 1431 | |
| 1432 | /* |
| 1433 | * we don't want to overwrite the superblock on the drive, |
| 1434 | * so we make sure to start at an offset of at least 1MB |
| 1435 | */ |
| 1436 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
| 1437 | return find_free_dev_extent_start(trans->transaction, device, |
| 1438 | num_bytes, search_start, start, len); |
| 1439 | } |
| 1440 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1441 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1442 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1443 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1444 | { |
| 1445 | int ret; |
| 1446 | struct btrfs_path *path; |
| 1447 | struct btrfs_root *root = device->dev_root; |
| 1448 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1449 | struct btrfs_key found_key; |
| 1450 | struct extent_buffer *leaf = NULL; |
| 1451 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1452 | |
| 1453 | path = btrfs_alloc_path(); |
| 1454 | if (!path) |
| 1455 | return -ENOMEM; |
| 1456 | |
| 1457 | key.objectid = device->devid; |
| 1458 | key.offset = start; |
| 1459 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1460 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1461 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1462 | if (ret > 0) { |
| 1463 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1464 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1465 | if (ret) |
| 1466 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1467 | leaf = path->nodes[0]; |
| 1468 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1469 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1470 | struct btrfs_dev_extent); |
| 1471 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1472 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1473 | key = found_key; |
| 1474 | btrfs_release_path(path); |
| 1475 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1476 | } else if (ret == 0) { |
| 1477 | leaf = path->nodes[0]; |
| 1478 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1479 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1480 | } else { |
| 1481 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1482 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1483 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1484 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1485 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1486 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1487 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1488 | if (ret) { |
| 1489 | btrfs_error(root->fs_info, ret, |
| 1490 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1491 | } else { |
| 1492 | trans->transaction->have_free_bgs = 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1493 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1494 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1495 | btrfs_free_path(path); |
| 1496 | return ret; |
| 1497 | } |
| 1498 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1499 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1500 | struct btrfs_device *device, |
| 1501 | u64 chunk_tree, u64 chunk_objectid, |
| 1502 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1503 | { |
| 1504 | int ret; |
| 1505 | struct btrfs_path *path; |
| 1506 | struct btrfs_root *root = device->dev_root; |
| 1507 | struct btrfs_dev_extent *extent; |
| 1508 | struct extent_buffer *leaf; |
| 1509 | struct btrfs_key key; |
| 1510 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1511 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1512 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1513 | path = btrfs_alloc_path(); |
| 1514 | if (!path) |
| 1515 | return -ENOMEM; |
| 1516 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1517 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1518 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1519 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1520 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1521 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1522 | if (ret) |
| 1523 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1524 | |
| 1525 | leaf = path->nodes[0]; |
| 1526 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1527 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1528 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1529 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1530 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1531 | |
| 1532 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1533 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1534 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1535 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1536 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1537 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1538 | btrfs_free_path(path); |
| 1539 | return ret; |
| 1540 | } |
| 1541 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1542 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1543 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1544 | struct extent_map_tree *em_tree; |
| 1545 | struct extent_map *em; |
| 1546 | struct rb_node *n; |
| 1547 | u64 ret = 0; |
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 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1550 | read_lock(&em_tree->lock); |
| 1551 | n = rb_last(&em_tree->map); |
| 1552 | if (n) { |
| 1553 | em = rb_entry(n, struct extent_map, rb_node); |
| 1554 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1555 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1556 | read_unlock(&em_tree->lock); |
| 1557 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1558 | return ret; |
| 1559 | } |
| 1560 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1561 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1562 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1563 | { |
| 1564 | int ret; |
| 1565 | struct btrfs_key key; |
| 1566 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1567 | struct btrfs_path *path; |
| 1568 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1569 | path = btrfs_alloc_path(); |
| 1570 | if (!path) |
| 1571 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1572 | |
| 1573 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1574 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1575 | key.offset = (u64)-1; |
| 1576 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1577 | 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] | 1578 | if (ret < 0) |
| 1579 | goto error; |
| 1580 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1581 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1582 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1583 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1584 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1585 | BTRFS_DEV_ITEM_KEY); |
| 1586 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1587 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1588 | } else { |
| 1589 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1590 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1591 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1592 | } |
| 1593 | ret = 0; |
| 1594 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1595 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1596 | return ret; |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * the device information is stored in the chunk root |
| 1601 | * the btrfs_device struct should be fully filled in |
| 1602 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1603 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1604 | struct btrfs_root *root, |
| 1605 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1606 | { |
| 1607 | int ret; |
| 1608 | struct btrfs_path *path; |
| 1609 | struct btrfs_dev_item *dev_item; |
| 1610 | struct extent_buffer *leaf; |
| 1611 | struct btrfs_key key; |
| 1612 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1613 | |
| 1614 | root = root->fs_info->chunk_root; |
| 1615 | |
| 1616 | path = btrfs_alloc_path(); |
| 1617 | if (!path) |
| 1618 | return -ENOMEM; |
| 1619 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1620 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1621 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1622 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1623 | |
| 1624 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1625 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1626 | if (ret) |
| 1627 | goto out; |
| 1628 | |
| 1629 | leaf = path->nodes[0]; |
| 1630 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1631 | |
| 1632 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1633 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1634 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1635 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1636 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1637 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1638 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1639 | btrfs_device_get_disk_total_bytes(device)); |
| 1640 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1641 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1642 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1643 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1644 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1645 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1646 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1647 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1648 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1649 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1650 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1651 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1652 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1653 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1654 | out: |
| 1655 | btrfs_free_path(path); |
| 1656 | return ret; |
| 1657 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1658 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1659 | /* |
| 1660 | * Function to update ctime/mtime for a given device path. |
| 1661 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1662 | */ |
| 1663 | static void update_dev_time(char *path_name) |
| 1664 | { |
| 1665 | struct file *filp; |
| 1666 | |
| 1667 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1668 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1669 | return; |
| 1670 | file_update_time(filp); |
| 1671 | filp_close(filp, NULL); |
| 1672 | return; |
| 1673 | } |
| 1674 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1675 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1676 | struct btrfs_device *device) |
| 1677 | { |
| 1678 | int ret; |
| 1679 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1680 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1681 | struct btrfs_trans_handle *trans; |
| 1682 | |
| 1683 | root = root->fs_info->chunk_root; |
| 1684 | |
| 1685 | path = btrfs_alloc_path(); |
| 1686 | if (!path) |
| 1687 | return -ENOMEM; |
| 1688 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1689 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1690 | if (IS_ERR(trans)) { |
| 1691 | btrfs_free_path(path); |
| 1692 | return PTR_ERR(trans); |
| 1693 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1694 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1695 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1696 | key.offset = device->devid; |
| 1697 | |
| 1698 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1699 | if (ret < 0) |
| 1700 | goto out; |
| 1701 | |
| 1702 | if (ret > 0) { |
| 1703 | ret = -ENOENT; |
| 1704 | goto out; |
| 1705 | } |
| 1706 | |
| 1707 | ret = btrfs_del_item(trans, root, path); |
| 1708 | if (ret) |
| 1709 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1710 | out: |
| 1711 | btrfs_free_path(path); |
| 1712 | btrfs_commit_transaction(trans, root); |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1717 | { |
| 1718 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1719 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1720 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1721 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1722 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1723 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1724 | u64 all_avail; |
| 1725 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1726 | u64 num_devices; |
| 1727 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1728 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1729 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1730 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1731 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1732 | mutex_lock(&uuid_mutex); |
| 1733 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1734 | do { |
| 1735 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1736 | |
| 1737 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1738 | root->fs_info->avail_system_alloc_bits | |
| 1739 | root->fs_info->avail_metadata_alloc_bits; |
| 1740 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1741 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1742 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1743 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1744 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1745 | WARN_ON(num_devices < 1); |
| 1746 | num_devices--; |
| 1747 | } |
| 1748 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1749 | |
| 1750 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1751 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1752 | goto out; |
| 1753 | } |
| 1754 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1755 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1756 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1757 | goto out; |
| 1758 | } |
| 1759 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1760 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1761 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1762 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1763 | goto out; |
| 1764 | } |
| 1765 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1766 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1767 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1768 | goto out; |
| 1769 | } |
| 1770 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1771 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1772 | struct list_head *devices; |
| 1773 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1774 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1775 | device = NULL; |
| 1776 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1777 | /* |
| 1778 | * It is safe to read the devices since the volume_mutex |
| 1779 | * is held. |
| 1780 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1781 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1782 | if (tmp->in_fs_metadata && |
| 1783 | !tmp->is_tgtdev_for_dev_replace && |
| 1784 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1785 | device = tmp; |
| 1786 | break; |
| 1787 | } |
| 1788 | } |
| 1789 | bdev = NULL; |
| 1790 | bh = NULL; |
| 1791 | disk_super = NULL; |
| 1792 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1793 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1794 | goto out; |
| 1795 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1796 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1797 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1798 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1799 | root->fs_info->bdev_holder, 0, |
| 1800 | &bdev, &bh); |
| 1801 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1802 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1803 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1804 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1805 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1806 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1807 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1808 | if (!device) { |
| 1809 | ret = -ENOENT; |
| 1810 | goto error_brelse; |
| 1811 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1812 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1813 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1814 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1815 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1816 | goto error_brelse; |
| 1817 | } |
| 1818 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1819 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1820 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1821 | goto error_brelse; |
| 1822 | } |
| 1823 | |
| 1824 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1825 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1826 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1827 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1828 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1829 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1830 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1831 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1832 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1833 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1834 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1835 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1836 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1837 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1838 | /* |
| 1839 | * TODO: the superblock still includes this device in its num_devices |
| 1840 | * counter although write_all_supers() is not locked out. This |
| 1841 | * could give a filesystem state which requires a degraded mount. |
| 1842 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1843 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1844 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1845 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1846 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1847 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1848 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1849 | |
| 1850 | /* |
| 1851 | * the device list mutex makes sure that we don't change |
| 1852 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1853 | * the device supers. Whoever is writing all supers, should |
| 1854 | * lock the device list mutex before getting the number of |
| 1855 | * devices in the super block (super_copy). Conversely, |
| 1856 | * whoever updates the number of devices in the super block |
| 1857 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1858 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1859 | |
| 1860 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1861 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1862 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1863 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1864 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1865 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1866 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1867 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1868 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1869 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1870 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1871 | struct btrfs_device, dev_list); |
| 1872 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1873 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1874 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1875 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1876 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1877 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1878 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1879 | /* remove sysfs entry */ |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 1880 | btrfs_kobj_rm_device(root->fs_info->fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1881 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1882 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1883 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1884 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1885 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1886 | 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] | 1887 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1888 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1889 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1890 | struct btrfs_fs_devices *fs_devices; |
| 1891 | fs_devices = root->fs_info->fs_devices; |
| 1892 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1893 | if (fs_devices->seed == cur_devices) { |
| 1894 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1895 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1896 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1897 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1898 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1899 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1900 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1901 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1902 | } |
| 1903 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1904 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1905 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1906 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1907 | /* |
| 1908 | * at this point, the device is zero sized. We want to |
| 1909 | * remove it from the devices list and zero out the old super |
| 1910 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1911 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1912 | u64 bytenr; |
| 1913 | int i; |
| 1914 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1915 | /* make sure this device isn't detected as part of |
| 1916 | * the FS anymore |
| 1917 | */ |
| 1918 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1919 | set_buffer_dirty(bh); |
| 1920 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1921 | |
| 1922 | /* clear the mirror copies of super block on the disk |
| 1923 | * being removed, 0th copy is been taken care above and |
| 1924 | * the below would take of the rest |
| 1925 | */ |
| 1926 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1927 | bytenr = btrfs_sb_offset(i); |
| 1928 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1929 | i_size_read(bdev->bd_inode)) |
| 1930 | break; |
| 1931 | |
| 1932 | brelse(bh); |
| 1933 | bh = __bread(bdev, bytenr / 4096, |
| 1934 | BTRFS_SUPER_INFO_SIZE); |
| 1935 | if (!bh) |
| 1936 | continue; |
| 1937 | |
| 1938 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1939 | |
| 1940 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1941 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1942 | continue; |
| 1943 | } |
| 1944 | memset(&disk_super->magic, 0, |
| 1945 | sizeof(disk_super->magic)); |
| 1946 | set_buffer_dirty(bh); |
| 1947 | sync_dirty_buffer(bh); |
| 1948 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1949 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1950 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1951 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1952 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1953 | if (bdev) { |
| 1954 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1955 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1956 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1957 | /* Update ctime/mtime for device path for libblkid */ |
| 1958 | update_dev_time(device_path); |
| 1959 | } |
| 1960 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1961 | error_brelse: |
| 1962 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1963 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1964 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1965 | out: |
| 1966 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1967 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1968 | error_undo: |
| 1969 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1970 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1971 | list_add(&device->dev_alloc_list, |
| 1972 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1973 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1974 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1975 | } |
| 1976 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1977 | } |
| 1978 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1979 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1980 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1981 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1982 | struct btrfs_fs_devices *fs_devices; |
| 1983 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1984 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1985 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1986 | /* |
| 1987 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1988 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1989 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1990 | * srcdev will have its correct fs_devices in both the cases. |
| 1991 | */ |
| 1992 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1993 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1994 | list_del_rcu(&srcdev->dev_list); |
| 1995 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1996 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1997 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1998 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1999 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2000 | if (srcdev->writeable) { |
| 2001 | fs_devices->rw_devices--; |
| 2002 | /* zero out the old super if it is writable */ |
| 2003 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 2004 | } |
| 2005 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 2006 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2007 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 2011 | struct btrfs_device *srcdev) |
| 2012 | { |
| 2013 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2014 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2015 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2016 | |
| 2017 | /* |
| 2018 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 2019 | * zero |
| 2020 | */ |
| 2021 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 2022 | |
| 2023 | /* if this is no devs we rather delete the fs_devices */ |
| 2024 | if (!fs_devices->num_devices) { |
| 2025 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2026 | |
| 2027 | tmp_fs_devices = fs_info->fs_devices; |
| 2028 | while (tmp_fs_devices) { |
| 2029 | if (tmp_fs_devices->seed == fs_devices) { |
| 2030 | tmp_fs_devices->seed = fs_devices->seed; |
| 2031 | break; |
| 2032 | } |
| 2033 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2034 | } |
| 2035 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2036 | __btrfs_close_devices(fs_devices); |
| 2037 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2038 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 2042 | struct btrfs_device *tgtdev) |
| 2043 | { |
| 2044 | struct btrfs_device *next_device; |
| 2045 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 2046 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2047 | WARN_ON(!tgtdev); |
| 2048 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2049 | |
| 2050 | btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev); |
| 2051 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2052 | if (tgtdev->bdev) { |
| 2053 | btrfs_scratch_superblock(tgtdev); |
| 2054 | fs_info->fs_devices->open_devices--; |
| 2055 | } |
| 2056 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2057 | |
| 2058 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 2059 | struct btrfs_device, dev_list); |
| 2060 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 2061 | fs_info->sb->s_bdev = next_device->bdev; |
| 2062 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 2063 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 2064 | list_del_rcu(&tgtdev->dev_list); |
| 2065 | |
| 2066 | call_rcu(&tgtdev->rcu, free_device); |
| 2067 | |
| 2068 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 2069 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2070 | } |
| 2071 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2072 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 2073 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2074 | { |
| 2075 | int ret = 0; |
| 2076 | struct btrfs_super_block *disk_super; |
| 2077 | u64 devid; |
| 2078 | u8 *dev_uuid; |
| 2079 | struct block_device *bdev; |
| 2080 | struct buffer_head *bh; |
| 2081 | |
| 2082 | *device = NULL; |
| 2083 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 2084 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 2085 | if (ret) |
| 2086 | return ret; |
| 2087 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2088 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2089 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2090 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2091 | disk_super->fsid); |
| 2092 | brelse(bh); |
| 2093 | if (!*device) |
| 2094 | ret = -ENOENT; |
| 2095 | blkdev_put(bdev, FMODE_READ); |
| 2096 | return ret; |
| 2097 | } |
| 2098 | |
| 2099 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 2100 | char *device_path, |
| 2101 | struct btrfs_device **device) |
| 2102 | { |
| 2103 | *device = NULL; |
| 2104 | if (strcmp(device_path, "missing") == 0) { |
| 2105 | struct list_head *devices; |
| 2106 | struct btrfs_device *tmp; |
| 2107 | |
| 2108 | devices = &root->fs_info->fs_devices->devices; |
| 2109 | /* |
| 2110 | * It is safe to read the devices since the volume_mutex |
| 2111 | * is held by the caller. |
| 2112 | */ |
| 2113 | list_for_each_entry(tmp, devices, dev_list) { |
| 2114 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 2115 | *device = tmp; |
| 2116 | break; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2121 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2122 | return -ENOENT; |
| 2123 | } |
| 2124 | |
| 2125 | return 0; |
| 2126 | } else { |
| 2127 | return btrfs_find_device_by_path(root, device_path, device); |
| 2128 | } |
| 2129 | } |
| 2130 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2131 | /* |
| 2132 | * does all the dirty work required for changing file system's UUID. |
| 2133 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2134 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2135 | { |
| 2136 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2137 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2138 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2139 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2140 | struct btrfs_device *device; |
| 2141 | u64 super_flags; |
| 2142 | |
| 2143 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2144 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2145 | return -EINVAL; |
| 2146 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2147 | seed_devices = __alloc_fs_devices(); |
| 2148 | if (IS_ERR(seed_devices)) |
| 2149 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2150 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2151 | old_devices = clone_fs_devices(fs_devices); |
| 2152 | if (IS_ERR(old_devices)) { |
| 2153 | kfree(seed_devices); |
| 2154 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2155 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2156 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2157 | list_add(&old_devices->list, &fs_uuids); |
| 2158 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2159 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2160 | seed_devices->opened = 1; |
| 2161 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2162 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2163 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2164 | |
| 2165 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2166 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2167 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2168 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2169 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2170 | |
| 2171 | lock_chunks(root); |
| 2172 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 2173 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2174 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2175 | fs_devices->seeding = 0; |
| 2176 | fs_devices->num_devices = 0; |
| 2177 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2178 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2179 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2180 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2181 | |
| 2182 | generate_random_uuid(fs_devices->fsid); |
| 2183 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2184 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2185 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2186 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2187 | super_flags = btrfs_super_flags(disk_super) & |
| 2188 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2189 | btrfs_set_super_flags(disk_super, super_flags); |
| 2190 | |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * strore the expected generation for seed devices in device items. |
| 2196 | */ |
| 2197 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2198 | struct btrfs_root *root) |
| 2199 | { |
| 2200 | struct btrfs_path *path; |
| 2201 | struct extent_buffer *leaf; |
| 2202 | struct btrfs_dev_item *dev_item; |
| 2203 | struct btrfs_device *device; |
| 2204 | struct btrfs_key key; |
| 2205 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2206 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2207 | u64 devid; |
| 2208 | int ret; |
| 2209 | |
| 2210 | path = btrfs_alloc_path(); |
| 2211 | if (!path) |
| 2212 | return -ENOMEM; |
| 2213 | |
| 2214 | root = root->fs_info->chunk_root; |
| 2215 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2216 | key.offset = 0; |
| 2217 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2218 | |
| 2219 | while (1) { |
| 2220 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2221 | if (ret < 0) |
| 2222 | goto error; |
| 2223 | |
| 2224 | leaf = path->nodes[0]; |
| 2225 | next_slot: |
| 2226 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2227 | ret = btrfs_next_leaf(root, path); |
| 2228 | if (ret > 0) |
| 2229 | break; |
| 2230 | if (ret < 0) |
| 2231 | goto error; |
| 2232 | leaf = path->nodes[0]; |
| 2233 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2234 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2235 | continue; |
| 2236 | } |
| 2237 | |
| 2238 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2239 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2240 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2241 | break; |
| 2242 | |
| 2243 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2244 | struct btrfs_dev_item); |
| 2245 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2246 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2247 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2248 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2249 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2250 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2251 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2252 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2253 | |
| 2254 | if (device->fs_devices->seeding) { |
| 2255 | btrfs_set_device_generation(leaf, dev_item, |
| 2256 | device->generation); |
| 2257 | btrfs_mark_buffer_dirty(leaf); |
| 2258 | } |
| 2259 | |
| 2260 | path->slots[0]++; |
| 2261 | goto next_slot; |
| 2262 | } |
| 2263 | ret = 0; |
| 2264 | error: |
| 2265 | btrfs_free_path(path); |
| 2266 | return ret; |
| 2267 | } |
| 2268 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2269 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2270 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2271 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2272 | struct btrfs_trans_handle *trans; |
| 2273 | struct btrfs_device *device; |
| 2274 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2275 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2276 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2277 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2278 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2279 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2280 | int ret = 0; |
| 2281 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2282 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2283 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2284 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2285 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2286 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2287 | if (IS_ERR(bdev)) |
| 2288 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2289 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2290 | if (root->fs_info->fs_devices->seeding) { |
| 2291 | seeding_dev = 1; |
| 2292 | down_write(&sb->s_umount); |
| 2293 | mutex_lock(&uuid_mutex); |
| 2294 | } |
| 2295 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2296 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2297 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2298 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2299 | |
| 2300 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2301 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2302 | if (device->bdev == bdev) { |
| 2303 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2304 | mutex_unlock( |
| 2305 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2306 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2307 | } |
| 2308 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2309 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2310 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2311 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2312 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2313 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2314 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2315 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2316 | } |
| 2317 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2318 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2319 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2320 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2321 | ret = -ENOMEM; |
| 2322 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2323 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2324 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2325 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2326 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2327 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2328 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2329 | kfree(device); |
| 2330 | ret = PTR_ERR(trans); |
| 2331 | goto error; |
| 2332 | } |
| 2333 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2334 | q = bdev_get_queue(bdev); |
| 2335 | if (blk_queue_discard(q)) |
| 2336 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2337 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2338 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2339 | device->io_width = root->sectorsize; |
| 2340 | device->io_align = root->sectorsize; |
| 2341 | device->sector_size = root->sectorsize; |
| 2342 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2343 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2344 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2345 | device->dev_root = root->fs_info->dev_root; |
| 2346 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2347 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2348 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2349 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2350 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2351 | set_blocksize(device->bdev, 4096); |
| 2352 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2353 | if (seeding_dev) { |
| 2354 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2355 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2356 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2360 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2361 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2362 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2363 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2364 | list_add(&device->dev_alloc_list, |
| 2365 | &root->fs_info->fs_devices->alloc_list); |
| 2366 | root->fs_info->fs_devices->num_devices++; |
| 2367 | root->fs_info->fs_devices->open_devices++; |
| 2368 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2369 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2370 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2371 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2372 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2373 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2374 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2375 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2376 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2377 | root->fs_info->fs_devices->rotating = 1; |
| 2378 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2379 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2380 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2381 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2382 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2383 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2384 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2385 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2386 | |
| 2387 | /* add sysfs device entry */ |
Anand Jain | 1ba4381 | 2015-03-10 06:38:33 +0800 | [diff] [blame] | 2388 | btrfs_kobj_add_device(root->fs_info->fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2389 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2390 | /* |
| 2391 | * we've got more storage, clear any full flags on the space |
| 2392 | * infos |
| 2393 | */ |
| 2394 | btrfs_clear_space_info_full(root->fs_info); |
| 2395 | |
| 2396 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2397 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2398 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2399 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2400 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2401 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2402 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2403 | if (ret) { |
| 2404 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2405 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2406 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | ret = btrfs_add_device(trans, root, device); |
| 2410 | if (ret) { |
| 2411 | btrfs_abort_transaction(trans, root, ret); |
| 2412 | goto error_trans; |
| 2413 | } |
| 2414 | |
| 2415 | if (seeding_dev) { |
| 2416 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2417 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2418 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2419 | if (ret) { |
| 2420 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2421 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2422 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2423 | |
| 2424 | /* Sprouting would change fsid of the mounted root, |
| 2425 | * so rename the fsid on the sysfs |
| 2426 | */ |
| 2427 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2428 | root->fs_info->fsid); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 2429 | if (kobject_rename(&root->fs_info->fs_devices->super_kobj, |
| 2430 | fsid_buf)) |
Anand Jain | 2421a8c | 2015-03-10 06:38:40 +0800 | [diff] [blame] | 2431 | pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2432 | } |
| 2433 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2434 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2435 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2436 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2437 | |
| 2438 | if (seeding_dev) { |
| 2439 | mutex_unlock(&uuid_mutex); |
| 2440 | up_write(&sb->s_umount); |
| 2441 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2442 | if (ret) /* transaction commit */ |
| 2443 | return ret; |
| 2444 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2445 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2446 | if (ret < 0) |
| 2447 | btrfs_error(root->fs_info, ret, |
| 2448 | "Failed to relocate sys chunks after " |
| 2449 | "device initialization. This can be fixed " |
| 2450 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2451 | trans = btrfs_attach_transaction(root); |
| 2452 | if (IS_ERR(trans)) { |
| 2453 | if (PTR_ERR(trans) == -ENOENT) |
| 2454 | return 0; |
| 2455 | return PTR_ERR(trans); |
| 2456 | } |
| 2457 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2458 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2459 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2460 | /* Update ctime/mtime for libblkid */ |
| 2461 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2462 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2463 | |
| 2464 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2465 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2466 | rcu_string_free(device->name); |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 2467 | btrfs_kobj_rm_device(root->fs_info->fs_devices, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2468 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2469 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2470 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2471 | if (seeding_dev) { |
| 2472 | mutex_unlock(&uuid_mutex); |
| 2473 | up_write(&sb->s_umount); |
| 2474 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2475 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2476 | } |
| 2477 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2478 | 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] | 2479 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2480 | struct btrfs_device **device_out) |
| 2481 | { |
| 2482 | struct request_queue *q; |
| 2483 | struct btrfs_device *device; |
| 2484 | struct block_device *bdev; |
| 2485 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2486 | struct list_head *devices; |
| 2487 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2488 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2489 | int ret = 0; |
| 2490 | |
| 2491 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2492 | if (fs_info->fs_devices->seeding) { |
| 2493 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2494 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2495 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2496 | |
| 2497 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2498 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2499 | if (IS_ERR(bdev)) { |
| 2500 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2501 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2502 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2503 | |
| 2504 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2505 | |
| 2506 | devices = &fs_info->fs_devices->devices; |
| 2507 | list_for_each_entry(device, devices, dev_list) { |
| 2508 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2509 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2510 | ret = -EEXIST; |
| 2511 | goto error; |
| 2512 | } |
| 2513 | } |
| 2514 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2515 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2516 | if (i_size_read(bdev->bd_inode) < |
| 2517 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2518 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2519 | ret = -EINVAL; |
| 2520 | goto error; |
| 2521 | } |
| 2522 | |
| 2523 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2524 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2525 | if (IS_ERR(device)) { |
| 2526 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2527 | goto error; |
| 2528 | } |
| 2529 | |
| 2530 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2531 | if (!name) { |
| 2532 | kfree(device); |
| 2533 | ret = -ENOMEM; |
| 2534 | goto error; |
| 2535 | } |
| 2536 | rcu_assign_pointer(device->name, name); |
| 2537 | |
| 2538 | q = bdev_get_queue(bdev); |
| 2539 | if (blk_queue_discard(q)) |
| 2540 | device->can_discard = 1; |
| 2541 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2542 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2543 | device->generation = 0; |
| 2544 | device->io_width = root->sectorsize; |
| 2545 | device->io_align = root->sectorsize; |
| 2546 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2547 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2548 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2549 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2550 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2551 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2552 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2553 | device->dev_root = fs_info->dev_root; |
| 2554 | device->bdev = bdev; |
| 2555 | device->in_fs_metadata = 1; |
| 2556 | device->is_tgtdev_for_dev_replace = 1; |
| 2557 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2558 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2559 | set_blocksize(device->bdev, 4096); |
| 2560 | device->fs_devices = fs_info->fs_devices; |
| 2561 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2562 | fs_info->fs_devices->num_devices++; |
| 2563 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2564 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2565 | |
| 2566 | *device_out = device; |
| 2567 | return ret; |
| 2568 | |
| 2569 | error: |
| 2570 | blkdev_put(bdev, FMODE_EXCL); |
| 2571 | return ret; |
| 2572 | } |
| 2573 | |
| 2574 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2575 | struct btrfs_device *tgtdev) |
| 2576 | { |
| 2577 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2578 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2579 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2580 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2581 | tgtdev->dev_root = fs_info->dev_root; |
| 2582 | tgtdev->in_fs_metadata = 1; |
| 2583 | } |
| 2584 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2585 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2586 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2587 | { |
| 2588 | int ret; |
| 2589 | struct btrfs_path *path; |
| 2590 | struct btrfs_root *root; |
| 2591 | struct btrfs_dev_item *dev_item; |
| 2592 | struct extent_buffer *leaf; |
| 2593 | struct btrfs_key key; |
| 2594 | |
| 2595 | root = device->dev_root->fs_info->chunk_root; |
| 2596 | |
| 2597 | path = btrfs_alloc_path(); |
| 2598 | if (!path) |
| 2599 | return -ENOMEM; |
| 2600 | |
| 2601 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2602 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2603 | key.offset = device->devid; |
| 2604 | |
| 2605 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2606 | if (ret < 0) |
| 2607 | goto out; |
| 2608 | |
| 2609 | if (ret > 0) { |
| 2610 | ret = -ENOENT; |
| 2611 | goto out; |
| 2612 | } |
| 2613 | |
| 2614 | leaf = path->nodes[0]; |
| 2615 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2616 | |
| 2617 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2618 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2619 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2620 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2621 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2622 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2623 | btrfs_device_get_disk_total_bytes(device)); |
| 2624 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2625 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2626 | btrfs_mark_buffer_dirty(leaf); |
| 2627 | |
| 2628 | out: |
| 2629 | btrfs_free_path(path); |
| 2630 | return ret; |
| 2631 | } |
| 2632 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2633 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2634 | struct btrfs_device *device, u64 new_size) |
| 2635 | { |
| 2636 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2637 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2638 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2639 | u64 old_total; |
| 2640 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2641 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2642 | if (!device->writeable) |
| 2643 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2644 | |
| 2645 | lock_chunks(device->dev_root); |
| 2646 | old_total = btrfs_super_total_bytes(super_copy); |
| 2647 | diff = new_size - device->total_bytes; |
| 2648 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2649 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2650 | device->is_tgtdev_for_dev_replace) { |
| 2651 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2652 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2653 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2654 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2655 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2656 | |
| 2657 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2658 | device->fs_devices->total_rw_bytes += diff; |
| 2659 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2660 | btrfs_device_set_total_bytes(device, new_size); |
| 2661 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2662 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2663 | if (list_empty(&device->resized_list)) |
| 2664 | list_add_tail(&device->resized_list, |
| 2665 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2666 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2667 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2668 | return btrfs_update_device(trans, device); |
| 2669 | } |
| 2670 | |
| 2671 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2672 | struct btrfs_root *root, u64 chunk_objectid, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2673 | u64 chunk_offset) |
| 2674 | { |
| 2675 | int ret; |
| 2676 | struct btrfs_path *path; |
| 2677 | struct btrfs_key key; |
| 2678 | |
| 2679 | root = root->fs_info->chunk_root; |
| 2680 | path = btrfs_alloc_path(); |
| 2681 | if (!path) |
| 2682 | return -ENOMEM; |
| 2683 | |
| 2684 | key.objectid = chunk_objectid; |
| 2685 | key.offset = chunk_offset; |
| 2686 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2687 | |
| 2688 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2689 | if (ret < 0) |
| 2690 | goto out; |
| 2691 | else if (ret > 0) { /* Logic error or corruption */ |
| 2692 | btrfs_error(root->fs_info, -ENOENT, |
| 2693 | "Failed lookup while freeing chunk."); |
| 2694 | ret = -ENOENT; |
| 2695 | goto out; |
| 2696 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2697 | |
| 2698 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2699 | if (ret < 0) |
| 2700 | btrfs_error(root->fs_info, ret, |
| 2701 | "Failed to delete chunk item."); |
| 2702 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2703 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2704 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2705 | } |
| 2706 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2707 | 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] | 2708 | chunk_offset) |
| 2709 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2710 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2711 | struct btrfs_disk_key *disk_key; |
| 2712 | struct btrfs_chunk *chunk; |
| 2713 | u8 *ptr; |
| 2714 | int ret = 0; |
| 2715 | u32 num_stripes; |
| 2716 | u32 array_size; |
| 2717 | u32 len = 0; |
| 2718 | u32 cur; |
| 2719 | struct btrfs_key key; |
| 2720 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2721 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2722 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2723 | |
| 2724 | ptr = super_copy->sys_chunk_array; |
| 2725 | cur = 0; |
| 2726 | |
| 2727 | while (cur < array_size) { |
| 2728 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2729 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2730 | |
| 2731 | len = sizeof(*disk_key); |
| 2732 | |
| 2733 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2734 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2735 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2736 | len += btrfs_chunk_item_size(num_stripes); |
| 2737 | } else { |
| 2738 | ret = -EIO; |
| 2739 | break; |
| 2740 | } |
| 2741 | if (key.objectid == chunk_objectid && |
| 2742 | key.offset == chunk_offset) { |
| 2743 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2744 | array_size -= len; |
| 2745 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2746 | } else { |
| 2747 | ptr += len; |
| 2748 | cur += len; |
| 2749 | } |
| 2750 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2751 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2752 | return ret; |
| 2753 | } |
| 2754 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2755 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2756 | struct btrfs_root *root, u64 chunk_offset) |
| 2757 | { |
| 2758 | struct extent_map_tree *em_tree; |
| 2759 | struct extent_map *em; |
| 2760 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2761 | struct map_lookup *map; |
| 2762 | u64 dev_extent_len = 0; |
| 2763 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2764 | int i, ret = 0; |
| 2765 | |
| 2766 | /* Just in case */ |
| 2767 | root = root->fs_info->chunk_root; |
| 2768 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2769 | |
| 2770 | read_lock(&em_tree->lock); |
| 2771 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2772 | read_unlock(&em_tree->lock); |
| 2773 | |
| 2774 | if (!em || em->start > chunk_offset || |
| 2775 | em->start + em->len < chunk_offset) { |
| 2776 | /* |
| 2777 | * This is a logic error, but we don't want to just rely on the |
| 2778 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2779 | * do anything we still error out. |
| 2780 | */ |
| 2781 | ASSERT(0); |
| 2782 | if (em) |
| 2783 | free_extent_map(em); |
| 2784 | return -EINVAL; |
| 2785 | } |
| 2786 | map = (struct map_lookup *)em->bdev; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2787 | lock_chunks(root->fs_info->chunk_root); |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 2788 | check_system_chunk(trans, extent_root, map->type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2789 | unlock_chunks(root->fs_info->chunk_root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2790 | |
| 2791 | for (i = 0; i < map->num_stripes; i++) { |
| 2792 | struct btrfs_device *device = map->stripes[i].dev; |
| 2793 | ret = btrfs_free_dev_extent(trans, device, |
| 2794 | map->stripes[i].physical, |
| 2795 | &dev_extent_len); |
| 2796 | if (ret) { |
| 2797 | btrfs_abort_transaction(trans, root, ret); |
| 2798 | goto out; |
| 2799 | } |
| 2800 | |
| 2801 | if (device->bytes_used > 0) { |
| 2802 | lock_chunks(root); |
| 2803 | btrfs_device_set_bytes_used(device, |
| 2804 | device->bytes_used - dev_extent_len); |
| 2805 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2806 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2807 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2808 | btrfs_clear_space_info_full(root->fs_info); |
| 2809 | unlock_chunks(root); |
| 2810 | } |
| 2811 | |
| 2812 | if (map->stripes[i].dev) { |
| 2813 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2814 | if (ret) { |
| 2815 | btrfs_abort_transaction(trans, root, ret); |
| 2816 | goto out; |
| 2817 | } |
| 2818 | } |
| 2819 | } |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2820 | ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2821 | if (ret) { |
| 2822 | btrfs_abort_transaction(trans, root, ret); |
| 2823 | goto out; |
| 2824 | } |
| 2825 | |
| 2826 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2827 | |
| 2828 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2829 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2830 | if (ret) { |
| 2831 | btrfs_abort_transaction(trans, root, ret); |
| 2832 | goto out; |
| 2833 | } |
| 2834 | } |
| 2835 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2836 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2837 | if (ret) { |
| 2838 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2839 | goto out; |
| 2840 | } |
| 2841 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2842 | out: |
| 2843 | /* once for us */ |
| 2844 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2845 | return ret; |
| 2846 | } |
| 2847 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 2848 | static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2849 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2850 | struct btrfs_root *extent_root; |
| 2851 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2852 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2853 | |
| 2854 | root = root->fs_info->chunk_root; |
| 2855 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2856 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2857 | /* |
| 2858 | * Prevent races with automatic removal of unused block groups. |
| 2859 | * After we relocate and before we remove the chunk with offset |
| 2860 | * chunk_offset, automatic removal of the block group can kick in, |
| 2861 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2862 | * |
| 2863 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2864 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2865 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2866 | * we release the path used to search the chunk/dev tree and before |
| 2867 | * the current task acquires this mutex and calls us. |
| 2868 | */ |
| 2869 | ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex)); |
| 2870 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2871 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2872 | if (ret) |
| 2873 | return -ENOSPC; |
| 2874 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2875 | /* step one, relocate all the extents inside this chunk */ |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2876 | btrfs_scrub_pause(root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2877 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2878 | btrfs_scrub_continue(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2879 | if (ret) |
| 2880 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2881 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2882 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2883 | if (IS_ERR(trans)) { |
| 2884 | ret = PTR_ERR(trans); |
| 2885 | btrfs_std_error(root->fs_info, ret); |
| 2886 | return ret; |
| 2887 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2888 | |
| 2889 | /* |
| 2890 | * step two, delete the device extents and the |
| 2891 | * chunk tree entries |
| 2892 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2893 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2894 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2895 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2896 | } |
| 2897 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2898 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2899 | { |
| 2900 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2901 | struct btrfs_path *path; |
| 2902 | struct extent_buffer *leaf; |
| 2903 | struct btrfs_chunk *chunk; |
| 2904 | struct btrfs_key key; |
| 2905 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2906 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2907 | bool retried = false; |
| 2908 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2909 | int ret; |
| 2910 | |
| 2911 | path = btrfs_alloc_path(); |
| 2912 | if (!path) |
| 2913 | return -ENOMEM; |
| 2914 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2915 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2916 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2917 | key.offset = (u64)-1; |
| 2918 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2919 | |
| 2920 | while (1) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2921 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2922 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2923 | if (ret < 0) { |
| 2924 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2925 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2926 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2927 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2928 | |
| 2929 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2930 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2931 | if (ret) |
| 2932 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2933 | if (ret < 0) |
| 2934 | goto error; |
| 2935 | if (ret > 0) |
| 2936 | break; |
| 2937 | |
| 2938 | leaf = path->nodes[0]; |
| 2939 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2940 | |
| 2941 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2942 | struct btrfs_chunk); |
| 2943 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2944 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2945 | |
| 2946 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2947 | ret = btrfs_relocate_chunk(chunk_root, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2948 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2949 | if (ret == -ENOSPC) |
| 2950 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2951 | else |
| 2952 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2953 | } |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2954 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2955 | |
| 2956 | if (found_key.offset == 0) |
| 2957 | break; |
| 2958 | key.offset = found_key.offset - 1; |
| 2959 | } |
| 2960 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2961 | if (failed && !retried) { |
| 2962 | failed = 0; |
| 2963 | retried = true; |
| 2964 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2965 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2966 | ret = -ENOSPC; |
| 2967 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2968 | error: |
| 2969 | btrfs_free_path(path); |
| 2970 | return ret; |
| 2971 | } |
| 2972 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2973 | static int insert_balance_item(struct btrfs_root *root, |
| 2974 | struct btrfs_balance_control *bctl) |
| 2975 | { |
| 2976 | struct btrfs_trans_handle *trans; |
| 2977 | struct btrfs_balance_item *item; |
| 2978 | struct btrfs_disk_balance_args disk_bargs; |
| 2979 | struct btrfs_path *path; |
| 2980 | struct extent_buffer *leaf; |
| 2981 | struct btrfs_key key; |
| 2982 | int ret, err; |
| 2983 | |
| 2984 | path = btrfs_alloc_path(); |
| 2985 | if (!path) |
| 2986 | return -ENOMEM; |
| 2987 | |
| 2988 | trans = btrfs_start_transaction(root, 0); |
| 2989 | if (IS_ERR(trans)) { |
| 2990 | btrfs_free_path(path); |
| 2991 | return PTR_ERR(trans); |
| 2992 | } |
| 2993 | |
| 2994 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2995 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2996 | key.offset = 0; |
| 2997 | |
| 2998 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2999 | sizeof(*item)); |
| 3000 | if (ret) |
| 3001 | goto out; |
| 3002 | |
| 3003 | leaf = path->nodes[0]; |
| 3004 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3005 | |
| 3006 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 3007 | |
| 3008 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3009 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3010 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3011 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3012 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3013 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3014 | |
| 3015 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3016 | |
| 3017 | btrfs_mark_buffer_dirty(leaf); |
| 3018 | out: |
| 3019 | btrfs_free_path(path); |
| 3020 | err = btrfs_commit_transaction(trans, root); |
| 3021 | if (err && !ret) |
| 3022 | ret = err; |
| 3023 | return ret; |
| 3024 | } |
| 3025 | |
| 3026 | static int del_balance_item(struct btrfs_root *root) |
| 3027 | { |
| 3028 | struct btrfs_trans_handle *trans; |
| 3029 | struct btrfs_path *path; |
| 3030 | struct btrfs_key key; |
| 3031 | int ret, err; |
| 3032 | |
| 3033 | path = btrfs_alloc_path(); |
| 3034 | if (!path) |
| 3035 | return -ENOMEM; |
| 3036 | |
| 3037 | trans = btrfs_start_transaction(root, 0); |
| 3038 | if (IS_ERR(trans)) { |
| 3039 | btrfs_free_path(path); |
| 3040 | return PTR_ERR(trans); |
| 3041 | } |
| 3042 | |
| 3043 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3044 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3045 | key.offset = 0; |
| 3046 | |
| 3047 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3048 | if (ret < 0) |
| 3049 | goto out; |
| 3050 | if (ret > 0) { |
| 3051 | ret = -ENOENT; |
| 3052 | goto out; |
| 3053 | } |
| 3054 | |
| 3055 | ret = btrfs_del_item(trans, root, path); |
| 3056 | out: |
| 3057 | btrfs_free_path(path); |
| 3058 | err = btrfs_commit_transaction(trans, root); |
| 3059 | if (err && !ret) |
| 3060 | ret = err; |
| 3061 | return ret; |
| 3062 | } |
| 3063 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3064 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3065 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3066 | * resume after balance was interrupted. |
| 3067 | */ |
| 3068 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3069 | { |
| 3070 | /* |
| 3071 | * Turn on soft mode for chunk types that were being converted. |
| 3072 | */ |
| 3073 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3074 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3075 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3076 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3077 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3078 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3079 | |
| 3080 | /* |
| 3081 | * Turn on usage filter if is not already used. The idea is |
| 3082 | * that chunks that we have already balanced should be |
| 3083 | * reasonably full. Don't do it for chunks that are being |
| 3084 | * converted - that will keep us from relocating unconverted |
| 3085 | * (albeit full) chunks. |
| 3086 | */ |
| 3087 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3088 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3089 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3090 | bctl->data.usage = 90; |
| 3091 | } |
| 3092 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3093 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3094 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3095 | bctl->sys.usage = 90; |
| 3096 | } |
| 3097 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3098 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3099 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3100 | bctl->meta.usage = 90; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3105 | * Should be called with both balance and volume mutexes held to |
| 3106 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 3107 | * restriper. Same goes for unset_balance_control. |
| 3108 | */ |
| 3109 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 3110 | { |
| 3111 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 3112 | |
| 3113 | BUG_ON(fs_info->balance_ctl); |
| 3114 | |
| 3115 | spin_lock(&fs_info->balance_lock); |
| 3116 | fs_info->balance_ctl = bctl; |
| 3117 | spin_unlock(&fs_info->balance_lock); |
| 3118 | } |
| 3119 | |
| 3120 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 3121 | { |
| 3122 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3123 | |
| 3124 | BUG_ON(!fs_info->balance_ctl); |
| 3125 | |
| 3126 | spin_lock(&fs_info->balance_lock); |
| 3127 | fs_info->balance_ctl = NULL; |
| 3128 | spin_unlock(&fs_info->balance_lock); |
| 3129 | |
| 3130 | kfree(bctl); |
| 3131 | } |
| 3132 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3133 | /* |
| 3134 | * Balance filters. Return 1 if chunk should be filtered out |
| 3135 | * (should not be balanced). |
| 3136 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3137 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3138 | struct btrfs_balance_args *bargs) |
| 3139 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3140 | chunk_type = chunk_to_extended(chunk_type) & |
| 3141 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3142 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3143 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3144 | return 0; |
| 3145 | |
| 3146 | return 1; |
| 3147 | } |
| 3148 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3149 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 3150 | struct btrfs_balance_args *bargs) |
| 3151 | { |
| 3152 | struct btrfs_block_group_cache *cache; |
| 3153 | u64 chunk_used, user_thresh; |
| 3154 | int ret = 1; |
| 3155 | |
| 3156 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3157 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3158 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3159 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3160 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3161 | else if (bargs->usage > 100) |
| 3162 | user_thresh = cache->key.offset; |
| 3163 | else |
| 3164 | user_thresh = div_factor_fine(cache->key.offset, |
| 3165 | bargs->usage); |
| 3166 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3167 | if (chunk_used < user_thresh) |
| 3168 | ret = 0; |
| 3169 | |
| 3170 | btrfs_put_block_group(cache); |
| 3171 | return ret; |
| 3172 | } |
| 3173 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3174 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3175 | struct btrfs_chunk *chunk, |
| 3176 | struct btrfs_balance_args *bargs) |
| 3177 | { |
| 3178 | struct btrfs_stripe *stripe; |
| 3179 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3180 | int i; |
| 3181 | |
| 3182 | for (i = 0; i < num_stripes; i++) { |
| 3183 | stripe = btrfs_stripe_nr(chunk, i); |
| 3184 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
| 3188 | return 1; |
| 3189 | } |
| 3190 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3191 | /* [pstart, pend) */ |
| 3192 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3193 | struct btrfs_chunk *chunk, |
| 3194 | u64 chunk_offset, |
| 3195 | struct btrfs_balance_args *bargs) |
| 3196 | { |
| 3197 | struct btrfs_stripe *stripe; |
| 3198 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3199 | u64 stripe_offset; |
| 3200 | u64 stripe_length; |
| 3201 | int factor; |
| 3202 | int i; |
| 3203 | |
| 3204 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3205 | return 0; |
| 3206 | |
| 3207 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3208 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3209 | factor = num_stripes / 2; |
| 3210 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3211 | factor = num_stripes - 1; |
| 3212 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3213 | factor = num_stripes - 2; |
| 3214 | } else { |
| 3215 | factor = num_stripes; |
| 3216 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3217 | |
| 3218 | for (i = 0; i < num_stripes; i++) { |
| 3219 | stripe = btrfs_stripe_nr(chunk, i); |
| 3220 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3221 | continue; |
| 3222 | |
| 3223 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3224 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3225 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3226 | |
| 3227 | if (stripe_offset < bargs->pend && |
| 3228 | stripe_offset + stripe_length > bargs->pstart) |
| 3229 | return 0; |
| 3230 | } |
| 3231 | |
| 3232 | return 1; |
| 3233 | } |
| 3234 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3235 | /* [vstart, vend) */ |
| 3236 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3237 | struct btrfs_chunk *chunk, |
| 3238 | u64 chunk_offset, |
| 3239 | struct btrfs_balance_args *bargs) |
| 3240 | { |
| 3241 | if (chunk_offset < bargs->vend && |
| 3242 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3243 | /* at least part of the chunk is inside this vrange */ |
| 3244 | return 0; |
| 3245 | |
| 3246 | return 1; |
| 3247 | } |
| 3248 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3249 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3250 | struct btrfs_balance_args *bargs) |
| 3251 | { |
| 3252 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3253 | return 0; |
| 3254 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3255 | chunk_type = chunk_to_extended(chunk_type) & |
| 3256 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3257 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3258 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3259 | return 1; |
| 3260 | |
| 3261 | return 0; |
| 3262 | } |
| 3263 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3264 | static int should_balance_chunk(struct btrfs_root *root, |
| 3265 | struct extent_buffer *leaf, |
| 3266 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3267 | { |
| 3268 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3269 | struct btrfs_balance_args *bargs = NULL; |
| 3270 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3271 | |
| 3272 | /* type filter */ |
| 3273 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3274 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3275 | return 0; |
| 3276 | } |
| 3277 | |
| 3278 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3279 | bargs = &bctl->data; |
| 3280 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3281 | bargs = &bctl->sys; |
| 3282 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3283 | bargs = &bctl->meta; |
| 3284 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3285 | /* profiles filter */ |
| 3286 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3287 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3288 | return 0; |
| 3289 | } |
| 3290 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3291 | /* usage filter */ |
| 3292 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3293 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3294 | return 0; |
| 3295 | } |
| 3296 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3297 | /* devid filter */ |
| 3298 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3299 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3300 | return 0; |
| 3301 | } |
| 3302 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3303 | /* drange filter, makes sense only with devid filter */ |
| 3304 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3305 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3306 | return 0; |
| 3307 | } |
| 3308 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3309 | /* vrange filter */ |
| 3310 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3311 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3312 | return 0; |
| 3313 | } |
| 3314 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3315 | /* soft profile changing mode */ |
| 3316 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3317 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3318 | return 0; |
| 3319 | } |
| 3320 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3321 | /* |
| 3322 | * limited by count, must be the last filter |
| 3323 | */ |
| 3324 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3325 | if (bargs->limit == 0) |
| 3326 | return 0; |
| 3327 | else |
| 3328 | bargs->limit--; |
| 3329 | } |
| 3330 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3331 | return 1; |
| 3332 | } |
| 3333 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3334 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3335 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3336 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3337 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3338 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3339 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3340 | struct btrfs_device *device; |
| 3341 | u64 old_size; |
| 3342 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3343 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3344 | struct btrfs_path *path; |
| 3345 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3346 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3347 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3348 | struct extent_buffer *leaf; |
| 3349 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3350 | int ret; |
| 3351 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3352 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3353 | u64 limit_data = bctl->data.limit; |
| 3354 | u64 limit_meta = bctl->meta.limit; |
| 3355 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3356 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3357 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3358 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3359 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3360 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3361 | size_to_free = div_factor(old_size, 1); |
| 3362 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3363 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3364 | btrfs_device_get_total_bytes(device) - |
| 3365 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3366 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3367 | continue; |
| 3368 | |
| 3369 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3370 | if (ret == -ENOSPC) |
| 3371 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3372 | BUG_ON(ret); |
| 3373 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3374 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3375 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3376 | |
| 3377 | ret = btrfs_grow_device(trans, device, old_size); |
| 3378 | BUG_ON(ret); |
| 3379 | |
| 3380 | btrfs_end_transaction(trans, dev_root); |
| 3381 | } |
| 3382 | |
| 3383 | /* step two, relocate all the chunks */ |
| 3384 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3385 | if (!path) { |
| 3386 | ret = -ENOMEM; |
| 3387 | goto error; |
| 3388 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3389 | |
| 3390 | /* zero out stat counters */ |
| 3391 | spin_lock(&fs_info->balance_lock); |
| 3392 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3393 | spin_unlock(&fs_info->balance_lock); |
| 3394 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3395 | if (!counting) { |
| 3396 | bctl->data.limit = limit_data; |
| 3397 | bctl->meta.limit = limit_meta; |
| 3398 | bctl->sys.limit = limit_sys; |
| 3399 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3400 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3401 | key.offset = (u64)-1; |
| 3402 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3403 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3404 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3405 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3406 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3407 | ret = -ECANCELED; |
| 3408 | goto error; |
| 3409 | } |
| 3410 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3411 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3412 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3413 | if (ret < 0) { |
| 3414 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3415 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3416 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3417 | |
| 3418 | /* |
| 3419 | * this shouldn't happen, it means the last relocate |
| 3420 | * failed |
| 3421 | */ |
| 3422 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3423 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3424 | |
| 3425 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3426 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3427 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3428 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3429 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3430 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3431 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3432 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3433 | leaf = path->nodes[0]; |
| 3434 | slot = path->slots[0]; |
| 3435 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3436 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3437 | if (found_key.objectid != key.objectid) { |
| 3438 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3439 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3440 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3441 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3442 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3443 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3444 | if (!counting) { |
| 3445 | spin_lock(&fs_info->balance_lock); |
| 3446 | bctl->stat.considered++; |
| 3447 | spin_unlock(&fs_info->balance_lock); |
| 3448 | } |
| 3449 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3450 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3451 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3452 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3453 | if (!ret) { |
| 3454 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3455 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3456 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3457 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3458 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3459 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3460 | spin_lock(&fs_info->balance_lock); |
| 3461 | bctl->stat.expected++; |
| 3462 | spin_unlock(&fs_info->balance_lock); |
| 3463 | goto loop; |
| 3464 | } |
| 3465 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3466 | ret = btrfs_relocate_chunk(chunk_root, |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3467 | found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3468 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3469 | if (ret && ret != -ENOSPC) |
| 3470 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3471 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3472 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3473 | } else { |
| 3474 | spin_lock(&fs_info->balance_lock); |
| 3475 | bctl->stat.completed++; |
| 3476 | spin_unlock(&fs_info->balance_lock); |
| 3477 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3478 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3479 | if (found_key.offset == 0) |
| 3480 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3481 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3482 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3483 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3484 | if (counting) { |
| 3485 | btrfs_release_path(path); |
| 3486 | counting = false; |
| 3487 | goto again; |
| 3488 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3489 | error: |
| 3490 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3491 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3492 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3493 | enospc_errors); |
| 3494 | if (!ret) |
| 3495 | ret = -ENOSPC; |
| 3496 | } |
| 3497 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3498 | return ret; |
| 3499 | } |
| 3500 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3501 | /** |
| 3502 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3503 | * @flags: profile to validate |
| 3504 | * @extended: if true @flags is treated as an extended profile |
| 3505 | */ |
| 3506 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3507 | { |
| 3508 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3509 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3510 | |
| 3511 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3512 | |
| 3513 | /* 1) check that all other bits are zeroed */ |
| 3514 | if (flags & ~mask) |
| 3515 | return 0; |
| 3516 | |
| 3517 | /* 2) see if profile is reduced */ |
| 3518 | if (flags == 0) |
| 3519 | return !extended; /* "0" is valid for usual profiles */ |
| 3520 | |
| 3521 | /* true if exactly one bit set */ |
| 3522 | return (flags & (flags - 1)) == 0; |
| 3523 | } |
| 3524 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3525 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3526 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3527 | /* cancel requested || normal exit path */ |
| 3528 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3529 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3530 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3531 | } |
| 3532 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3533 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3534 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3535 | int ret; |
| 3536 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3537 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3538 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3539 | if (ret) |
| 3540 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3541 | |
| 3542 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3543 | } |
| 3544 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3545 | /* |
| 3546 | * Should be called with both balance and volume mutexes held |
| 3547 | */ |
| 3548 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3549 | struct btrfs_ioctl_balance_args *bargs) |
| 3550 | { |
| 3551 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3552 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3553 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3554 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3555 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3556 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3557 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3558 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3559 | atomic_read(&fs_info->balance_pause_req) || |
| 3560 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3561 | ret = -EINVAL; |
| 3562 | goto out; |
| 3563 | } |
| 3564 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3565 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3566 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3567 | mixed = 1; |
| 3568 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3569 | /* |
| 3570 | * In case of mixed groups both data and meta should be picked, |
| 3571 | * and identical options should be given for both of them. |
| 3572 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3573 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3574 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3575 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3576 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3577 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3578 | btrfs_err(fs_info, "with mixed groups data and " |
| 3579 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3580 | ret = -EINVAL; |
| 3581 | goto out; |
| 3582 | } |
| 3583 | } |
| 3584 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3585 | num_devices = fs_info->fs_devices->num_devices; |
| 3586 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3587 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3588 | BUG_ON(num_devices < 1); |
| 3589 | num_devices--; |
| 3590 | } |
| 3591 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3592 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3593 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3594 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3595 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3596 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3597 | if (num_devices > 2) |
| 3598 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3599 | if (num_devices > 3) |
| 3600 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3601 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3602 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3603 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3604 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3605 | btrfs_err(fs_info, "unable to start balance with target " |
| 3606 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3607 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3608 | ret = -EINVAL; |
| 3609 | goto out; |
| 3610 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3611 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3612 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3613 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3614 | btrfs_err(fs_info, |
| 3615 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3616 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3617 | ret = -EINVAL; |
| 3618 | goto out; |
| 3619 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3620 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3621 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3622 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3623 | btrfs_err(fs_info, |
| 3624 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3625 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3626 | ret = -EINVAL; |
| 3627 | goto out; |
| 3628 | } |
| 3629 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3630 | /* allow dup'ed data chunks only in mixed mode */ |
| 3631 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3632 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3633 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3634 | ret = -EINVAL; |
| 3635 | goto out; |
| 3636 | } |
| 3637 | |
| 3638 | /* allow to reduce meta or sys integrity only if force set */ |
| 3639 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3640 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3641 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3642 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3643 | do { |
| 3644 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3645 | |
| 3646 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3647 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3648 | !(bctl->sys.target & allowed)) || |
| 3649 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3650 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3651 | !(bctl->meta.target & allowed))) { |
| 3652 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3653 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3654 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3655 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3656 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3657 | ret = -EINVAL; |
| 3658 | goto out; |
| 3659 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3660 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3661 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3662 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3663 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
Zhao Lei | 943c6e9 | 2015-08-19 15:54:15 +0800 | [diff] [blame] | 3664 | fs_info->num_tolerated_disk_barrier_failures = min( |
| 3665 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info), |
| 3666 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 3667 | bctl->sys.target)); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3668 | } |
| 3669 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3670 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3671 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3672 | goto out; |
| 3673 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3674 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3675 | BUG_ON(ret == -EEXIST); |
| 3676 | set_balance_control(bctl); |
| 3677 | } else { |
| 3678 | BUG_ON(ret != -EEXIST); |
| 3679 | spin_lock(&fs_info->balance_lock); |
| 3680 | update_balance_args(bctl); |
| 3681 | spin_unlock(&fs_info->balance_lock); |
| 3682 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3683 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3684 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3685 | mutex_unlock(&fs_info->balance_mutex); |
| 3686 | |
| 3687 | ret = __btrfs_balance(fs_info); |
| 3688 | |
| 3689 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3690 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3691 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3692 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3693 | fs_info->num_tolerated_disk_barrier_failures = |
| 3694 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3695 | } |
| 3696 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3697 | if (bargs) { |
| 3698 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3699 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3700 | } |
| 3701 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3702 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3703 | balance_need_close(fs_info)) { |
| 3704 | __cancel_balance(fs_info); |
| 3705 | } |
| 3706 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3707 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3708 | |
| 3709 | return ret; |
| 3710 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3711 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3712 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3713 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3714 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3715 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3716 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3717 | return ret; |
| 3718 | } |
| 3719 | |
| 3720 | static int balance_kthread(void *data) |
| 3721 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3722 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3723 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3724 | |
| 3725 | mutex_lock(&fs_info->volume_mutex); |
| 3726 | mutex_lock(&fs_info->balance_mutex); |
| 3727 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3728 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3729 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3730 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3731 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3732 | |
| 3733 | mutex_unlock(&fs_info->balance_mutex); |
| 3734 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3735 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3736 | return ret; |
| 3737 | } |
| 3738 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3739 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3740 | { |
| 3741 | struct task_struct *tsk; |
| 3742 | |
| 3743 | spin_lock(&fs_info->balance_lock); |
| 3744 | if (!fs_info->balance_ctl) { |
| 3745 | spin_unlock(&fs_info->balance_lock); |
| 3746 | return 0; |
| 3747 | } |
| 3748 | spin_unlock(&fs_info->balance_lock); |
| 3749 | |
| 3750 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3751 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3752 | return 0; |
| 3753 | } |
| 3754 | |
| 3755 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3756 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3757 | } |
| 3758 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3759 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3760 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3761 | struct btrfs_balance_control *bctl; |
| 3762 | struct btrfs_balance_item *item; |
| 3763 | struct btrfs_disk_balance_args disk_bargs; |
| 3764 | struct btrfs_path *path; |
| 3765 | struct extent_buffer *leaf; |
| 3766 | struct btrfs_key key; |
| 3767 | int ret; |
| 3768 | |
| 3769 | path = btrfs_alloc_path(); |
| 3770 | if (!path) |
| 3771 | return -ENOMEM; |
| 3772 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3773 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3774 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3775 | key.offset = 0; |
| 3776 | |
| 3777 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3778 | if (ret < 0) |
| 3779 | goto out; |
| 3780 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3781 | ret = 0; |
| 3782 | goto out; |
| 3783 | } |
| 3784 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3785 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3786 | if (!bctl) { |
| 3787 | ret = -ENOMEM; |
| 3788 | goto out; |
| 3789 | } |
| 3790 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3791 | leaf = path->nodes[0]; |
| 3792 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3793 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3794 | bctl->fs_info = fs_info; |
| 3795 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3796 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3797 | |
| 3798 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3799 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3800 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3801 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3802 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3803 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3804 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3805 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3806 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3807 | mutex_lock(&fs_info->volume_mutex); |
| 3808 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3809 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3810 | set_balance_control(bctl); |
| 3811 | |
| 3812 | mutex_unlock(&fs_info->balance_mutex); |
| 3813 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3814 | out: |
| 3815 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3816 | return ret; |
| 3817 | } |
| 3818 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3819 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3820 | { |
| 3821 | int ret = 0; |
| 3822 | |
| 3823 | mutex_lock(&fs_info->balance_mutex); |
| 3824 | if (!fs_info->balance_ctl) { |
| 3825 | mutex_unlock(&fs_info->balance_mutex); |
| 3826 | return -ENOTCONN; |
| 3827 | } |
| 3828 | |
| 3829 | if (atomic_read(&fs_info->balance_running)) { |
| 3830 | atomic_inc(&fs_info->balance_pause_req); |
| 3831 | mutex_unlock(&fs_info->balance_mutex); |
| 3832 | |
| 3833 | wait_event(fs_info->balance_wait_q, |
| 3834 | atomic_read(&fs_info->balance_running) == 0); |
| 3835 | |
| 3836 | mutex_lock(&fs_info->balance_mutex); |
| 3837 | /* we are good with balance_ctl ripped off from under us */ |
| 3838 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3839 | atomic_dec(&fs_info->balance_pause_req); |
| 3840 | } else { |
| 3841 | ret = -ENOTCONN; |
| 3842 | } |
| 3843 | |
| 3844 | mutex_unlock(&fs_info->balance_mutex); |
| 3845 | return ret; |
| 3846 | } |
| 3847 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3848 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3849 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3850 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3851 | return -EROFS; |
| 3852 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3853 | mutex_lock(&fs_info->balance_mutex); |
| 3854 | if (!fs_info->balance_ctl) { |
| 3855 | mutex_unlock(&fs_info->balance_mutex); |
| 3856 | return -ENOTCONN; |
| 3857 | } |
| 3858 | |
| 3859 | atomic_inc(&fs_info->balance_cancel_req); |
| 3860 | /* |
| 3861 | * if we are running just wait and return, balance item is |
| 3862 | * deleted in btrfs_balance in this case |
| 3863 | */ |
| 3864 | if (atomic_read(&fs_info->balance_running)) { |
| 3865 | mutex_unlock(&fs_info->balance_mutex); |
| 3866 | wait_event(fs_info->balance_wait_q, |
| 3867 | atomic_read(&fs_info->balance_running) == 0); |
| 3868 | mutex_lock(&fs_info->balance_mutex); |
| 3869 | } else { |
| 3870 | /* __cancel_balance needs volume_mutex */ |
| 3871 | mutex_unlock(&fs_info->balance_mutex); |
| 3872 | mutex_lock(&fs_info->volume_mutex); |
| 3873 | mutex_lock(&fs_info->balance_mutex); |
| 3874 | |
| 3875 | if (fs_info->balance_ctl) |
| 3876 | __cancel_balance(fs_info); |
| 3877 | |
| 3878 | mutex_unlock(&fs_info->volume_mutex); |
| 3879 | } |
| 3880 | |
| 3881 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3882 | atomic_dec(&fs_info->balance_cancel_req); |
| 3883 | mutex_unlock(&fs_info->balance_mutex); |
| 3884 | return 0; |
| 3885 | } |
| 3886 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3887 | static int btrfs_uuid_scan_kthread(void *data) |
| 3888 | { |
| 3889 | struct btrfs_fs_info *fs_info = data; |
| 3890 | struct btrfs_root *root = fs_info->tree_root; |
| 3891 | struct btrfs_key key; |
| 3892 | struct btrfs_key max_key; |
| 3893 | struct btrfs_path *path = NULL; |
| 3894 | int ret = 0; |
| 3895 | struct extent_buffer *eb; |
| 3896 | int slot; |
| 3897 | struct btrfs_root_item root_item; |
| 3898 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3899 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3900 | |
| 3901 | path = btrfs_alloc_path(); |
| 3902 | if (!path) { |
| 3903 | ret = -ENOMEM; |
| 3904 | goto out; |
| 3905 | } |
| 3906 | |
| 3907 | key.objectid = 0; |
| 3908 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3909 | key.offset = 0; |
| 3910 | |
| 3911 | max_key.objectid = (u64)-1; |
| 3912 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3913 | max_key.offset = (u64)-1; |
| 3914 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3915 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3916 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3917 | if (ret) { |
| 3918 | if (ret > 0) |
| 3919 | ret = 0; |
| 3920 | break; |
| 3921 | } |
| 3922 | |
| 3923 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3924 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3925 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3926 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3927 | goto skip; |
| 3928 | |
| 3929 | eb = path->nodes[0]; |
| 3930 | slot = path->slots[0]; |
| 3931 | item_size = btrfs_item_size_nr(eb, slot); |
| 3932 | if (item_size < sizeof(root_item)) |
| 3933 | goto skip; |
| 3934 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3935 | read_extent_buffer(eb, &root_item, |
| 3936 | btrfs_item_ptr_offset(eb, slot), |
| 3937 | (int)sizeof(root_item)); |
| 3938 | if (btrfs_root_refs(&root_item) == 0) |
| 3939 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3940 | |
| 3941 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3942 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3943 | if (trans) |
| 3944 | goto update_tree; |
| 3945 | |
| 3946 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3947 | /* |
| 3948 | * 1 - subvol uuid item |
| 3949 | * 1 - received_subvol uuid item |
| 3950 | */ |
| 3951 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3952 | if (IS_ERR(trans)) { |
| 3953 | ret = PTR_ERR(trans); |
| 3954 | break; |
| 3955 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3956 | continue; |
| 3957 | } else { |
| 3958 | goto skip; |
| 3959 | } |
| 3960 | update_tree: |
| 3961 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3962 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3963 | root_item.uuid, |
| 3964 | BTRFS_UUID_KEY_SUBVOL, |
| 3965 | key.objectid); |
| 3966 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3967 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3968 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3969 | break; |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3974 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3975 | root_item.received_uuid, |
| 3976 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3977 | key.objectid); |
| 3978 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3979 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3980 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3981 | break; |
| 3982 | } |
| 3983 | } |
| 3984 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3985 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3986 | if (trans) { |
| 3987 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3988 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3989 | if (ret) |
| 3990 | break; |
| 3991 | } |
| 3992 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3993 | btrfs_release_path(path); |
| 3994 | if (key.offset < (u64)-1) { |
| 3995 | key.offset++; |
| 3996 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3997 | key.offset = 0; |
| 3998 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3999 | } else if (key.objectid < (u64)-1) { |
| 4000 | key.offset = 0; |
| 4001 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4002 | key.objectid++; |
| 4003 | } else { |
| 4004 | break; |
| 4005 | } |
| 4006 | cond_resched(); |
| 4007 | } |
| 4008 | |
| 4009 | out: |
| 4010 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4011 | if (trans && !IS_ERR(trans)) |
| 4012 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4013 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4014 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4015 | else |
| 4016 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4017 | up(&fs_info->uuid_tree_rescan_sem); |
| 4018 | return 0; |
| 4019 | } |
| 4020 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4021 | /* |
| 4022 | * Callback for btrfs_uuid_tree_iterate(). |
| 4023 | * returns: |
| 4024 | * 0 check succeeded, the entry is not outdated. |
| 4025 | * < 0 if an error occured. |
| 4026 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4027 | */ |
| 4028 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4029 | u8 *uuid, u8 type, u64 subid) |
| 4030 | { |
| 4031 | struct btrfs_key key; |
| 4032 | int ret = 0; |
| 4033 | struct btrfs_root *subvol_root; |
| 4034 | |
| 4035 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4036 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4037 | goto out; |
| 4038 | |
| 4039 | key.objectid = subid; |
| 4040 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4041 | key.offset = (u64)-1; |
| 4042 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4043 | if (IS_ERR(subvol_root)) { |
| 4044 | ret = PTR_ERR(subvol_root); |
| 4045 | if (ret == -ENOENT) |
| 4046 | ret = 1; |
| 4047 | goto out; |
| 4048 | } |
| 4049 | |
| 4050 | switch (type) { |
| 4051 | case BTRFS_UUID_KEY_SUBVOL: |
| 4052 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4053 | ret = 1; |
| 4054 | break; |
| 4055 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4056 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4057 | BTRFS_UUID_SIZE)) |
| 4058 | ret = 1; |
| 4059 | break; |
| 4060 | } |
| 4061 | |
| 4062 | out: |
| 4063 | return ret; |
| 4064 | } |
| 4065 | |
| 4066 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4067 | { |
| 4068 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4069 | int ret; |
| 4070 | |
| 4071 | /* |
| 4072 | * 1st step is to iterate through the existing UUID tree and |
| 4073 | * to delete all entries that contain outdated data. |
| 4074 | * 2nd step is to add all missing entries to the UUID tree. |
| 4075 | */ |
| 4076 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4077 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4078 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4079 | up(&fs_info->uuid_tree_rescan_sem); |
| 4080 | return ret; |
| 4081 | } |
| 4082 | return btrfs_uuid_scan_kthread(data); |
| 4083 | } |
| 4084 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4085 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4086 | { |
| 4087 | struct btrfs_trans_handle *trans; |
| 4088 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4089 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4090 | struct task_struct *task; |
| 4091 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4092 | |
| 4093 | /* |
| 4094 | * 1 - root node |
| 4095 | * 1 - root item |
| 4096 | */ |
| 4097 | trans = btrfs_start_transaction(tree_root, 2); |
| 4098 | if (IS_ERR(trans)) |
| 4099 | return PTR_ERR(trans); |
| 4100 | |
| 4101 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4102 | BTRFS_UUID_TREE_OBJECTID); |
| 4103 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4104 | ret = PTR_ERR(uuid_root); |
| 4105 | btrfs_abort_transaction(trans, tree_root, ret); |
| 4106 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | fs_info->uuid_root = uuid_root; |
| 4110 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4111 | ret = btrfs_commit_transaction(trans, tree_root); |
| 4112 | if (ret) |
| 4113 | return ret; |
| 4114 | |
| 4115 | down(&fs_info->uuid_tree_rescan_sem); |
| 4116 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4117 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4118 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4119 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4120 | up(&fs_info->uuid_tree_rescan_sem); |
| 4121 | return PTR_ERR(task); |
| 4122 | } |
| 4123 | |
| 4124 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4125 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4126 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4127 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4128 | { |
| 4129 | struct task_struct *task; |
| 4130 | |
| 4131 | down(&fs_info->uuid_tree_rescan_sem); |
| 4132 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4133 | if (IS_ERR(task)) { |
| 4134 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4135 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4136 | up(&fs_info->uuid_tree_rescan_sem); |
| 4137 | return PTR_ERR(task); |
| 4138 | } |
| 4139 | |
| 4140 | return 0; |
| 4141 | } |
| 4142 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4143 | /* |
| 4144 | * shrinking a device means finding all of the device extents past |
| 4145 | * the new size, and then following the back refs to the chunks. |
| 4146 | * The chunk relocation code actually frees the device extent |
| 4147 | */ |
| 4148 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4149 | { |
| 4150 | struct btrfs_trans_handle *trans; |
| 4151 | struct btrfs_root *root = device->dev_root; |
| 4152 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4153 | struct btrfs_path *path; |
| 4154 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4155 | u64 chunk_offset; |
| 4156 | int ret; |
| 4157 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4158 | int failed = 0; |
| 4159 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4160 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4161 | struct extent_buffer *l; |
| 4162 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4163 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4164 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4165 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 4166 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4167 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4168 | if (device->is_tgtdev_for_dev_replace) |
| 4169 | return -EINVAL; |
| 4170 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4171 | path = btrfs_alloc_path(); |
| 4172 | if (!path) |
| 4173 | return -ENOMEM; |
| 4174 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4175 | path->reada = 2; |
| 4176 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4177 | lock_chunks(root); |
| 4178 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4179 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4180 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4181 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4182 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4183 | root->fs_info->free_chunk_space -= diff; |
| 4184 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4185 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4186 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4187 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4188 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4189 | key.objectid = device->devid; |
| 4190 | key.offset = (u64)-1; |
| 4191 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4192 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4193 | do { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4194 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4195 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4196 | if (ret < 0) { |
| 4197 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4198 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4199 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4200 | |
| 4201 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4202 | if (ret) |
| 4203 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4204 | if (ret < 0) |
| 4205 | goto done; |
| 4206 | if (ret) { |
| 4207 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4208 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4209 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | l = path->nodes[0]; |
| 4213 | slot = path->slots[0]; |
| 4214 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4215 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4216 | if (key.objectid != device->devid) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4217 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4218 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4219 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4220 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4221 | |
| 4222 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4223 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4224 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4225 | if (key.offset + length <= new_size) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4226 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4227 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4228 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4229 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4230 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4231 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4232 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4233 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 4234 | ret = btrfs_relocate_chunk(root, chunk_offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4235 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4236 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4237 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4238 | if (ret == -ENOSPC) |
| 4239 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4240 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4241 | |
| 4242 | if (failed && !retried) { |
| 4243 | failed = 0; |
| 4244 | retried = true; |
| 4245 | goto again; |
| 4246 | } else if (failed && retried) { |
| 4247 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4248 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4249 | } |
| 4250 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4251 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4252 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4253 | if (IS_ERR(trans)) { |
| 4254 | ret = PTR_ERR(trans); |
| 4255 | goto done; |
| 4256 | } |
| 4257 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4258 | lock_chunks(root); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4259 | |
| 4260 | /* |
| 4261 | * We checked in the above loop all device extents that were already in |
| 4262 | * the device tree. However before we have updated the device's |
| 4263 | * total_bytes to the new size, we might have had chunk allocations that |
| 4264 | * have not complete yet (new block groups attached to transaction |
| 4265 | * handles), and therefore their device extents were not yet in the |
| 4266 | * device tree and we missed them in the loop above. So if we have any |
| 4267 | * pending chunk using a device extent that overlaps the device range |
| 4268 | * that we can not use anymore, commit the current transaction and |
| 4269 | * repeat the search on the device tree - this way we guarantee we will |
| 4270 | * not have chunks using device extents that end beyond 'new_size'. |
| 4271 | */ |
| 4272 | if (!checked_pending_chunks) { |
| 4273 | u64 start = new_size; |
| 4274 | u64 len = old_size - new_size; |
| 4275 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 4276 | if (contains_pending_extent(trans->transaction, device, |
| 4277 | &start, len)) { |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4278 | unlock_chunks(root); |
| 4279 | checked_pending_chunks = true; |
| 4280 | failed = 0; |
| 4281 | retried = false; |
| 4282 | ret = btrfs_commit_transaction(trans, root); |
| 4283 | if (ret) |
| 4284 | goto done; |
| 4285 | goto again; |
| 4286 | } |
| 4287 | } |
| 4288 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4289 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4290 | if (list_empty(&device->resized_list)) |
| 4291 | list_add_tail(&device->resized_list, |
| 4292 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4293 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4294 | WARN_ON(diff > old_total); |
| 4295 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4296 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4297 | |
| 4298 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4299 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4300 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4301 | done: |
| 4302 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4303 | if (ret) { |
| 4304 | lock_chunks(root); |
| 4305 | btrfs_device_set_total_bytes(device, old_size); |
| 4306 | if (device->writeable) |
| 4307 | device->fs_devices->total_rw_bytes += diff; |
| 4308 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4309 | root->fs_info->free_chunk_space += diff; |
| 4310 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4311 | unlock_chunks(root); |
| 4312 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4313 | return ret; |
| 4314 | } |
| 4315 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4316 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4317 | struct btrfs_key *key, |
| 4318 | struct btrfs_chunk *chunk, int item_size) |
| 4319 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4320 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4321 | struct btrfs_disk_key disk_key; |
| 4322 | u32 array_size; |
| 4323 | u8 *ptr; |
| 4324 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4325 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4326 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4327 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4328 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4329 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4330 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4331 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4332 | |
| 4333 | ptr = super_copy->sys_chunk_array + array_size; |
| 4334 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4335 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4336 | ptr += sizeof(disk_key); |
| 4337 | memcpy(ptr, chunk, item_size); |
| 4338 | item_size += sizeof(disk_key); |
| 4339 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4340 | unlock_chunks(root); |
| 4341 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4342 | return 0; |
| 4343 | } |
| 4344 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4345 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4346 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4347 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4348 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4349 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4350 | const struct btrfs_device_info *di_a = a; |
| 4351 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4352 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4353 | if (di_a->max_avail > di_b->max_avail) |
| 4354 | return -1; |
| 4355 | if (di_a->max_avail < di_b->max_avail) |
| 4356 | return 1; |
| 4357 | if (di_a->total_avail > di_b->total_avail) |
| 4358 | return -1; |
| 4359 | if (di_a->total_avail < di_b->total_avail) |
| 4360 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4361 | return 0; |
| 4362 | } |
| 4363 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4364 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4365 | { |
| 4366 | /* TODO allow them to set a preferred stripe size */ |
| 4367 | return 64 * 1024; |
| 4368 | } |
| 4369 | |
| 4370 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4371 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4372 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4373 | return; |
| 4374 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4375 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4376 | } |
| 4377 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4378 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4379 | - sizeof(struct btrfs_item) \ |
| 4380 | - sizeof(struct btrfs_chunk)) \ |
| 4381 | / sizeof(struct btrfs_stripe) + 1) |
| 4382 | |
| 4383 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4384 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4385 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4386 | / sizeof(struct btrfs_stripe) + 1) |
| 4387 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4388 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4389 | struct btrfs_root *extent_root, u64 start, |
| 4390 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4391 | { |
| 4392 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4393 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4394 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4395 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4396 | struct extent_map_tree *em_tree; |
| 4397 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4398 | struct btrfs_device_info *devices_info = NULL; |
| 4399 | u64 total_avail; |
| 4400 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4401 | int data_stripes; /* number of stripes that count for |
| 4402 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4403 | int sub_stripes; /* sub_stripes info for map */ |
| 4404 | int dev_stripes; /* stripes per dev */ |
| 4405 | int devs_max; /* max devs to use */ |
| 4406 | int devs_min; /* min devs needed */ |
| 4407 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4408 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4409 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4410 | u64 max_stripe_size; |
| 4411 | u64 max_chunk_size; |
| 4412 | u64 stripe_size; |
| 4413 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4414 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4415 | int ndevs; |
| 4416 | int i; |
| 4417 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4418 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4419 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4420 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4421 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4422 | if (list_empty(&fs_devices->alloc_list)) |
| 4423 | return -ENOSPC; |
| 4424 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4425 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4426 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4427 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4428 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4429 | devs_max = btrfs_raid_array[index].devs_max; |
| 4430 | devs_min = btrfs_raid_array[index].devs_min; |
| 4431 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4432 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4433 | |
| 4434 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4435 | max_stripe_size = 1024 * 1024 * 1024; |
| 4436 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4437 | if (!devs_max) |
| 4438 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4439 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4440 | /* for larger filesystems, use larger metadata chunks */ |
| 4441 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4442 | max_stripe_size = 1024 * 1024 * 1024; |
| 4443 | else |
| 4444 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4445 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4446 | if (!devs_max) |
| 4447 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4448 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4449 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4450 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4451 | if (!devs_max) |
| 4452 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4453 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4454 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4455 | type); |
| 4456 | BUG_ON(1); |
| 4457 | } |
| 4458 | |
| 4459 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4460 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4461 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4462 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4463 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4464 | GFP_NOFS); |
| 4465 | if (!devices_info) |
| 4466 | return -ENOMEM; |
| 4467 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4468 | cur = fs_devices->alloc_list.next; |
| 4469 | |
| 4470 | /* |
| 4471 | * in the first pass through the devices list, we gather information |
| 4472 | * about the available holes on each device. |
| 4473 | */ |
| 4474 | ndevs = 0; |
| 4475 | while (cur != &fs_devices->alloc_list) { |
| 4476 | struct btrfs_device *device; |
| 4477 | u64 max_avail; |
| 4478 | u64 dev_offset; |
| 4479 | |
| 4480 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4481 | |
| 4482 | cur = cur->next; |
| 4483 | |
| 4484 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4485 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4486 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4487 | continue; |
| 4488 | } |
| 4489 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4490 | if (!device->in_fs_metadata || |
| 4491 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4492 | continue; |
| 4493 | |
| 4494 | if (device->total_bytes > device->bytes_used) |
| 4495 | total_avail = device->total_bytes - device->bytes_used; |
| 4496 | else |
| 4497 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4498 | |
| 4499 | /* If there is no space on this device, skip it. */ |
| 4500 | if (total_avail == 0) |
| 4501 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4502 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4503 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4504 | max_stripe_size * dev_stripes, |
| 4505 | &dev_offset, &max_avail); |
| 4506 | if (ret && ret != -ENOSPC) |
| 4507 | goto error; |
| 4508 | |
| 4509 | if (ret == 0) |
| 4510 | max_avail = max_stripe_size * dev_stripes; |
| 4511 | |
| 4512 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4513 | continue; |
| 4514 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4515 | if (ndevs == fs_devices->rw_devices) { |
| 4516 | WARN(1, "%s: found more than %llu devices\n", |
| 4517 | __func__, fs_devices->rw_devices); |
| 4518 | break; |
| 4519 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4520 | devices_info[ndevs].dev_offset = dev_offset; |
| 4521 | devices_info[ndevs].max_avail = max_avail; |
| 4522 | devices_info[ndevs].total_avail = total_avail; |
| 4523 | devices_info[ndevs].dev = device; |
| 4524 | ++ndevs; |
| 4525 | } |
| 4526 | |
| 4527 | /* |
| 4528 | * now sort the devices by hole size / available space |
| 4529 | */ |
| 4530 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4531 | btrfs_cmp_device_info, NULL); |
| 4532 | |
| 4533 | /* round down to number of usable stripes */ |
| 4534 | ndevs -= ndevs % devs_increment; |
| 4535 | |
| 4536 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4537 | ret = -ENOSPC; |
| 4538 | goto error; |
| 4539 | } |
| 4540 | |
| 4541 | if (devs_max && ndevs > devs_max) |
| 4542 | ndevs = devs_max; |
| 4543 | /* |
| 4544 | * the primary goal is to maximize the number of stripes, so use as many |
| 4545 | * devices as possible, even if the stripes are not maximum sized. |
| 4546 | */ |
| 4547 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4548 | num_stripes = ndevs * dev_stripes; |
| 4549 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4550 | /* |
| 4551 | * this will have to be fixed for RAID1 and RAID10 over |
| 4552 | * more drives |
| 4553 | */ |
| 4554 | data_stripes = num_stripes / ncopies; |
| 4555 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4556 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4557 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4558 | btrfs_super_stripesize(info->super_copy)); |
| 4559 | data_stripes = num_stripes - 1; |
| 4560 | } |
| 4561 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4562 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4563 | btrfs_super_stripesize(info->super_copy)); |
| 4564 | data_stripes = num_stripes - 2; |
| 4565 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4566 | |
| 4567 | /* |
| 4568 | * Use the number of data stripes to figure out how big this chunk |
| 4569 | * is really going to be in terms of logical address space, |
| 4570 | * and compare that answer with the max chunk size |
| 4571 | */ |
| 4572 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4573 | u64 mask = (1ULL << 24) - 1; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4574 | |
| 4575 | stripe_size = div_u64(max_chunk_size, data_stripes); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4576 | |
| 4577 | /* bump the answer up to a 16MB boundary */ |
| 4578 | stripe_size = (stripe_size + mask) & ~mask; |
| 4579 | |
| 4580 | /* but don't go higher than the limits we found |
| 4581 | * while searching for free extents |
| 4582 | */ |
| 4583 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4584 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4585 | } |
| 4586 | |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4587 | stripe_size = div_u64(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4588 | |
| 4589 | /* align to BTRFS_STRIPE_LEN */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4590 | stripe_size = div_u64(stripe_size, raid_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4591 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4592 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4593 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4594 | if (!map) { |
| 4595 | ret = -ENOMEM; |
| 4596 | goto error; |
| 4597 | } |
| 4598 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4599 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4600 | for (i = 0; i < ndevs; ++i) { |
| 4601 | for (j = 0; j < dev_stripes; ++j) { |
| 4602 | int s = i * dev_stripes + j; |
| 4603 | map->stripes[s].dev = devices_info[i].dev; |
| 4604 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4605 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4606 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4607 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4608 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4609 | map->stripe_len = raid_stripe_len; |
| 4610 | map->io_align = raid_stripe_len; |
| 4611 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4612 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4613 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4614 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4615 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4616 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4617 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4618 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4619 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4620 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4621 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4622 | ret = -ENOMEM; |
| 4623 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4624 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4625 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4626 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4627 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4628 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4629 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4630 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4631 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4632 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4633 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4634 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4635 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4636 | if (!ret) { |
| 4637 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4638 | atomic_inc(&em->refs); |
| 4639 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4640 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4641 | if (ret) { |
| 4642 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4643 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4644 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4645 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4646 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4647 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4648 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4649 | if (ret) |
| 4650 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4651 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4652 | for (i = 0; i < map->num_stripes; i++) { |
| 4653 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4654 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4655 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4656 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4657 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4658 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4659 | map->num_stripes); |
| 4660 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4661 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4662 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4663 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4664 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4665 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4666 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4667 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4668 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4669 | write_lock(&em_tree->lock); |
| 4670 | remove_extent_mapping(em_tree, em); |
| 4671 | write_unlock(&em_tree->lock); |
| 4672 | |
| 4673 | /* One for our allocation */ |
| 4674 | free_extent_map(em); |
| 4675 | /* One for the tree reference */ |
| 4676 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4677 | /* One for the pending_chunks list reference */ |
| 4678 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4679 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4680 | kfree(devices_info); |
| 4681 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4682 | } |
| 4683 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4684 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4685 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4686 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4687 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4688 | struct btrfs_key key; |
| 4689 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4690 | struct btrfs_device *device; |
| 4691 | struct btrfs_chunk *chunk; |
| 4692 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4693 | struct extent_map_tree *em_tree; |
| 4694 | struct extent_map *em; |
| 4695 | struct map_lookup *map; |
| 4696 | size_t item_size; |
| 4697 | u64 dev_offset; |
| 4698 | u64 stripe_size; |
| 4699 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4700 | int ret; |
| 4701 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4702 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4703 | read_lock(&em_tree->lock); |
| 4704 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4705 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4706 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4707 | if (!em) { |
| 4708 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4709 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4710 | return -EINVAL; |
| 4711 | } |
| 4712 | |
| 4713 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4714 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4715 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4716 | chunk_size, em->start, em->len); |
| 4717 | free_extent_map(em); |
| 4718 | return -EINVAL; |
| 4719 | } |
| 4720 | |
| 4721 | map = (struct map_lookup *)em->bdev; |
| 4722 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4723 | stripe_size = em->orig_block_len; |
| 4724 | |
| 4725 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4726 | if (!chunk) { |
| 4727 | ret = -ENOMEM; |
| 4728 | goto out; |
| 4729 | } |
| 4730 | |
| 4731 | for (i = 0; i < map->num_stripes; i++) { |
| 4732 | device = map->stripes[i].dev; |
| 4733 | dev_offset = map->stripes[i].physical; |
| 4734 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4735 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4736 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4737 | goto out; |
| 4738 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4739 | chunk_root->root_key.objectid, |
| 4740 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4741 | chunk_offset, dev_offset, |
| 4742 | stripe_size); |
| 4743 | if (ret) |
| 4744 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4745 | } |
| 4746 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4747 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4748 | for (i = 0; i < map->num_stripes; i++) { |
| 4749 | device = map->stripes[i].dev; |
| 4750 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4751 | |
| 4752 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4753 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4754 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4755 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4759 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4760 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4761 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4762 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4763 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4764 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4765 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4766 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4767 | |
| 4768 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4769 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4770 | key.offset = chunk_offset; |
| 4771 | |
| 4772 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4773 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4774 | /* |
| 4775 | * TODO: Cleanup of inserted chunk root in case of |
| 4776 | * failure. |
| 4777 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4778 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4779 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4780 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4781 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4782 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4783 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4784 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4785 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | /* |
| 4789 | * Chunk allocation falls into two parts. The first part does works |
| 4790 | * that make the new allocated chunk useable, but not do any operation |
| 4791 | * that modifies the chunk tree. The second part does the works that |
| 4792 | * require modifying the chunk tree. This division is important for the |
| 4793 | * bootstrap process of adding storage to a seed btrfs. |
| 4794 | */ |
| 4795 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4796 | struct btrfs_root *extent_root, u64 type) |
| 4797 | { |
| 4798 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4799 | |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 4800 | ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex)); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4801 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4802 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4803 | } |
| 4804 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4805 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4806 | struct btrfs_root *root, |
| 4807 | struct btrfs_device *device) |
| 4808 | { |
| 4809 | u64 chunk_offset; |
| 4810 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4811 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4812 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4813 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4814 | int ret; |
| 4815 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4816 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4817 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4818 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4819 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4820 | if (ret) |
| 4821 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4822 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4823 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4824 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4825 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4826 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4827 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4828 | } |
| 4829 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4830 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4831 | { |
| 4832 | int max_errors; |
| 4833 | |
| 4834 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4835 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4836 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4837 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4838 | max_errors = 1; |
| 4839 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4840 | max_errors = 2; |
| 4841 | } else { |
| 4842 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4843 | } |
| 4844 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4845 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4846 | } |
| 4847 | |
| 4848 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4849 | { |
| 4850 | struct extent_map *em; |
| 4851 | struct map_lookup *map; |
| 4852 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4853 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4854 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4855 | int i; |
| 4856 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4857 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4858 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4859 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4860 | if (!em) |
| 4861 | return 1; |
| 4862 | |
| 4863 | map = (struct map_lookup *)em->bdev; |
| 4864 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4865 | if (map->stripes[i].dev->missing) { |
| 4866 | miss_ndevs++; |
| 4867 | continue; |
| 4868 | } |
| 4869 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4870 | if (!map->stripes[i].dev->writeable) { |
| 4871 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4872 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4873 | } |
| 4874 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4875 | |
| 4876 | /* |
| 4877 | * If the number of missing devices is larger than max errors, |
| 4878 | * we can not write the data into that chunk successfully, so |
| 4879 | * set it readonly. |
| 4880 | */ |
| 4881 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4882 | readonly = 1; |
| 4883 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4884 | free_extent_map(em); |
| 4885 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4889 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4890 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4894 | { |
| 4895 | struct extent_map *em; |
| 4896 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4897 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4898 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4899 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4900 | if (em) |
| 4901 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4902 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4903 | if (!em) |
| 4904 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4905 | /* once for us */ |
| 4906 | free_extent_map(em); |
| 4907 | /* once for the tree */ |
| 4908 | free_extent_map(em); |
| 4909 | } |
| 4910 | } |
| 4911 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4912 | 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] | 4913 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4914 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4915 | struct extent_map *em; |
| 4916 | struct map_lookup *map; |
| 4917 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4918 | int ret; |
| 4919 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4920 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4921 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4922 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4923 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4924 | /* |
| 4925 | * We could return errors for these cases, but that could get ugly and |
| 4926 | * we'd probably do the same thing which is just not do anything else |
| 4927 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4928 | */ |
| 4929 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4930 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4931 | logical+len); |
| 4932 | return 1; |
| 4933 | } |
| 4934 | |
| 4935 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4936 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4937 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4938 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4939 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4940 | return 1; |
| 4941 | } |
| 4942 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4943 | map = (struct map_lookup *)em->bdev; |
| 4944 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4945 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4946 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4947 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4948 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4949 | ret = 2; |
| 4950 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4951 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4952 | else |
| 4953 | ret = 1; |
| 4954 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4955 | |
| 4956 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4957 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4958 | ret++; |
| 4959 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4960 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4961 | return ret; |
| 4962 | } |
| 4963 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4964 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4965 | struct btrfs_mapping_tree *map_tree, |
| 4966 | u64 logical) |
| 4967 | { |
| 4968 | struct extent_map *em; |
| 4969 | struct map_lookup *map; |
| 4970 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4971 | unsigned long len = root->sectorsize; |
| 4972 | |
| 4973 | read_lock(&em_tree->lock); |
| 4974 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4975 | read_unlock(&em_tree->lock); |
| 4976 | BUG_ON(!em); |
| 4977 | |
| 4978 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4979 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4980 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4981 | len = map->stripe_len * nr_data_stripes(map); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4982 | free_extent_map(em); |
| 4983 | return len; |
| 4984 | } |
| 4985 | |
| 4986 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4987 | u64 logical, u64 len, int mirror_num) |
| 4988 | { |
| 4989 | struct extent_map *em; |
| 4990 | struct map_lookup *map; |
| 4991 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4992 | int ret = 0; |
| 4993 | |
| 4994 | read_lock(&em_tree->lock); |
| 4995 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4996 | read_unlock(&em_tree->lock); |
| 4997 | BUG_ON(!em); |
| 4998 | |
| 4999 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 5000 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5001 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5002 | ret = 1; |
| 5003 | free_extent_map(em); |
| 5004 | return ret; |
| 5005 | } |
| 5006 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5007 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 5008 | struct map_lookup *map, int first, int num, |
| 5009 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5010 | { |
| 5011 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5012 | int tolerance; |
| 5013 | struct btrfs_device *srcdev; |
| 5014 | |
| 5015 | if (dev_replace_is_ongoing && |
| 5016 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5017 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5018 | srcdev = fs_info->dev_replace.srcdev; |
| 5019 | else |
| 5020 | srcdev = NULL; |
| 5021 | |
| 5022 | /* |
| 5023 | * try to avoid the drive that is the source drive for a |
| 5024 | * dev-replace procedure, only choose it if no other non-missing |
| 5025 | * mirror is available |
| 5026 | */ |
| 5027 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 5028 | if (map->stripes[optimal].dev->bdev && |
| 5029 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 5030 | return optimal; |
| 5031 | for (i = first; i < first + num; i++) { |
| 5032 | if (map->stripes[i].dev->bdev && |
| 5033 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5034 | return i; |
| 5035 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5036 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5037 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5038 | /* we couldn't find one that doesn't fail. Just return something |
| 5039 | * and the io error handling code will clean up eventually |
| 5040 | */ |
| 5041 | return optimal; |
| 5042 | } |
| 5043 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5044 | static inline int parity_smaller(u64 a, u64 b) |
| 5045 | { |
| 5046 | return a > b; |
| 5047 | } |
| 5048 | |
| 5049 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5050 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5051 | { |
| 5052 | struct btrfs_bio_stripe s; |
| 5053 | int i; |
| 5054 | u64 l; |
| 5055 | int again = 1; |
| 5056 | |
| 5057 | while (again) { |
| 5058 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5059 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5060 | if (parity_smaller(bbio->raid_map[i], |
| 5061 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5062 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5063 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5064 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5065 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5066 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5067 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5068 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5069 | again = 1; |
| 5070 | } |
| 5071 | } |
| 5072 | } |
| 5073 | } |
| 5074 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5075 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5076 | { |
| 5077 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5078 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5079 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5080 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5081 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5082 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5083 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5084 | /* |
| 5085 | * plus the raid_map, which includes both the tgt dev |
| 5086 | * and the stripes |
| 5087 | */ |
| 5088 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5089 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5090 | |
| 5091 | atomic_set(&bbio->error, 0); |
| 5092 | atomic_set(&bbio->refs, 1); |
| 5093 | |
| 5094 | return bbio; |
| 5095 | } |
| 5096 | |
| 5097 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5098 | { |
| 5099 | WARN_ON(!atomic_read(&bbio->refs)); |
| 5100 | atomic_inc(&bbio->refs); |
| 5101 | } |
| 5102 | |
| 5103 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5104 | { |
| 5105 | if (!bbio) |
| 5106 | return; |
| 5107 | if (atomic_dec_and_test(&bbio->refs)) |
| 5108 | kfree(bbio); |
| 5109 | } |
| 5110 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5111 | 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] | 5112 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5113 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5114 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5115 | { |
| 5116 | struct extent_map *em; |
| 5117 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5118 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5119 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5120 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5121 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5122 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5123 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5124 | u64 stripe_nr_orig; |
| 5125 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5126 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5127 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5128 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5129 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5130 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5131 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5132 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5133 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5134 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5135 | int dev_replace_is_ongoing = 0; |
| 5136 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5137 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5138 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5139 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5140 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5141 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5142 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5143 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5144 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5145 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5146 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5147 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5148 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5149 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5150 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5151 | if (em->start > logical || em->start + em->len < logical) { |
| 5152 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5153 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5154 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 5155 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5156 | return -EINVAL; |
| 5157 | } |
| 5158 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5159 | map = (struct map_lookup *)em->bdev; |
| 5160 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5161 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5162 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5163 | stripe_nr = offset; |
| 5164 | /* |
| 5165 | * stripe_nr counts the total number of stripes we have to stride |
| 5166 | * to get to this block |
| 5167 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5168 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5169 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5170 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5171 | BUG_ON(offset < stripe_offset); |
| 5172 | |
| 5173 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5174 | stripe_offset = offset - stripe_offset; |
| 5175 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5176 | /* 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] | 5177 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5178 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5179 | raid56_full_stripe_start = offset; |
| 5180 | |
| 5181 | /* allow a write of a full stripe, but make sure we don't |
| 5182 | * allow straddling of stripes |
| 5183 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5184 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5185 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5186 | raid56_full_stripe_start *= full_stripe_len; |
| 5187 | } |
| 5188 | |
| 5189 | if (rw & REQ_DISCARD) { |
| 5190 | /* we don't discard raid56 yet */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5191 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5192 | ret = -EOPNOTSUPP; |
| 5193 | goto out; |
| 5194 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5195 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5196 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5197 | u64 max_len; |
| 5198 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5199 | For other RAID types and for RAID[56] reads, just allow a single |
| 5200 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5201 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5202 | (rw & REQ_WRITE)) { |
| 5203 | max_len = stripe_len * nr_data_stripes(map) - |
| 5204 | (offset - raid56_full_stripe_start); |
| 5205 | } else { |
| 5206 | /* we limit the length of each bio to what fits in a stripe */ |
| 5207 | max_len = stripe_len - stripe_offset; |
| 5208 | } |
| 5209 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5210 | } else { |
| 5211 | *length = em->len - offset; |
| 5212 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5213 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5214 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5215 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5216 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5217 | goto out; |
| 5218 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5219 | btrfs_dev_replace_lock(dev_replace); |
| 5220 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5221 | if (!dev_replace_is_ongoing) |
| 5222 | btrfs_dev_replace_unlock(dev_replace); |
| 5223 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5224 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5225 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5226 | dev_replace->tgtdev != NULL) { |
| 5227 | /* |
| 5228 | * in dev-replace case, for repair case (that's the only |
| 5229 | * case where the mirror is selected explicitly when |
| 5230 | * calling btrfs_map_block), blocks left of the left cursor |
| 5231 | * can also be read from the target drive. |
| 5232 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5233 | * the last one to the array of stripes. For READ, it also |
| 5234 | * needs to be supported using the same mirror number. |
| 5235 | * If the requested block is not left of the left cursor, |
| 5236 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5237 | * returns one more in the dev-replace case. |
| 5238 | */ |
| 5239 | u64 tmp_length = *length; |
| 5240 | struct btrfs_bio *tmp_bbio = NULL; |
| 5241 | int tmp_num_stripes; |
| 5242 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5243 | int index_srcdev = 0; |
| 5244 | int found = 0; |
| 5245 | u64 physical_of_found = 0; |
| 5246 | |
| 5247 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5248 | logical, &tmp_length, &tmp_bbio, 0, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5249 | if (ret) { |
| 5250 | WARN_ON(tmp_bbio != NULL); |
| 5251 | goto out; |
| 5252 | } |
| 5253 | |
| 5254 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5255 | if (mirror_num > tmp_num_stripes) { |
| 5256 | /* |
| 5257 | * REQ_GET_READ_MIRRORS does not contain this |
| 5258 | * mirror, that means that the requested area |
| 5259 | * is not left of the left cursor |
| 5260 | */ |
| 5261 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5262 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5263 | goto out; |
| 5264 | } |
| 5265 | |
| 5266 | /* |
| 5267 | * process the rest of the function using the mirror_num |
| 5268 | * of the source drive. Therefore look it up first. |
| 5269 | * At the end, patch the device pointer to the one of the |
| 5270 | * target drive. |
| 5271 | */ |
| 5272 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5273 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5274 | /* |
| 5275 | * In case of DUP, in order to keep it |
| 5276 | * simple, only add the mirror with the |
| 5277 | * lowest physical address |
| 5278 | */ |
| 5279 | if (found && |
| 5280 | physical_of_found <= |
| 5281 | tmp_bbio->stripes[i].physical) |
| 5282 | continue; |
| 5283 | index_srcdev = i; |
| 5284 | found = 1; |
| 5285 | physical_of_found = |
| 5286 | tmp_bbio->stripes[i].physical; |
| 5287 | } |
| 5288 | } |
| 5289 | |
| 5290 | if (found) { |
| 5291 | mirror_num = index_srcdev + 1; |
| 5292 | patch_the_first_stripe_for_dev_replace = 1; |
| 5293 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5294 | } else { |
| 5295 | WARN_ON(1); |
| 5296 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5297 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5298 | goto out; |
| 5299 | } |
| 5300 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5301 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5302 | } else if (mirror_num > map->num_stripes) { |
| 5303 | mirror_num = 0; |
| 5304 | } |
| 5305 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5306 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5307 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5308 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5309 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5310 | stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5311 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5312 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5313 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5314 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5315 | if (rw & REQ_DISCARD) |
| 5316 | num_stripes = min_t(u64, map->num_stripes, |
| 5317 | stripe_nr_end - stripe_nr_orig); |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5318 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5319 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5320 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5321 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5322 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5323 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5324 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5325 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5326 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5327 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5328 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5329 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5330 | current->pid % map->num_stripes, |
| 5331 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5332 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5333 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5334 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5335 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5336 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5337 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5338 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5339 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5340 | } else { |
| 5341 | mirror_num = 1; |
| 5342 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5343 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5344 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5345 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5346 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5347 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5348 | stripe_index *= map->sub_stripes; |
| 5349 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5350 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5351 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5352 | else if (rw & REQ_DISCARD) |
| 5353 | num_stripes = min_t(u64, map->sub_stripes * |
| 5354 | (stripe_nr_end - stripe_nr_orig), |
| 5355 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5356 | else if (mirror_num) |
| 5357 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5358 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5359 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5360 | stripe_index = find_live_mirror(fs_info, map, |
| 5361 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5362 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5363 | current->pid % map->sub_stripes, |
| 5364 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5365 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5366 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5367 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5368 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5369 | if (need_raid_map && |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5370 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5371 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5372 | /* push stripe_nr back to the start of the full stripe */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5373 | stripe_nr = div_u64(raid56_full_stripe_start, |
| 5374 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5375 | |
| 5376 | /* RAID[56] write or recovery. Return all stripes */ |
| 5377 | num_stripes = map->num_stripes; |
| 5378 | max_errors = nr_parity_stripes(map); |
| 5379 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5380 | *length = map->stripe_len; |
| 5381 | stripe_index = 0; |
| 5382 | stripe_offset = 0; |
| 5383 | } else { |
| 5384 | /* |
| 5385 | * Mirror #0 or #1 means the original data block. |
| 5386 | * Mirror #2 is RAID5 parity block. |
| 5387 | * Mirror #3 is RAID6 Q block. |
| 5388 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5389 | stripe_nr = div_u64_rem(stripe_nr, |
| 5390 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5391 | if (mirror_num > 1) |
| 5392 | stripe_index = nr_data_stripes(map) + |
| 5393 | mirror_num - 2; |
| 5394 | |
| 5395 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5396 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5397 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5398 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5399 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5400 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5401 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5402 | } else { |
| 5403 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5404 | * after this, stripe_nr is the number of stripes on this |
| 5405 | * device we have to walk to find the data, and stripe_index is |
| 5406 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5407 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5408 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5409 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5410 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5411 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5412 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5413 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5414 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5415 | if (dev_replace_is_ongoing) { |
| 5416 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5417 | num_alloc_stripes <<= 1; |
| 5418 | if (rw & REQ_GET_READ_MIRRORS) |
| 5419 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5420 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5421 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5422 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5423 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5424 | if (!bbio) { |
| 5425 | ret = -ENOMEM; |
| 5426 | goto out; |
| 5427 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5428 | if (dev_replace_is_ongoing) |
| 5429 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5430 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5431 | /* build raid_map */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5432 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5433 | need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5434 | mirror_num > 1)) { |
| 5435 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5436 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5437 | |
| 5438 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5439 | sizeof(struct btrfs_bio_stripe) * |
| 5440 | num_alloc_stripes + |
| 5441 | sizeof(int) * tgtdev_indexes); |
| 5442 | |
| 5443 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5444 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5445 | |
| 5446 | /* Fill in the logical address of each stripe */ |
| 5447 | tmp = stripe_nr * nr_data_stripes(map); |
| 5448 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5449 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5450 | em->start + (tmp + i) * map->stripe_len; |
| 5451 | |
| 5452 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5453 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5454 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5455 | RAID6_Q_STRIPE; |
| 5456 | } |
| 5457 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5458 | if (rw & REQ_DISCARD) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5459 | u32 factor = 0; |
| 5460 | u32 sub_stripes = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5461 | u64 stripes_per_dev = 0; |
| 5462 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5463 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5464 | |
| 5465 | if (map->type & |
| 5466 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5467 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5468 | sub_stripes = 1; |
| 5469 | else |
| 5470 | sub_stripes = map->sub_stripes; |
| 5471 | |
| 5472 | factor = map->num_stripes / sub_stripes; |
| 5473 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5474 | stripe_nr_orig, |
| 5475 | factor, |
| 5476 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5477 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5478 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5479 | } |
| 5480 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5481 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5482 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5483 | map->stripes[stripe_index].physical + |
| 5484 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5485 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5486 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5487 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5488 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5489 | bbio->stripes[i].length = stripes_per_dev * |
| 5490 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5491 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5492 | if (i / sub_stripes < remaining_stripes) |
| 5493 | bbio->stripes[i].length += |
| 5494 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5495 | |
| 5496 | /* |
| 5497 | * Special for the first stripe and |
| 5498 | * the last stripe: |
| 5499 | * |
| 5500 | * |-------|...|-------| |
| 5501 | * |----------| |
| 5502 | * off end_off |
| 5503 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5504 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5505 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5506 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5507 | |
| 5508 | if (stripe_index >= last_stripe && |
| 5509 | stripe_index <= (last_stripe + |
| 5510 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5511 | bbio->stripes[i].length -= |
| 5512 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5513 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5514 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5515 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5516 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5517 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5518 | |
| 5519 | stripe_index++; |
| 5520 | if (stripe_index == map->num_stripes) { |
| 5521 | /* This could only happen for RAID0/10 */ |
| 5522 | stripe_index = 0; |
| 5523 | stripe_nr++; |
| 5524 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5525 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5526 | } else { |
| 5527 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5528 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5529 | map->stripes[stripe_index].physical + |
| 5530 | stripe_offset + |
| 5531 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5532 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5533 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5534 | stripe_index++; |
| 5535 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5536 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5537 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5538 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5539 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5540 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5541 | if (bbio->raid_map) |
| 5542 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5543 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5544 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5545 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5546 | dev_replace->tgtdev != NULL) { |
| 5547 | int index_where_to_add; |
| 5548 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5549 | |
| 5550 | /* |
| 5551 | * duplicate the write operations while the dev replace |
| 5552 | * procedure is running. Since the copying of the old disk |
| 5553 | * to the new disk takes place at run time while the |
| 5554 | * filesystem is mounted writable, the regular write |
| 5555 | * operations to the old disk have to be duplicated to go |
| 5556 | * to the new disk as well. |
| 5557 | * Note that device->missing is handled by the caller, and |
| 5558 | * that the write to the old disk is already set up in the |
| 5559 | * stripes array. |
| 5560 | */ |
| 5561 | index_where_to_add = num_stripes; |
| 5562 | for (i = 0; i < num_stripes; i++) { |
| 5563 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5564 | /* write to new disk, too */ |
| 5565 | struct btrfs_bio_stripe *new = |
| 5566 | bbio->stripes + index_where_to_add; |
| 5567 | struct btrfs_bio_stripe *old = |
| 5568 | bbio->stripes + i; |
| 5569 | |
| 5570 | new->physical = old->physical; |
| 5571 | new->length = old->length; |
| 5572 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5573 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5574 | index_where_to_add++; |
| 5575 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5576 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5577 | } |
| 5578 | } |
| 5579 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5580 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5581 | dev_replace->tgtdev != NULL) { |
| 5582 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5583 | int index_srcdev = 0; |
| 5584 | int found = 0; |
| 5585 | u64 physical_of_found = 0; |
| 5586 | |
| 5587 | /* |
| 5588 | * During the dev-replace procedure, the target drive can |
| 5589 | * also be used to read data in case it is needed to repair |
| 5590 | * a corrupt block elsewhere. This is possible if the |
| 5591 | * requested area is left of the left cursor. In this area, |
| 5592 | * the target drive is a full copy of the source drive. |
| 5593 | */ |
| 5594 | for (i = 0; i < num_stripes; i++) { |
| 5595 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5596 | /* |
| 5597 | * In case of DUP, in order to keep it |
| 5598 | * simple, only add the mirror with the |
| 5599 | * lowest physical address |
| 5600 | */ |
| 5601 | if (found && |
| 5602 | physical_of_found <= |
| 5603 | bbio->stripes[i].physical) |
| 5604 | continue; |
| 5605 | index_srcdev = i; |
| 5606 | found = 1; |
| 5607 | physical_of_found = bbio->stripes[i].physical; |
| 5608 | } |
| 5609 | } |
| 5610 | if (found) { |
David Sterba | 258ece0 | 2015-02-24 19:45:15 +0100 | [diff] [blame] | 5611 | if (physical_of_found + map->stripe_len <= |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5612 | dev_replace->cursor_left) { |
| 5613 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5614 | bbio->stripes + num_stripes; |
| 5615 | |
| 5616 | tgtdev_stripe->physical = physical_of_found; |
| 5617 | tgtdev_stripe->length = |
| 5618 | bbio->stripes[index_srcdev].length; |
| 5619 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5620 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5621 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5622 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5623 | num_stripes++; |
| 5624 | } |
| 5625 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5626 | } |
| 5627 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5628 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5629 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5630 | bbio->num_stripes = num_stripes; |
| 5631 | bbio->max_errors = max_errors; |
| 5632 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5633 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5634 | |
| 5635 | /* |
| 5636 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5637 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5638 | * available as a mirror |
| 5639 | */ |
| 5640 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5641 | WARN_ON(num_stripes > 1); |
| 5642 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5643 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5644 | bbio->mirror_num = map->num_stripes + 1; |
| 5645 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5646 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5647 | if (dev_replace_is_ongoing) |
| 5648 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5649 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5650 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5651 | } |
| 5652 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5653 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5654 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5655 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5656 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5657 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5658 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5659 | } |
| 5660 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5661 | /* For Scrub/replace */ |
| 5662 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5663 | u64 logical, u64 *length, |
| 5664 | struct btrfs_bio **bbio_ret, int mirror_num, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5665 | int need_raid_map) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5666 | { |
| 5667 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5668 | mirror_num, need_raid_map); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5669 | } |
| 5670 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5671 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5672 | u64 chunk_start, u64 physical, u64 devid, |
| 5673 | u64 **logical, int *naddrs, int *stripe_len) |
| 5674 | { |
| 5675 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5676 | struct extent_map *em; |
| 5677 | struct map_lookup *map; |
| 5678 | u64 *buf; |
| 5679 | u64 bytenr; |
| 5680 | u64 length; |
| 5681 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5682 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5683 | int i, j, nr = 0; |
| 5684 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5685 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5686 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5687 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5688 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5689 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5690 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5691 | chunk_start); |
| 5692 | return -EIO; |
| 5693 | } |
| 5694 | |
| 5695 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5696 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5697 | em->start, chunk_start); |
| 5698 | free_extent_map(em); |
| 5699 | return -EIO; |
| 5700 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5701 | map = (struct map_lookup *)em->bdev; |
| 5702 | |
| 5703 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5704 | rmap_len = map->stripe_len; |
| 5705 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5706 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5707 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5708 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5709 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5710 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5711 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5712 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5713 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5714 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5715 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5716 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5717 | |
| 5718 | for (i = 0; i < map->num_stripes; i++) { |
| 5719 | if (devid && map->stripes[i].dev->devid != devid) |
| 5720 | continue; |
| 5721 | if (map->stripes[i].physical > physical || |
| 5722 | map->stripes[i].physical + length <= physical) |
| 5723 | continue; |
| 5724 | |
| 5725 | stripe_nr = physical - map->stripes[i].physical; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5726 | stripe_nr = div_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5727 | |
| 5728 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5729 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5730 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5731 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5732 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5733 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5734 | * Alternatively, just use rmap_len below instead of |
| 5735 | * map->stripe_len */ |
| 5736 | |
| 5737 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5738 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5739 | for (j = 0; j < nr; j++) { |
| 5740 | if (buf[j] == bytenr) |
| 5741 | break; |
| 5742 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5743 | if (j == nr) { |
| 5744 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5745 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5746 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5747 | } |
| 5748 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5749 | *logical = buf; |
| 5750 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5751 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5752 | |
| 5753 | free_extent_map(em); |
| 5754 | return 0; |
| 5755 | } |
| 5756 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5757 | 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] | 5758 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5759 | bio->bi_private = bbio->private; |
| 5760 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5761 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5762 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5763 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5764 | } |
| 5765 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5766 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5767 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5768 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5769 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5770 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5771 | if (bio->bi_error) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5772 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5773 | if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5774 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5775 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 5776 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5777 | |
| 5778 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5779 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5780 | if (dev->bdev) { |
| 5781 | if (bio->bi_rw & WRITE) |
| 5782 | btrfs_dev_stat_inc(dev, |
| 5783 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5784 | else |
| 5785 | btrfs_dev_stat_inc(dev, |
| 5786 | BTRFS_DEV_STAT_READ_ERRS); |
| 5787 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5788 | btrfs_dev_stat_inc(dev, |
| 5789 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5790 | btrfs_dev_stat_print_on_error(dev); |
| 5791 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5792 | } |
| 5793 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5794 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5795 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5796 | is_orig_bio = 1; |
| 5797 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5798 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5799 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5800 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5801 | if (!is_orig_bio) { |
| 5802 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5803 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5804 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5805 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5806 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5807 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5808 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5809 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5810 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5811 | bio->bi_error = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5812 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5813 | /* |
| 5814 | * this bio is actually up to date, we didn't |
| 5815 | * go over the max number of errors |
| 5816 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5817 | bio->bi_error = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5818 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5819 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5820 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5821 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5822 | bio_put(bio); |
| 5823 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5824 | } |
| 5825 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5826 | /* |
| 5827 | * see run_scheduled_bios for a description of why bios are collected for |
| 5828 | * async submit. |
| 5829 | * |
| 5830 | * This will add one bio to the pending list for a device and make sure |
| 5831 | * the work struct is scheduled. |
| 5832 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5833 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5834 | struct btrfs_device *device, |
| 5835 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5836 | { |
| 5837 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5838 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5839 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5840 | if (device->missing || !device->bdev) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5841 | bio_io_error(bio); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5842 | return; |
| 5843 | } |
| 5844 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5845 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5846 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5847 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5848 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5849 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5850 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5854 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5855 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5856 | * made progress against dirty pages when we've really just put it |
| 5857 | * on a queue for later |
| 5858 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5859 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5860 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5861 | bio->bi_next = NULL; |
| 5862 | bio->bi_rw |= rw; |
| 5863 | |
| 5864 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5865 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5866 | pending_bios = &device->pending_sync_bios; |
| 5867 | else |
| 5868 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5869 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5870 | if (pending_bios->tail) |
| 5871 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5872 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5873 | pending_bios->tail = bio; |
| 5874 | if (!pending_bios->head) |
| 5875 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5876 | if (device->running_pending) |
| 5877 | should_queue = 0; |
| 5878 | |
| 5879 | spin_unlock(&device->io_lock); |
| 5880 | |
| 5881 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5882 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5883 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5884 | } |
| 5885 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5886 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5887 | struct bio *bio, u64 physical, int dev_nr, |
| 5888 | int rw, int async) |
| 5889 | { |
| 5890 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5891 | |
| 5892 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5893 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5894 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5895 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5896 | #ifdef DEBUG |
| 5897 | { |
| 5898 | struct rcu_string *name; |
| 5899 | |
| 5900 | rcu_read_lock(); |
| 5901 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5902 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5903 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5904 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5905 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5906 | rcu_read_unlock(); |
| 5907 | } |
| 5908 | #endif |
| 5909 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5910 | |
| 5911 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5912 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5913 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5914 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5915 | else |
| 5916 | btrfsic_submit_bio(rw, bio); |
| 5917 | } |
| 5918 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5919 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5920 | { |
| 5921 | atomic_inc(&bbio->error); |
| 5922 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5923 | /* Shoud be the original bio. */ |
| 5924 | WARN_ON(bio != bbio->orig_bio); |
| 5925 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5926 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5927 | bio->bi_iter.bi_sector = logical >> 9; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5928 | bio->bi_error = -EIO; |
| 5929 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5930 | } |
| 5931 | } |
| 5932 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5933 | 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] | 5934 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5935 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5936 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5937 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5938 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5939 | u64 length = 0; |
| 5940 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5941 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5942 | int dev_nr; |
| 5943 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5944 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5945 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5946 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5947 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5948 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5949 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5950 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5951 | mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5952 | if (ret) { |
| 5953 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5954 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5955 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5956 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5957 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5958 | bbio->orig_bio = first_bio; |
| 5959 | bbio->private = first_bio->bi_private; |
| 5960 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5961 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5962 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5963 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5964 | if (bbio->raid_map) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5965 | /* In this case, map_length has been set to the length of |
| 5966 | a single stripe; not the whole write */ |
| 5967 | if (rw & WRITE) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5968 | ret = raid56_parity_write(root, bio, bbio, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5969 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5970 | ret = raid56_parity_recover(root, bio, bbio, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5971 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5972 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5973 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5974 | btrfs_bio_counter_dec(root->fs_info); |
| 5975 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5976 | } |
| 5977 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5978 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5979 | 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] | 5980 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5981 | BUG(); |
| 5982 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5983 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5984 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5985 | dev = bbio->stripes[dev_nr].dev; |
| 5986 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5987 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5988 | continue; |
| 5989 | } |
| 5990 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5991 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5992 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5993 | BUG_ON(!bio); /* -ENOMEM */ |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5994 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5995 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5996 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5997 | submit_stripe_bio(root, bbio, bio, |
| 5998 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5999 | async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6000 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6001 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6002 | return 0; |
| 6003 | } |
| 6004 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6005 | 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] | 6006 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6007 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6008 | struct btrfs_device *device; |
| 6009 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6010 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6011 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6012 | while (cur_devices) { |
| 6013 | if (!fsid || |
| 6014 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 6015 | device = __find_device(&cur_devices->devices, |
| 6016 | devid, uuid); |
| 6017 | if (device) |
| 6018 | return device; |
| 6019 | } |
| 6020 | cur_devices = cur_devices->seed; |
| 6021 | } |
| 6022 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6023 | } |
| 6024 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6025 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6026 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6027 | u64 devid, u8 *dev_uuid) |
| 6028 | { |
| 6029 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6030 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6031 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6032 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 6033 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6034 | |
| 6035 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6036 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6037 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6038 | |
| 6039 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6040 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6041 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6042 | return device; |
| 6043 | } |
| 6044 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6045 | /** |
| 6046 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6047 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6048 | * devid is provided (i.e. @devid != NULL). |
| 6049 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6050 | * is generated. |
| 6051 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6052 | * is generated. |
| 6053 | * |
| 6054 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 6055 | * on error. Returned struct is not linked onto any lists and can be |
| 6056 | * destroyed with kfree() right away. |
| 6057 | */ |
| 6058 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6059 | const u64 *devid, |
| 6060 | const u8 *uuid) |
| 6061 | { |
| 6062 | struct btrfs_device *dev; |
| 6063 | u64 tmp; |
| 6064 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6065 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6066 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6067 | |
| 6068 | dev = __alloc_device(); |
| 6069 | if (IS_ERR(dev)) |
| 6070 | return dev; |
| 6071 | |
| 6072 | if (devid) |
| 6073 | tmp = *devid; |
| 6074 | else { |
| 6075 | int ret; |
| 6076 | |
| 6077 | ret = find_next_devid(fs_info, &tmp); |
| 6078 | if (ret) { |
| 6079 | kfree(dev); |
| 6080 | return ERR_PTR(ret); |
| 6081 | } |
| 6082 | } |
| 6083 | dev->devid = tmp; |
| 6084 | |
| 6085 | if (uuid) |
| 6086 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6087 | else |
| 6088 | generate_random_uuid(dev->uuid); |
| 6089 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6090 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6091 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6092 | |
| 6093 | return dev; |
| 6094 | } |
| 6095 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6096 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 6097 | struct extent_buffer *leaf, |
| 6098 | struct btrfs_chunk *chunk) |
| 6099 | { |
| 6100 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6101 | struct map_lookup *map; |
| 6102 | struct extent_map *em; |
| 6103 | u64 logical; |
| 6104 | u64 length; |
| 6105 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6106 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6107 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6108 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6109 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6110 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6111 | logical = key->offset; |
| 6112 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6113 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6114 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6115 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6116 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6117 | |
| 6118 | /* already mapped? */ |
| 6119 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6120 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6121 | return 0; |
| 6122 | } else if (em) { |
| 6123 | free_extent_map(em); |
| 6124 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6125 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6126 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6127 | if (!em) |
| 6128 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6129 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6130 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6131 | if (!map) { |
| 6132 | free_extent_map(em); |
| 6133 | return -ENOMEM; |
| 6134 | } |
| 6135 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6136 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6137 | em->bdev = (struct block_device *)map; |
| 6138 | em->start = logical; |
| 6139 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6140 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6141 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6142 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6143 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6144 | map->num_stripes = num_stripes; |
| 6145 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6146 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6147 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6148 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6149 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6150 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6151 | for (i = 0; i < num_stripes; i++) { |
| 6152 | map->stripes[i].physical = |
| 6153 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6154 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6155 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6156 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6157 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6158 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6159 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6160 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6161 | free_extent_map(em); |
| 6162 | return -EIO; |
| 6163 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6164 | if (!map->stripes[i].dev) { |
| 6165 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6166 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6167 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6168 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6169 | free_extent_map(em); |
| 6170 | return -EIO; |
| 6171 | } |
Anand Jain | 816fceb | 2015-04-27 12:46:18 +0800 | [diff] [blame] | 6172 | btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing", |
| 6173 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6174 | } |
| 6175 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6176 | } |
| 6177 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6178 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6179 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6180 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6181 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6182 | free_extent_map(em); |
| 6183 | |
| 6184 | return 0; |
| 6185 | } |
| 6186 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6187 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6188 | struct btrfs_dev_item *dev_item, |
| 6189 | struct btrfs_device *device) |
| 6190 | { |
| 6191 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6192 | |
| 6193 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6194 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6195 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6196 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6197 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6198 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6199 | device->type = btrfs_device_type(leaf, dev_item); |
| 6200 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6201 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6202 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6203 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6204 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6205 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6206 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6207 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6208 | } |
| 6209 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6210 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6211 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6212 | { |
| 6213 | struct btrfs_fs_devices *fs_devices; |
| 6214 | int ret; |
| 6215 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6216 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6217 | |
| 6218 | fs_devices = root->fs_info->fs_devices->seed; |
| 6219 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6220 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6221 | return fs_devices; |
| 6222 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6223 | fs_devices = fs_devices->seed; |
| 6224 | } |
| 6225 | |
| 6226 | fs_devices = find_fsid(fsid); |
| 6227 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6228 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6229 | return ERR_PTR(-ENOENT); |
| 6230 | |
| 6231 | fs_devices = alloc_fs_devices(fsid); |
| 6232 | if (IS_ERR(fs_devices)) |
| 6233 | return fs_devices; |
| 6234 | |
| 6235 | fs_devices->seeding = 1; |
| 6236 | fs_devices->opened = 1; |
| 6237 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6238 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6239 | |
| 6240 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6241 | if (IS_ERR(fs_devices)) |
| 6242 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6243 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6244 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6245 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6246 | if (ret) { |
| 6247 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6248 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6249 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6250 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6251 | |
| 6252 | if (!fs_devices->seeding) { |
| 6253 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6254 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6255 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6256 | goto out; |
| 6257 | } |
| 6258 | |
| 6259 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6260 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6261 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6262 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6263 | } |
| 6264 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6265 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6266 | struct extent_buffer *leaf, |
| 6267 | struct btrfs_dev_item *dev_item) |
| 6268 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6269 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6270 | struct btrfs_device *device; |
| 6271 | u64 devid; |
| 6272 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6273 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6274 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6275 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6276 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6277 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6278 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6279 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6280 | BTRFS_UUID_SIZE); |
| 6281 | |
| 6282 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6283 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6284 | if (IS_ERR(fs_devices)) |
| 6285 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6286 | } |
| 6287 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6288 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6289 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6290 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6291 | return -EIO; |
| 6292 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6293 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6294 | if (!device) |
| 6295 | return -ENOMEM; |
Anand Jain | 33b97e4 | 2015-05-08 04:34:35 +0800 | [diff] [blame] | 6296 | btrfs_warn(root->fs_info, "devid %llu uuid %pU missing", |
| 6297 | devid, dev_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6298 | } else { |
| 6299 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6300 | return -EIO; |
| 6301 | |
| 6302 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6303 | /* |
| 6304 | * this happens when a device that was properly setup |
| 6305 | * in the device info lists suddenly goes bad. |
| 6306 | * device->bdev is NULL, and so we have to set |
| 6307 | * device->missing to one here |
| 6308 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6309 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6310 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6311 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6312 | |
| 6313 | /* Move the device to its own fs_devices */ |
| 6314 | if (device->fs_devices != fs_devices) { |
| 6315 | ASSERT(device->missing); |
| 6316 | |
| 6317 | list_move(&device->dev_list, &fs_devices->devices); |
| 6318 | device->fs_devices->num_devices--; |
| 6319 | fs_devices->num_devices++; |
| 6320 | |
| 6321 | device->fs_devices->missing_devices--; |
| 6322 | fs_devices->missing_devices++; |
| 6323 | |
| 6324 | device->fs_devices = fs_devices; |
| 6325 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6326 | } |
| 6327 | |
| 6328 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6329 | BUG_ON(device->writeable); |
| 6330 | if (device->generation != |
| 6331 | btrfs_device_generation(leaf, dev_item)) |
| 6332 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6333 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6334 | |
| 6335 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6336 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6337 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6338 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6339 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6340 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6341 | device->bytes_used; |
| 6342 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6343 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6344 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6345 | return ret; |
| 6346 | } |
| 6347 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6348 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6349 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6350 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6351 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6352 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6353 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6354 | u8 *array_ptr; |
| 6355 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6356 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6357 | u32 num_stripes; |
| 6358 | u32 array_size; |
| 6359 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6360 | u32 cur_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6361 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6362 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6363 | ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize); |
| 6364 | /* |
| 6365 | * This will create extent buffer of nodesize, superblock size is |
| 6366 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6367 | * overallocate but we can keep it as-is, only the first page is used. |
| 6368 | */ |
| 6369 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6370 | if (!sb) |
| 6371 | return -ENOMEM; |
| 6372 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6373 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6374 | /* |
| 6375 | * The sb extent buffer is artifical and just used to read the system array. |
| 6376 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6377 | * pages up-to-date when the page is larger: extent does not cover the |
| 6378 | * whole page and consequently check_page_uptodate does not find all |
| 6379 | * the page's extents up-to-date (the hole beyond sb), |
| 6380 | * write_extent_buffer then triggers a WARN_ON. |
| 6381 | * |
| 6382 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6383 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6384 | * to silence the warning eg. on PowerPC 64. |
| 6385 | */ |
| 6386 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6387 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6388 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6389 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6390 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6391 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6392 | array_ptr = super_copy->sys_chunk_array; |
| 6393 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6394 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6395 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6396 | while (cur_offset < array_size) { |
| 6397 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6398 | len = sizeof(*disk_key); |
| 6399 | if (cur_offset + len > array_size) |
| 6400 | goto out_short_read; |
| 6401 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6402 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6403 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6404 | array_ptr += len; |
| 6405 | sb_array_offset += len; |
| 6406 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6407 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6408 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6409 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6410 | /* |
| 6411 | * At least one btrfs_chunk with one stripe must be |
| 6412 | * present, exact stripe count check comes afterwards |
| 6413 | */ |
| 6414 | len = btrfs_chunk_item_size(1); |
| 6415 | if (cur_offset + len > array_size) |
| 6416 | goto out_short_read; |
| 6417 | |
| 6418 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6419 | len = btrfs_chunk_item_size(num_stripes); |
| 6420 | if (cur_offset + len > array_size) |
| 6421 | goto out_short_read; |
| 6422 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6423 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6424 | if (ret) |
| 6425 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6426 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6427 | ret = -EIO; |
| 6428 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6429 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6430 | array_ptr += len; |
| 6431 | sb_array_offset += len; |
| 6432 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6433 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6434 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6435 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6436 | |
| 6437 | out_short_read: |
| 6438 | printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n", |
| 6439 | len, cur_offset); |
| 6440 | free_extent_buffer(sb); |
| 6441 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6445 | { |
| 6446 | struct btrfs_path *path; |
| 6447 | struct extent_buffer *leaf; |
| 6448 | struct btrfs_key key; |
| 6449 | struct btrfs_key found_key; |
| 6450 | int ret; |
| 6451 | int slot; |
| 6452 | |
| 6453 | root = root->fs_info->chunk_root; |
| 6454 | |
| 6455 | path = btrfs_alloc_path(); |
| 6456 | if (!path) |
| 6457 | return -ENOMEM; |
| 6458 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6459 | mutex_lock(&uuid_mutex); |
| 6460 | lock_chunks(root); |
| 6461 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6462 | /* |
| 6463 | * Read all device items, and then all the chunk items. All |
| 6464 | * device items are found before any chunk item (their object id |
| 6465 | * is smaller than the lowest possible object id for a chunk |
| 6466 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6467 | */ |
| 6468 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6469 | key.offset = 0; |
| 6470 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6471 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6472 | if (ret < 0) |
| 6473 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6474 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6475 | leaf = path->nodes[0]; |
| 6476 | slot = path->slots[0]; |
| 6477 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6478 | ret = btrfs_next_leaf(root, path); |
| 6479 | if (ret == 0) |
| 6480 | continue; |
| 6481 | if (ret < 0) |
| 6482 | goto error; |
| 6483 | break; |
| 6484 | } |
| 6485 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6486 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6487 | struct btrfs_dev_item *dev_item; |
| 6488 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6489 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6490 | ret = read_one_dev(root, leaf, dev_item); |
| 6491 | if (ret) |
| 6492 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6493 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6494 | struct btrfs_chunk *chunk; |
| 6495 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6496 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6497 | if (ret) |
| 6498 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6499 | } |
| 6500 | path->slots[0]++; |
| 6501 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6502 | ret = 0; |
| 6503 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6504 | unlock_chunks(root); |
| 6505 | mutex_unlock(&uuid_mutex); |
| 6506 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6507 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6508 | return ret; |
| 6509 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6510 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6511 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6512 | { |
| 6513 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6514 | struct btrfs_device *device; |
| 6515 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6516 | while (fs_devices) { |
| 6517 | mutex_lock(&fs_devices->device_list_mutex); |
| 6518 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6519 | device->dev_root = fs_info->dev_root; |
| 6520 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6521 | |
| 6522 | fs_devices = fs_devices->seed; |
| 6523 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6524 | } |
| 6525 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6526 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6527 | { |
| 6528 | int i; |
| 6529 | |
| 6530 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6531 | btrfs_dev_stat_reset(dev, i); |
| 6532 | } |
| 6533 | |
| 6534 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6535 | { |
| 6536 | struct btrfs_key key; |
| 6537 | struct btrfs_key found_key; |
| 6538 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6539 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6540 | struct extent_buffer *eb; |
| 6541 | int slot; |
| 6542 | int ret = 0; |
| 6543 | struct btrfs_device *device; |
| 6544 | struct btrfs_path *path = NULL; |
| 6545 | int i; |
| 6546 | |
| 6547 | path = btrfs_alloc_path(); |
| 6548 | if (!path) { |
| 6549 | ret = -ENOMEM; |
| 6550 | goto out; |
| 6551 | } |
| 6552 | |
| 6553 | mutex_lock(&fs_devices->device_list_mutex); |
| 6554 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6555 | int item_size; |
| 6556 | struct btrfs_dev_stats_item *ptr; |
| 6557 | |
| 6558 | key.objectid = 0; |
| 6559 | key.type = BTRFS_DEV_STATS_KEY; |
| 6560 | key.offset = device->devid; |
| 6561 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6562 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6563 | __btrfs_reset_dev_stats(device); |
| 6564 | device->dev_stats_valid = 1; |
| 6565 | btrfs_release_path(path); |
| 6566 | continue; |
| 6567 | } |
| 6568 | slot = path->slots[0]; |
| 6569 | eb = path->nodes[0]; |
| 6570 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6571 | item_size = btrfs_item_size_nr(eb, slot); |
| 6572 | |
| 6573 | ptr = btrfs_item_ptr(eb, slot, |
| 6574 | struct btrfs_dev_stats_item); |
| 6575 | |
| 6576 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6577 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6578 | btrfs_dev_stat_set(device, i, |
| 6579 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6580 | else |
| 6581 | btrfs_dev_stat_reset(device, i); |
| 6582 | } |
| 6583 | |
| 6584 | device->dev_stats_valid = 1; |
| 6585 | btrfs_dev_stat_print_on_load(device); |
| 6586 | btrfs_release_path(path); |
| 6587 | } |
| 6588 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6589 | |
| 6590 | out: |
| 6591 | btrfs_free_path(path); |
| 6592 | return ret < 0 ? ret : 0; |
| 6593 | } |
| 6594 | |
| 6595 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6596 | struct btrfs_root *dev_root, |
| 6597 | struct btrfs_device *device) |
| 6598 | { |
| 6599 | struct btrfs_path *path; |
| 6600 | struct btrfs_key key; |
| 6601 | struct extent_buffer *eb; |
| 6602 | struct btrfs_dev_stats_item *ptr; |
| 6603 | int ret; |
| 6604 | int i; |
| 6605 | |
| 6606 | key.objectid = 0; |
| 6607 | key.type = BTRFS_DEV_STATS_KEY; |
| 6608 | key.offset = device->devid; |
| 6609 | |
| 6610 | path = btrfs_alloc_path(); |
| 6611 | BUG_ON(!path); |
| 6612 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6613 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6614 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6615 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6616 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6617 | goto out; |
| 6618 | } |
| 6619 | |
| 6620 | if (ret == 0 && |
| 6621 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6622 | /* need to delete old one and insert a new one */ |
| 6623 | ret = btrfs_del_item(trans, dev_root, path); |
| 6624 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6625 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6626 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6627 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6628 | goto out; |
| 6629 | } |
| 6630 | ret = 1; |
| 6631 | } |
| 6632 | |
| 6633 | if (ret == 1) { |
| 6634 | /* need to insert a new item */ |
| 6635 | btrfs_release_path(path); |
| 6636 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6637 | &key, sizeof(*ptr)); |
| 6638 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6639 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6640 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6641 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6642 | goto out; |
| 6643 | } |
| 6644 | } |
| 6645 | |
| 6646 | eb = path->nodes[0]; |
| 6647 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6648 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6649 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6650 | btrfs_dev_stat_read(device, i)); |
| 6651 | btrfs_mark_buffer_dirty(eb); |
| 6652 | |
| 6653 | out: |
| 6654 | btrfs_free_path(path); |
| 6655 | return ret; |
| 6656 | } |
| 6657 | |
| 6658 | /* |
| 6659 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6660 | */ |
| 6661 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6662 | struct btrfs_fs_info *fs_info) |
| 6663 | { |
| 6664 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6665 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6666 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6667 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6668 | int ret = 0; |
| 6669 | |
| 6670 | mutex_lock(&fs_devices->device_list_mutex); |
| 6671 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6672 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6673 | continue; |
| 6674 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6675 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6676 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6677 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6678 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6679 | } |
| 6680 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6681 | |
| 6682 | return ret; |
| 6683 | } |
| 6684 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6685 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6686 | { |
| 6687 | btrfs_dev_stat_inc(dev, index); |
| 6688 | btrfs_dev_stat_print_on_error(dev); |
| 6689 | } |
| 6690 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6691 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6692 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6693 | if (!dev->dev_stats_valid) |
| 6694 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6695 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6696 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6697 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6698 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6699 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6700 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6701 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6702 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6703 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6704 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6705 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6706 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6707 | int i; |
| 6708 | |
| 6709 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6710 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6711 | break; |
| 6712 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6713 | return; /* all values == 0, suppress message */ |
| 6714 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6715 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6716 | "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6717 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6718 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6719 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6720 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6721 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6722 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6723 | } |
| 6724 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6725 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6726 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6727 | { |
| 6728 | struct btrfs_device *dev; |
| 6729 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6730 | int i; |
| 6731 | |
| 6732 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6733 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6734 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6735 | |
| 6736 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6737 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6738 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6739 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6740 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6741 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6742 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6743 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6744 | if (stats->nr_items > i) |
| 6745 | stats->values[i] = |
| 6746 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6747 | else |
| 6748 | btrfs_dev_stat_reset(dev, i); |
| 6749 | } |
| 6750 | } else { |
| 6751 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6752 | if (stats->nr_items > i) |
| 6753 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6754 | } |
| 6755 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6756 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6757 | return 0; |
| 6758 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6759 | |
| 6760 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6761 | { |
| 6762 | struct buffer_head *bh; |
| 6763 | struct btrfs_super_block *disk_super; |
| 6764 | |
| 6765 | bh = btrfs_read_dev_super(device->bdev); |
| 6766 | if (!bh) |
| 6767 | return -EINVAL; |
| 6768 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6769 | |
| 6770 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6771 | set_buffer_dirty(bh); |
| 6772 | sync_dirty_buffer(bh); |
| 6773 | brelse(bh); |
| 6774 | |
| 6775 | return 0; |
| 6776 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6777 | |
| 6778 | /* |
| 6779 | * Update the size of all devices, which is used for writing out the |
| 6780 | * super blocks. |
| 6781 | */ |
| 6782 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6783 | { |
| 6784 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6785 | struct btrfs_device *curr, *next; |
| 6786 | |
| 6787 | if (list_empty(&fs_devices->resized_devices)) |
| 6788 | return; |
| 6789 | |
| 6790 | mutex_lock(&fs_devices->device_list_mutex); |
| 6791 | lock_chunks(fs_info->dev_root); |
| 6792 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6793 | resized_list) { |
| 6794 | list_del_init(&curr->resized_list); |
| 6795 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6796 | } |
| 6797 | unlock_chunks(fs_info->dev_root); |
| 6798 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6799 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6800 | |
| 6801 | /* Must be invoked during the transaction commit */ |
| 6802 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6803 | struct btrfs_transaction *transaction) |
| 6804 | { |
| 6805 | struct extent_map *em; |
| 6806 | struct map_lookup *map; |
| 6807 | struct btrfs_device *dev; |
| 6808 | int i; |
| 6809 | |
| 6810 | if (list_empty(&transaction->pending_chunks)) |
| 6811 | return; |
| 6812 | |
| 6813 | /* In order to kick the device replace finish process */ |
| 6814 | lock_chunks(root); |
| 6815 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6816 | map = (struct map_lookup *)em->bdev; |
| 6817 | |
| 6818 | for (i = 0; i < map->num_stripes; i++) { |
| 6819 | dev = map->stripes[i].dev; |
| 6820 | dev->commit_bytes_used = dev->bytes_used; |
| 6821 | } |
| 6822 | } |
| 6823 | unlock_chunks(root); |
| 6824 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 6825 | |
| 6826 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6827 | { |
| 6828 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6829 | while (fs_devices) { |
| 6830 | fs_devices->fs_info = fs_info; |
| 6831 | fs_devices = fs_devices->seed; |
| 6832 | } |
| 6833 | } |
| 6834 | |
| 6835 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6836 | { |
| 6837 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6838 | while (fs_devices) { |
| 6839 | fs_devices->fs_info = NULL; |
| 6840 | fs_devices = fs_devices->seed; |
| 6841 | } |
| 6842 | } |