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, |
| 51 | .devs_increment = 2, |
| 52 | .ncopies = 2, |
| 53 | }, |
| 54 | [BTRFS_RAID_RAID1] = { |
| 55 | .sub_stripes = 1, |
| 56 | .dev_stripes = 1, |
| 57 | .devs_max = 2, |
| 58 | .devs_min = 2, |
| 59 | .devs_increment = 2, |
| 60 | .ncopies = 2, |
| 61 | }, |
| 62 | [BTRFS_RAID_DUP] = { |
| 63 | .sub_stripes = 1, |
| 64 | .dev_stripes = 2, |
| 65 | .devs_max = 1, |
| 66 | .devs_min = 1, |
| 67 | .devs_increment = 1, |
| 68 | .ncopies = 2, |
| 69 | }, |
| 70 | [BTRFS_RAID_RAID0] = { |
| 71 | .sub_stripes = 1, |
| 72 | .dev_stripes = 1, |
| 73 | .devs_max = 0, |
| 74 | .devs_min = 2, |
| 75 | .devs_increment = 1, |
| 76 | .ncopies = 1, |
| 77 | }, |
| 78 | [BTRFS_RAID_SINGLE] = { |
| 79 | .sub_stripes = 1, |
| 80 | .dev_stripes = 1, |
| 81 | .devs_max = 1, |
| 82 | .devs_min = 1, |
| 83 | .devs_increment = 1, |
| 84 | .ncopies = 1, |
| 85 | }, |
| 86 | [BTRFS_RAID_RAID5] = { |
| 87 | .sub_stripes = 1, |
| 88 | .dev_stripes = 1, |
| 89 | .devs_max = 0, |
| 90 | .devs_min = 2, |
| 91 | .devs_increment = 1, |
| 92 | .ncopies = 2, |
| 93 | }, |
| 94 | [BTRFS_RAID_RAID6] = { |
| 95 | .sub_stripes = 1, |
| 96 | .dev_stripes = 1, |
| 97 | .devs_max = 0, |
| 98 | .devs_min = 3, |
| 99 | .devs_increment = 1, |
| 100 | .ncopies = 3, |
| 101 | }, |
| 102 | }; |
| 103 | |
| 104 | const u64 const btrfs_raid_group[BTRFS_NR_RAID_TYPES] = { |
| 105 | [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10, |
| 106 | [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1, |
| 107 | [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP, |
| 108 | [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0, |
| 109 | [BTRFS_RAID_SINGLE] = 0, |
| 110 | [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5, |
| 111 | [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6, |
| 112 | }; |
| 113 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 114 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 115 | struct btrfs_root *root, |
| 116 | struct btrfs_device *device); |
| 117 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 118 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 119 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 120 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 121 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 122 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 123 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 124 | struct list_head *btrfs_get_fs_uuids(void) |
| 125 | { |
| 126 | return &fs_uuids; |
| 127 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 128 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 129 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 130 | { |
| 131 | struct btrfs_fs_devices *fs_devs; |
| 132 | |
| 133 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 134 | if (!fs_devs) |
| 135 | return ERR_PTR(-ENOMEM); |
| 136 | |
| 137 | mutex_init(&fs_devs->device_list_mutex); |
| 138 | |
| 139 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 140 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 141 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 142 | INIT_LIST_HEAD(&fs_devs->list); |
| 143 | |
| 144 | return fs_devs; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 149 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 150 | * generated. |
| 151 | * |
| 152 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 153 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 154 | * can be destroyed with kfree() right away. |
| 155 | */ |
| 156 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 157 | { |
| 158 | struct btrfs_fs_devices *fs_devs; |
| 159 | |
| 160 | fs_devs = __alloc_fs_devices(); |
| 161 | if (IS_ERR(fs_devs)) |
| 162 | return fs_devs; |
| 163 | |
| 164 | if (fsid) |
| 165 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 166 | else |
| 167 | generate_random_uuid(fs_devs->fsid); |
| 168 | |
| 169 | return fs_devs; |
| 170 | } |
| 171 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 172 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 173 | { |
| 174 | struct btrfs_device *device; |
| 175 | WARN_ON(fs_devices->opened); |
| 176 | while (!list_empty(&fs_devices->devices)) { |
| 177 | device = list_entry(fs_devices->devices.next, |
| 178 | struct btrfs_device, dev_list); |
| 179 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 180 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 181 | kfree(device); |
| 182 | } |
| 183 | kfree(fs_devices); |
| 184 | } |
| 185 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 186 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 187 | enum kobject_action action) |
| 188 | { |
| 189 | int ret; |
| 190 | |
| 191 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 192 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 193 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 194 | action, |
| 195 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 196 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 197 | } |
| 198 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 199 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 200 | { |
| 201 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 202 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 203 | while (!list_empty(&fs_uuids)) { |
| 204 | fs_devices = list_entry(fs_uuids.next, |
| 205 | struct btrfs_fs_devices, list); |
| 206 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 207 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 208 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 211 | static struct btrfs_device *__alloc_device(void) |
| 212 | { |
| 213 | struct btrfs_device *dev; |
| 214 | |
| 215 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 216 | if (!dev) |
| 217 | return ERR_PTR(-ENOMEM); |
| 218 | |
| 219 | INIT_LIST_HEAD(&dev->dev_list); |
| 220 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 221 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 222 | |
| 223 | spin_lock_init(&dev->io_lock); |
| 224 | |
| 225 | spin_lock_init(&dev->reada_lock); |
| 226 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 227 | atomic_set(&dev->dev_stats_ccnt, 0); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 228 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 229 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 230 | |
| 231 | return dev; |
| 232 | } |
| 233 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 234 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 235 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 236 | { |
| 237 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 238 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 239 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 240 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 241 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 242 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 243 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 244 | } |
| 245 | return NULL; |
| 246 | } |
| 247 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 248 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 249 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 250 | struct btrfs_fs_devices *fs_devices; |
| 251 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 252 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 253 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 254 | return fs_devices; |
| 255 | } |
| 256 | return NULL; |
| 257 | } |
| 258 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 259 | static int |
| 260 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 261 | int flush, struct block_device **bdev, |
| 262 | struct buffer_head **bh) |
| 263 | { |
| 264 | int ret; |
| 265 | |
| 266 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 267 | |
| 268 | if (IS_ERR(*bdev)) { |
| 269 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 270 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 271 | goto error; |
| 272 | } |
| 273 | |
| 274 | if (flush) |
| 275 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 276 | ret = set_blocksize(*bdev, 4096); |
| 277 | if (ret) { |
| 278 | blkdev_put(*bdev, flags); |
| 279 | goto error; |
| 280 | } |
| 281 | invalidate_bdev(*bdev); |
| 282 | *bh = btrfs_read_dev_super(*bdev); |
| 283 | if (!*bh) { |
| 284 | ret = -EINVAL; |
| 285 | blkdev_put(*bdev, flags); |
| 286 | goto error; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | |
| 291 | error: |
| 292 | *bdev = NULL; |
| 293 | *bh = NULL; |
| 294 | return ret; |
| 295 | } |
| 296 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 297 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 298 | struct bio *head, struct bio *tail) |
| 299 | { |
| 300 | |
| 301 | struct bio *old_head; |
| 302 | |
| 303 | old_head = pending_bios->head; |
| 304 | pending_bios->head = head; |
| 305 | if (pending_bios->tail) |
| 306 | tail->bi_next = old_head; |
| 307 | else |
| 308 | pending_bios->tail = tail; |
| 309 | } |
| 310 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 311 | /* |
| 312 | * we try to collect pending bios for a device so we don't get a large |
| 313 | * number of procs sending bios down to the same device. This greatly |
| 314 | * improves the schedulers ability to collect and merge the bios. |
| 315 | * |
| 316 | * But, it also turns into a long list of bios to process and that is sure |
| 317 | * to eventually make the worker thread block. The solution here is to |
| 318 | * make some progress and then put this work struct back at the end of |
| 319 | * the list if the block device is congested. This way, multiple devices |
| 320 | * can make progress from a single worker thread. |
| 321 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 322 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 323 | { |
| 324 | struct bio *pending; |
| 325 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 326 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 327 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 328 | struct bio *tail; |
| 329 | struct bio *cur; |
| 330 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 331 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 332 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 333 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 334 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 335 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 336 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 337 | struct blk_plug plug; |
| 338 | |
| 339 | /* |
| 340 | * this function runs all the bios we've collected for |
| 341 | * a particular device. We don't want to wander off to |
| 342 | * another device without first sending all of these down. |
| 343 | * So, setup a plug here and finish it off before we return |
| 344 | */ |
| 345 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 346 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 347 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 348 | fs_info = device->dev_root->fs_info; |
| 349 | limit = btrfs_async_submit_limit(fs_info); |
| 350 | limit = limit * 2 / 3; |
| 351 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 352 | loop: |
| 353 | spin_lock(&device->io_lock); |
| 354 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 355 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 356 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 357 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 358 | /* take all the bios off the list at once and process them |
| 359 | * later on (without the lock held). But, remember the |
| 360 | * tail and other pointers so the bios can be properly reinserted |
| 361 | * into the list if we hit congestion |
| 362 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 363 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 364 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 365 | force_reg = 1; |
| 366 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 367 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 368 | force_reg = 0; |
| 369 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 370 | |
| 371 | pending = pending_bios->head; |
| 372 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 373 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * if pending was null this time around, no bios need processing |
| 377 | * at all and we can stop. Otherwise it'll loop back up again |
| 378 | * and do an additional check so no bios are missed. |
| 379 | * |
| 380 | * device->running_pending is used to synchronize with the |
| 381 | * schedule_bio code. |
| 382 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 383 | if (device->pending_sync_bios.head == NULL && |
| 384 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 385 | again = 0; |
| 386 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 387 | } else { |
| 388 | again = 1; |
| 389 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 390 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 391 | |
| 392 | pending_bios->head = NULL; |
| 393 | pending_bios->tail = NULL; |
| 394 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 395 | spin_unlock(&device->io_lock); |
| 396 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 397 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 398 | |
| 399 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 400 | /* we want to work on both lists, but do more bios on the |
| 401 | * sync list than the regular list |
| 402 | */ |
| 403 | if ((num_run > 32 && |
| 404 | pending_bios != &device->pending_sync_bios && |
| 405 | device->pending_sync_bios.head) || |
| 406 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 407 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 408 | spin_lock(&device->io_lock); |
| 409 | requeue_list(pending_bios, pending, tail); |
| 410 | goto loop_lock; |
| 411 | } |
| 412 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 413 | cur = pending; |
| 414 | pending = pending->bi_next; |
| 415 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 416 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 417 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 418 | waitqueue_active(&fs_info->async_submit_wait)) |
| 419 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 420 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 421 | BUG_ON(atomic_read(&cur->__bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 422 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 423 | /* |
| 424 | * if we're doing the sync list, record that our |
| 425 | * plug has some sync requests on it |
| 426 | * |
| 427 | * If we're doing the regular list and there are |
| 428 | * sync requests sitting around, unplug before |
| 429 | * we add more |
| 430 | */ |
| 431 | if (pending_bios == &device->pending_sync_bios) { |
| 432 | sync_pending = 1; |
| 433 | } else if (sync_pending) { |
| 434 | blk_finish_plug(&plug); |
| 435 | blk_start_plug(&plug); |
| 436 | sync_pending = 0; |
| 437 | } |
| 438 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 439 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 440 | num_run++; |
| 441 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 442 | |
| 443 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * we made progress, there is more work to do and the bdi |
| 447 | * is now congested. Back off and let other work structs |
| 448 | * run instead |
| 449 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 450 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 451 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 452 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 453 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 454 | ioc = current->io_context; |
| 455 | |
| 456 | /* |
| 457 | * the main goal here is that we don't want to |
| 458 | * block if we're going to be able to submit |
| 459 | * more requests without blocking. |
| 460 | * |
| 461 | * This code does two great things, it pokes into |
| 462 | * the elevator code from a filesystem _and_ |
| 463 | * it makes assumptions about how batching works. |
| 464 | */ |
| 465 | if (ioc && ioc->nr_batch_requests > 0 && |
| 466 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 467 | (last_waited == 0 || |
| 468 | ioc->last_waited == last_waited)) { |
| 469 | /* |
| 470 | * we want to go through our batch of |
| 471 | * requests and stop. So, we copy out |
| 472 | * the ioc->last_waited time and test |
| 473 | * against it before looping |
| 474 | */ |
| 475 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 476 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 477 | continue; |
| 478 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 479 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 480 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 481 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 482 | |
| 483 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 484 | btrfs_queue_work(fs_info->submit_workers, |
| 485 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 486 | goto done; |
| 487 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 488 | /* unplug every 64 requests just for good measure */ |
| 489 | if (batch_run % 64 == 0) { |
| 490 | blk_finish_plug(&plug); |
| 491 | blk_start_plug(&plug); |
| 492 | sync_pending = 0; |
| 493 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 494 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 495 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 496 | cond_resched(); |
| 497 | if (again) |
| 498 | goto loop; |
| 499 | |
| 500 | spin_lock(&device->io_lock); |
| 501 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 502 | goto loop_lock; |
| 503 | spin_unlock(&device->io_lock); |
| 504 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 505 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 506 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 509 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 510 | { |
| 511 | struct btrfs_device *device; |
| 512 | |
| 513 | device = container_of(work, struct btrfs_device, work); |
| 514 | run_scheduled_bios(device); |
| 515 | } |
| 516 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 517 | |
| 518 | void btrfs_free_stale_device(struct btrfs_device *cur_dev) |
| 519 | { |
| 520 | struct btrfs_fs_devices *fs_devs; |
| 521 | struct btrfs_device *dev; |
| 522 | |
| 523 | if (!cur_dev->name) |
| 524 | return; |
| 525 | |
| 526 | list_for_each_entry(fs_devs, &fs_uuids, list) { |
| 527 | int del = 1; |
| 528 | |
| 529 | if (fs_devs->opened) |
| 530 | continue; |
| 531 | if (fs_devs->seeding) |
| 532 | continue; |
| 533 | |
| 534 | list_for_each_entry(dev, &fs_devs->devices, dev_list) { |
| 535 | |
| 536 | if (dev == cur_dev) |
| 537 | continue; |
| 538 | if (!dev->name) |
| 539 | continue; |
| 540 | |
| 541 | /* |
| 542 | * Todo: This won't be enough. What if the same device |
| 543 | * comes back (with new uuid and) with its mapper path? |
| 544 | * But for now, this does help as mostly an admin will |
| 545 | * either use mapper or non mapper path throughout. |
| 546 | */ |
| 547 | rcu_read_lock(); |
| 548 | del = strcmp(rcu_str_deref(dev->name), |
| 549 | rcu_str_deref(cur_dev->name)); |
| 550 | rcu_read_unlock(); |
| 551 | if (!del) |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | if (!del) { |
| 556 | /* delete the stale device */ |
| 557 | if (fs_devs->num_devices == 1) { |
| 558 | btrfs_sysfs_remove_fsid(fs_devs); |
| 559 | list_del(&fs_devs->list); |
| 560 | free_fs_devices(fs_devs); |
| 561 | } else { |
| 562 | fs_devs->num_devices--; |
| 563 | list_del(&dev->dev_list); |
| 564 | rcu_string_free(dev->name); |
| 565 | kfree(dev); |
| 566 | } |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 572 | /* |
| 573 | * Add new device to list of registered devices |
| 574 | * |
| 575 | * Returns: |
| 576 | * 1 - first time device is seen |
| 577 | * 0 - device already known |
| 578 | * < 0 - error |
| 579 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 580 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 581 | struct btrfs_super_block *disk_super, |
| 582 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 583 | { |
| 584 | struct btrfs_device *device; |
| 585 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 586 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 587 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 588 | u64 found_transid = btrfs_super_generation(disk_super); |
| 589 | |
| 590 | fs_devices = find_fsid(disk_super->fsid); |
| 591 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 592 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 593 | if (IS_ERR(fs_devices)) |
| 594 | return PTR_ERR(fs_devices); |
| 595 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 596 | list_add(&fs_devices->list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 597 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 598 | device = NULL; |
| 599 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 600 | device = __find_device(&fs_devices->devices, devid, |
| 601 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 602 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 603 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 604 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 605 | if (fs_devices->opened) |
| 606 | return -EBUSY; |
| 607 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 608 | device = btrfs_alloc_device(NULL, &devid, |
| 609 | disk_super->dev_item.uuid); |
| 610 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 611 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 612 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 613 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 614 | |
| 615 | name = rcu_string_strdup(path, GFP_NOFS); |
| 616 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 617 | kfree(device); |
| 618 | return -ENOMEM; |
| 619 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 620 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 621 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 622 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 623 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 624 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 625 | mutex_unlock(&fs_devices->device_list_mutex); |
| 626 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 627 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 628 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 629 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 630 | /* |
| 631 | * When FS is already mounted. |
| 632 | * 1. If you are here and if the device->name is NULL that |
| 633 | * means this device was missing at time of FS mount. |
| 634 | * 2. If you are here and if the device->name is different |
| 635 | * from 'path' that means either |
| 636 | * a. The same device disappeared and reappeared with |
| 637 | * different name. or |
| 638 | * b. The missing-disk-which-was-replaced, has |
| 639 | * reappeared now. |
| 640 | * |
| 641 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 642 | * and unintentional. |
| 643 | * |
| 644 | * Further in case of 1 and 2a above, the disk at 'path' |
| 645 | * would have missed some transaction when it was away and |
| 646 | * in case of 2a the stale bdev has to be updated as well. |
| 647 | * 2b must not be allowed at all time. |
| 648 | */ |
| 649 | |
| 650 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 651 | * For now, we do allow update to btrfs_fs_device through the |
| 652 | * btrfs dev scan cli after FS has been mounted. We're still |
| 653 | * tracking a problem where systems fail mount by subvolume id |
| 654 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 655 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 656 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 657 | /* |
| 658 | * That is if the FS is _not_ mounted and if you |
| 659 | * are here, that means there is more than one |
| 660 | * disk with same uuid and devid.We keep the one |
| 661 | * with larger generation number or the last-in if |
| 662 | * generation are equal. |
| 663 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 664 | return -EEXIST; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 665 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 666 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 667 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 668 | if (!name) |
| 669 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 670 | rcu_string_free(device->name); |
| 671 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 672 | if (device->missing) { |
| 673 | fs_devices->missing_devices--; |
| 674 | device->missing = 0; |
| 675 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 678 | /* |
| 679 | * Unmount does not free the btrfs_device struct but would zero |
| 680 | * generation along with most of the other members. So just update |
| 681 | * it back. We need it to pick the disk with largest generation |
| 682 | * (as above). |
| 683 | */ |
| 684 | if (!fs_devices->opened) |
| 685 | device->generation = found_transid; |
| 686 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 687 | /* |
| 688 | * if there is new btrfs on an already registered device, |
| 689 | * then remove the stale device entry. |
| 690 | */ |
| 691 | btrfs_free_stale_device(device); |
| 692 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 693 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 694 | |
| 695 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 696 | } |
| 697 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 698 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 699 | { |
| 700 | struct btrfs_fs_devices *fs_devices; |
| 701 | struct btrfs_device *device; |
| 702 | struct btrfs_device *orig_dev; |
| 703 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 704 | fs_devices = alloc_fs_devices(orig->fsid); |
| 705 | if (IS_ERR(fs_devices)) |
| 706 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 707 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 708 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 709 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 710 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 711 | /* 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] | 712 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 713 | struct rcu_string *name; |
| 714 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 715 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 716 | orig_dev->uuid); |
| 717 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 718 | goto error; |
| 719 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 720 | /* |
| 721 | * This is ok to do without rcu read locked because we hold the |
| 722 | * uuid mutex so nothing we touch in here is going to disappear. |
| 723 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 724 | if (orig_dev->name) { |
| 725 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 726 | if (!name) { |
| 727 | kfree(device); |
| 728 | goto error; |
| 729 | } |
| 730 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 731 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 732 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 733 | list_add(&device->dev_list, &fs_devices->devices); |
| 734 | device->fs_devices = fs_devices; |
| 735 | fs_devices->num_devices++; |
| 736 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 737 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 738 | return fs_devices; |
| 739 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 740 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 741 | free_fs_devices(fs_devices); |
| 742 | return ERR_PTR(-ENOMEM); |
| 743 | } |
| 744 | |
Eric Sandeen | 9eaed21 | 2014-08-01 18:12:35 -0500 | [diff] [blame] | 745 | 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] | 746 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 747 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 748 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 749 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 750 | mutex_lock(&uuid_mutex); |
| 751 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 752 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 753 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 754 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 755 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 756 | (!latest_dev || |
| 757 | device->generation > latest_dev->generation)) { |
| 758 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 759 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 760 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 761 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 762 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 763 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 764 | /* |
| 765 | * In the first step, keep the device which has |
| 766 | * the correct fsid and the devid that is used |
| 767 | * for the dev_replace procedure. |
| 768 | * In the second step, the dev_replace state is |
| 769 | * read from the device tree and it is known |
| 770 | * whether the procedure is really active or |
| 771 | * not, which means whether this device is |
| 772 | * used or whether it should be removed. |
| 773 | */ |
| 774 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 775 | continue; |
| 776 | } |
| 777 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 778 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 779 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 780 | device->bdev = NULL; |
| 781 | fs_devices->open_devices--; |
| 782 | } |
| 783 | if (device->writeable) { |
| 784 | list_del_init(&device->dev_alloc_list); |
| 785 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 786 | if (!device->is_tgtdev_for_dev_replace) |
| 787 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 788 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 789 | list_del_init(&device->dev_list); |
| 790 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 791 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 792 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 793 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 794 | |
| 795 | if (fs_devices->seed) { |
| 796 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 797 | goto again; |
| 798 | } |
| 799 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 800 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 801 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 802 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 803 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 804 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 805 | static void __free_device(struct work_struct *work) |
| 806 | { |
| 807 | struct btrfs_device *device; |
| 808 | |
| 809 | device = container_of(work, struct btrfs_device, rcu_work); |
| 810 | |
| 811 | if (device->bdev) |
| 812 | blkdev_put(device->bdev, device->mode); |
| 813 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 814 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 815 | kfree(device); |
| 816 | } |
| 817 | |
| 818 | static void free_device(struct rcu_head *head) |
| 819 | { |
| 820 | struct btrfs_device *device; |
| 821 | |
| 822 | device = container_of(head, struct btrfs_device, rcu); |
| 823 | |
| 824 | INIT_WORK(&device->rcu_work, __free_device); |
| 825 | schedule_work(&device->rcu_work); |
| 826 | } |
| 827 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 828 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 829 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 830 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 831 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 832 | if (--fs_devices->opened > 0) |
| 833 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 834 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 835 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 836 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 837 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 838 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 839 | |
| 840 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 841 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 842 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 843 | if (device->writeable && |
| 844 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 845 | list_del_init(&device->dev_alloc_list); |
| 846 | fs_devices->rw_devices--; |
| 847 | } |
| 848 | |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 849 | if (device->missing) |
| 850 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 851 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 852 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 853 | device->uuid); |
| 854 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 855 | |
| 856 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 857 | if (device->name) { |
| 858 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 859 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 860 | rcu_assign_pointer(new_device->name, name); |
| 861 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 862 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 863 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 864 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 865 | |
| 866 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 867 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 868 | mutex_unlock(&fs_devices->device_list_mutex); |
| 869 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 870 | WARN_ON(fs_devices->open_devices); |
| 871 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 872 | fs_devices->opened = 0; |
| 873 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 874 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 878 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 879 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 880 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 881 | int ret; |
| 882 | |
| 883 | mutex_lock(&uuid_mutex); |
| 884 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 885 | if (!fs_devices->opened) { |
| 886 | seed_devices = fs_devices->seed; |
| 887 | fs_devices->seed = NULL; |
| 888 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 889 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 890 | |
| 891 | while (seed_devices) { |
| 892 | fs_devices = seed_devices; |
| 893 | seed_devices = fs_devices->seed; |
| 894 | __btrfs_close_devices(fs_devices); |
| 895 | free_fs_devices(fs_devices); |
| 896 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 897 | /* |
| 898 | * Wait for rcu kworkers under __btrfs_close_devices |
| 899 | * to finish all blkdev_puts so device is really |
| 900 | * free when umount is done. |
| 901 | */ |
| 902 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 903 | return ret; |
| 904 | } |
| 905 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 906 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 907 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 908 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 909 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 910 | struct block_device *bdev; |
| 911 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 912 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 913 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 914 | struct buffer_head *bh; |
| 915 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 916 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 917 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 918 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 919 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 920 | flags |= FMODE_EXCL; |
| 921 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 922 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 923 | if (device->bdev) |
| 924 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 925 | if (!device->name) |
| 926 | continue; |
| 927 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 928 | /* Just open everything we can; ignore failures here */ |
| 929 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 930 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 931 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 932 | |
| 933 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 934 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 935 | if (devid != device->devid) |
| 936 | goto error_brelse; |
| 937 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 938 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 939 | BTRFS_UUID_SIZE)) |
| 940 | goto error_brelse; |
| 941 | |
| 942 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 943 | if (!latest_dev || |
| 944 | device->generation > latest_dev->generation) |
| 945 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 946 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 947 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 948 | device->writeable = 0; |
| 949 | } else { |
| 950 | device->writeable = !bdev_read_only(bdev); |
| 951 | seeding = 0; |
| 952 | } |
| 953 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 954 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 955 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 956 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 957 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 958 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 959 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 960 | device->mode = flags; |
| 961 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 962 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 963 | fs_devices->rotating = 1; |
| 964 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 965 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 966 | if (device->writeable && |
| 967 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 968 | fs_devices->rw_devices++; |
| 969 | list_add(&device->dev_alloc_list, |
| 970 | &fs_devices->alloc_list); |
| 971 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 972 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 973 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 974 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 975 | error_brelse: |
| 976 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 977 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 978 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 979 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 980 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 981 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 982 | goto out; |
| 983 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 984 | fs_devices->seeding = seeding; |
| 985 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 986 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 987 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 988 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 989 | return ret; |
| 990 | } |
| 991 | |
| 992 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 993 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 994 | { |
| 995 | int ret; |
| 996 | |
| 997 | mutex_lock(&uuid_mutex); |
| 998 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 999 | fs_devices->opened++; |
| 1000 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1001 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1002 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1003 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1004 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1005 | return ret; |
| 1006 | } |
| 1007 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1008 | /* |
| 1009 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 1010 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 1011 | * is read via pagecache |
| 1012 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 1013 | 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] | 1014 | struct btrfs_fs_devices **fs_devices_ret) |
| 1015 | { |
| 1016 | struct btrfs_super_block *disk_super; |
| 1017 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1018 | struct page *page; |
| 1019 | void *p; |
| 1020 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1021 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1022 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1023 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1024 | u64 bytenr; |
| 1025 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1026 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1027 | /* |
| 1028 | * we would like to check all the supers, but that would make |
| 1029 | * a btrfs mount succeed after a mkfs from a different FS. |
| 1030 | * So, we need to add a special mount option to scan for |
| 1031 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 1032 | */ |
| 1033 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1034 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 1035 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1036 | |
| 1037 | bdev = blkdev_get_by_path(path, flags, holder); |
| 1038 | |
| 1039 | if (IS_ERR(bdev)) { |
| 1040 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1041 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /* make sure our super fits in the device */ |
| 1045 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 1046 | goto error_bdev_put; |
| 1047 | |
| 1048 | /* make sure our super fits in the page */ |
| 1049 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 1050 | goto error_bdev_put; |
| 1051 | |
| 1052 | /* make sure our super doesn't straddle pages on disk */ |
| 1053 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 1054 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 1055 | goto error_bdev_put; |
| 1056 | |
| 1057 | /* pull in the page with our super */ |
| 1058 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 1059 | index, GFP_NOFS); |
| 1060 | |
| 1061 | if (IS_ERR_OR_NULL(page)) |
| 1062 | goto error_bdev_put; |
| 1063 | |
| 1064 | p = kmap(page); |
| 1065 | |
| 1066 | /* align our pointer to the offset of the super block */ |
| 1067 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 1068 | |
| 1069 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1070 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1071 | goto error_unmap; |
| 1072 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1073 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1074 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1075 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1076 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1077 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 1078 | if (ret > 0) { |
| 1079 | if (disk_super->label[0]) { |
| 1080 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 1081 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1082 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 1083 | } else { |
| 1084 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 1085 | } |
| 1086 | |
| 1087 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 1088 | ret = 0; |
| 1089 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1090 | if (!ret && fs_devices_ret) |
| 1091 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1092 | |
| 1093 | error_unmap: |
| 1094 | kunmap(page); |
| 1095 | page_cache_release(page); |
| 1096 | |
| 1097 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1098 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1099 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1100 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1101 | return ret; |
| 1102 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1103 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1104 | /* helper to account the used device space in the range */ |
| 1105 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 1106 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1107 | { |
| 1108 | struct btrfs_key key; |
| 1109 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1110 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1111 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1112 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1113 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1114 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1115 | struct extent_buffer *l; |
| 1116 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1117 | *length = 0; |
| 1118 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1119 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1120 | return 0; |
| 1121 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1122 | path = btrfs_alloc_path(); |
| 1123 | if (!path) |
| 1124 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1125 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1126 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1127 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1128 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1129 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1130 | |
| 1131 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1132 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1133 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1134 | if (ret > 0) { |
| 1135 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1136 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1137 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1138 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1139 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1140 | while (1) { |
| 1141 | l = path->nodes[0]; |
| 1142 | slot = path->slots[0]; |
| 1143 | if (slot >= btrfs_header_nritems(l)) { |
| 1144 | ret = btrfs_next_leaf(root, path); |
| 1145 | if (ret == 0) |
| 1146 | continue; |
| 1147 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1148 | goto out; |
| 1149 | |
| 1150 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1151 | } |
| 1152 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1153 | |
| 1154 | if (key.objectid < device->devid) |
| 1155 | goto next; |
| 1156 | |
| 1157 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1158 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1159 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1160 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1161 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1162 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1163 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1164 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1165 | dev_extent); |
| 1166 | if (key.offset <= start && extent_end > end) { |
| 1167 | *length = end - start + 1; |
| 1168 | break; |
| 1169 | } else if (key.offset <= start && extent_end > start) |
| 1170 | *length += extent_end - start; |
| 1171 | else if (key.offset > start && extent_end <= end) |
| 1172 | *length += extent_end - key.offset; |
| 1173 | else if (key.offset > start && key.offset <= end) { |
| 1174 | *length += end - key.offset + 1; |
| 1175 | break; |
| 1176 | } else if (key.offset > end) |
| 1177 | break; |
| 1178 | |
| 1179 | next: |
| 1180 | path->slots[0]++; |
| 1181 | } |
| 1182 | ret = 0; |
| 1183 | out: |
| 1184 | btrfs_free_path(path); |
| 1185 | return ret; |
| 1186 | } |
| 1187 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1188 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1189 | struct btrfs_device *device, |
| 1190 | u64 *start, u64 len) |
| 1191 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1192 | struct btrfs_fs_info *fs_info = device->dev_root->fs_info; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1193 | struct extent_map *em; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1194 | struct list_head *search_list = &fs_info->pinned_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1195 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1196 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1197 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1198 | if (transaction) |
| 1199 | search_list = &transaction->pending_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1200 | again: |
| 1201 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1202 | struct map_lookup *map; |
| 1203 | int i; |
| 1204 | |
| 1205 | map = (struct map_lookup *)em->bdev; |
| 1206 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1207 | u64 end; |
| 1208 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1209 | if (map->stripes[i].dev != device) |
| 1210 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1211 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1212 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1213 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1214 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1215 | /* |
| 1216 | * Make sure that while processing the pinned list we do |
| 1217 | * not override our *start with a lower value, because |
| 1218 | * we can have pinned chunks that fall within this |
| 1219 | * device hole and that have lower physical addresses |
| 1220 | * than the pending chunks we processed before. If we |
| 1221 | * do not take this special care we can end up getting |
| 1222 | * 2 pending chunks that start at the same physical |
| 1223 | * device offsets because the end offset of a pinned |
| 1224 | * chunk can be equal to the start offset of some |
| 1225 | * pending chunk. |
| 1226 | */ |
| 1227 | end = map->stripes[i].physical + em->orig_block_len; |
| 1228 | if (end > *start) { |
| 1229 | *start = end; |
| 1230 | ret = 1; |
| 1231 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1232 | } |
| 1233 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1234 | if (search_list != &fs_info->pinned_chunks) { |
| 1235 | search_list = &fs_info->pinned_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1236 | goto again; |
| 1237 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1238 | |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
| 1242 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1243 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1244 | * find_free_dev_extent_start - find free space in the specified device |
| 1245 | * @device: the device which we search the free space in |
| 1246 | * @num_bytes: the size of the free space that we need |
| 1247 | * @search_start: the position from which to begin the search |
| 1248 | * @start: store the start of the free space. |
| 1249 | * @len: the size of the free space. that we find, or the size |
| 1250 | * of the max free space if we don't find suitable free space |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1251 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1252 | * this uses a pretty simple search, the expectation is that it is |
| 1253 | * called very infrequently and that a given device has a small number |
| 1254 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1255 | * |
| 1256 | * @start is used to store the start of the free space if we find. But if we |
| 1257 | * don't find suitable free space, it will be used to store the start position |
| 1258 | * of the max free space. |
| 1259 | * |
| 1260 | * @len is used to store the size of the free space that we find. |
| 1261 | * But if we don't find suitable free space, it is used to store the size of |
| 1262 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1263 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1264 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
| 1265 | struct btrfs_device *device, u64 num_bytes, |
| 1266 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1267 | { |
| 1268 | struct btrfs_key key; |
| 1269 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1270 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1271 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1272 | u64 hole_size; |
| 1273 | u64 max_hole_start; |
| 1274 | u64 max_hole_size; |
| 1275 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1276 | u64 search_end = device->total_bytes; |
| 1277 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1278 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1279 | struct extent_buffer *l; |
| 1280 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1281 | path = btrfs_alloc_path(); |
| 1282 | if (!path) |
| 1283 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1284 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1285 | max_hole_start = search_start; |
| 1286 | max_hole_size = 0; |
| 1287 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1288 | again: |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1289 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1290 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1291 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1294 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1295 | path->search_commit_root = 1; |
| 1296 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1297 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1298 | key.objectid = device->devid; |
| 1299 | key.offset = search_start; |
| 1300 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1301 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1302 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1303 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1304 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1305 | if (ret > 0) { |
| 1306 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1307 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1308 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1309 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1310 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1311 | while (1) { |
| 1312 | l = path->nodes[0]; |
| 1313 | slot = path->slots[0]; |
| 1314 | if (slot >= btrfs_header_nritems(l)) { |
| 1315 | ret = btrfs_next_leaf(root, path); |
| 1316 | if (ret == 0) |
| 1317 | continue; |
| 1318 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1319 | goto out; |
| 1320 | |
| 1321 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1322 | } |
| 1323 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1324 | |
| 1325 | if (key.objectid < device->devid) |
| 1326 | goto next; |
| 1327 | |
| 1328 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1329 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1330 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1331 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1332 | goto next; |
| 1333 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1334 | if (key.offset > search_start) { |
| 1335 | hole_size = key.offset - search_start; |
| 1336 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1337 | /* |
| 1338 | * Have to check before we set max_hole_start, otherwise |
| 1339 | * we could end up sending back this offset anyway. |
| 1340 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1341 | if (contains_pending_extent(transaction, device, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1342 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1343 | hole_size)) { |
| 1344 | if (key.offset >= search_start) { |
| 1345 | hole_size = key.offset - search_start; |
| 1346 | } else { |
| 1347 | WARN_ON_ONCE(1); |
| 1348 | hole_size = 0; |
| 1349 | } |
| 1350 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1351 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1352 | if (hole_size > max_hole_size) { |
| 1353 | max_hole_start = search_start; |
| 1354 | max_hole_size = hole_size; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * If this free space is greater than which we need, |
| 1359 | * it must be the max free space that we have found |
| 1360 | * until now, so max_hole_start must point to the start |
| 1361 | * of this free space and the length of this free space |
| 1362 | * is stored in max_hole_size. Thus, we return |
| 1363 | * max_hole_start and max_hole_size and go back to the |
| 1364 | * caller. |
| 1365 | */ |
| 1366 | if (hole_size >= num_bytes) { |
| 1367 | ret = 0; |
| 1368 | goto out; |
| 1369 | } |
| 1370 | } |
| 1371 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1372 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1373 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1374 | dev_extent); |
| 1375 | if (extent_end > search_start) |
| 1376 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1377 | next: |
| 1378 | path->slots[0]++; |
| 1379 | cond_resched(); |
| 1380 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1381 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1382 | /* |
| 1383 | * At this point, search_start should be the end of |
| 1384 | * allocated dev extents, and when shrinking the device, |
| 1385 | * search_end may be smaller than search_start. |
| 1386 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1387 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1388 | hole_size = search_end - search_start; |
| 1389 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1390 | if (contains_pending_extent(transaction, device, &search_start, |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1391 | hole_size)) { |
| 1392 | btrfs_release_path(path); |
| 1393 | goto again; |
| 1394 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1395 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1396 | if (hole_size > max_hole_size) { |
| 1397 | max_hole_start = search_start; |
| 1398 | max_hole_size = hole_size; |
| 1399 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1400 | } |
| 1401 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1402 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1403 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1404 | ret = -ENOSPC; |
| 1405 | else |
| 1406 | ret = 0; |
| 1407 | |
| 1408 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1409 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1410 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1411 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1412 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | return ret; |
| 1414 | } |
| 1415 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1416 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1417 | struct btrfs_device *device, u64 num_bytes, |
| 1418 | u64 *start, u64 *len) |
| 1419 | { |
| 1420 | struct btrfs_root *root = device->dev_root; |
| 1421 | u64 search_start; |
| 1422 | |
| 1423 | /* FIXME use last free of some kind */ |
| 1424 | |
| 1425 | /* |
| 1426 | * we don't want to overwrite the superblock on the drive, |
| 1427 | * so we make sure to start at an offset of at least 1MB |
| 1428 | */ |
| 1429 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
| 1430 | return find_free_dev_extent_start(trans->transaction, device, |
| 1431 | num_bytes, search_start, start, len); |
| 1432 | } |
| 1433 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1434 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1435 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1436 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1437 | { |
| 1438 | int ret; |
| 1439 | struct btrfs_path *path; |
| 1440 | struct btrfs_root *root = device->dev_root; |
| 1441 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1442 | struct btrfs_key found_key; |
| 1443 | struct extent_buffer *leaf = NULL; |
| 1444 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1445 | |
| 1446 | path = btrfs_alloc_path(); |
| 1447 | if (!path) |
| 1448 | return -ENOMEM; |
| 1449 | |
| 1450 | key.objectid = device->devid; |
| 1451 | key.offset = start; |
| 1452 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1453 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1454 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1455 | if (ret > 0) { |
| 1456 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1457 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1458 | if (ret) |
| 1459 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1460 | leaf = path->nodes[0]; |
| 1461 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1462 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1463 | struct btrfs_dev_extent); |
| 1464 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1465 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1466 | key = found_key; |
| 1467 | btrfs_release_path(path); |
| 1468 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1469 | } else if (ret == 0) { |
| 1470 | leaf = path->nodes[0]; |
| 1471 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1472 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1473 | } else { |
| 1474 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1475 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1476 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1477 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1478 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1479 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1480 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1481 | if (ret) { |
| 1482 | btrfs_error(root->fs_info, ret, |
| 1483 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1484 | } else { |
| 1485 | trans->transaction->have_free_bgs = 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1486 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1487 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1488 | btrfs_free_path(path); |
| 1489 | return ret; |
| 1490 | } |
| 1491 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1492 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1493 | struct btrfs_device *device, |
| 1494 | u64 chunk_tree, u64 chunk_objectid, |
| 1495 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1496 | { |
| 1497 | int ret; |
| 1498 | struct btrfs_path *path; |
| 1499 | struct btrfs_root *root = device->dev_root; |
| 1500 | struct btrfs_dev_extent *extent; |
| 1501 | struct extent_buffer *leaf; |
| 1502 | struct btrfs_key key; |
| 1503 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1504 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1505 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1506 | path = btrfs_alloc_path(); |
| 1507 | if (!path) |
| 1508 | return -ENOMEM; |
| 1509 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1510 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1511 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1512 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1513 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1514 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1515 | if (ret) |
| 1516 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1517 | |
| 1518 | leaf = path->nodes[0]; |
| 1519 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1520 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1521 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1522 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1523 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1524 | |
| 1525 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1526 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1527 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1528 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1529 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1530 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1531 | btrfs_free_path(path); |
| 1532 | return ret; |
| 1533 | } |
| 1534 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1535 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1536 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1537 | struct extent_map_tree *em_tree; |
| 1538 | struct extent_map *em; |
| 1539 | struct rb_node *n; |
| 1540 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1541 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1542 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1543 | read_lock(&em_tree->lock); |
| 1544 | n = rb_last(&em_tree->map); |
| 1545 | if (n) { |
| 1546 | em = rb_entry(n, struct extent_map, rb_node); |
| 1547 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1548 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1549 | read_unlock(&em_tree->lock); |
| 1550 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1551 | return ret; |
| 1552 | } |
| 1553 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1554 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1555 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1556 | { |
| 1557 | int ret; |
| 1558 | struct btrfs_key key; |
| 1559 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1560 | struct btrfs_path *path; |
| 1561 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1562 | path = btrfs_alloc_path(); |
| 1563 | if (!path) |
| 1564 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1565 | |
| 1566 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1567 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1568 | key.offset = (u64)-1; |
| 1569 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1570 | ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1571 | if (ret < 0) |
| 1572 | goto error; |
| 1573 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1574 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1575 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1576 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1577 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1578 | BTRFS_DEV_ITEM_KEY); |
| 1579 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1580 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1581 | } else { |
| 1582 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1583 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1584 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1585 | } |
| 1586 | ret = 0; |
| 1587 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1588 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1589 | return ret; |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * the device information is stored in the chunk root |
| 1594 | * the btrfs_device struct should be fully filled in |
| 1595 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1596 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1597 | struct btrfs_root *root, |
| 1598 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1599 | { |
| 1600 | int ret; |
| 1601 | struct btrfs_path *path; |
| 1602 | struct btrfs_dev_item *dev_item; |
| 1603 | struct extent_buffer *leaf; |
| 1604 | struct btrfs_key key; |
| 1605 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1606 | |
| 1607 | root = root->fs_info->chunk_root; |
| 1608 | |
| 1609 | path = btrfs_alloc_path(); |
| 1610 | if (!path) |
| 1611 | return -ENOMEM; |
| 1612 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1613 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1614 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1615 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1616 | |
| 1617 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1618 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1619 | if (ret) |
| 1620 | goto out; |
| 1621 | |
| 1622 | leaf = path->nodes[0]; |
| 1623 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1624 | |
| 1625 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1626 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1627 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1628 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1629 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1630 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1631 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1632 | btrfs_device_get_disk_total_bytes(device)); |
| 1633 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1634 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1635 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1636 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1637 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1638 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1639 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1640 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1641 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1642 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1643 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1644 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1645 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1646 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1647 | out: |
| 1648 | btrfs_free_path(path); |
| 1649 | return ret; |
| 1650 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1651 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1652 | /* |
| 1653 | * Function to update ctime/mtime for a given device path. |
| 1654 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1655 | */ |
| 1656 | static void update_dev_time(char *path_name) |
| 1657 | { |
| 1658 | struct file *filp; |
| 1659 | |
| 1660 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1661 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1662 | return; |
| 1663 | file_update_time(filp); |
| 1664 | filp_close(filp, NULL); |
| 1665 | return; |
| 1666 | } |
| 1667 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1668 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1669 | struct btrfs_device *device) |
| 1670 | { |
| 1671 | int ret; |
| 1672 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1673 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1674 | struct btrfs_trans_handle *trans; |
| 1675 | |
| 1676 | root = root->fs_info->chunk_root; |
| 1677 | |
| 1678 | path = btrfs_alloc_path(); |
| 1679 | if (!path) |
| 1680 | return -ENOMEM; |
| 1681 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1682 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1683 | if (IS_ERR(trans)) { |
| 1684 | btrfs_free_path(path); |
| 1685 | return PTR_ERR(trans); |
| 1686 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1687 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1688 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1689 | key.offset = device->devid; |
| 1690 | |
| 1691 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1692 | if (ret < 0) |
| 1693 | goto out; |
| 1694 | |
| 1695 | if (ret > 0) { |
| 1696 | ret = -ENOENT; |
| 1697 | goto out; |
| 1698 | } |
| 1699 | |
| 1700 | ret = btrfs_del_item(trans, root, path); |
| 1701 | if (ret) |
| 1702 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1703 | out: |
| 1704 | btrfs_free_path(path); |
| 1705 | btrfs_commit_transaction(trans, root); |
| 1706 | return ret; |
| 1707 | } |
| 1708 | |
| 1709 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1710 | { |
| 1711 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1712 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1713 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1714 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1715 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1716 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1717 | u64 all_avail; |
| 1718 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1719 | u64 num_devices; |
| 1720 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1721 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1722 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1723 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1724 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1725 | mutex_lock(&uuid_mutex); |
| 1726 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1727 | do { |
| 1728 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1729 | |
| 1730 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1731 | root->fs_info->avail_system_alloc_bits | |
| 1732 | root->fs_info->avail_metadata_alloc_bits; |
| 1733 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1734 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1735 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1736 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1737 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1738 | WARN_ON(num_devices < 1); |
| 1739 | num_devices--; |
| 1740 | } |
| 1741 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1742 | |
| 1743 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1744 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1745 | goto out; |
| 1746 | } |
| 1747 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1748 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1749 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1750 | goto out; |
| 1751 | } |
| 1752 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1753 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1754 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1755 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1756 | goto out; |
| 1757 | } |
| 1758 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1759 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1760 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1761 | goto out; |
| 1762 | } |
| 1763 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1764 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1765 | struct list_head *devices; |
| 1766 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1767 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1768 | device = NULL; |
| 1769 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1770 | /* |
| 1771 | * It is safe to read the devices since the volume_mutex |
| 1772 | * is held. |
| 1773 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1774 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1775 | if (tmp->in_fs_metadata && |
| 1776 | !tmp->is_tgtdev_for_dev_replace && |
| 1777 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1778 | device = tmp; |
| 1779 | break; |
| 1780 | } |
| 1781 | } |
| 1782 | bdev = NULL; |
| 1783 | bh = NULL; |
| 1784 | disk_super = NULL; |
| 1785 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1786 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1787 | goto out; |
| 1788 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1789 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1790 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1791 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1792 | root->fs_info->bdev_holder, 0, |
| 1793 | &bdev, &bh); |
| 1794 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1795 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1796 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1797 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1798 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1799 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1800 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1801 | if (!device) { |
| 1802 | ret = -ENOENT; |
| 1803 | goto error_brelse; |
| 1804 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1805 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1806 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1807 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1808 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1809 | goto error_brelse; |
| 1810 | } |
| 1811 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1812 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1813 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1814 | goto error_brelse; |
| 1815 | } |
| 1816 | |
| 1817 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1818 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1819 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1820 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1821 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1822 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1823 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1824 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1825 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1826 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1827 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1828 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1829 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1830 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1831 | /* |
| 1832 | * TODO: the superblock still includes this device in its num_devices |
| 1833 | * counter although write_all_supers() is not locked out. This |
| 1834 | * could give a filesystem state which requires a degraded mount. |
| 1835 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1836 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1837 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1838 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1839 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1840 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1841 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1842 | |
| 1843 | /* |
| 1844 | * the device list mutex makes sure that we don't change |
| 1845 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1846 | * the device supers. Whoever is writing all supers, should |
| 1847 | * lock the device list mutex before getting the number of |
| 1848 | * devices in the super block (super_copy). Conversely, |
| 1849 | * whoever updates the number of devices in the super block |
| 1850 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1851 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1852 | |
| 1853 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1854 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1855 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1856 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1857 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1858 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1859 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1860 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1861 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1862 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1863 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1864 | struct btrfs_device, dev_list); |
| 1865 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1866 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1867 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1868 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1869 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1870 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1871 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1872 | /* remove sysfs entry */ |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 1873 | btrfs_kobj_rm_device(root->fs_info->fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1874 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1875 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1876 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1877 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1878 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1879 | 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] | 1880 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1881 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1882 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1883 | struct btrfs_fs_devices *fs_devices; |
| 1884 | fs_devices = root->fs_info->fs_devices; |
| 1885 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1886 | if (fs_devices->seed == cur_devices) { |
| 1887 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1888 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1889 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1890 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1891 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1892 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1893 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1894 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1895 | } |
| 1896 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1897 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1898 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1899 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1900 | /* |
| 1901 | * at this point, the device is zero sized. We want to |
| 1902 | * remove it from the devices list and zero out the old super |
| 1903 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1904 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1905 | u64 bytenr; |
| 1906 | int i; |
| 1907 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1908 | /* make sure this device isn't detected as part of |
| 1909 | * the FS anymore |
| 1910 | */ |
| 1911 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1912 | set_buffer_dirty(bh); |
| 1913 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1914 | |
| 1915 | /* clear the mirror copies of super block on the disk |
| 1916 | * being removed, 0th copy is been taken care above and |
| 1917 | * the below would take of the rest |
| 1918 | */ |
| 1919 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1920 | bytenr = btrfs_sb_offset(i); |
| 1921 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1922 | i_size_read(bdev->bd_inode)) |
| 1923 | break; |
| 1924 | |
| 1925 | brelse(bh); |
| 1926 | bh = __bread(bdev, bytenr / 4096, |
| 1927 | BTRFS_SUPER_INFO_SIZE); |
| 1928 | if (!bh) |
| 1929 | continue; |
| 1930 | |
| 1931 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1932 | |
| 1933 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1934 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1935 | continue; |
| 1936 | } |
| 1937 | memset(&disk_super->magic, 0, |
| 1938 | sizeof(disk_super->magic)); |
| 1939 | set_buffer_dirty(bh); |
| 1940 | sync_dirty_buffer(bh); |
| 1941 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1942 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1943 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1944 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1945 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1946 | if (bdev) { |
| 1947 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1948 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1949 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1950 | /* Update ctime/mtime for device path for libblkid */ |
| 1951 | update_dev_time(device_path); |
| 1952 | } |
| 1953 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1954 | error_brelse: |
| 1955 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1956 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1957 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1958 | out: |
| 1959 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1960 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1961 | error_undo: |
| 1962 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1963 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1964 | list_add(&device->dev_alloc_list, |
| 1965 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1966 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1967 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1968 | } |
| 1969 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1970 | } |
| 1971 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1972 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1973 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1974 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1975 | struct btrfs_fs_devices *fs_devices; |
| 1976 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1977 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1978 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1979 | /* |
| 1980 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1981 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1982 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1983 | * srcdev will have its correct fs_devices in both the cases. |
| 1984 | */ |
| 1985 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1986 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1987 | list_del_rcu(&srcdev->dev_list); |
| 1988 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1989 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1990 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1991 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1992 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1993 | if (srcdev->writeable) { |
| 1994 | fs_devices->rw_devices--; |
| 1995 | /* zero out the old super if it is writable */ |
| 1996 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1997 | } |
| 1998 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1999 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 2000 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 2004 | struct btrfs_device *srcdev) |
| 2005 | { |
| 2006 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2007 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2008 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2009 | |
| 2010 | /* |
| 2011 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 2012 | * zero |
| 2013 | */ |
| 2014 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 2015 | |
| 2016 | /* if this is no devs we rather delete the fs_devices */ |
| 2017 | if (!fs_devices->num_devices) { |
| 2018 | struct btrfs_fs_devices *tmp_fs_devices; |
| 2019 | |
| 2020 | tmp_fs_devices = fs_info->fs_devices; |
| 2021 | while (tmp_fs_devices) { |
| 2022 | if (tmp_fs_devices->seed == fs_devices) { |
| 2023 | tmp_fs_devices->seed = fs_devices->seed; |
| 2024 | break; |
| 2025 | } |
| 2026 | tmp_fs_devices = tmp_fs_devices->seed; |
| 2027 | } |
| 2028 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 2029 | __btrfs_close_devices(fs_devices); |
| 2030 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 2031 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 2035 | struct btrfs_device *tgtdev) |
| 2036 | { |
| 2037 | struct btrfs_device *next_device; |
| 2038 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 2039 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2040 | WARN_ON(!tgtdev); |
| 2041 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 2042 | |
| 2043 | btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev); |
| 2044 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2045 | if (tgtdev->bdev) { |
| 2046 | btrfs_scratch_superblock(tgtdev); |
| 2047 | fs_info->fs_devices->open_devices--; |
| 2048 | } |
| 2049 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2050 | |
| 2051 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 2052 | struct btrfs_device, dev_list); |
| 2053 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 2054 | fs_info->sb->s_bdev = next_device->bdev; |
| 2055 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 2056 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 2057 | list_del_rcu(&tgtdev->dev_list); |
| 2058 | |
| 2059 | call_rcu(&tgtdev->rcu, free_device); |
| 2060 | |
| 2061 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 2062 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 2065 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 2066 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2067 | { |
| 2068 | int ret = 0; |
| 2069 | struct btrfs_super_block *disk_super; |
| 2070 | u64 devid; |
| 2071 | u8 *dev_uuid; |
| 2072 | struct block_device *bdev; |
| 2073 | struct buffer_head *bh; |
| 2074 | |
| 2075 | *device = NULL; |
| 2076 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 2077 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 2078 | if (ret) |
| 2079 | return ret; |
| 2080 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2081 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2082 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2083 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2084 | disk_super->fsid); |
| 2085 | brelse(bh); |
| 2086 | if (!*device) |
| 2087 | ret = -ENOENT; |
| 2088 | blkdev_put(bdev, FMODE_READ); |
| 2089 | return ret; |
| 2090 | } |
| 2091 | |
| 2092 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 2093 | char *device_path, |
| 2094 | struct btrfs_device **device) |
| 2095 | { |
| 2096 | *device = NULL; |
| 2097 | if (strcmp(device_path, "missing") == 0) { |
| 2098 | struct list_head *devices; |
| 2099 | struct btrfs_device *tmp; |
| 2100 | |
| 2101 | devices = &root->fs_info->fs_devices->devices; |
| 2102 | /* |
| 2103 | * It is safe to read the devices since the volume_mutex |
| 2104 | * is held by the caller. |
| 2105 | */ |
| 2106 | list_for_each_entry(tmp, devices, dev_list) { |
| 2107 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 2108 | *device = tmp; |
| 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2114 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2115 | return -ENOENT; |
| 2116 | } |
| 2117 | |
| 2118 | return 0; |
| 2119 | } else { |
| 2120 | return btrfs_find_device_by_path(root, device_path, device); |
| 2121 | } |
| 2122 | } |
| 2123 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2124 | /* |
| 2125 | * does all the dirty work required for changing file system's UUID. |
| 2126 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2127 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2128 | { |
| 2129 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2130 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2131 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2132 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2133 | struct btrfs_device *device; |
| 2134 | u64 super_flags; |
| 2135 | |
| 2136 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2137 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2138 | return -EINVAL; |
| 2139 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2140 | seed_devices = __alloc_fs_devices(); |
| 2141 | if (IS_ERR(seed_devices)) |
| 2142 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2143 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2144 | old_devices = clone_fs_devices(fs_devices); |
| 2145 | if (IS_ERR(old_devices)) { |
| 2146 | kfree(seed_devices); |
| 2147 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2148 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2149 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2150 | list_add(&old_devices->list, &fs_uuids); |
| 2151 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2152 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2153 | seed_devices->opened = 1; |
| 2154 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2155 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2156 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2157 | |
| 2158 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2159 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2160 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2161 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2162 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2163 | |
| 2164 | lock_chunks(root); |
| 2165 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 2166 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2167 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2168 | fs_devices->seeding = 0; |
| 2169 | fs_devices->num_devices = 0; |
| 2170 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2171 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2172 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2173 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2174 | |
| 2175 | generate_random_uuid(fs_devices->fsid); |
| 2176 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2177 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2178 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2179 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2180 | super_flags = btrfs_super_flags(disk_super) & |
| 2181 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2182 | btrfs_set_super_flags(disk_super, super_flags); |
| 2183 | |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * strore the expected generation for seed devices in device items. |
| 2189 | */ |
| 2190 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2191 | struct btrfs_root *root) |
| 2192 | { |
| 2193 | struct btrfs_path *path; |
| 2194 | struct extent_buffer *leaf; |
| 2195 | struct btrfs_dev_item *dev_item; |
| 2196 | struct btrfs_device *device; |
| 2197 | struct btrfs_key key; |
| 2198 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2199 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2200 | u64 devid; |
| 2201 | int ret; |
| 2202 | |
| 2203 | path = btrfs_alloc_path(); |
| 2204 | if (!path) |
| 2205 | return -ENOMEM; |
| 2206 | |
| 2207 | root = root->fs_info->chunk_root; |
| 2208 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2209 | key.offset = 0; |
| 2210 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2211 | |
| 2212 | while (1) { |
| 2213 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2214 | if (ret < 0) |
| 2215 | goto error; |
| 2216 | |
| 2217 | leaf = path->nodes[0]; |
| 2218 | next_slot: |
| 2219 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2220 | ret = btrfs_next_leaf(root, path); |
| 2221 | if (ret > 0) |
| 2222 | break; |
| 2223 | if (ret < 0) |
| 2224 | goto error; |
| 2225 | leaf = path->nodes[0]; |
| 2226 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2227 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2228 | continue; |
| 2229 | } |
| 2230 | |
| 2231 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2232 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2233 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2234 | break; |
| 2235 | |
| 2236 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2237 | struct btrfs_dev_item); |
| 2238 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2239 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2240 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2241 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2242 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2243 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2244 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2245 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2246 | |
| 2247 | if (device->fs_devices->seeding) { |
| 2248 | btrfs_set_device_generation(leaf, dev_item, |
| 2249 | device->generation); |
| 2250 | btrfs_mark_buffer_dirty(leaf); |
| 2251 | } |
| 2252 | |
| 2253 | path->slots[0]++; |
| 2254 | goto next_slot; |
| 2255 | } |
| 2256 | ret = 0; |
| 2257 | error: |
| 2258 | btrfs_free_path(path); |
| 2259 | return ret; |
| 2260 | } |
| 2261 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2262 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2263 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2264 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2265 | struct btrfs_trans_handle *trans; |
| 2266 | struct btrfs_device *device; |
| 2267 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2268 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2269 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2270 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2271 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2272 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2273 | int ret = 0; |
| 2274 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2275 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2276 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2277 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2278 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2279 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2280 | if (IS_ERR(bdev)) |
| 2281 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2282 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2283 | if (root->fs_info->fs_devices->seeding) { |
| 2284 | seeding_dev = 1; |
| 2285 | down_write(&sb->s_umount); |
| 2286 | mutex_lock(&uuid_mutex); |
| 2287 | } |
| 2288 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2289 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2290 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2291 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2292 | |
| 2293 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2294 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2295 | if (device->bdev == bdev) { |
| 2296 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2297 | mutex_unlock( |
| 2298 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2299 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2300 | } |
| 2301 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2302 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2303 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2304 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2305 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2306 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2307 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2308 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2309 | } |
| 2310 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2311 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2312 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2313 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2314 | ret = -ENOMEM; |
| 2315 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2316 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2317 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2318 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2319 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2320 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2321 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2322 | kfree(device); |
| 2323 | ret = PTR_ERR(trans); |
| 2324 | goto error; |
| 2325 | } |
| 2326 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2327 | q = bdev_get_queue(bdev); |
| 2328 | if (blk_queue_discard(q)) |
| 2329 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2330 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2331 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2332 | device->io_width = root->sectorsize; |
| 2333 | device->io_align = root->sectorsize; |
| 2334 | device->sector_size = root->sectorsize; |
| 2335 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2336 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2337 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2338 | device->dev_root = root->fs_info->dev_root; |
| 2339 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2340 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2341 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2342 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2343 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2344 | set_blocksize(device->bdev, 4096); |
| 2345 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2346 | if (seeding_dev) { |
| 2347 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2348 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2349 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2353 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2354 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2355 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2356 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2357 | list_add(&device->dev_alloc_list, |
| 2358 | &root->fs_info->fs_devices->alloc_list); |
| 2359 | root->fs_info->fs_devices->num_devices++; |
| 2360 | root->fs_info->fs_devices->open_devices++; |
| 2361 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2362 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2363 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2364 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2365 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2366 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2367 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2368 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2369 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2370 | root->fs_info->fs_devices->rotating = 1; |
| 2371 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2372 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2373 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2374 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2375 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2376 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2377 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2378 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2379 | |
| 2380 | /* add sysfs device entry */ |
Anand Jain | 1ba4381 | 2015-03-10 06:38:33 +0800 | [diff] [blame] | 2381 | btrfs_kobj_add_device(root->fs_info->fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2382 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2383 | /* |
| 2384 | * we've got more storage, clear any full flags on the space |
| 2385 | * infos |
| 2386 | */ |
| 2387 | btrfs_clear_space_info_full(root->fs_info); |
| 2388 | |
| 2389 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2390 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2391 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2392 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2393 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2394 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2395 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2396 | if (ret) { |
| 2397 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2398 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2399 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | ret = btrfs_add_device(trans, root, device); |
| 2403 | if (ret) { |
| 2404 | btrfs_abort_transaction(trans, root, ret); |
| 2405 | goto error_trans; |
| 2406 | } |
| 2407 | |
| 2408 | if (seeding_dev) { |
| 2409 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2410 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2411 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2412 | if (ret) { |
| 2413 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2414 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2415 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2416 | |
| 2417 | /* Sprouting would change fsid of the mounted root, |
| 2418 | * so rename the fsid on the sysfs |
| 2419 | */ |
| 2420 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2421 | root->fs_info->fsid); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 2422 | if (kobject_rename(&root->fs_info->fs_devices->super_kobj, |
| 2423 | fsid_buf)) |
Anand Jain | 2421a8c | 2015-03-10 06:38:40 +0800 | [diff] [blame] | 2424 | pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2425 | } |
| 2426 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2427 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2428 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2429 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2430 | |
| 2431 | if (seeding_dev) { |
| 2432 | mutex_unlock(&uuid_mutex); |
| 2433 | up_write(&sb->s_umount); |
| 2434 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2435 | if (ret) /* transaction commit */ |
| 2436 | return ret; |
| 2437 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2438 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2439 | if (ret < 0) |
| 2440 | btrfs_error(root->fs_info, ret, |
| 2441 | "Failed to relocate sys chunks after " |
| 2442 | "device initialization. This can be fixed " |
| 2443 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2444 | trans = btrfs_attach_transaction(root); |
| 2445 | if (IS_ERR(trans)) { |
| 2446 | if (PTR_ERR(trans) == -ENOENT) |
| 2447 | return 0; |
| 2448 | return PTR_ERR(trans); |
| 2449 | } |
| 2450 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2451 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2452 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2453 | /* Update ctime/mtime for libblkid */ |
| 2454 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2455 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2456 | |
| 2457 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2458 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2459 | rcu_string_free(device->name); |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 2460 | btrfs_kobj_rm_device(root->fs_info->fs_devices, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2461 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2462 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2463 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2464 | if (seeding_dev) { |
| 2465 | mutex_unlock(&uuid_mutex); |
| 2466 | up_write(&sb->s_umount); |
| 2467 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2468 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2469 | } |
| 2470 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2471 | 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] | 2472 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2473 | struct btrfs_device **device_out) |
| 2474 | { |
| 2475 | struct request_queue *q; |
| 2476 | struct btrfs_device *device; |
| 2477 | struct block_device *bdev; |
| 2478 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2479 | struct list_head *devices; |
| 2480 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2481 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2482 | int ret = 0; |
| 2483 | |
| 2484 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2485 | if (fs_info->fs_devices->seeding) { |
| 2486 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2487 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2488 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2489 | |
| 2490 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2491 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2492 | if (IS_ERR(bdev)) { |
| 2493 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2494 | return PTR_ERR(bdev); |
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 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2498 | |
| 2499 | devices = &fs_info->fs_devices->devices; |
| 2500 | list_for_each_entry(device, devices, dev_list) { |
| 2501 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2502 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2503 | ret = -EEXIST; |
| 2504 | goto error; |
| 2505 | } |
| 2506 | } |
| 2507 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2508 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2509 | if (i_size_read(bdev->bd_inode) < |
| 2510 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2511 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2512 | ret = -EINVAL; |
| 2513 | goto error; |
| 2514 | } |
| 2515 | |
| 2516 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2517 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2518 | if (IS_ERR(device)) { |
| 2519 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2520 | goto error; |
| 2521 | } |
| 2522 | |
| 2523 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2524 | if (!name) { |
| 2525 | kfree(device); |
| 2526 | ret = -ENOMEM; |
| 2527 | goto error; |
| 2528 | } |
| 2529 | rcu_assign_pointer(device->name, name); |
| 2530 | |
| 2531 | q = bdev_get_queue(bdev); |
| 2532 | if (blk_queue_discard(q)) |
| 2533 | device->can_discard = 1; |
| 2534 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2535 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2536 | device->generation = 0; |
| 2537 | device->io_width = root->sectorsize; |
| 2538 | device->io_align = root->sectorsize; |
| 2539 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2540 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2541 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2542 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2543 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2544 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2545 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2546 | device->dev_root = fs_info->dev_root; |
| 2547 | device->bdev = bdev; |
| 2548 | device->in_fs_metadata = 1; |
| 2549 | device->is_tgtdev_for_dev_replace = 1; |
| 2550 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2551 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2552 | set_blocksize(device->bdev, 4096); |
| 2553 | device->fs_devices = fs_info->fs_devices; |
| 2554 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2555 | fs_info->fs_devices->num_devices++; |
| 2556 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2557 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2558 | |
| 2559 | *device_out = device; |
| 2560 | return ret; |
| 2561 | |
| 2562 | error: |
| 2563 | blkdev_put(bdev, FMODE_EXCL); |
| 2564 | return ret; |
| 2565 | } |
| 2566 | |
| 2567 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2568 | struct btrfs_device *tgtdev) |
| 2569 | { |
| 2570 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2571 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2572 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2573 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2574 | tgtdev->dev_root = fs_info->dev_root; |
| 2575 | tgtdev->in_fs_metadata = 1; |
| 2576 | } |
| 2577 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2578 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2579 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2580 | { |
| 2581 | int ret; |
| 2582 | struct btrfs_path *path; |
| 2583 | struct btrfs_root *root; |
| 2584 | struct btrfs_dev_item *dev_item; |
| 2585 | struct extent_buffer *leaf; |
| 2586 | struct btrfs_key key; |
| 2587 | |
| 2588 | root = device->dev_root->fs_info->chunk_root; |
| 2589 | |
| 2590 | path = btrfs_alloc_path(); |
| 2591 | if (!path) |
| 2592 | return -ENOMEM; |
| 2593 | |
| 2594 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2595 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2596 | key.offset = device->devid; |
| 2597 | |
| 2598 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2599 | if (ret < 0) |
| 2600 | goto out; |
| 2601 | |
| 2602 | if (ret > 0) { |
| 2603 | ret = -ENOENT; |
| 2604 | goto out; |
| 2605 | } |
| 2606 | |
| 2607 | leaf = path->nodes[0]; |
| 2608 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2609 | |
| 2610 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2611 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2612 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2613 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2614 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2615 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2616 | btrfs_device_get_disk_total_bytes(device)); |
| 2617 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2618 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2619 | btrfs_mark_buffer_dirty(leaf); |
| 2620 | |
| 2621 | out: |
| 2622 | btrfs_free_path(path); |
| 2623 | return ret; |
| 2624 | } |
| 2625 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2626 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2627 | struct btrfs_device *device, u64 new_size) |
| 2628 | { |
| 2629 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2630 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2631 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2632 | u64 old_total; |
| 2633 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2634 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2635 | if (!device->writeable) |
| 2636 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2637 | |
| 2638 | lock_chunks(device->dev_root); |
| 2639 | old_total = btrfs_super_total_bytes(super_copy); |
| 2640 | diff = new_size - device->total_bytes; |
| 2641 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2642 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2643 | device->is_tgtdev_for_dev_replace) { |
| 2644 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2645 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2646 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2647 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2648 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2649 | |
| 2650 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2651 | device->fs_devices->total_rw_bytes += diff; |
| 2652 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2653 | btrfs_device_set_total_bytes(device, new_size); |
| 2654 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2655 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2656 | if (list_empty(&device->resized_list)) |
| 2657 | list_add_tail(&device->resized_list, |
| 2658 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2659 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2660 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2661 | return btrfs_update_device(trans, device); |
| 2662 | } |
| 2663 | |
| 2664 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2665 | struct btrfs_root *root, u64 chunk_objectid, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2666 | u64 chunk_offset) |
| 2667 | { |
| 2668 | int ret; |
| 2669 | struct btrfs_path *path; |
| 2670 | struct btrfs_key key; |
| 2671 | |
| 2672 | root = root->fs_info->chunk_root; |
| 2673 | path = btrfs_alloc_path(); |
| 2674 | if (!path) |
| 2675 | return -ENOMEM; |
| 2676 | |
| 2677 | key.objectid = chunk_objectid; |
| 2678 | key.offset = chunk_offset; |
| 2679 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2680 | |
| 2681 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2682 | if (ret < 0) |
| 2683 | goto out; |
| 2684 | else if (ret > 0) { /* Logic error or corruption */ |
| 2685 | btrfs_error(root->fs_info, -ENOENT, |
| 2686 | "Failed lookup while freeing chunk."); |
| 2687 | ret = -ENOENT; |
| 2688 | goto out; |
| 2689 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2690 | |
| 2691 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2692 | if (ret < 0) |
| 2693 | btrfs_error(root->fs_info, ret, |
| 2694 | "Failed to delete chunk item."); |
| 2695 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2696 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2697 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2698 | } |
| 2699 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2700 | 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] | 2701 | chunk_offset) |
| 2702 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2703 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2704 | struct btrfs_disk_key *disk_key; |
| 2705 | struct btrfs_chunk *chunk; |
| 2706 | u8 *ptr; |
| 2707 | int ret = 0; |
| 2708 | u32 num_stripes; |
| 2709 | u32 array_size; |
| 2710 | u32 len = 0; |
| 2711 | u32 cur; |
| 2712 | struct btrfs_key key; |
| 2713 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2714 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2715 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2716 | |
| 2717 | ptr = super_copy->sys_chunk_array; |
| 2718 | cur = 0; |
| 2719 | |
| 2720 | while (cur < array_size) { |
| 2721 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2722 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2723 | |
| 2724 | len = sizeof(*disk_key); |
| 2725 | |
| 2726 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2727 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2728 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2729 | len += btrfs_chunk_item_size(num_stripes); |
| 2730 | } else { |
| 2731 | ret = -EIO; |
| 2732 | break; |
| 2733 | } |
| 2734 | if (key.objectid == chunk_objectid && |
| 2735 | key.offset == chunk_offset) { |
| 2736 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2737 | array_size -= len; |
| 2738 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2739 | } else { |
| 2740 | ptr += len; |
| 2741 | cur += len; |
| 2742 | } |
| 2743 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2744 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2745 | return ret; |
| 2746 | } |
| 2747 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2748 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2749 | struct btrfs_root *root, u64 chunk_offset) |
| 2750 | { |
| 2751 | struct extent_map_tree *em_tree; |
| 2752 | struct extent_map *em; |
| 2753 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2754 | struct map_lookup *map; |
| 2755 | u64 dev_extent_len = 0; |
| 2756 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2757 | int i, ret = 0; |
| 2758 | |
| 2759 | /* Just in case */ |
| 2760 | root = root->fs_info->chunk_root; |
| 2761 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2762 | |
| 2763 | read_lock(&em_tree->lock); |
| 2764 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2765 | read_unlock(&em_tree->lock); |
| 2766 | |
| 2767 | if (!em || em->start > chunk_offset || |
| 2768 | em->start + em->len < chunk_offset) { |
| 2769 | /* |
| 2770 | * This is a logic error, but we don't want to just rely on the |
| 2771 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2772 | * do anything we still error out. |
| 2773 | */ |
| 2774 | ASSERT(0); |
| 2775 | if (em) |
| 2776 | free_extent_map(em); |
| 2777 | return -EINVAL; |
| 2778 | } |
| 2779 | map = (struct map_lookup *)em->bdev; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2780 | lock_chunks(root->fs_info->chunk_root); |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 2781 | check_system_chunk(trans, extent_root, map->type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2782 | unlock_chunks(root->fs_info->chunk_root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2783 | |
| 2784 | for (i = 0; i < map->num_stripes; i++) { |
| 2785 | struct btrfs_device *device = map->stripes[i].dev; |
| 2786 | ret = btrfs_free_dev_extent(trans, device, |
| 2787 | map->stripes[i].physical, |
| 2788 | &dev_extent_len); |
| 2789 | if (ret) { |
| 2790 | btrfs_abort_transaction(trans, root, ret); |
| 2791 | goto out; |
| 2792 | } |
| 2793 | |
| 2794 | if (device->bytes_used > 0) { |
| 2795 | lock_chunks(root); |
| 2796 | btrfs_device_set_bytes_used(device, |
| 2797 | device->bytes_used - dev_extent_len); |
| 2798 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2799 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2800 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2801 | btrfs_clear_space_info_full(root->fs_info); |
| 2802 | unlock_chunks(root); |
| 2803 | } |
| 2804 | |
| 2805 | if (map->stripes[i].dev) { |
| 2806 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2807 | if (ret) { |
| 2808 | btrfs_abort_transaction(trans, root, ret); |
| 2809 | goto out; |
| 2810 | } |
| 2811 | } |
| 2812 | } |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2813 | ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2814 | if (ret) { |
| 2815 | btrfs_abort_transaction(trans, root, ret); |
| 2816 | goto out; |
| 2817 | } |
| 2818 | |
| 2819 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2820 | |
| 2821 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2822 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2823 | if (ret) { |
| 2824 | btrfs_abort_transaction(trans, root, ret); |
| 2825 | goto out; |
| 2826 | } |
| 2827 | } |
| 2828 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2829 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2830 | if (ret) { |
| 2831 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2832 | goto out; |
| 2833 | } |
| 2834 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2835 | out: |
| 2836 | /* once for us */ |
| 2837 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2838 | return ret; |
| 2839 | } |
| 2840 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 2841 | static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2842 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2843 | struct btrfs_root *extent_root; |
| 2844 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2845 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2846 | |
| 2847 | root = root->fs_info->chunk_root; |
| 2848 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2849 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2850 | /* |
| 2851 | * Prevent races with automatic removal of unused block groups. |
| 2852 | * After we relocate and before we remove the chunk with offset |
| 2853 | * chunk_offset, automatic removal of the block group can kick in, |
| 2854 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2855 | * |
| 2856 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2857 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2858 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2859 | * we release the path used to search the chunk/dev tree and before |
| 2860 | * the current task acquires this mutex and calls us. |
| 2861 | */ |
| 2862 | ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex)); |
| 2863 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2864 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2865 | if (ret) |
| 2866 | return -ENOSPC; |
| 2867 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2868 | /* step one, relocate all the extents inside this chunk */ |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2869 | btrfs_scrub_pause(root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2870 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2871 | btrfs_scrub_continue(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2872 | if (ret) |
| 2873 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2874 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2875 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2876 | if (IS_ERR(trans)) { |
| 2877 | ret = PTR_ERR(trans); |
| 2878 | btrfs_std_error(root->fs_info, ret); |
| 2879 | return ret; |
| 2880 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2881 | |
| 2882 | /* |
| 2883 | * step two, delete the device extents and the |
| 2884 | * chunk tree entries |
| 2885 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2886 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2887 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2888 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2889 | } |
| 2890 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2891 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2892 | { |
| 2893 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2894 | struct btrfs_path *path; |
| 2895 | struct extent_buffer *leaf; |
| 2896 | struct btrfs_chunk *chunk; |
| 2897 | struct btrfs_key key; |
| 2898 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2899 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2900 | bool retried = false; |
| 2901 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2902 | int ret; |
| 2903 | |
| 2904 | path = btrfs_alloc_path(); |
| 2905 | if (!path) |
| 2906 | return -ENOMEM; |
| 2907 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2908 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2909 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2910 | key.offset = (u64)-1; |
| 2911 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2912 | |
| 2913 | while (1) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2914 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2915 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2916 | if (ret < 0) { |
| 2917 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2918 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2919 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2920 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2921 | |
| 2922 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2923 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2924 | if (ret) |
| 2925 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2926 | if (ret < 0) |
| 2927 | goto error; |
| 2928 | if (ret > 0) |
| 2929 | break; |
| 2930 | |
| 2931 | leaf = path->nodes[0]; |
| 2932 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2933 | |
| 2934 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2935 | struct btrfs_chunk); |
| 2936 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2937 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2938 | |
| 2939 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2940 | ret = btrfs_relocate_chunk(chunk_root, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2941 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2942 | if (ret == -ENOSPC) |
| 2943 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2944 | else |
| 2945 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2946 | } |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2947 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2948 | |
| 2949 | if (found_key.offset == 0) |
| 2950 | break; |
| 2951 | key.offset = found_key.offset - 1; |
| 2952 | } |
| 2953 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2954 | if (failed && !retried) { |
| 2955 | failed = 0; |
| 2956 | retried = true; |
| 2957 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2958 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2959 | ret = -ENOSPC; |
| 2960 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2961 | error: |
| 2962 | btrfs_free_path(path); |
| 2963 | return ret; |
| 2964 | } |
| 2965 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2966 | static int insert_balance_item(struct btrfs_root *root, |
| 2967 | struct btrfs_balance_control *bctl) |
| 2968 | { |
| 2969 | struct btrfs_trans_handle *trans; |
| 2970 | struct btrfs_balance_item *item; |
| 2971 | struct btrfs_disk_balance_args disk_bargs; |
| 2972 | struct btrfs_path *path; |
| 2973 | struct extent_buffer *leaf; |
| 2974 | struct btrfs_key key; |
| 2975 | int ret, err; |
| 2976 | |
| 2977 | path = btrfs_alloc_path(); |
| 2978 | if (!path) |
| 2979 | return -ENOMEM; |
| 2980 | |
| 2981 | trans = btrfs_start_transaction(root, 0); |
| 2982 | if (IS_ERR(trans)) { |
| 2983 | btrfs_free_path(path); |
| 2984 | return PTR_ERR(trans); |
| 2985 | } |
| 2986 | |
| 2987 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2988 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2989 | key.offset = 0; |
| 2990 | |
| 2991 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2992 | sizeof(*item)); |
| 2993 | if (ret) |
| 2994 | goto out; |
| 2995 | |
| 2996 | leaf = path->nodes[0]; |
| 2997 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2998 | |
| 2999 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 3000 | |
| 3001 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 3002 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 3003 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 3004 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 3005 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 3006 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 3007 | |
| 3008 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 3009 | |
| 3010 | btrfs_mark_buffer_dirty(leaf); |
| 3011 | out: |
| 3012 | btrfs_free_path(path); |
| 3013 | err = btrfs_commit_transaction(trans, root); |
| 3014 | if (err && !ret) |
| 3015 | ret = err; |
| 3016 | return ret; |
| 3017 | } |
| 3018 | |
| 3019 | static int del_balance_item(struct btrfs_root *root) |
| 3020 | { |
| 3021 | struct btrfs_trans_handle *trans; |
| 3022 | struct btrfs_path *path; |
| 3023 | struct btrfs_key key; |
| 3024 | int ret, err; |
| 3025 | |
| 3026 | path = btrfs_alloc_path(); |
| 3027 | if (!path) |
| 3028 | return -ENOMEM; |
| 3029 | |
| 3030 | trans = btrfs_start_transaction(root, 0); |
| 3031 | if (IS_ERR(trans)) { |
| 3032 | btrfs_free_path(path); |
| 3033 | return PTR_ERR(trans); |
| 3034 | } |
| 3035 | |
| 3036 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3037 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3038 | key.offset = 0; |
| 3039 | |
| 3040 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3041 | if (ret < 0) |
| 3042 | goto out; |
| 3043 | if (ret > 0) { |
| 3044 | ret = -ENOENT; |
| 3045 | goto out; |
| 3046 | } |
| 3047 | |
| 3048 | ret = btrfs_del_item(trans, root, path); |
| 3049 | out: |
| 3050 | btrfs_free_path(path); |
| 3051 | err = btrfs_commit_transaction(trans, root); |
| 3052 | if (err && !ret) |
| 3053 | ret = err; |
| 3054 | return ret; |
| 3055 | } |
| 3056 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3057 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3058 | * This is a heuristic used to reduce the number of chunks balanced on |
| 3059 | * resume after balance was interrupted. |
| 3060 | */ |
| 3061 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 3062 | { |
| 3063 | /* |
| 3064 | * Turn on soft mode for chunk types that were being converted. |
| 3065 | */ |
| 3066 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3067 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3068 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3069 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3070 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3071 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3072 | |
| 3073 | /* |
| 3074 | * Turn on usage filter if is not already used. The idea is |
| 3075 | * that chunks that we have already balanced should be |
| 3076 | * reasonably full. Don't do it for chunks that are being |
| 3077 | * converted - that will keep us from relocating unconverted |
| 3078 | * (albeit full) chunks. |
| 3079 | */ |
| 3080 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3081 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3082 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3083 | bctl->data.usage = 90; |
| 3084 | } |
| 3085 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3086 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3087 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3088 | bctl->sys.usage = 90; |
| 3089 | } |
| 3090 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3091 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3092 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3093 | bctl->meta.usage = 90; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3098 | * Should be called with both balance and volume mutexes held to |
| 3099 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 3100 | * restriper. Same goes for unset_balance_control. |
| 3101 | */ |
| 3102 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 3103 | { |
| 3104 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 3105 | |
| 3106 | BUG_ON(fs_info->balance_ctl); |
| 3107 | |
| 3108 | spin_lock(&fs_info->balance_lock); |
| 3109 | fs_info->balance_ctl = bctl; |
| 3110 | spin_unlock(&fs_info->balance_lock); |
| 3111 | } |
| 3112 | |
| 3113 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 3114 | { |
| 3115 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3116 | |
| 3117 | BUG_ON(!fs_info->balance_ctl); |
| 3118 | |
| 3119 | spin_lock(&fs_info->balance_lock); |
| 3120 | fs_info->balance_ctl = NULL; |
| 3121 | spin_unlock(&fs_info->balance_lock); |
| 3122 | |
| 3123 | kfree(bctl); |
| 3124 | } |
| 3125 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3126 | /* |
| 3127 | * Balance filters. Return 1 if chunk should be filtered out |
| 3128 | * (should not be balanced). |
| 3129 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3130 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3131 | struct btrfs_balance_args *bargs) |
| 3132 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3133 | chunk_type = chunk_to_extended(chunk_type) & |
| 3134 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3135 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3136 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3137 | return 0; |
| 3138 | |
| 3139 | return 1; |
| 3140 | } |
| 3141 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3142 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 3143 | struct btrfs_balance_args *bargs) |
| 3144 | { |
| 3145 | struct btrfs_block_group_cache *cache; |
| 3146 | u64 chunk_used, user_thresh; |
| 3147 | int ret = 1; |
| 3148 | |
| 3149 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3150 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3151 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3152 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3153 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3154 | else if (bargs->usage > 100) |
| 3155 | user_thresh = cache->key.offset; |
| 3156 | else |
| 3157 | user_thresh = div_factor_fine(cache->key.offset, |
| 3158 | bargs->usage); |
| 3159 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3160 | if (chunk_used < user_thresh) |
| 3161 | ret = 0; |
| 3162 | |
| 3163 | btrfs_put_block_group(cache); |
| 3164 | return ret; |
| 3165 | } |
| 3166 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3167 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3168 | struct btrfs_chunk *chunk, |
| 3169 | struct btrfs_balance_args *bargs) |
| 3170 | { |
| 3171 | struct btrfs_stripe *stripe; |
| 3172 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3173 | int i; |
| 3174 | |
| 3175 | for (i = 0; i < num_stripes; i++) { |
| 3176 | stripe = btrfs_stripe_nr(chunk, i); |
| 3177 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3178 | return 0; |
| 3179 | } |
| 3180 | |
| 3181 | return 1; |
| 3182 | } |
| 3183 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3184 | /* [pstart, pend) */ |
| 3185 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3186 | struct btrfs_chunk *chunk, |
| 3187 | u64 chunk_offset, |
| 3188 | struct btrfs_balance_args *bargs) |
| 3189 | { |
| 3190 | struct btrfs_stripe *stripe; |
| 3191 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3192 | u64 stripe_offset; |
| 3193 | u64 stripe_length; |
| 3194 | int factor; |
| 3195 | int i; |
| 3196 | |
| 3197 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3198 | return 0; |
| 3199 | |
| 3200 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3201 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3202 | factor = num_stripes / 2; |
| 3203 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3204 | factor = num_stripes - 1; |
| 3205 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3206 | factor = num_stripes - 2; |
| 3207 | } else { |
| 3208 | factor = num_stripes; |
| 3209 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3210 | |
| 3211 | for (i = 0; i < num_stripes; i++) { |
| 3212 | stripe = btrfs_stripe_nr(chunk, i); |
| 3213 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3214 | continue; |
| 3215 | |
| 3216 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3217 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3218 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3219 | |
| 3220 | if (stripe_offset < bargs->pend && |
| 3221 | stripe_offset + stripe_length > bargs->pstart) |
| 3222 | return 0; |
| 3223 | } |
| 3224 | |
| 3225 | return 1; |
| 3226 | } |
| 3227 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3228 | /* [vstart, vend) */ |
| 3229 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3230 | struct btrfs_chunk *chunk, |
| 3231 | u64 chunk_offset, |
| 3232 | struct btrfs_balance_args *bargs) |
| 3233 | { |
| 3234 | if (chunk_offset < bargs->vend && |
| 3235 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3236 | /* at least part of the chunk is inside this vrange */ |
| 3237 | return 0; |
| 3238 | |
| 3239 | return 1; |
| 3240 | } |
| 3241 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3242 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3243 | struct btrfs_balance_args *bargs) |
| 3244 | { |
| 3245 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3246 | return 0; |
| 3247 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3248 | chunk_type = chunk_to_extended(chunk_type) & |
| 3249 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3250 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3251 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3252 | return 1; |
| 3253 | |
| 3254 | return 0; |
| 3255 | } |
| 3256 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3257 | static int should_balance_chunk(struct btrfs_root *root, |
| 3258 | struct extent_buffer *leaf, |
| 3259 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3260 | { |
| 3261 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3262 | struct btrfs_balance_args *bargs = NULL; |
| 3263 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3264 | |
| 3265 | /* type filter */ |
| 3266 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3267 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3268 | return 0; |
| 3269 | } |
| 3270 | |
| 3271 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3272 | bargs = &bctl->data; |
| 3273 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3274 | bargs = &bctl->sys; |
| 3275 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3276 | bargs = &bctl->meta; |
| 3277 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3278 | /* profiles filter */ |
| 3279 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3280 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3281 | return 0; |
| 3282 | } |
| 3283 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3284 | /* usage filter */ |
| 3285 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3286 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3287 | return 0; |
| 3288 | } |
| 3289 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3290 | /* devid filter */ |
| 3291 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3292 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3293 | return 0; |
| 3294 | } |
| 3295 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3296 | /* drange filter, makes sense only with devid filter */ |
| 3297 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3298 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3302 | /* vrange filter */ |
| 3303 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3304 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3305 | return 0; |
| 3306 | } |
| 3307 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3308 | /* soft profile changing mode */ |
| 3309 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3310 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3311 | return 0; |
| 3312 | } |
| 3313 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3314 | /* |
| 3315 | * limited by count, must be the last filter |
| 3316 | */ |
| 3317 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3318 | if (bargs->limit == 0) |
| 3319 | return 0; |
| 3320 | else |
| 3321 | bargs->limit--; |
| 3322 | } |
| 3323 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3324 | return 1; |
| 3325 | } |
| 3326 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3327 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3328 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3329 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3330 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3331 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3332 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3333 | struct btrfs_device *device; |
| 3334 | u64 old_size; |
| 3335 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3336 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3337 | struct btrfs_path *path; |
| 3338 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3339 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3340 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3341 | struct extent_buffer *leaf; |
| 3342 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3343 | int ret; |
| 3344 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3345 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3346 | u64 limit_data = bctl->data.limit; |
| 3347 | u64 limit_meta = bctl->meta.limit; |
| 3348 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3349 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3350 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3351 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3352 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3353 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3354 | size_to_free = div_factor(old_size, 1); |
| 3355 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3356 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3357 | btrfs_device_get_total_bytes(device) - |
| 3358 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3359 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3360 | continue; |
| 3361 | |
| 3362 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3363 | if (ret == -ENOSPC) |
| 3364 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3365 | BUG_ON(ret); |
| 3366 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3367 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3368 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3369 | |
| 3370 | ret = btrfs_grow_device(trans, device, old_size); |
| 3371 | BUG_ON(ret); |
| 3372 | |
| 3373 | btrfs_end_transaction(trans, dev_root); |
| 3374 | } |
| 3375 | |
| 3376 | /* step two, relocate all the chunks */ |
| 3377 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3378 | if (!path) { |
| 3379 | ret = -ENOMEM; |
| 3380 | goto error; |
| 3381 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3382 | |
| 3383 | /* zero out stat counters */ |
| 3384 | spin_lock(&fs_info->balance_lock); |
| 3385 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3386 | spin_unlock(&fs_info->balance_lock); |
| 3387 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3388 | if (!counting) { |
| 3389 | bctl->data.limit = limit_data; |
| 3390 | bctl->meta.limit = limit_meta; |
| 3391 | bctl->sys.limit = limit_sys; |
| 3392 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3393 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3394 | key.offset = (u64)-1; |
| 3395 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3396 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3397 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3398 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3399 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3400 | ret = -ECANCELED; |
| 3401 | goto error; |
| 3402 | } |
| 3403 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3404 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3405 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3406 | if (ret < 0) { |
| 3407 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3408 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3409 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3410 | |
| 3411 | /* |
| 3412 | * this shouldn't happen, it means the last relocate |
| 3413 | * failed |
| 3414 | */ |
| 3415 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3416 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3417 | |
| 3418 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3419 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3420 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3421 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3422 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3423 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3424 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3425 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3426 | leaf = path->nodes[0]; |
| 3427 | slot = path->slots[0]; |
| 3428 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3429 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3430 | if (found_key.objectid != key.objectid) { |
| 3431 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3432 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3433 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3434 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3435 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3436 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3437 | if (!counting) { |
| 3438 | spin_lock(&fs_info->balance_lock); |
| 3439 | bctl->stat.considered++; |
| 3440 | spin_unlock(&fs_info->balance_lock); |
| 3441 | } |
| 3442 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3443 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3444 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3445 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3446 | if (!ret) { |
| 3447 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3448 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3449 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3450 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3451 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3452 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3453 | spin_lock(&fs_info->balance_lock); |
| 3454 | bctl->stat.expected++; |
| 3455 | spin_unlock(&fs_info->balance_lock); |
| 3456 | goto loop; |
| 3457 | } |
| 3458 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3459 | ret = btrfs_relocate_chunk(chunk_root, |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3460 | found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3461 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3462 | if (ret && ret != -ENOSPC) |
| 3463 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3464 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3465 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3466 | } else { |
| 3467 | spin_lock(&fs_info->balance_lock); |
| 3468 | bctl->stat.completed++; |
| 3469 | spin_unlock(&fs_info->balance_lock); |
| 3470 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3471 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3472 | if (found_key.offset == 0) |
| 3473 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3474 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3475 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3476 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3477 | if (counting) { |
| 3478 | btrfs_release_path(path); |
| 3479 | counting = false; |
| 3480 | goto again; |
| 3481 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3482 | error: |
| 3483 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3484 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3485 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3486 | enospc_errors); |
| 3487 | if (!ret) |
| 3488 | ret = -ENOSPC; |
| 3489 | } |
| 3490 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3491 | return ret; |
| 3492 | } |
| 3493 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3494 | /** |
| 3495 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3496 | * @flags: profile to validate |
| 3497 | * @extended: if true @flags is treated as an extended profile |
| 3498 | */ |
| 3499 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3500 | { |
| 3501 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3502 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3503 | |
| 3504 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3505 | |
| 3506 | /* 1) check that all other bits are zeroed */ |
| 3507 | if (flags & ~mask) |
| 3508 | return 0; |
| 3509 | |
| 3510 | /* 2) see if profile is reduced */ |
| 3511 | if (flags == 0) |
| 3512 | return !extended; /* "0" is valid for usual profiles */ |
| 3513 | |
| 3514 | /* true if exactly one bit set */ |
| 3515 | return (flags & (flags - 1)) == 0; |
| 3516 | } |
| 3517 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3518 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3519 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3520 | /* cancel requested || normal exit path */ |
| 3521 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3522 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3523 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3526 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3527 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3528 | int ret; |
| 3529 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3530 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3531 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3532 | if (ret) |
| 3533 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3534 | |
| 3535 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3536 | } |
| 3537 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3538 | /* |
| 3539 | * Should be called with both balance and volume mutexes held |
| 3540 | */ |
| 3541 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3542 | struct btrfs_ioctl_balance_args *bargs) |
| 3543 | { |
| 3544 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3545 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3546 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3547 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3548 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3549 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3550 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3551 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3552 | atomic_read(&fs_info->balance_pause_req) || |
| 3553 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3554 | ret = -EINVAL; |
| 3555 | goto out; |
| 3556 | } |
| 3557 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3558 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3559 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3560 | mixed = 1; |
| 3561 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3562 | /* |
| 3563 | * In case of mixed groups both data and meta should be picked, |
| 3564 | * and identical options should be given for both of them. |
| 3565 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3566 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3567 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3568 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3569 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3570 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3571 | btrfs_err(fs_info, "with mixed groups data and " |
| 3572 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3573 | ret = -EINVAL; |
| 3574 | goto out; |
| 3575 | } |
| 3576 | } |
| 3577 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3578 | num_devices = fs_info->fs_devices->num_devices; |
| 3579 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3580 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3581 | BUG_ON(num_devices < 1); |
| 3582 | num_devices--; |
| 3583 | } |
| 3584 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3585 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3586 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3587 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3588 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3589 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3590 | if (num_devices > 2) |
| 3591 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3592 | if (num_devices > 3) |
| 3593 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3594 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3595 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3596 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3597 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3598 | btrfs_err(fs_info, "unable to start balance with target " |
| 3599 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3600 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3601 | ret = -EINVAL; |
| 3602 | goto out; |
| 3603 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3604 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3605 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3606 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3607 | btrfs_err(fs_info, |
| 3608 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3609 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3610 | ret = -EINVAL; |
| 3611 | goto out; |
| 3612 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3613 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3614 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3615 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3616 | btrfs_err(fs_info, |
| 3617 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3618 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3619 | ret = -EINVAL; |
| 3620 | goto out; |
| 3621 | } |
| 3622 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3623 | /* allow dup'ed data chunks only in mixed mode */ |
| 3624 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3625 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3626 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3627 | ret = -EINVAL; |
| 3628 | goto out; |
| 3629 | } |
| 3630 | |
| 3631 | /* allow to reduce meta or sys integrity only if force set */ |
| 3632 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3633 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3634 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3635 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3636 | do { |
| 3637 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3638 | |
| 3639 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3640 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3641 | !(bctl->sys.target & allowed)) || |
| 3642 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3643 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3644 | !(bctl->meta.target & allowed))) { |
| 3645 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3646 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3647 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3648 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3649 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3650 | ret = -EINVAL; |
| 3651 | goto out; |
| 3652 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3653 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3654 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3655 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3656 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
Zhao Lei | 943c6e9 | 2015-08-19 15:54:15 +0800 | [diff] [blame] | 3657 | fs_info->num_tolerated_disk_barrier_failures = min( |
| 3658 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info), |
| 3659 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 3660 | bctl->sys.target)); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3661 | } |
| 3662 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3663 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3664 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3665 | goto out; |
| 3666 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3667 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3668 | BUG_ON(ret == -EEXIST); |
| 3669 | set_balance_control(bctl); |
| 3670 | } else { |
| 3671 | BUG_ON(ret != -EEXIST); |
| 3672 | spin_lock(&fs_info->balance_lock); |
| 3673 | update_balance_args(bctl); |
| 3674 | spin_unlock(&fs_info->balance_lock); |
| 3675 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3676 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3677 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3678 | mutex_unlock(&fs_info->balance_mutex); |
| 3679 | |
| 3680 | ret = __btrfs_balance(fs_info); |
| 3681 | |
| 3682 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3683 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3684 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3685 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3686 | fs_info->num_tolerated_disk_barrier_failures = |
| 3687 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3688 | } |
| 3689 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3690 | if (bargs) { |
| 3691 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3692 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3695 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3696 | balance_need_close(fs_info)) { |
| 3697 | __cancel_balance(fs_info); |
| 3698 | } |
| 3699 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3700 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3701 | |
| 3702 | return ret; |
| 3703 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3704 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3705 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3706 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3707 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3708 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3709 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3710 | return ret; |
| 3711 | } |
| 3712 | |
| 3713 | static int balance_kthread(void *data) |
| 3714 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3715 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3716 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3717 | |
| 3718 | mutex_lock(&fs_info->volume_mutex); |
| 3719 | mutex_lock(&fs_info->balance_mutex); |
| 3720 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3721 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3722 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3723 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3724 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3725 | |
| 3726 | mutex_unlock(&fs_info->balance_mutex); |
| 3727 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3728 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3729 | return ret; |
| 3730 | } |
| 3731 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3732 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3733 | { |
| 3734 | struct task_struct *tsk; |
| 3735 | |
| 3736 | spin_lock(&fs_info->balance_lock); |
| 3737 | if (!fs_info->balance_ctl) { |
| 3738 | spin_unlock(&fs_info->balance_lock); |
| 3739 | return 0; |
| 3740 | } |
| 3741 | spin_unlock(&fs_info->balance_lock); |
| 3742 | |
| 3743 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3744 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3745 | return 0; |
| 3746 | } |
| 3747 | |
| 3748 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3749 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3750 | } |
| 3751 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3752 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3753 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3754 | struct btrfs_balance_control *bctl; |
| 3755 | struct btrfs_balance_item *item; |
| 3756 | struct btrfs_disk_balance_args disk_bargs; |
| 3757 | struct btrfs_path *path; |
| 3758 | struct extent_buffer *leaf; |
| 3759 | struct btrfs_key key; |
| 3760 | int ret; |
| 3761 | |
| 3762 | path = btrfs_alloc_path(); |
| 3763 | if (!path) |
| 3764 | return -ENOMEM; |
| 3765 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3766 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3767 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3768 | key.offset = 0; |
| 3769 | |
| 3770 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3771 | if (ret < 0) |
| 3772 | goto out; |
| 3773 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3774 | ret = 0; |
| 3775 | goto out; |
| 3776 | } |
| 3777 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3778 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3779 | if (!bctl) { |
| 3780 | ret = -ENOMEM; |
| 3781 | goto out; |
| 3782 | } |
| 3783 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3784 | leaf = path->nodes[0]; |
| 3785 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3786 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3787 | bctl->fs_info = fs_info; |
| 3788 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3789 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3790 | |
| 3791 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3792 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3793 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3794 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3795 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3796 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3797 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3798 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3799 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3800 | mutex_lock(&fs_info->volume_mutex); |
| 3801 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3802 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3803 | set_balance_control(bctl); |
| 3804 | |
| 3805 | mutex_unlock(&fs_info->balance_mutex); |
| 3806 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3807 | out: |
| 3808 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3809 | return ret; |
| 3810 | } |
| 3811 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3812 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3813 | { |
| 3814 | int ret = 0; |
| 3815 | |
| 3816 | mutex_lock(&fs_info->balance_mutex); |
| 3817 | if (!fs_info->balance_ctl) { |
| 3818 | mutex_unlock(&fs_info->balance_mutex); |
| 3819 | return -ENOTCONN; |
| 3820 | } |
| 3821 | |
| 3822 | if (atomic_read(&fs_info->balance_running)) { |
| 3823 | atomic_inc(&fs_info->balance_pause_req); |
| 3824 | mutex_unlock(&fs_info->balance_mutex); |
| 3825 | |
| 3826 | wait_event(fs_info->balance_wait_q, |
| 3827 | atomic_read(&fs_info->balance_running) == 0); |
| 3828 | |
| 3829 | mutex_lock(&fs_info->balance_mutex); |
| 3830 | /* we are good with balance_ctl ripped off from under us */ |
| 3831 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3832 | atomic_dec(&fs_info->balance_pause_req); |
| 3833 | } else { |
| 3834 | ret = -ENOTCONN; |
| 3835 | } |
| 3836 | |
| 3837 | mutex_unlock(&fs_info->balance_mutex); |
| 3838 | return ret; |
| 3839 | } |
| 3840 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3841 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3842 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3843 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3844 | return -EROFS; |
| 3845 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3846 | mutex_lock(&fs_info->balance_mutex); |
| 3847 | if (!fs_info->balance_ctl) { |
| 3848 | mutex_unlock(&fs_info->balance_mutex); |
| 3849 | return -ENOTCONN; |
| 3850 | } |
| 3851 | |
| 3852 | atomic_inc(&fs_info->balance_cancel_req); |
| 3853 | /* |
| 3854 | * if we are running just wait and return, balance item is |
| 3855 | * deleted in btrfs_balance in this case |
| 3856 | */ |
| 3857 | if (atomic_read(&fs_info->balance_running)) { |
| 3858 | mutex_unlock(&fs_info->balance_mutex); |
| 3859 | wait_event(fs_info->balance_wait_q, |
| 3860 | atomic_read(&fs_info->balance_running) == 0); |
| 3861 | mutex_lock(&fs_info->balance_mutex); |
| 3862 | } else { |
| 3863 | /* __cancel_balance needs volume_mutex */ |
| 3864 | mutex_unlock(&fs_info->balance_mutex); |
| 3865 | mutex_lock(&fs_info->volume_mutex); |
| 3866 | mutex_lock(&fs_info->balance_mutex); |
| 3867 | |
| 3868 | if (fs_info->balance_ctl) |
| 3869 | __cancel_balance(fs_info); |
| 3870 | |
| 3871 | mutex_unlock(&fs_info->volume_mutex); |
| 3872 | } |
| 3873 | |
| 3874 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3875 | atomic_dec(&fs_info->balance_cancel_req); |
| 3876 | mutex_unlock(&fs_info->balance_mutex); |
| 3877 | return 0; |
| 3878 | } |
| 3879 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3880 | static int btrfs_uuid_scan_kthread(void *data) |
| 3881 | { |
| 3882 | struct btrfs_fs_info *fs_info = data; |
| 3883 | struct btrfs_root *root = fs_info->tree_root; |
| 3884 | struct btrfs_key key; |
| 3885 | struct btrfs_key max_key; |
| 3886 | struct btrfs_path *path = NULL; |
| 3887 | int ret = 0; |
| 3888 | struct extent_buffer *eb; |
| 3889 | int slot; |
| 3890 | struct btrfs_root_item root_item; |
| 3891 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3892 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3893 | |
| 3894 | path = btrfs_alloc_path(); |
| 3895 | if (!path) { |
| 3896 | ret = -ENOMEM; |
| 3897 | goto out; |
| 3898 | } |
| 3899 | |
| 3900 | key.objectid = 0; |
| 3901 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3902 | key.offset = 0; |
| 3903 | |
| 3904 | max_key.objectid = (u64)-1; |
| 3905 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3906 | max_key.offset = (u64)-1; |
| 3907 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3908 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3909 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3910 | if (ret) { |
| 3911 | if (ret > 0) |
| 3912 | ret = 0; |
| 3913 | break; |
| 3914 | } |
| 3915 | |
| 3916 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3917 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3918 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3919 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3920 | goto skip; |
| 3921 | |
| 3922 | eb = path->nodes[0]; |
| 3923 | slot = path->slots[0]; |
| 3924 | item_size = btrfs_item_size_nr(eb, slot); |
| 3925 | if (item_size < sizeof(root_item)) |
| 3926 | goto skip; |
| 3927 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3928 | read_extent_buffer(eb, &root_item, |
| 3929 | btrfs_item_ptr_offset(eb, slot), |
| 3930 | (int)sizeof(root_item)); |
| 3931 | if (btrfs_root_refs(&root_item) == 0) |
| 3932 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3933 | |
| 3934 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3935 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3936 | if (trans) |
| 3937 | goto update_tree; |
| 3938 | |
| 3939 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3940 | /* |
| 3941 | * 1 - subvol uuid item |
| 3942 | * 1 - received_subvol uuid item |
| 3943 | */ |
| 3944 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3945 | if (IS_ERR(trans)) { |
| 3946 | ret = PTR_ERR(trans); |
| 3947 | break; |
| 3948 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3949 | continue; |
| 3950 | } else { |
| 3951 | goto skip; |
| 3952 | } |
| 3953 | update_tree: |
| 3954 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3955 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3956 | root_item.uuid, |
| 3957 | BTRFS_UUID_KEY_SUBVOL, |
| 3958 | key.objectid); |
| 3959 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3960 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3961 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3962 | break; |
| 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3967 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3968 | root_item.received_uuid, |
| 3969 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3970 | key.objectid); |
| 3971 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3972 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3973 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3974 | break; |
| 3975 | } |
| 3976 | } |
| 3977 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3978 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3979 | if (trans) { |
| 3980 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3981 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3982 | if (ret) |
| 3983 | break; |
| 3984 | } |
| 3985 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3986 | btrfs_release_path(path); |
| 3987 | if (key.offset < (u64)-1) { |
| 3988 | key.offset++; |
| 3989 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3990 | key.offset = 0; |
| 3991 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3992 | } else if (key.objectid < (u64)-1) { |
| 3993 | key.offset = 0; |
| 3994 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3995 | key.objectid++; |
| 3996 | } else { |
| 3997 | break; |
| 3998 | } |
| 3999 | cond_resched(); |
| 4000 | } |
| 4001 | |
| 4002 | out: |
| 4003 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 4004 | if (trans && !IS_ERR(trans)) |
| 4005 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4006 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4007 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4008 | else |
| 4009 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4010 | up(&fs_info->uuid_tree_rescan_sem); |
| 4011 | return 0; |
| 4012 | } |
| 4013 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4014 | /* |
| 4015 | * Callback for btrfs_uuid_tree_iterate(). |
| 4016 | * returns: |
| 4017 | * 0 check succeeded, the entry is not outdated. |
| 4018 | * < 0 if an error occured. |
| 4019 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 4020 | */ |
| 4021 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 4022 | u8 *uuid, u8 type, u64 subid) |
| 4023 | { |
| 4024 | struct btrfs_key key; |
| 4025 | int ret = 0; |
| 4026 | struct btrfs_root *subvol_root; |
| 4027 | |
| 4028 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 4029 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 4030 | goto out; |
| 4031 | |
| 4032 | key.objectid = subid; |
| 4033 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4034 | key.offset = (u64)-1; |
| 4035 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4036 | if (IS_ERR(subvol_root)) { |
| 4037 | ret = PTR_ERR(subvol_root); |
| 4038 | if (ret == -ENOENT) |
| 4039 | ret = 1; |
| 4040 | goto out; |
| 4041 | } |
| 4042 | |
| 4043 | switch (type) { |
| 4044 | case BTRFS_UUID_KEY_SUBVOL: |
| 4045 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 4046 | ret = 1; |
| 4047 | break; |
| 4048 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 4049 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 4050 | BTRFS_UUID_SIZE)) |
| 4051 | ret = 1; |
| 4052 | break; |
| 4053 | } |
| 4054 | |
| 4055 | out: |
| 4056 | return ret; |
| 4057 | } |
| 4058 | |
| 4059 | static int btrfs_uuid_rescan_kthread(void *data) |
| 4060 | { |
| 4061 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 4062 | int ret; |
| 4063 | |
| 4064 | /* |
| 4065 | * 1st step is to iterate through the existing UUID tree and |
| 4066 | * to delete all entries that contain outdated data. |
| 4067 | * 2nd step is to add all missing entries to the UUID tree. |
| 4068 | */ |
| 4069 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4070 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4071 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4072 | up(&fs_info->uuid_tree_rescan_sem); |
| 4073 | return ret; |
| 4074 | } |
| 4075 | return btrfs_uuid_scan_kthread(data); |
| 4076 | } |
| 4077 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4078 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4079 | { |
| 4080 | struct btrfs_trans_handle *trans; |
| 4081 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4082 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4083 | struct task_struct *task; |
| 4084 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4085 | |
| 4086 | /* |
| 4087 | * 1 - root node |
| 4088 | * 1 - root item |
| 4089 | */ |
| 4090 | trans = btrfs_start_transaction(tree_root, 2); |
| 4091 | if (IS_ERR(trans)) |
| 4092 | return PTR_ERR(trans); |
| 4093 | |
| 4094 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4095 | BTRFS_UUID_TREE_OBJECTID); |
| 4096 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4097 | ret = PTR_ERR(uuid_root); |
| 4098 | btrfs_abort_transaction(trans, tree_root, ret); |
| 4099 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | fs_info->uuid_root = uuid_root; |
| 4103 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4104 | ret = btrfs_commit_transaction(trans, tree_root); |
| 4105 | if (ret) |
| 4106 | return ret; |
| 4107 | |
| 4108 | down(&fs_info->uuid_tree_rescan_sem); |
| 4109 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4110 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4111 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4112 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4113 | up(&fs_info->uuid_tree_rescan_sem); |
| 4114 | return PTR_ERR(task); |
| 4115 | } |
| 4116 | |
| 4117 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4118 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4119 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4120 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4121 | { |
| 4122 | struct task_struct *task; |
| 4123 | |
| 4124 | down(&fs_info->uuid_tree_rescan_sem); |
| 4125 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4126 | if (IS_ERR(task)) { |
| 4127 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4128 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4129 | up(&fs_info->uuid_tree_rescan_sem); |
| 4130 | return PTR_ERR(task); |
| 4131 | } |
| 4132 | |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4136 | /* |
| 4137 | * shrinking a device means finding all of the device extents past |
| 4138 | * the new size, and then following the back refs to the chunks. |
| 4139 | * The chunk relocation code actually frees the device extent |
| 4140 | */ |
| 4141 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4142 | { |
| 4143 | struct btrfs_trans_handle *trans; |
| 4144 | struct btrfs_root *root = device->dev_root; |
| 4145 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4146 | struct btrfs_path *path; |
| 4147 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4148 | u64 chunk_offset; |
| 4149 | int ret; |
| 4150 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4151 | int failed = 0; |
| 4152 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4153 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4154 | struct extent_buffer *l; |
| 4155 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4156 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4157 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4158 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 4159 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4160 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4161 | if (device->is_tgtdev_for_dev_replace) |
| 4162 | return -EINVAL; |
| 4163 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4164 | path = btrfs_alloc_path(); |
| 4165 | if (!path) |
| 4166 | return -ENOMEM; |
| 4167 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4168 | path->reada = 2; |
| 4169 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4170 | lock_chunks(root); |
| 4171 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4172 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4173 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4174 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4175 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4176 | root->fs_info->free_chunk_space -= diff; |
| 4177 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4178 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4179 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4180 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4181 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4182 | key.objectid = device->devid; |
| 4183 | key.offset = (u64)-1; |
| 4184 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4185 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4186 | do { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4187 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4188 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4189 | if (ret < 0) { |
| 4190 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4191 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4192 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4193 | |
| 4194 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4195 | if (ret) |
| 4196 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4197 | if (ret < 0) |
| 4198 | goto done; |
| 4199 | if (ret) { |
| 4200 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4201 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4202 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4203 | } |
| 4204 | |
| 4205 | l = path->nodes[0]; |
| 4206 | slot = path->slots[0]; |
| 4207 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4208 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4209 | if (key.objectid != device->devid) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4210 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4211 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4212 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4213 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4214 | |
| 4215 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4216 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4217 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4218 | if (key.offset + length <= new_size) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4219 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4220 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4221 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4222 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4223 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4224 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4225 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4226 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 4227 | ret = btrfs_relocate_chunk(root, chunk_offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4228 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4229 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4230 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4231 | if (ret == -ENOSPC) |
| 4232 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4233 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4234 | |
| 4235 | if (failed && !retried) { |
| 4236 | failed = 0; |
| 4237 | retried = true; |
| 4238 | goto again; |
| 4239 | } else if (failed && retried) { |
| 4240 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4241 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4242 | } |
| 4243 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4244 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4245 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4246 | if (IS_ERR(trans)) { |
| 4247 | ret = PTR_ERR(trans); |
| 4248 | goto done; |
| 4249 | } |
| 4250 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4251 | lock_chunks(root); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4252 | |
| 4253 | /* |
| 4254 | * We checked in the above loop all device extents that were already in |
| 4255 | * the device tree. However before we have updated the device's |
| 4256 | * total_bytes to the new size, we might have had chunk allocations that |
| 4257 | * have not complete yet (new block groups attached to transaction |
| 4258 | * handles), and therefore their device extents were not yet in the |
| 4259 | * device tree and we missed them in the loop above. So if we have any |
| 4260 | * pending chunk using a device extent that overlaps the device range |
| 4261 | * that we can not use anymore, commit the current transaction and |
| 4262 | * repeat the search on the device tree - this way we guarantee we will |
| 4263 | * not have chunks using device extents that end beyond 'new_size'. |
| 4264 | */ |
| 4265 | if (!checked_pending_chunks) { |
| 4266 | u64 start = new_size; |
| 4267 | u64 len = old_size - new_size; |
| 4268 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 4269 | if (contains_pending_extent(trans->transaction, device, |
| 4270 | &start, len)) { |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4271 | unlock_chunks(root); |
| 4272 | checked_pending_chunks = true; |
| 4273 | failed = 0; |
| 4274 | retried = false; |
| 4275 | ret = btrfs_commit_transaction(trans, root); |
| 4276 | if (ret) |
| 4277 | goto done; |
| 4278 | goto again; |
| 4279 | } |
| 4280 | } |
| 4281 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4282 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4283 | if (list_empty(&device->resized_list)) |
| 4284 | list_add_tail(&device->resized_list, |
| 4285 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4286 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4287 | WARN_ON(diff > old_total); |
| 4288 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4289 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4290 | |
| 4291 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4292 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4293 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4294 | done: |
| 4295 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4296 | if (ret) { |
| 4297 | lock_chunks(root); |
| 4298 | btrfs_device_set_total_bytes(device, old_size); |
| 4299 | if (device->writeable) |
| 4300 | device->fs_devices->total_rw_bytes += diff; |
| 4301 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4302 | root->fs_info->free_chunk_space += diff; |
| 4303 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4304 | unlock_chunks(root); |
| 4305 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4306 | return ret; |
| 4307 | } |
| 4308 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4309 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4310 | struct btrfs_key *key, |
| 4311 | struct btrfs_chunk *chunk, int item_size) |
| 4312 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4313 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4314 | struct btrfs_disk_key disk_key; |
| 4315 | u32 array_size; |
| 4316 | u8 *ptr; |
| 4317 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4318 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4319 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4320 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4321 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4322 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4323 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4324 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4325 | |
| 4326 | ptr = super_copy->sys_chunk_array + array_size; |
| 4327 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4328 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4329 | ptr += sizeof(disk_key); |
| 4330 | memcpy(ptr, chunk, item_size); |
| 4331 | item_size += sizeof(disk_key); |
| 4332 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4333 | unlock_chunks(root); |
| 4334 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4335 | return 0; |
| 4336 | } |
| 4337 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4338 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4339 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4340 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4341 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4342 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4343 | const struct btrfs_device_info *di_a = a; |
| 4344 | const struct btrfs_device_info *di_b = b; |
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 | if (di_a->max_avail > di_b->max_avail) |
| 4347 | return -1; |
| 4348 | if (di_a->max_avail < di_b->max_avail) |
| 4349 | return 1; |
| 4350 | if (di_a->total_avail > di_b->total_avail) |
| 4351 | return -1; |
| 4352 | if (di_a->total_avail < di_b->total_avail) |
| 4353 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4354 | return 0; |
| 4355 | } |
| 4356 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4357 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4358 | { |
| 4359 | /* TODO allow them to set a preferred stripe size */ |
| 4360 | return 64 * 1024; |
| 4361 | } |
| 4362 | |
| 4363 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4364 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4365 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4366 | return; |
| 4367 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4368 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4369 | } |
| 4370 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4371 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4372 | - sizeof(struct btrfs_item) \ |
| 4373 | - sizeof(struct btrfs_chunk)) \ |
| 4374 | / sizeof(struct btrfs_stripe) + 1) |
| 4375 | |
| 4376 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4377 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4378 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4379 | / sizeof(struct btrfs_stripe) + 1) |
| 4380 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4381 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4382 | struct btrfs_root *extent_root, u64 start, |
| 4383 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4384 | { |
| 4385 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4386 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4387 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4388 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4389 | struct extent_map_tree *em_tree; |
| 4390 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4391 | struct btrfs_device_info *devices_info = NULL; |
| 4392 | u64 total_avail; |
| 4393 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4394 | int data_stripes; /* number of stripes that count for |
| 4395 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4396 | int sub_stripes; /* sub_stripes info for map */ |
| 4397 | int dev_stripes; /* stripes per dev */ |
| 4398 | int devs_max; /* max devs to use */ |
| 4399 | int devs_min; /* min devs needed */ |
| 4400 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4401 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4402 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4403 | u64 max_stripe_size; |
| 4404 | u64 max_chunk_size; |
| 4405 | u64 stripe_size; |
| 4406 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4407 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4408 | int ndevs; |
| 4409 | int i; |
| 4410 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4411 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4412 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4413 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4414 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4415 | if (list_empty(&fs_devices->alloc_list)) |
| 4416 | return -ENOSPC; |
| 4417 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4418 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4419 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4420 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4421 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4422 | devs_max = btrfs_raid_array[index].devs_max; |
| 4423 | devs_min = btrfs_raid_array[index].devs_min; |
| 4424 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4425 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4426 | |
| 4427 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4428 | max_stripe_size = 1024 * 1024 * 1024; |
| 4429 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4430 | if (!devs_max) |
| 4431 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4432 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4433 | /* for larger filesystems, use larger metadata chunks */ |
| 4434 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4435 | max_stripe_size = 1024 * 1024 * 1024; |
| 4436 | else |
| 4437 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4438 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4439 | if (!devs_max) |
| 4440 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4441 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4442 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4443 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4444 | if (!devs_max) |
| 4445 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4446 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4447 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4448 | type); |
| 4449 | BUG_ON(1); |
| 4450 | } |
| 4451 | |
| 4452 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4453 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4454 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4455 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4456 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4457 | GFP_NOFS); |
| 4458 | if (!devices_info) |
| 4459 | return -ENOMEM; |
| 4460 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4461 | cur = fs_devices->alloc_list.next; |
| 4462 | |
| 4463 | /* |
| 4464 | * in the first pass through the devices list, we gather information |
| 4465 | * about the available holes on each device. |
| 4466 | */ |
| 4467 | ndevs = 0; |
| 4468 | while (cur != &fs_devices->alloc_list) { |
| 4469 | struct btrfs_device *device; |
| 4470 | u64 max_avail; |
| 4471 | u64 dev_offset; |
| 4472 | |
| 4473 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4474 | |
| 4475 | cur = cur->next; |
| 4476 | |
| 4477 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4478 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4479 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4480 | continue; |
| 4481 | } |
| 4482 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4483 | if (!device->in_fs_metadata || |
| 4484 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4485 | continue; |
| 4486 | |
| 4487 | if (device->total_bytes > device->bytes_used) |
| 4488 | total_avail = device->total_bytes - device->bytes_used; |
| 4489 | else |
| 4490 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4491 | |
| 4492 | /* If there is no space on this device, skip it. */ |
| 4493 | if (total_avail == 0) |
| 4494 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4495 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4496 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4497 | max_stripe_size * dev_stripes, |
| 4498 | &dev_offset, &max_avail); |
| 4499 | if (ret && ret != -ENOSPC) |
| 4500 | goto error; |
| 4501 | |
| 4502 | if (ret == 0) |
| 4503 | max_avail = max_stripe_size * dev_stripes; |
| 4504 | |
| 4505 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4506 | continue; |
| 4507 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4508 | if (ndevs == fs_devices->rw_devices) { |
| 4509 | WARN(1, "%s: found more than %llu devices\n", |
| 4510 | __func__, fs_devices->rw_devices); |
| 4511 | break; |
| 4512 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4513 | devices_info[ndevs].dev_offset = dev_offset; |
| 4514 | devices_info[ndevs].max_avail = max_avail; |
| 4515 | devices_info[ndevs].total_avail = total_avail; |
| 4516 | devices_info[ndevs].dev = device; |
| 4517 | ++ndevs; |
| 4518 | } |
| 4519 | |
| 4520 | /* |
| 4521 | * now sort the devices by hole size / available space |
| 4522 | */ |
| 4523 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4524 | btrfs_cmp_device_info, NULL); |
| 4525 | |
| 4526 | /* round down to number of usable stripes */ |
| 4527 | ndevs -= ndevs % devs_increment; |
| 4528 | |
| 4529 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4530 | ret = -ENOSPC; |
| 4531 | goto error; |
| 4532 | } |
| 4533 | |
| 4534 | if (devs_max && ndevs > devs_max) |
| 4535 | ndevs = devs_max; |
| 4536 | /* |
| 4537 | * the primary goal is to maximize the number of stripes, so use as many |
| 4538 | * devices as possible, even if the stripes are not maximum sized. |
| 4539 | */ |
| 4540 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4541 | num_stripes = ndevs * dev_stripes; |
| 4542 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4543 | /* |
| 4544 | * this will have to be fixed for RAID1 and RAID10 over |
| 4545 | * more drives |
| 4546 | */ |
| 4547 | data_stripes = num_stripes / ncopies; |
| 4548 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4549 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4550 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4551 | btrfs_super_stripesize(info->super_copy)); |
| 4552 | data_stripes = num_stripes - 1; |
| 4553 | } |
| 4554 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4555 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4556 | btrfs_super_stripesize(info->super_copy)); |
| 4557 | data_stripes = num_stripes - 2; |
| 4558 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4559 | |
| 4560 | /* |
| 4561 | * Use the number of data stripes to figure out how big this chunk |
| 4562 | * is really going to be in terms of logical address space, |
| 4563 | * and compare that answer with the max chunk size |
| 4564 | */ |
| 4565 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4566 | u64 mask = (1ULL << 24) - 1; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4567 | |
| 4568 | stripe_size = div_u64(max_chunk_size, data_stripes); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4569 | |
| 4570 | /* bump the answer up to a 16MB boundary */ |
| 4571 | stripe_size = (stripe_size + mask) & ~mask; |
| 4572 | |
| 4573 | /* but don't go higher than the limits we found |
| 4574 | * while searching for free extents |
| 4575 | */ |
| 4576 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4577 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4578 | } |
| 4579 | |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4580 | stripe_size = div_u64(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4581 | |
| 4582 | /* align to BTRFS_STRIPE_LEN */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4583 | stripe_size = div_u64(stripe_size, raid_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4584 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4585 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4586 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4587 | if (!map) { |
| 4588 | ret = -ENOMEM; |
| 4589 | goto error; |
| 4590 | } |
| 4591 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4592 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4593 | for (i = 0; i < ndevs; ++i) { |
| 4594 | for (j = 0; j < dev_stripes; ++j) { |
| 4595 | int s = i * dev_stripes + j; |
| 4596 | map->stripes[s].dev = devices_info[i].dev; |
| 4597 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4598 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4599 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4600 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4601 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4602 | map->stripe_len = raid_stripe_len; |
| 4603 | map->io_align = raid_stripe_len; |
| 4604 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4605 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4606 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4607 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4608 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4609 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4610 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4611 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4612 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4613 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4614 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4615 | ret = -ENOMEM; |
| 4616 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4617 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4618 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4619 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4620 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4621 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4622 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4623 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4624 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4625 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4626 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4627 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4628 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4629 | if (!ret) { |
| 4630 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4631 | atomic_inc(&em->refs); |
| 4632 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4633 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4634 | if (ret) { |
| 4635 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4636 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4637 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4638 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4639 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4640 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4641 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4642 | if (ret) |
| 4643 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4644 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4645 | for (i = 0; i < map->num_stripes; i++) { |
| 4646 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4647 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4648 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4649 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4650 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4651 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4652 | map->num_stripes); |
| 4653 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4654 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4655 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4656 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4657 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4658 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4659 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4660 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4661 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4662 | write_lock(&em_tree->lock); |
| 4663 | remove_extent_mapping(em_tree, em); |
| 4664 | write_unlock(&em_tree->lock); |
| 4665 | |
| 4666 | /* One for our allocation */ |
| 4667 | free_extent_map(em); |
| 4668 | /* One for the tree reference */ |
| 4669 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4670 | /* One for the pending_chunks list reference */ |
| 4671 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4672 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4673 | kfree(devices_info); |
| 4674 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4675 | } |
| 4676 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4677 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4678 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4679 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4680 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4681 | struct btrfs_key key; |
| 4682 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4683 | struct btrfs_device *device; |
| 4684 | struct btrfs_chunk *chunk; |
| 4685 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4686 | struct extent_map_tree *em_tree; |
| 4687 | struct extent_map *em; |
| 4688 | struct map_lookup *map; |
| 4689 | size_t item_size; |
| 4690 | u64 dev_offset; |
| 4691 | u64 stripe_size; |
| 4692 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4693 | int ret; |
| 4694 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4695 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4696 | read_lock(&em_tree->lock); |
| 4697 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4698 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4699 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4700 | if (!em) { |
| 4701 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4702 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4703 | return -EINVAL; |
| 4704 | } |
| 4705 | |
| 4706 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4707 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4708 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4709 | chunk_size, em->start, em->len); |
| 4710 | free_extent_map(em); |
| 4711 | return -EINVAL; |
| 4712 | } |
| 4713 | |
| 4714 | map = (struct map_lookup *)em->bdev; |
| 4715 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4716 | stripe_size = em->orig_block_len; |
| 4717 | |
| 4718 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4719 | if (!chunk) { |
| 4720 | ret = -ENOMEM; |
| 4721 | goto out; |
| 4722 | } |
| 4723 | |
| 4724 | for (i = 0; i < map->num_stripes; i++) { |
| 4725 | device = map->stripes[i].dev; |
| 4726 | dev_offset = map->stripes[i].physical; |
| 4727 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4728 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4729 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4730 | goto out; |
| 4731 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4732 | chunk_root->root_key.objectid, |
| 4733 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4734 | chunk_offset, dev_offset, |
| 4735 | stripe_size); |
| 4736 | if (ret) |
| 4737 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4738 | } |
| 4739 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4740 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4741 | for (i = 0; i < map->num_stripes; i++) { |
| 4742 | device = map->stripes[i].dev; |
| 4743 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4744 | |
| 4745 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4746 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4747 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4748 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4752 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4753 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4754 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4755 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4756 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4757 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4758 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4759 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4760 | |
| 4761 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4762 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4763 | key.offset = chunk_offset; |
| 4764 | |
| 4765 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4766 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4767 | /* |
| 4768 | * TODO: Cleanup of inserted chunk root in case of |
| 4769 | * failure. |
| 4770 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4771 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4772 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4773 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4774 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4775 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4776 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4777 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4778 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | /* |
| 4782 | * Chunk allocation falls into two parts. The first part does works |
| 4783 | * that make the new allocated chunk useable, but not do any operation |
| 4784 | * that modifies the chunk tree. The second part does the works that |
| 4785 | * require modifying the chunk tree. This division is important for the |
| 4786 | * bootstrap process of adding storage to a seed btrfs. |
| 4787 | */ |
| 4788 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4789 | struct btrfs_root *extent_root, u64 type) |
| 4790 | { |
| 4791 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4792 | |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 4793 | ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex)); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4794 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4795 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4796 | } |
| 4797 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4798 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4799 | struct btrfs_root *root, |
| 4800 | struct btrfs_device *device) |
| 4801 | { |
| 4802 | u64 chunk_offset; |
| 4803 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4804 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4805 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4806 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4807 | int ret; |
| 4808 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4809 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4810 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4811 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4812 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4813 | if (ret) |
| 4814 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4815 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4816 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4817 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4818 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4819 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4820 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4821 | } |
| 4822 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4823 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4824 | { |
| 4825 | int max_errors; |
| 4826 | |
| 4827 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4828 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4829 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4830 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4831 | max_errors = 1; |
| 4832 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4833 | max_errors = 2; |
| 4834 | } else { |
| 4835 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4836 | } |
| 4837 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4838 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4842 | { |
| 4843 | struct extent_map *em; |
| 4844 | struct map_lookup *map; |
| 4845 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4846 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4847 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4848 | int i; |
| 4849 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4850 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4851 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4852 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4853 | if (!em) |
| 4854 | return 1; |
| 4855 | |
| 4856 | map = (struct map_lookup *)em->bdev; |
| 4857 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4858 | if (map->stripes[i].dev->missing) { |
| 4859 | miss_ndevs++; |
| 4860 | continue; |
| 4861 | } |
| 4862 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4863 | if (!map->stripes[i].dev->writeable) { |
| 4864 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4865 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4866 | } |
| 4867 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4868 | |
| 4869 | /* |
| 4870 | * If the number of missing devices is larger than max errors, |
| 4871 | * we can not write the data into that chunk successfully, so |
| 4872 | * set it readonly. |
| 4873 | */ |
| 4874 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4875 | readonly = 1; |
| 4876 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4877 | free_extent_map(em); |
| 4878 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4882 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4883 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4887 | { |
| 4888 | struct extent_map *em; |
| 4889 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4890 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4891 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4892 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4893 | if (em) |
| 4894 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4895 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4896 | if (!em) |
| 4897 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4898 | /* once for us */ |
| 4899 | free_extent_map(em); |
| 4900 | /* once for the tree */ |
| 4901 | free_extent_map(em); |
| 4902 | } |
| 4903 | } |
| 4904 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4905 | 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] | 4906 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4907 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4908 | struct extent_map *em; |
| 4909 | struct map_lookup *map; |
| 4910 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4911 | int ret; |
| 4912 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4913 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4914 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4915 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4916 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4917 | /* |
| 4918 | * We could return errors for these cases, but that could get ugly and |
| 4919 | * we'd probably do the same thing which is just not do anything else |
| 4920 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4921 | */ |
| 4922 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4923 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4924 | logical+len); |
| 4925 | return 1; |
| 4926 | } |
| 4927 | |
| 4928 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4929 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4930 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4931 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4932 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4933 | return 1; |
| 4934 | } |
| 4935 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4936 | map = (struct map_lookup *)em->bdev; |
| 4937 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4938 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4939 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4940 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4941 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4942 | ret = 2; |
| 4943 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4944 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4945 | else |
| 4946 | ret = 1; |
| 4947 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4948 | |
| 4949 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4950 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4951 | ret++; |
| 4952 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4953 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4954 | return ret; |
| 4955 | } |
| 4956 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4957 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4958 | struct btrfs_mapping_tree *map_tree, |
| 4959 | u64 logical) |
| 4960 | { |
| 4961 | struct extent_map *em; |
| 4962 | struct map_lookup *map; |
| 4963 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4964 | unsigned long len = root->sectorsize; |
| 4965 | |
| 4966 | read_lock(&em_tree->lock); |
| 4967 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4968 | read_unlock(&em_tree->lock); |
| 4969 | BUG_ON(!em); |
| 4970 | |
| 4971 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4972 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4973 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4974 | len = map->stripe_len * nr_data_stripes(map); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4975 | free_extent_map(em); |
| 4976 | return len; |
| 4977 | } |
| 4978 | |
| 4979 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4980 | u64 logical, u64 len, int mirror_num) |
| 4981 | { |
| 4982 | struct extent_map *em; |
| 4983 | struct map_lookup *map; |
| 4984 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4985 | int ret = 0; |
| 4986 | |
| 4987 | read_lock(&em_tree->lock); |
| 4988 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4989 | read_unlock(&em_tree->lock); |
| 4990 | BUG_ON(!em); |
| 4991 | |
| 4992 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4993 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4994 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4995 | ret = 1; |
| 4996 | free_extent_map(em); |
| 4997 | return ret; |
| 4998 | } |
| 4999 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5000 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 5001 | struct map_lookup *map, int first, int num, |
| 5002 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5003 | { |
| 5004 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5005 | int tolerance; |
| 5006 | struct btrfs_device *srcdev; |
| 5007 | |
| 5008 | if (dev_replace_is_ongoing && |
| 5009 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5010 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5011 | srcdev = fs_info->dev_replace.srcdev; |
| 5012 | else |
| 5013 | srcdev = NULL; |
| 5014 | |
| 5015 | /* |
| 5016 | * try to avoid the drive that is the source drive for a |
| 5017 | * dev-replace procedure, only choose it if no other non-missing |
| 5018 | * mirror is available |
| 5019 | */ |
| 5020 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 5021 | if (map->stripes[optimal].dev->bdev && |
| 5022 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 5023 | return optimal; |
| 5024 | for (i = first; i < first + num; i++) { |
| 5025 | if (map->stripes[i].dev->bdev && |
| 5026 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5027 | return i; |
| 5028 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5029 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5030 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5031 | /* we couldn't find one that doesn't fail. Just return something |
| 5032 | * and the io error handling code will clean up eventually |
| 5033 | */ |
| 5034 | return optimal; |
| 5035 | } |
| 5036 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5037 | static inline int parity_smaller(u64 a, u64 b) |
| 5038 | { |
| 5039 | return a > b; |
| 5040 | } |
| 5041 | |
| 5042 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5043 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5044 | { |
| 5045 | struct btrfs_bio_stripe s; |
| 5046 | int i; |
| 5047 | u64 l; |
| 5048 | int again = 1; |
| 5049 | |
| 5050 | while (again) { |
| 5051 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5052 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5053 | if (parity_smaller(bbio->raid_map[i], |
| 5054 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5055 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5056 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5057 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5058 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5059 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5060 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5061 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5062 | again = 1; |
| 5063 | } |
| 5064 | } |
| 5065 | } |
| 5066 | } |
| 5067 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5068 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5069 | { |
| 5070 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5071 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5072 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5073 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5074 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5075 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5076 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5077 | /* |
| 5078 | * plus the raid_map, which includes both the tgt dev |
| 5079 | * and the stripes |
| 5080 | */ |
| 5081 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5082 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5083 | |
| 5084 | atomic_set(&bbio->error, 0); |
| 5085 | atomic_set(&bbio->refs, 1); |
| 5086 | |
| 5087 | return bbio; |
| 5088 | } |
| 5089 | |
| 5090 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5091 | { |
| 5092 | WARN_ON(!atomic_read(&bbio->refs)); |
| 5093 | atomic_inc(&bbio->refs); |
| 5094 | } |
| 5095 | |
| 5096 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5097 | { |
| 5098 | if (!bbio) |
| 5099 | return; |
| 5100 | if (atomic_dec_and_test(&bbio->refs)) |
| 5101 | kfree(bbio); |
| 5102 | } |
| 5103 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5104 | 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] | 5105 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5106 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5107 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5108 | { |
| 5109 | struct extent_map *em; |
| 5110 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5111 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5112 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5113 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5114 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5115 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5116 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5117 | u64 stripe_nr_orig; |
| 5118 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5119 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5120 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5121 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5122 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5123 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5124 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5125 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5126 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5127 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5128 | int dev_replace_is_ongoing = 0; |
| 5129 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5130 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5131 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5132 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5133 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5134 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5135 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5136 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5137 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5138 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5139 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5140 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5141 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5142 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5143 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5144 | if (em->start > logical || em->start + em->len < logical) { |
| 5145 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5146 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5147 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 5148 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5149 | return -EINVAL; |
| 5150 | } |
| 5151 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5152 | map = (struct map_lookup *)em->bdev; |
| 5153 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5154 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5155 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5156 | stripe_nr = offset; |
| 5157 | /* |
| 5158 | * stripe_nr counts the total number of stripes we have to stride |
| 5159 | * to get to this block |
| 5160 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5161 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5162 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5163 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5164 | BUG_ON(offset < stripe_offset); |
| 5165 | |
| 5166 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5167 | stripe_offset = offset - stripe_offset; |
| 5168 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5169 | /* 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] | 5170 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5171 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5172 | raid56_full_stripe_start = offset; |
| 5173 | |
| 5174 | /* allow a write of a full stripe, but make sure we don't |
| 5175 | * allow straddling of stripes |
| 5176 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5177 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5178 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5179 | raid56_full_stripe_start *= full_stripe_len; |
| 5180 | } |
| 5181 | |
| 5182 | if (rw & REQ_DISCARD) { |
| 5183 | /* we don't discard raid56 yet */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5184 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5185 | ret = -EOPNOTSUPP; |
| 5186 | goto out; |
| 5187 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5188 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5189 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5190 | u64 max_len; |
| 5191 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5192 | For other RAID types and for RAID[56] reads, just allow a single |
| 5193 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5194 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5195 | (rw & REQ_WRITE)) { |
| 5196 | max_len = stripe_len * nr_data_stripes(map) - |
| 5197 | (offset - raid56_full_stripe_start); |
| 5198 | } else { |
| 5199 | /* we limit the length of each bio to what fits in a stripe */ |
| 5200 | max_len = stripe_len - stripe_offset; |
| 5201 | } |
| 5202 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5203 | } else { |
| 5204 | *length = em->len - offset; |
| 5205 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5206 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5207 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5208 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5209 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5210 | goto out; |
| 5211 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5212 | btrfs_dev_replace_lock(dev_replace); |
| 5213 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5214 | if (!dev_replace_is_ongoing) |
| 5215 | btrfs_dev_replace_unlock(dev_replace); |
| 5216 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5217 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5218 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5219 | dev_replace->tgtdev != NULL) { |
| 5220 | /* |
| 5221 | * in dev-replace case, for repair case (that's the only |
| 5222 | * case where the mirror is selected explicitly when |
| 5223 | * calling btrfs_map_block), blocks left of the left cursor |
| 5224 | * can also be read from the target drive. |
| 5225 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5226 | * the last one to the array of stripes. For READ, it also |
| 5227 | * needs to be supported using the same mirror number. |
| 5228 | * If the requested block is not left of the left cursor, |
| 5229 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5230 | * returns one more in the dev-replace case. |
| 5231 | */ |
| 5232 | u64 tmp_length = *length; |
| 5233 | struct btrfs_bio *tmp_bbio = NULL; |
| 5234 | int tmp_num_stripes; |
| 5235 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5236 | int index_srcdev = 0; |
| 5237 | int found = 0; |
| 5238 | u64 physical_of_found = 0; |
| 5239 | |
| 5240 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5241 | logical, &tmp_length, &tmp_bbio, 0, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5242 | if (ret) { |
| 5243 | WARN_ON(tmp_bbio != NULL); |
| 5244 | goto out; |
| 5245 | } |
| 5246 | |
| 5247 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5248 | if (mirror_num > tmp_num_stripes) { |
| 5249 | /* |
| 5250 | * REQ_GET_READ_MIRRORS does not contain this |
| 5251 | * mirror, that means that the requested area |
| 5252 | * is not left of the left cursor |
| 5253 | */ |
| 5254 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5255 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5256 | goto out; |
| 5257 | } |
| 5258 | |
| 5259 | /* |
| 5260 | * process the rest of the function using the mirror_num |
| 5261 | * of the source drive. Therefore look it up first. |
| 5262 | * At the end, patch the device pointer to the one of the |
| 5263 | * target drive. |
| 5264 | */ |
| 5265 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5266 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5267 | /* |
| 5268 | * In case of DUP, in order to keep it |
| 5269 | * simple, only add the mirror with the |
| 5270 | * lowest physical address |
| 5271 | */ |
| 5272 | if (found && |
| 5273 | physical_of_found <= |
| 5274 | tmp_bbio->stripes[i].physical) |
| 5275 | continue; |
| 5276 | index_srcdev = i; |
| 5277 | found = 1; |
| 5278 | physical_of_found = |
| 5279 | tmp_bbio->stripes[i].physical; |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | if (found) { |
| 5284 | mirror_num = index_srcdev + 1; |
| 5285 | patch_the_first_stripe_for_dev_replace = 1; |
| 5286 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5287 | } else { |
| 5288 | WARN_ON(1); |
| 5289 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5290 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5291 | goto out; |
| 5292 | } |
| 5293 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5294 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5295 | } else if (mirror_num > map->num_stripes) { |
| 5296 | mirror_num = 0; |
| 5297 | } |
| 5298 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5299 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5300 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5301 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5302 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5303 | stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5304 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5305 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5306 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5307 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5308 | if (rw & REQ_DISCARD) |
| 5309 | num_stripes = min_t(u64, map->num_stripes, |
| 5310 | stripe_nr_end - stripe_nr_orig); |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5311 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5312 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5313 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5314 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5315 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5316 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5317 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5318 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5319 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5320 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5321 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5322 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5323 | current->pid % map->num_stripes, |
| 5324 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5325 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5326 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5327 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5328 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5329 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5330 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5331 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5332 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5333 | } else { |
| 5334 | mirror_num = 1; |
| 5335 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5336 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5337 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5338 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5339 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5340 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5341 | stripe_index *= map->sub_stripes; |
| 5342 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5343 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5344 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5345 | else if (rw & REQ_DISCARD) |
| 5346 | num_stripes = min_t(u64, map->sub_stripes * |
| 5347 | (stripe_nr_end - stripe_nr_orig), |
| 5348 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5349 | else if (mirror_num) |
| 5350 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5351 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5352 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5353 | stripe_index = find_live_mirror(fs_info, map, |
| 5354 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5355 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5356 | current->pid % map->sub_stripes, |
| 5357 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5358 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5359 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5360 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5361 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5362 | if (need_raid_map && |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5363 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5364 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5365 | /* push stripe_nr back to the start of the full stripe */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5366 | stripe_nr = div_u64(raid56_full_stripe_start, |
| 5367 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5368 | |
| 5369 | /* RAID[56] write or recovery. Return all stripes */ |
| 5370 | num_stripes = map->num_stripes; |
| 5371 | max_errors = nr_parity_stripes(map); |
| 5372 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5373 | *length = map->stripe_len; |
| 5374 | stripe_index = 0; |
| 5375 | stripe_offset = 0; |
| 5376 | } else { |
| 5377 | /* |
| 5378 | * Mirror #0 or #1 means the original data block. |
| 5379 | * Mirror #2 is RAID5 parity block. |
| 5380 | * Mirror #3 is RAID6 Q block. |
| 5381 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5382 | stripe_nr = div_u64_rem(stripe_nr, |
| 5383 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5384 | if (mirror_num > 1) |
| 5385 | stripe_index = nr_data_stripes(map) + |
| 5386 | mirror_num - 2; |
| 5387 | |
| 5388 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5389 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5390 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5391 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5392 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5393 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5394 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5395 | } else { |
| 5396 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5397 | * after this, stripe_nr is the number of stripes on this |
| 5398 | * device we have to walk to find the data, and stripe_index is |
| 5399 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5400 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5401 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5402 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5403 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5404 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5405 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5406 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5407 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5408 | if (dev_replace_is_ongoing) { |
| 5409 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5410 | num_alloc_stripes <<= 1; |
| 5411 | if (rw & REQ_GET_READ_MIRRORS) |
| 5412 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5413 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5414 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5415 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5416 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5417 | if (!bbio) { |
| 5418 | ret = -ENOMEM; |
| 5419 | goto out; |
| 5420 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5421 | if (dev_replace_is_ongoing) |
| 5422 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5423 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5424 | /* build raid_map */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5425 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5426 | need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5427 | mirror_num > 1)) { |
| 5428 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5429 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5430 | |
| 5431 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5432 | sizeof(struct btrfs_bio_stripe) * |
| 5433 | num_alloc_stripes + |
| 5434 | sizeof(int) * tgtdev_indexes); |
| 5435 | |
| 5436 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5437 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5438 | |
| 5439 | /* Fill in the logical address of each stripe */ |
| 5440 | tmp = stripe_nr * nr_data_stripes(map); |
| 5441 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5442 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5443 | em->start + (tmp + i) * map->stripe_len; |
| 5444 | |
| 5445 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5446 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5447 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5448 | RAID6_Q_STRIPE; |
| 5449 | } |
| 5450 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5451 | if (rw & REQ_DISCARD) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5452 | u32 factor = 0; |
| 5453 | u32 sub_stripes = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5454 | u64 stripes_per_dev = 0; |
| 5455 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5456 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5457 | |
| 5458 | if (map->type & |
| 5459 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5460 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5461 | sub_stripes = 1; |
| 5462 | else |
| 5463 | sub_stripes = map->sub_stripes; |
| 5464 | |
| 5465 | factor = map->num_stripes / sub_stripes; |
| 5466 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5467 | stripe_nr_orig, |
| 5468 | factor, |
| 5469 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5470 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5471 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5472 | } |
| 5473 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5474 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5475 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5476 | map->stripes[stripe_index].physical + |
| 5477 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5478 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5479 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5480 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5481 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5482 | bbio->stripes[i].length = stripes_per_dev * |
| 5483 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5484 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5485 | if (i / sub_stripes < remaining_stripes) |
| 5486 | bbio->stripes[i].length += |
| 5487 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5488 | |
| 5489 | /* |
| 5490 | * Special for the first stripe and |
| 5491 | * the last stripe: |
| 5492 | * |
| 5493 | * |-------|...|-------| |
| 5494 | * |----------| |
| 5495 | * off end_off |
| 5496 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5497 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5498 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5499 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5500 | |
| 5501 | if (stripe_index >= last_stripe && |
| 5502 | stripe_index <= (last_stripe + |
| 5503 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5504 | bbio->stripes[i].length -= |
| 5505 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5506 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5507 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5508 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5509 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5510 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5511 | |
| 5512 | stripe_index++; |
| 5513 | if (stripe_index == map->num_stripes) { |
| 5514 | /* This could only happen for RAID0/10 */ |
| 5515 | stripe_index = 0; |
| 5516 | stripe_nr++; |
| 5517 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5518 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5519 | } else { |
| 5520 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5521 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5522 | map->stripes[stripe_index].physical + |
| 5523 | stripe_offset + |
| 5524 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5525 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5526 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5527 | stripe_index++; |
| 5528 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5529 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5530 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5531 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5532 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5533 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5534 | if (bbio->raid_map) |
| 5535 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5536 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5537 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5538 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5539 | dev_replace->tgtdev != NULL) { |
| 5540 | int index_where_to_add; |
| 5541 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5542 | |
| 5543 | /* |
| 5544 | * duplicate the write operations while the dev replace |
| 5545 | * procedure is running. Since the copying of the old disk |
| 5546 | * to the new disk takes place at run time while the |
| 5547 | * filesystem is mounted writable, the regular write |
| 5548 | * operations to the old disk have to be duplicated to go |
| 5549 | * to the new disk as well. |
| 5550 | * Note that device->missing is handled by the caller, and |
| 5551 | * that the write to the old disk is already set up in the |
| 5552 | * stripes array. |
| 5553 | */ |
| 5554 | index_where_to_add = num_stripes; |
| 5555 | for (i = 0; i < num_stripes; i++) { |
| 5556 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5557 | /* write to new disk, too */ |
| 5558 | struct btrfs_bio_stripe *new = |
| 5559 | bbio->stripes + index_where_to_add; |
| 5560 | struct btrfs_bio_stripe *old = |
| 5561 | bbio->stripes + i; |
| 5562 | |
| 5563 | new->physical = old->physical; |
| 5564 | new->length = old->length; |
| 5565 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5566 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5567 | index_where_to_add++; |
| 5568 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5569 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5570 | } |
| 5571 | } |
| 5572 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5573 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5574 | dev_replace->tgtdev != NULL) { |
| 5575 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5576 | int index_srcdev = 0; |
| 5577 | int found = 0; |
| 5578 | u64 physical_of_found = 0; |
| 5579 | |
| 5580 | /* |
| 5581 | * During the dev-replace procedure, the target drive can |
| 5582 | * also be used to read data in case it is needed to repair |
| 5583 | * a corrupt block elsewhere. This is possible if the |
| 5584 | * requested area is left of the left cursor. In this area, |
| 5585 | * the target drive is a full copy of the source drive. |
| 5586 | */ |
| 5587 | for (i = 0; i < num_stripes; i++) { |
| 5588 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5589 | /* |
| 5590 | * In case of DUP, in order to keep it |
| 5591 | * simple, only add the mirror with the |
| 5592 | * lowest physical address |
| 5593 | */ |
| 5594 | if (found && |
| 5595 | physical_of_found <= |
| 5596 | bbio->stripes[i].physical) |
| 5597 | continue; |
| 5598 | index_srcdev = i; |
| 5599 | found = 1; |
| 5600 | physical_of_found = bbio->stripes[i].physical; |
| 5601 | } |
| 5602 | } |
| 5603 | if (found) { |
David Sterba | 258ece0 | 2015-02-24 19:45:15 +0100 | [diff] [blame] | 5604 | if (physical_of_found + map->stripe_len <= |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5605 | dev_replace->cursor_left) { |
| 5606 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5607 | bbio->stripes + num_stripes; |
| 5608 | |
| 5609 | tgtdev_stripe->physical = physical_of_found; |
| 5610 | tgtdev_stripe->length = |
| 5611 | bbio->stripes[index_srcdev].length; |
| 5612 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5613 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5614 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5615 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5616 | num_stripes++; |
| 5617 | } |
| 5618 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5619 | } |
| 5620 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5621 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5622 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5623 | bbio->num_stripes = num_stripes; |
| 5624 | bbio->max_errors = max_errors; |
| 5625 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5626 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5627 | |
| 5628 | /* |
| 5629 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5630 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5631 | * available as a mirror |
| 5632 | */ |
| 5633 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5634 | WARN_ON(num_stripes > 1); |
| 5635 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5636 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5637 | bbio->mirror_num = map->num_stripes + 1; |
| 5638 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5639 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5640 | if (dev_replace_is_ongoing) |
| 5641 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5642 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5643 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5644 | } |
| 5645 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5646 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5647 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5648 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5649 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5650 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5651 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5652 | } |
| 5653 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5654 | /* For Scrub/replace */ |
| 5655 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5656 | u64 logical, u64 *length, |
| 5657 | struct btrfs_bio **bbio_ret, int mirror_num, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5658 | int need_raid_map) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5659 | { |
| 5660 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5661 | mirror_num, need_raid_map); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5662 | } |
| 5663 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5664 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5665 | u64 chunk_start, u64 physical, u64 devid, |
| 5666 | u64 **logical, int *naddrs, int *stripe_len) |
| 5667 | { |
| 5668 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5669 | struct extent_map *em; |
| 5670 | struct map_lookup *map; |
| 5671 | u64 *buf; |
| 5672 | u64 bytenr; |
| 5673 | u64 length; |
| 5674 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5675 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5676 | int i, j, nr = 0; |
| 5677 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5678 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5679 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5680 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5681 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5682 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5683 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5684 | chunk_start); |
| 5685 | return -EIO; |
| 5686 | } |
| 5687 | |
| 5688 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5689 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5690 | em->start, chunk_start); |
| 5691 | free_extent_map(em); |
| 5692 | return -EIO; |
| 5693 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5694 | map = (struct map_lookup *)em->bdev; |
| 5695 | |
| 5696 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5697 | rmap_len = map->stripe_len; |
| 5698 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5699 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5700 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5701 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5702 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5703 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5704 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5705 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5706 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5707 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5708 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5709 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5710 | |
| 5711 | for (i = 0; i < map->num_stripes; i++) { |
| 5712 | if (devid && map->stripes[i].dev->devid != devid) |
| 5713 | continue; |
| 5714 | if (map->stripes[i].physical > physical || |
| 5715 | map->stripes[i].physical + length <= physical) |
| 5716 | continue; |
| 5717 | |
| 5718 | stripe_nr = physical - map->stripes[i].physical; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5719 | stripe_nr = div_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5720 | |
| 5721 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5722 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5723 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5724 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5725 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5726 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5727 | * Alternatively, just use rmap_len below instead of |
| 5728 | * map->stripe_len */ |
| 5729 | |
| 5730 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5731 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5732 | for (j = 0; j < nr; j++) { |
| 5733 | if (buf[j] == bytenr) |
| 5734 | break; |
| 5735 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5736 | if (j == nr) { |
| 5737 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5738 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5739 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5740 | } |
| 5741 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5742 | *logical = buf; |
| 5743 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5744 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5745 | |
| 5746 | free_extent_map(em); |
| 5747 | return 0; |
| 5748 | } |
| 5749 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5750 | 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] | 5751 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5752 | bio->bi_private = bbio->private; |
| 5753 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5754 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5755 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5756 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5757 | } |
| 5758 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5759 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5760 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5761 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5762 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5763 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5764 | if (bio->bi_error) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5765 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5766 | if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5767 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5768 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 5769 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5770 | |
| 5771 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5772 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5773 | if (dev->bdev) { |
| 5774 | if (bio->bi_rw & WRITE) |
| 5775 | btrfs_dev_stat_inc(dev, |
| 5776 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5777 | else |
| 5778 | btrfs_dev_stat_inc(dev, |
| 5779 | BTRFS_DEV_STAT_READ_ERRS); |
| 5780 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5781 | btrfs_dev_stat_inc(dev, |
| 5782 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5783 | btrfs_dev_stat_print_on_error(dev); |
| 5784 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5785 | } |
| 5786 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5787 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5788 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5789 | is_orig_bio = 1; |
| 5790 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5791 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5792 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5793 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5794 | if (!is_orig_bio) { |
| 5795 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5796 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5797 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5798 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5799 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5800 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5801 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5802 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5803 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5804 | bio->bi_error = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5805 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5806 | /* |
| 5807 | * this bio is actually up to date, we didn't |
| 5808 | * go over the max number of errors |
| 5809 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5810 | bio->bi_error = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5811 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5812 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5813 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5814 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5815 | bio_put(bio); |
| 5816 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5817 | } |
| 5818 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5819 | /* |
| 5820 | * see run_scheduled_bios for a description of why bios are collected for |
| 5821 | * async submit. |
| 5822 | * |
| 5823 | * This will add one bio to the pending list for a device and make sure |
| 5824 | * the work struct is scheduled. |
| 5825 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5826 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5827 | struct btrfs_device *device, |
| 5828 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5829 | { |
| 5830 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5831 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5832 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5833 | if (device->missing || !device->bdev) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5834 | bio_io_error(bio); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5835 | return; |
| 5836 | } |
| 5837 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5838 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5839 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5840 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5841 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5842 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5843 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5844 | } |
| 5845 | |
| 5846 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5847 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5848 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5849 | * made progress against dirty pages when we've really just put it |
| 5850 | * on a queue for later |
| 5851 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5852 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5853 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5854 | bio->bi_next = NULL; |
| 5855 | bio->bi_rw |= rw; |
| 5856 | |
| 5857 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5858 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5859 | pending_bios = &device->pending_sync_bios; |
| 5860 | else |
| 5861 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5862 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5863 | if (pending_bios->tail) |
| 5864 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5865 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5866 | pending_bios->tail = bio; |
| 5867 | if (!pending_bios->head) |
| 5868 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5869 | if (device->running_pending) |
| 5870 | should_queue = 0; |
| 5871 | |
| 5872 | spin_unlock(&device->io_lock); |
| 5873 | |
| 5874 | if (should_queue) |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5875 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5876 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5877 | } |
| 5878 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5879 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5880 | struct bio *bio, u64 physical, int dev_nr, |
| 5881 | int rw, int async) |
| 5882 | { |
| 5883 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5884 | |
| 5885 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5886 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5887 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5888 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5889 | #ifdef DEBUG |
| 5890 | { |
| 5891 | struct rcu_string *name; |
| 5892 | |
| 5893 | rcu_read_lock(); |
| 5894 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5895 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5896 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5897 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5898 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5899 | rcu_read_unlock(); |
| 5900 | } |
| 5901 | #endif |
| 5902 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5903 | |
| 5904 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5905 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5906 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5907 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5908 | else |
| 5909 | btrfsic_submit_bio(rw, bio); |
| 5910 | } |
| 5911 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5912 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5913 | { |
| 5914 | atomic_inc(&bbio->error); |
| 5915 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5916 | /* Shoud be the original bio. */ |
| 5917 | WARN_ON(bio != bbio->orig_bio); |
| 5918 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5919 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5920 | bio->bi_iter.bi_sector = logical >> 9; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5921 | bio->bi_error = -EIO; |
| 5922 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5923 | } |
| 5924 | } |
| 5925 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5926 | 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] | 5927 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5928 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5929 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5930 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5931 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5932 | u64 length = 0; |
| 5933 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5934 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5935 | int dev_nr; |
| 5936 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5937 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5938 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5939 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5940 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5941 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5942 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5943 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5944 | mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5945 | if (ret) { |
| 5946 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5947 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5948 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5949 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5950 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5951 | bbio->orig_bio = first_bio; |
| 5952 | bbio->private = first_bio->bi_private; |
| 5953 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5954 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5955 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5956 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5957 | if (bbio->raid_map) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5958 | /* In this case, map_length has been set to the length of |
| 5959 | a single stripe; not the whole write */ |
| 5960 | if (rw & WRITE) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5961 | ret = raid56_parity_write(root, bio, bbio, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5962 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5963 | ret = raid56_parity_recover(root, bio, bbio, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5964 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5965 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5966 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5967 | btrfs_bio_counter_dec(root->fs_info); |
| 5968 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5969 | } |
| 5970 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5971 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5972 | 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] | 5973 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5974 | BUG(); |
| 5975 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5976 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5977 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5978 | dev = bbio->stripes[dev_nr].dev; |
| 5979 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5980 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5981 | continue; |
| 5982 | } |
| 5983 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5984 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5985 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5986 | BUG_ON(!bio); /* -ENOMEM */ |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5987 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5988 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5989 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5990 | submit_stripe_bio(root, bbio, bio, |
| 5991 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5992 | async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5993 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5994 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5995 | return 0; |
| 5996 | } |
| 5997 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5998 | 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] | 5999 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6000 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6001 | struct btrfs_device *device; |
| 6002 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6003 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6004 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6005 | while (cur_devices) { |
| 6006 | if (!fsid || |
| 6007 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 6008 | device = __find_device(&cur_devices->devices, |
| 6009 | devid, uuid); |
| 6010 | if (device) |
| 6011 | return device; |
| 6012 | } |
| 6013 | cur_devices = cur_devices->seed; |
| 6014 | } |
| 6015 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6016 | } |
| 6017 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6018 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6019 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6020 | u64 devid, u8 *dev_uuid) |
| 6021 | { |
| 6022 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6023 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6024 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6025 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 6026 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6027 | |
| 6028 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6029 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6030 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6031 | |
| 6032 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6033 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6034 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6035 | return device; |
| 6036 | } |
| 6037 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6038 | /** |
| 6039 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6040 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6041 | * devid is provided (i.e. @devid != NULL). |
| 6042 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6043 | * is generated. |
| 6044 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6045 | * is generated. |
| 6046 | * |
| 6047 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 6048 | * on error. Returned struct is not linked onto any lists and can be |
| 6049 | * destroyed with kfree() right away. |
| 6050 | */ |
| 6051 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6052 | const u64 *devid, |
| 6053 | const u8 *uuid) |
| 6054 | { |
| 6055 | struct btrfs_device *dev; |
| 6056 | u64 tmp; |
| 6057 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6058 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6059 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6060 | |
| 6061 | dev = __alloc_device(); |
| 6062 | if (IS_ERR(dev)) |
| 6063 | return dev; |
| 6064 | |
| 6065 | if (devid) |
| 6066 | tmp = *devid; |
| 6067 | else { |
| 6068 | int ret; |
| 6069 | |
| 6070 | ret = find_next_devid(fs_info, &tmp); |
| 6071 | if (ret) { |
| 6072 | kfree(dev); |
| 6073 | return ERR_PTR(ret); |
| 6074 | } |
| 6075 | } |
| 6076 | dev->devid = tmp; |
| 6077 | |
| 6078 | if (uuid) |
| 6079 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6080 | else |
| 6081 | generate_random_uuid(dev->uuid); |
| 6082 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6083 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6084 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6085 | |
| 6086 | return dev; |
| 6087 | } |
| 6088 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6089 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 6090 | struct extent_buffer *leaf, |
| 6091 | struct btrfs_chunk *chunk) |
| 6092 | { |
| 6093 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6094 | struct map_lookup *map; |
| 6095 | struct extent_map *em; |
| 6096 | u64 logical; |
| 6097 | u64 length; |
| 6098 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6099 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6100 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6101 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6102 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6103 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6104 | logical = key->offset; |
| 6105 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6106 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6107 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6108 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6109 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6110 | |
| 6111 | /* already mapped? */ |
| 6112 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6113 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6114 | return 0; |
| 6115 | } else if (em) { |
| 6116 | free_extent_map(em); |
| 6117 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6118 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6119 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6120 | if (!em) |
| 6121 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6122 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6123 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6124 | if (!map) { |
| 6125 | free_extent_map(em); |
| 6126 | return -ENOMEM; |
| 6127 | } |
| 6128 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6129 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6130 | em->bdev = (struct block_device *)map; |
| 6131 | em->start = logical; |
| 6132 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6133 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6134 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6135 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6136 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6137 | map->num_stripes = num_stripes; |
| 6138 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6139 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6140 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6141 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6142 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6143 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6144 | for (i = 0; i < num_stripes; i++) { |
| 6145 | map->stripes[i].physical = |
| 6146 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6147 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6148 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6149 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6150 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6151 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6152 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6153 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6154 | free_extent_map(em); |
| 6155 | return -EIO; |
| 6156 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6157 | if (!map->stripes[i].dev) { |
| 6158 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6159 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6160 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6161 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6162 | free_extent_map(em); |
| 6163 | return -EIO; |
| 6164 | } |
Anand Jain | 816fceb | 2015-04-27 12:46:18 +0800 | [diff] [blame] | 6165 | btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing", |
| 6166 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6167 | } |
| 6168 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6169 | } |
| 6170 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6171 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6172 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6173 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6174 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6175 | free_extent_map(em); |
| 6176 | |
| 6177 | return 0; |
| 6178 | } |
| 6179 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6180 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6181 | struct btrfs_dev_item *dev_item, |
| 6182 | struct btrfs_device *device) |
| 6183 | { |
| 6184 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6185 | |
| 6186 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6187 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6188 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6189 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6190 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6191 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6192 | device->type = btrfs_device_type(leaf, dev_item); |
| 6193 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6194 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6195 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6196 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6197 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6198 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6199 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6200 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6201 | } |
| 6202 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6203 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6204 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6205 | { |
| 6206 | struct btrfs_fs_devices *fs_devices; |
| 6207 | int ret; |
| 6208 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6209 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6210 | |
| 6211 | fs_devices = root->fs_info->fs_devices->seed; |
| 6212 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6213 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6214 | return fs_devices; |
| 6215 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6216 | fs_devices = fs_devices->seed; |
| 6217 | } |
| 6218 | |
| 6219 | fs_devices = find_fsid(fsid); |
| 6220 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6221 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6222 | return ERR_PTR(-ENOENT); |
| 6223 | |
| 6224 | fs_devices = alloc_fs_devices(fsid); |
| 6225 | if (IS_ERR(fs_devices)) |
| 6226 | return fs_devices; |
| 6227 | |
| 6228 | fs_devices->seeding = 1; |
| 6229 | fs_devices->opened = 1; |
| 6230 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6231 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6232 | |
| 6233 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6234 | if (IS_ERR(fs_devices)) |
| 6235 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6236 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6237 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6238 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6239 | if (ret) { |
| 6240 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6241 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6242 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6243 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6244 | |
| 6245 | if (!fs_devices->seeding) { |
| 6246 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6247 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6248 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6249 | goto out; |
| 6250 | } |
| 6251 | |
| 6252 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6253 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6254 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6255 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6256 | } |
| 6257 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6258 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6259 | struct extent_buffer *leaf, |
| 6260 | struct btrfs_dev_item *dev_item) |
| 6261 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6262 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6263 | struct btrfs_device *device; |
| 6264 | u64 devid; |
| 6265 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6266 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6267 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6268 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6269 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6270 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6271 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6272 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6273 | BTRFS_UUID_SIZE); |
| 6274 | |
| 6275 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6276 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6277 | if (IS_ERR(fs_devices)) |
| 6278 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6279 | } |
| 6280 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6281 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6282 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6283 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6284 | return -EIO; |
| 6285 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6286 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6287 | if (!device) |
| 6288 | return -ENOMEM; |
Anand Jain | 33b97e4 | 2015-05-08 04:34:35 +0800 | [diff] [blame] | 6289 | btrfs_warn(root->fs_info, "devid %llu uuid %pU missing", |
| 6290 | devid, dev_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6291 | } else { |
| 6292 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6293 | return -EIO; |
| 6294 | |
| 6295 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6296 | /* |
| 6297 | * this happens when a device that was properly setup |
| 6298 | * in the device info lists suddenly goes bad. |
| 6299 | * device->bdev is NULL, and so we have to set |
| 6300 | * device->missing to one here |
| 6301 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6302 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6303 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6304 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6305 | |
| 6306 | /* Move the device to its own fs_devices */ |
| 6307 | if (device->fs_devices != fs_devices) { |
| 6308 | ASSERT(device->missing); |
| 6309 | |
| 6310 | list_move(&device->dev_list, &fs_devices->devices); |
| 6311 | device->fs_devices->num_devices--; |
| 6312 | fs_devices->num_devices++; |
| 6313 | |
| 6314 | device->fs_devices->missing_devices--; |
| 6315 | fs_devices->missing_devices++; |
| 6316 | |
| 6317 | device->fs_devices = fs_devices; |
| 6318 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6319 | } |
| 6320 | |
| 6321 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6322 | BUG_ON(device->writeable); |
| 6323 | if (device->generation != |
| 6324 | btrfs_device_generation(leaf, dev_item)) |
| 6325 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6326 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6327 | |
| 6328 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6329 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6330 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6331 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6332 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6333 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6334 | device->bytes_used; |
| 6335 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6336 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6337 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6338 | return ret; |
| 6339 | } |
| 6340 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6341 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6342 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6343 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6344 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6345 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6346 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6347 | u8 *array_ptr; |
| 6348 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6349 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6350 | u32 num_stripes; |
| 6351 | u32 array_size; |
| 6352 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6353 | u32 cur_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6354 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6355 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6356 | ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize); |
| 6357 | /* |
| 6358 | * This will create extent buffer of nodesize, superblock size is |
| 6359 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6360 | * overallocate but we can keep it as-is, only the first page is used. |
| 6361 | */ |
| 6362 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6363 | if (!sb) |
| 6364 | return -ENOMEM; |
| 6365 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6366 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6367 | /* |
| 6368 | * The sb extent buffer is artifical and just used to read the system array. |
| 6369 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6370 | * pages up-to-date when the page is larger: extent does not cover the |
| 6371 | * whole page and consequently check_page_uptodate does not find all |
| 6372 | * the page's extents up-to-date (the hole beyond sb), |
| 6373 | * write_extent_buffer then triggers a WARN_ON. |
| 6374 | * |
| 6375 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6376 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6377 | * to silence the warning eg. on PowerPC 64. |
| 6378 | */ |
| 6379 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6380 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6381 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6382 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6383 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6384 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6385 | array_ptr = super_copy->sys_chunk_array; |
| 6386 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6387 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6388 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6389 | while (cur_offset < array_size) { |
| 6390 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6391 | len = sizeof(*disk_key); |
| 6392 | if (cur_offset + len > array_size) |
| 6393 | goto out_short_read; |
| 6394 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6395 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6396 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6397 | array_ptr += len; |
| 6398 | sb_array_offset += len; |
| 6399 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6400 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6401 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6402 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6403 | /* |
| 6404 | * At least one btrfs_chunk with one stripe must be |
| 6405 | * present, exact stripe count check comes afterwards |
| 6406 | */ |
| 6407 | len = btrfs_chunk_item_size(1); |
| 6408 | if (cur_offset + len > array_size) |
| 6409 | goto out_short_read; |
| 6410 | |
| 6411 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6412 | len = btrfs_chunk_item_size(num_stripes); |
| 6413 | if (cur_offset + len > array_size) |
| 6414 | goto out_short_read; |
| 6415 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6416 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6417 | if (ret) |
| 6418 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6419 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6420 | ret = -EIO; |
| 6421 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6422 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6423 | array_ptr += len; |
| 6424 | sb_array_offset += len; |
| 6425 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6426 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6427 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6428 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6429 | |
| 6430 | out_short_read: |
| 6431 | printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n", |
| 6432 | len, cur_offset); |
| 6433 | free_extent_buffer(sb); |
| 6434 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6438 | { |
| 6439 | struct btrfs_path *path; |
| 6440 | struct extent_buffer *leaf; |
| 6441 | struct btrfs_key key; |
| 6442 | struct btrfs_key found_key; |
| 6443 | int ret; |
| 6444 | int slot; |
| 6445 | |
| 6446 | root = root->fs_info->chunk_root; |
| 6447 | |
| 6448 | path = btrfs_alloc_path(); |
| 6449 | if (!path) |
| 6450 | return -ENOMEM; |
| 6451 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6452 | mutex_lock(&uuid_mutex); |
| 6453 | lock_chunks(root); |
| 6454 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6455 | /* |
| 6456 | * Read all device items, and then all the chunk items. All |
| 6457 | * device items are found before any chunk item (their object id |
| 6458 | * is smaller than the lowest possible object id for a chunk |
| 6459 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6460 | */ |
| 6461 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6462 | key.offset = 0; |
| 6463 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6464 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6465 | if (ret < 0) |
| 6466 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6467 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6468 | leaf = path->nodes[0]; |
| 6469 | slot = path->slots[0]; |
| 6470 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6471 | ret = btrfs_next_leaf(root, path); |
| 6472 | if (ret == 0) |
| 6473 | continue; |
| 6474 | if (ret < 0) |
| 6475 | goto error; |
| 6476 | break; |
| 6477 | } |
| 6478 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6479 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6480 | struct btrfs_dev_item *dev_item; |
| 6481 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6482 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6483 | ret = read_one_dev(root, leaf, dev_item); |
| 6484 | if (ret) |
| 6485 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6486 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6487 | struct btrfs_chunk *chunk; |
| 6488 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6489 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6490 | if (ret) |
| 6491 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6492 | } |
| 6493 | path->slots[0]++; |
| 6494 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6495 | ret = 0; |
| 6496 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6497 | unlock_chunks(root); |
| 6498 | mutex_unlock(&uuid_mutex); |
| 6499 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6500 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6501 | return ret; |
| 6502 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6503 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6504 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6505 | { |
| 6506 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6507 | struct btrfs_device *device; |
| 6508 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6509 | while (fs_devices) { |
| 6510 | mutex_lock(&fs_devices->device_list_mutex); |
| 6511 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6512 | device->dev_root = fs_info->dev_root; |
| 6513 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6514 | |
| 6515 | fs_devices = fs_devices->seed; |
| 6516 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6517 | } |
| 6518 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6519 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6520 | { |
| 6521 | int i; |
| 6522 | |
| 6523 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6524 | btrfs_dev_stat_reset(dev, i); |
| 6525 | } |
| 6526 | |
| 6527 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6528 | { |
| 6529 | struct btrfs_key key; |
| 6530 | struct btrfs_key found_key; |
| 6531 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6532 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6533 | struct extent_buffer *eb; |
| 6534 | int slot; |
| 6535 | int ret = 0; |
| 6536 | struct btrfs_device *device; |
| 6537 | struct btrfs_path *path = NULL; |
| 6538 | int i; |
| 6539 | |
| 6540 | path = btrfs_alloc_path(); |
| 6541 | if (!path) { |
| 6542 | ret = -ENOMEM; |
| 6543 | goto out; |
| 6544 | } |
| 6545 | |
| 6546 | mutex_lock(&fs_devices->device_list_mutex); |
| 6547 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6548 | int item_size; |
| 6549 | struct btrfs_dev_stats_item *ptr; |
| 6550 | |
| 6551 | key.objectid = 0; |
| 6552 | key.type = BTRFS_DEV_STATS_KEY; |
| 6553 | key.offset = device->devid; |
| 6554 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6555 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6556 | __btrfs_reset_dev_stats(device); |
| 6557 | device->dev_stats_valid = 1; |
| 6558 | btrfs_release_path(path); |
| 6559 | continue; |
| 6560 | } |
| 6561 | slot = path->slots[0]; |
| 6562 | eb = path->nodes[0]; |
| 6563 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6564 | item_size = btrfs_item_size_nr(eb, slot); |
| 6565 | |
| 6566 | ptr = btrfs_item_ptr(eb, slot, |
| 6567 | struct btrfs_dev_stats_item); |
| 6568 | |
| 6569 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6570 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6571 | btrfs_dev_stat_set(device, i, |
| 6572 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6573 | else |
| 6574 | btrfs_dev_stat_reset(device, i); |
| 6575 | } |
| 6576 | |
| 6577 | device->dev_stats_valid = 1; |
| 6578 | btrfs_dev_stat_print_on_load(device); |
| 6579 | btrfs_release_path(path); |
| 6580 | } |
| 6581 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6582 | |
| 6583 | out: |
| 6584 | btrfs_free_path(path); |
| 6585 | return ret < 0 ? ret : 0; |
| 6586 | } |
| 6587 | |
| 6588 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6589 | struct btrfs_root *dev_root, |
| 6590 | struct btrfs_device *device) |
| 6591 | { |
| 6592 | struct btrfs_path *path; |
| 6593 | struct btrfs_key key; |
| 6594 | struct extent_buffer *eb; |
| 6595 | struct btrfs_dev_stats_item *ptr; |
| 6596 | int ret; |
| 6597 | int i; |
| 6598 | |
| 6599 | key.objectid = 0; |
| 6600 | key.type = BTRFS_DEV_STATS_KEY; |
| 6601 | key.offset = device->devid; |
| 6602 | |
| 6603 | path = btrfs_alloc_path(); |
| 6604 | BUG_ON(!path); |
| 6605 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6606 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6607 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6608 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6609 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6610 | goto out; |
| 6611 | } |
| 6612 | |
| 6613 | if (ret == 0 && |
| 6614 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6615 | /* need to delete old one and insert a new one */ |
| 6616 | ret = btrfs_del_item(trans, dev_root, path); |
| 6617 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6618 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6619 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6620 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6621 | goto out; |
| 6622 | } |
| 6623 | ret = 1; |
| 6624 | } |
| 6625 | |
| 6626 | if (ret == 1) { |
| 6627 | /* need to insert a new item */ |
| 6628 | btrfs_release_path(path); |
| 6629 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6630 | &key, sizeof(*ptr)); |
| 6631 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6632 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6633 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6634 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6635 | goto out; |
| 6636 | } |
| 6637 | } |
| 6638 | |
| 6639 | eb = path->nodes[0]; |
| 6640 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6641 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6642 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6643 | btrfs_dev_stat_read(device, i)); |
| 6644 | btrfs_mark_buffer_dirty(eb); |
| 6645 | |
| 6646 | out: |
| 6647 | btrfs_free_path(path); |
| 6648 | return ret; |
| 6649 | } |
| 6650 | |
| 6651 | /* |
| 6652 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6653 | */ |
| 6654 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6655 | struct btrfs_fs_info *fs_info) |
| 6656 | { |
| 6657 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6658 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6659 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6660 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6661 | int ret = 0; |
| 6662 | |
| 6663 | mutex_lock(&fs_devices->device_list_mutex); |
| 6664 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6665 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6666 | continue; |
| 6667 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6668 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6669 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6670 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6671 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6672 | } |
| 6673 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6674 | |
| 6675 | return ret; |
| 6676 | } |
| 6677 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6678 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6679 | { |
| 6680 | btrfs_dev_stat_inc(dev, index); |
| 6681 | btrfs_dev_stat_print_on_error(dev); |
| 6682 | } |
| 6683 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6684 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6685 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6686 | if (!dev->dev_stats_valid) |
| 6687 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6688 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6689 | "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] | 6690 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6691 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6692 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6693 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6694 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6695 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6696 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6697 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6698 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6699 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6700 | int i; |
| 6701 | |
| 6702 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6703 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6704 | break; |
| 6705 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6706 | return; /* all values == 0, suppress message */ |
| 6707 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6708 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6709 | "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] | 6710 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6711 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6712 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6713 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6714 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6715 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6716 | } |
| 6717 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6718 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6719 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6720 | { |
| 6721 | struct btrfs_device *dev; |
| 6722 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6723 | int i; |
| 6724 | |
| 6725 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6726 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6727 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6728 | |
| 6729 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6730 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6731 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6732 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6733 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6734 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6735 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6736 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6737 | if (stats->nr_items > i) |
| 6738 | stats->values[i] = |
| 6739 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6740 | else |
| 6741 | btrfs_dev_stat_reset(dev, i); |
| 6742 | } |
| 6743 | } else { |
| 6744 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6745 | if (stats->nr_items > i) |
| 6746 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6747 | } |
| 6748 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6749 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6750 | return 0; |
| 6751 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6752 | |
| 6753 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6754 | { |
| 6755 | struct buffer_head *bh; |
| 6756 | struct btrfs_super_block *disk_super; |
| 6757 | |
| 6758 | bh = btrfs_read_dev_super(device->bdev); |
| 6759 | if (!bh) |
| 6760 | return -EINVAL; |
| 6761 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6762 | |
| 6763 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6764 | set_buffer_dirty(bh); |
| 6765 | sync_dirty_buffer(bh); |
| 6766 | brelse(bh); |
| 6767 | |
| 6768 | return 0; |
| 6769 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6770 | |
| 6771 | /* |
| 6772 | * Update the size of all devices, which is used for writing out the |
| 6773 | * super blocks. |
| 6774 | */ |
| 6775 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6776 | { |
| 6777 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6778 | struct btrfs_device *curr, *next; |
| 6779 | |
| 6780 | if (list_empty(&fs_devices->resized_devices)) |
| 6781 | return; |
| 6782 | |
| 6783 | mutex_lock(&fs_devices->device_list_mutex); |
| 6784 | lock_chunks(fs_info->dev_root); |
| 6785 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6786 | resized_list) { |
| 6787 | list_del_init(&curr->resized_list); |
| 6788 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6789 | } |
| 6790 | unlock_chunks(fs_info->dev_root); |
| 6791 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6792 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6793 | |
| 6794 | /* Must be invoked during the transaction commit */ |
| 6795 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6796 | struct btrfs_transaction *transaction) |
| 6797 | { |
| 6798 | struct extent_map *em; |
| 6799 | struct map_lookup *map; |
| 6800 | struct btrfs_device *dev; |
| 6801 | int i; |
| 6802 | |
| 6803 | if (list_empty(&transaction->pending_chunks)) |
| 6804 | return; |
| 6805 | |
| 6806 | /* In order to kick the device replace finish process */ |
| 6807 | lock_chunks(root); |
| 6808 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6809 | map = (struct map_lookup *)em->bdev; |
| 6810 | |
| 6811 | for (i = 0; i < map->num_stripes; i++) { |
| 6812 | dev = map->stripes[i].dev; |
| 6813 | dev->commit_bytes_used = dev->bytes_used; |
| 6814 | } |
| 6815 | } |
| 6816 | unlock_chunks(root); |
| 6817 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 6818 | |
| 6819 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6820 | { |
| 6821 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6822 | while (fs_devices) { |
| 6823 | fs_devices->fs_info = fs_info; |
| 6824 | fs_devices = fs_devices->seed; |
| 6825 | } |
| 6826 | } |
| 6827 | |
| 6828 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6829 | { |
| 6830 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6831 | while (fs_devices) { |
| 6832 | fs_devices->fs_info = NULL; |
| 6833 | fs_devices = fs_devices->seed; |
| 6834 | } |
| 6835 | } |