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 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 45 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 46 | struct btrfs_root *root, |
| 47 | struct btrfs_device *device); |
| 48 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 49 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 50 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 51 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 52 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 53 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 54 | static LIST_HEAD(fs_uuids); |
Anand Jain | c73eccf | 2015-03-10 06:38:30 +0800 | [diff] [blame] | 55 | struct list_head *btrfs_get_fs_uuids(void) |
| 56 | { |
| 57 | return &fs_uuids; |
| 58 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 59 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 60 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 61 | { |
| 62 | struct btrfs_fs_devices *fs_devs; |
| 63 | |
| 64 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 65 | if (!fs_devs) |
| 66 | return ERR_PTR(-ENOMEM); |
| 67 | |
| 68 | mutex_init(&fs_devs->device_list_mutex); |
| 69 | |
| 70 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 71 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 72 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 73 | INIT_LIST_HEAD(&fs_devs->list); |
| 74 | |
| 75 | return fs_devs; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 80 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 81 | * generated. |
| 82 | * |
| 83 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 84 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 85 | * can be destroyed with kfree() right away. |
| 86 | */ |
| 87 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 88 | { |
| 89 | struct btrfs_fs_devices *fs_devs; |
| 90 | |
| 91 | fs_devs = __alloc_fs_devices(); |
| 92 | if (IS_ERR(fs_devs)) |
| 93 | return fs_devs; |
| 94 | |
| 95 | if (fsid) |
| 96 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 97 | else |
| 98 | generate_random_uuid(fs_devs->fsid); |
| 99 | |
| 100 | return fs_devs; |
| 101 | } |
| 102 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 103 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 104 | { |
| 105 | struct btrfs_device *device; |
| 106 | WARN_ON(fs_devices->opened); |
| 107 | while (!list_empty(&fs_devices->devices)) { |
| 108 | device = list_entry(fs_devices->devices.next, |
| 109 | struct btrfs_device, dev_list); |
| 110 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 111 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 112 | kfree(device); |
| 113 | } |
| 114 | kfree(fs_devices); |
| 115 | } |
| 116 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 117 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 118 | enum kobject_action action) |
| 119 | { |
| 120 | int ret; |
| 121 | |
| 122 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 123 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 124 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 125 | action, |
| 126 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 127 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 128 | } |
| 129 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 130 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 131 | { |
| 132 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 133 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 134 | while (!list_empty(&fs_uuids)) { |
| 135 | fs_devices = list_entry(fs_uuids.next, |
| 136 | struct btrfs_fs_devices, list); |
| 137 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 138 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 139 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 142 | static struct btrfs_device *__alloc_device(void) |
| 143 | { |
| 144 | struct btrfs_device *dev; |
| 145 | |
| 146 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 147 | if (!dev) |
| 148 | return ERR_PTR(-ENOMEM); |
| 149 | |
| 150 | INIT_LIST_HEAD(&dev->dev_list); |
| 151 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 152 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 153 | |
| 154 | spin_lock_init(&dev->io_lock); |
| 155 | |
| 156 | spin_lock_init(&dev->reada_lock); |
| 157 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 158 | atomic_set(&dev->dev_stats_ccnt, 0); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 159 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 160 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 161 | |
| 162 | return dev; |
| 163 | } |
| 164 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 165 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 166 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 167 | { |
| 168 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 169 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 170 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 171 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 172 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 173 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 174 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 175 | } |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 179 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 180 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 181 | struct btrfs_fs_devices *fs_devices; |
| 182 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 183 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 184 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 185 | return fs_devices; |
| 186 | } |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 190 | static int |
| 191 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 192 | int flush, struct block_device **bdev, |
| 193 | struct buffer_head **bh) |
| 194 | { |
| 195 | int ret; |
| 196 | |
| 197 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 198 | |
| 199 | if (IS_ERR(*bdev)) { |
| 200 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 201 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 202 | goto error; |
| 203 | } |
| 204 | |
| 205 | if (flush) |
| 206 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 207 | ret = set_blocksize(*bdev, 4096); |
| 208 | if (ret) { |
| 209 | blkdev_put(*bdev, flags); |
| 210 | goto error; |
| 211 | } |
| 212 | invalidate_bdev(*bdev); |
| 213 | *bh = btrfs_read_dev_super(*bdev); |
| 214 | if (!*bh) { |
| 215 | ret = -EINVAL; |
| 216 | blkdev_put(*bdev, flags); |
| 217 | goto error; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | |
| 222 | error: |
| 223 | *bdev = NULL; |
| 224 | *bh = NULL; |
| 225 | return ret; |
| 226 | } |
| 227 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 228 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 229 | struct bio *head, struct bio *tail) |
| 230 | { |
| 231 | |
| 232 | struct bio *old_head; |
| 233 | |
| 234 | old_head = pending_bios->head; |
| 235 | pending_bios->head = head; |
| 236 | if (pending_bios->tail) |
| 237 | tail->bi_next = old_head; |
| 238 | else |
| 239 | pending_bios->tail = tail; |
| 240 | } |
| 241 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 242 | /* |
| 243 | * we try to collect pending bios for a device so we don't get a large |
| 244 | * number of procs sending bios down to the same device. This greatly |
| 245 | * improves the schedulers ability to collect and merge the bios. |
| 246 | * |
| 247 | * But, it also turns into a long list of bios to process and that is sure |
| 248 | * to eventually make the worker thread block. The solution here is to |
| 249 | * make some progress and then put this work struct back at the end of |
| 250 | * the list if the block device is congested. This way, multiple devices |
| 251 | * can make progress from a single worker thread. |
| 252 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 253 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 254 | { |
| 255 | struct bio *pending; |
| 256 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 257 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 258 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 259 | struct bio *tail; |
| 260 | struct bio *cur; |
| 261 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 262 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 263 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 264 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 265 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 266 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 267 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 268 | struct blk_plug plug; |
| 269 | |
| 270 | /* |
| 271 | * this function runs all the bios we've collected for |
| 272 | * a particular device. We don't want to wander off to |
| 273 | * another device without first sending all of these down. |
| 274 | * So, setup a plug here and finish it off before we return |
| 275 | */ |
| 276 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 277 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 278 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 279 | fs_info = device->dev_root->fs_info; |
| 280 | limit = btrfs_async_submit_limit(fs_info); |
| 281 | limit = limit * 2 / 3; |
| 282 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 283 | loop: |
| 284 | spin_lock(&device->io_lock); |
| 285 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 286 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 287 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 288 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 289 | /* take all the bios off the list at once and process them |
| 290 | * later on (without the lock held). But, remember the |
| 291 | * tail and other pointers so the bios can be properly reinserted |
| 292 | * into the list if we hit congestion |
| 293 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 294 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 295 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 296 | force_reg = 1; |
| 297 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 298 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 299 | force_reg = 0; |
| 300 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 301 | |
| 302 | pending = pending_bios->head; |
| 303 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 304 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * if pending was null this time around, no bios need processing |
| 308 | * at all and we can stop. Otherwise it'll loop back up again |
| 309 | * and do an additional check so no bios are missed. |
| 310 | * |
| 311 | * device->running_pending is used to synchronize with the |
| 312 | * schedule_bio code. |
| 313 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 314 | if (device->pending_sync_bios.head == NULL && |
| 315 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 316 | again = 0; |
| 317 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 318 | } else { |
| 319 | again = 1; |
| 320 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 321 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 322 | |
| 323 | pending_bios->head = NULL; |
| 324 | pending_bios->tail = NULL; |
| 325 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 326 | spin_unlock(&device->io_lock); |
| 327 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 328 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 329 | |
| 330 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 331 | /* we want to work on both lists, but do more bios on the |
| 332 | * sync list than the regular list |
| 333 | */ |
| 334 | if ((num_run > 32 && |
| 335 | pending_bios != &device->pending_sync_bios && |
| 336 | device->pending_sync_bios.head) || |
| 337 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 338 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 339 | spin_lock(&device->io_lock); |
| 340 | requeue_list(pending_bios, pending, tail); |
| 341 | goto loop_lock; |
| 342 | } |
| 343 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 344 | cur = pending; |
| 345 | pending = pending->bi_next; |
| 346 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 347 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 348 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 349 | waitqueue_active(&fs_info->async_submit_wait)) |
| 350 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 351 | |
| 352 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 353 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 354 | /* |
| 355 | * if we're doing the sync list, record that our |
| 356 | * plug has some sync requests on it |
| 357 | * |
| 358 | * If we're doing the regular list and there are |
| 359 | * sync requests sitting around, unplug before |
| 360 | * we add more |
| 361 | */ |
| 362 | if (pending_bios == &device->pending_sync_bios) { |
| 363 | sync_pending = 1; |
| 364 | } else if (sync_pending) { |
| 365 | blk_finish_plug(&plug); |
| 366 | blk_start_plug(&plug); |
| 367 | sync_pending = 0; |
| 368 | } |
| 369 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 370 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 371 | num_run++; |
| 372 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 373 | |
| 374 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * we made progress, there is more work to do and the bdi |
| 378 | * is now congested. Back off and let other work structs |
| 379 | * run instead |
| 380 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 381 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 382 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 383 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 384 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 385 | ioc = current->io_context; |
| 386 | |
| 387 | /* |
| 388 | * the main goal here is that we don't want to |
| 389 | * block if we're going to be able to submit |
| 390 | * more requests without blocking. |
| 391 | * |
| 392 | * This code does two great things, it pokes into |
| 393 | * the elevator code from a filesystem _and_ |
| 394 | * it makes assumptions about how batching works. |
| 395 | */ |
| 396 | if (ioc && ioc->nr_batch_requests > 0 && |
| 397 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 398 | (last_waited == 0 || |
| 399 | ioc->last_waited == last_waited)) { |
| 400 | /* |
| 401 | * we want to go through our batch of |
| 402 | * requests and stop. So, we copy out |
| 403 | * the ioc->last_waited time and test |
| 404 | * against it before looping |
| 405 | */ |
| 406 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 407 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 408 | continue; |
| 409 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 410 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 411 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 412 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 413 | |
| 414 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 415 | btrfs_queue_work(fs_info->submit_workers, |
| 416 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 417 | goto done; |
| 418 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 419 | /* unplug every 64 requests just for good measure */ |
| 420 | if (batch_run % 64 == 0) { |
| 421 | blk_finish_plug(&plug); |
| 422 | blk_start_plug(&plug); |
| 423 | sync_pending = 0; |
| 424 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 425 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 426 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 427 | cond_resched(); |
| 428 | if (again) |
| 429 | goto loop; |
| 430 | |
| 431 | spin_lock(&device->io_lock); |
| 432 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 433 | goto loop_lock; |
| 434 | spin_unlock(&device->io_lock); |
| 435 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 436 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 437 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 440 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 441 | { |
| 442 | struct btrfs_device *device; |
| 443 | |
| 444 | device = container_of(work, struct btrfs_device, work); |
| 445 | run_scheduled_bios(device); |
| 446 | } |
| 447 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 448 | |
| 449 | void btrfs_free_stale_device(struct btrfs_device *cur_dev) |
| 450 | { |
| 451 | struct btrfs_fs_devices *fs_devs; |
| 452 | struct btrfs_device *dev; |
| 453 | |
| 454 | if (!cur_dev->name) |
| 455 | return; |
| 456 | |
| 457 | list_for_each_entry(fs_devs, &fs_uuids, list) { |
| 458 | int del = 1; |
| 459 | |
| 460 | if (fs_devs->opened) |
| 461 | continue; |
| 462 | if (fs_devs->seeding) |
| 463 | continue; |
| 464 | |
| 465 | list_for_each_entry(dev, &fs_devs->devices, dev_list) { |
| 466 | |
| 467 | if (dev == cur_dev) |
| 468 | continue; |
| 469 | if (!dev->name) |
| 470 | continue; |
| 471 | |
| 472 | /* |
| 473 | * Todo: This won't be enough. What if the same device |
| 474 | * comes back (with new uuid and) with its mapper path? |
| 475 | * But for now, this does help as mostly an admin will |
| 476 | * either use mapper or non mapper path throughout. |
| 477 | */ |
| 478 | rcu_read_lock(); |
| 479 | del = strcmp(rcu_str_deref(dev->name), |
| 480 | rcu_str_deref(cur_dev->name)); |
| 481 | rcu_read_unlock(); |
| 482 | if (!del) |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | if (!del) { |
| 487 | /* delete the stale device */ |
| 488 | if (fs_devs->num_devices == 1) { |
| 489 | btrfs_sysfs_remove_fsid(fs_devs); |
| 490 | list_del(&fs_devs->list); |
| 491 | free_fs_devices(fs_devs); |
| 492 | } else { |
| 493 | fs_devs->num_devices--; |
| 494 | list_del(&dev->dev_list); |
| 495 | rcu_string_free(dev->name); |
| 496 | kfree(dev); |
| 497 | } |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 503 | /* |
| 504 | * Add new device to list of registered devices |
| 505 | * |
| 506 | * Returns: |
| 507 | * 1 - first time device is seen |
| 508 | * 0 - device already known |
| 509 | * < 0 - error |
| 510 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 511 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 512 | struct btrfs_super_block *disk_super, |
| 513 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 514 | { |
| 515 | struct btrfs_device *device; |
| 516 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 517 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 518 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 519 | u64 found_transid = btrfs_super_generation(disk_super); |
| 520 | |
| 521 | fs_devices = find_fsid(disk_super->fsid); |
| 522 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 523 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 524 | if (IS_ERR(fs_devices)) |
| 525 | return PTR_ERR(fs_devices); |
| 526 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 527 | list_add(&fs_devices->list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 528 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 529 | device = NULL; |
| 530 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 531 | device = __find_device(&fs_devices->devices, devid, |
| 532 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 533 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 534 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 535 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 536 | if (fs_devices->opened) |
| 537 | return -EBUSY; |
| 538 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 539 | device = btrfs_alloc_device(NULL, &devid, |
| 540 | disk_super->dev_item.uuid); |
| 541 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 542 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 543 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 544 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 545 | |
| 546 | name = rcu_string_strdup(path, GFP_NOFS); |
| 547 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 548 | kfree(device); |
| 549 | return -ENOMEM; |
| 550 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 551 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 552 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 553 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 554 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 555 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 556 | mutex_unlock(&fs_devices->device_list_mutex); |
| 557 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 558 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 559 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 560 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 561 | /* |
| 562 | * When FS is already mounted. |
| 563 | * 1. If you are here and if the device->name is NULL that |
| 564 | * means this device was missing at time of FS mount. |
| 565 | * 2. If you are here and if the device->name is different |
| 566 | * from 'path' that means either |
| 567 | * a. The same device disappeared and reappeared with |
| 568 | * different name. or |
| 569 | * b. The missing-disk-which-was-replaced, has |
| 570 | * reappeared now. |
| 571 | * |
| 572 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 573 | * and unintentional. |
| 574 | * |
| 575 | * Further in case of 1 and 2a above, the disk at 'path' |
| 576 | * would have missed some transaction when it was away and |
| 577 | * in case of 2a the stale bdev has to be updated as well. |
| 578 | * 2b must not be allowed at all time. |
| 579 | */ |
| 580 | |
| 581 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 582 | * For now, we do allow update to btrfs_fs_device through the |
| 583 | * btrfs dev scan cli after FS has been mounted. We're still |
| 584 | * tracking a problem where systems fail mount by subvolume id |
| 585 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 586 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 587 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 588 | /* |
| 589 | * That is if the FS is _not_ mounted and if you |
| 590 | * are here, that means there is more than one |
| 591 | * disk with same uuid and devid.We keep the one |
| 592 | * with larger generation number or the last-in if |
| 593 | * generation are equal. |
| 594 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 595 | return -EEXIST; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 596 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 597 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 598 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 599 | if (!name) |
| 600 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 601 | rcu_string_free(device->name); |
| 602 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 603 | if (device->missing) { |
| 604 | fs_devices->missing_devices--; |
| 605 | device->missing = 0; |
| 606 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 607 | } |
| 608 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 609 | /* |
| 610 | * Unmount does not free the btrfs_device struct but would zero |
| 611 | * generation along with most of the other members. So just update |
| 612 | * it back. We need it to pick the disk with largest generation |
| 613 | * (as above). |
| 614 | */ |
| 615 | if (!fs_devices->opened) |
| 616 | device->generation = found_transid; |
| 617 | |
Anand Jain | 4fde46f | 2015-06-17 21:10:48 +0800 | [diff] [blame] | 618 | /* |
| 619 | * if there is new btrfs on an already registered device, |
| 620 | * then remove the stale device entry. |
| 621 | */ |
| 622 | btrfs_free_stale_device(device); |
| 623 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 624 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 625 | |
| 626 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 629 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 630 | { |
| 631 | struct btrfs_fs_devices *fs_devices; |
| 632 | struct btrfs_device *device; |
| 633 | struct btrfs_device *orig_dev; |
| 634 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 635 | fs_devices = alloc_fs_devices(orig->fsid); |
| 636 | if (IS_ERR(fs_devices)) |
| 637 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 638 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 639 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 640 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 641 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 642 | /* 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] | 643 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 644 | struct rcu_string *name; |
| 645 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 646 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 647 | orig_dev->uuid); |
| 648 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 649 | goto error; |
| 650 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 651 | /* |
| 652 | * This is ok to do without rcu read locked because we hold the |
| 653 | * uuid mutex so nothing we touch in here is going to disappear. |
| 654 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 655 | if (orig_dev->name) { |
| 656 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 657 | if (!name) { |
| 658 | kfree(device); |
| 659 | goto error; |
| 660 | } |
| 661 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 662 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 663 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 664 | list_add(&device->dev_list, &fs_devices->devices); |
| 665 | device->fs_devices = fs_devices; |
| 666 | fs_devices->num_devices++; |
| 667 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 668 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 669 | return fs_devices; |
| 670 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 671 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 672 | free_fs_devices(fs_devices); |
| 673 | return ERR_PTR(-ENOMEM); |
| 674 | } |
| 675 | |
Eric Sandeen | 9eaed21 | 2014-08-01 18:12:35 -0500 | [diff] [blame] | 676 | 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] | 677 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 678 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 679 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 680 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 681 | mutex_lock(&uuid_mutex); |
| 682 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 683 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 684 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 685 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 686 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 687 | (!latest_dev || |
| 688 | device->generation > latest_dev->generation)) { |
| 689 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 690 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 691 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 692 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 693 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 694 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 695 | /* |
| 696 | * In the first step, keep the device which has |
| 697 | * the correct fsid and the devid that is used |
| 698 | * for the dev_replace procedure. |
| 699 | * In the second step, the dev_replace state is |
| 700 | * read from the device tree and it is known |
| 701 | * whether the procedure is really active or |
| 702 | * not, which means whether this device is |
| 703 | * used or whether it should be removed. |
| 704 | */ |
| 705 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 706 | continue; |
| 707 | } |
| 708 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 709 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 710 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 711 | device->bdev = NULL; |
| 712 | fs_devices->open_devices--; |
| 713 | } |
| 714 | if (device->writeable) { |
| 715 | list_del_init(&device->dev_alloc_list); |
| 716 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 717 | if (!device->is_tgtdev_for_dev_replace) |
| 718 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 719 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 720 | list_del_init(&device->dev_list); |
| 721 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 722 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 723 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 724 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 725 | |
| 726 | if (fs_devices->seed) { |
| 727 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 728 | goto again; |
| 729 | } |
| 730 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 731 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 732 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 733 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 734 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 735 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 736 | static void __free_device(struct work_struct *work) |
| 737 | { |
| 738 | struct btrfs_device *device; |
| 739 | |
| 740 | device = container_of(work, struct btrfs_device, rcu_work); |
| 741 | |
| 742 | if (device->bdev) |
| 743 | blkdev_put(device->bdev, device->mode); |
| 744 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 745 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 746 | kfree(device); |
| 747 | } |
| 748 | |
| 749 | static void free_device(struct rcu_head *head) |
| 750 | { |
| 751 | struct btrfs_device *device; |
| 752 | |
| 753 | device = container_of(head, struct btrfs_device, rcu); |
| 754 | |
| 755 | INIT_WORK(&device->rcu_work, __free_device); |
| 756 | schedule_work(&device->rcu_work); |
| 757 | } |
| 758 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 759 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 760 | { |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 761 | struct btrfs_device *device, *tmp; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 762 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 763 | if (--fs_devices->opened > 0) |
| 764 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 765 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 766 | mutex_lock(&fs_devices->device_list_mutex); |
Sasha Levin | 2037a09 | 2015-05-12 19:31:37 -0400 | [diff] [blame] | 767 | list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 768 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 769 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 770 | |
| 771 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 772 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 773 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 774 | if (device->writeable && |
| 775 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 776 | list_del_init(&device->dev_alloc_list); |
| 777 | fs_devices->rw_devices--; |
| 778 | } |
| 779 | |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 780 | if (device->missing) |
| 781 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 782 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 783 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 784 | device->uuid); |
| 785 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 786 | |
| 787 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 788 | if (device->name) { |
| 789 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 790 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 791 | rcu_assign_pointer(new_device->name, name); |
| 792 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 793 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 794 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 795 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 796 | |
| 797 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 798 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 799 | mutex_unlock(&fs_devices->device_list_mutex); |
| 800 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 801 | WARN_ON(fs_devices->open_devices); |
| 802 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 803 | fs_devices->opened = 0; |
| 804 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 805 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 809 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 810 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 811 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 812 | int ret; |
| 813 | |
| 814 | mutex_lock(&uuid_mutex); |
| 815 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 816 | if (!fs_devices->opened) { |
| 817 | seed_devices = fs_devices->seed; |
| 818 | fs_devices->seed = NULL; |
| 819 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 820 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 821 | |
| 822 | while (seed_devices) { |
| 823 | fs_devices = seed_devices; |
| 824 | seed_devices = fs_devices->seed; |
| 825 | __btrfs_close_devices(fs_devices); |
| 826 | free_fs_devices(fs_devices); |
| 827 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 828 | /* |
| 829 | * Wait for rcu kworkers under __btrfs_close_devices |
| 830 | * to finish all blkdev_puts so device is really |
| 831 | * free when umount is done. |
| 832 | */ |
| 833 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 834 | return ret; |
| 835 | } |
| 836 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 837 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 838 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 839 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 840 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 841 | struct block_device *bdev; |
| 842 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 843 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 844 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 845 | struct buffer_head *bh; |
| 846 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 847 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 848 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 849 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 850 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 851 | flags |= FMODE_EXCL; |
| 852 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 853 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 854 | if (device->bdev) |
| 855 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 856 | if (!device->name) |
| 857 | continue; |
| 858 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 859 | /* Just open everything we can; ignore failures here */ |
| 860 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 861 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 862 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 863 | |
| 864 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 865 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 866 | if (devid != device->devid) |
| 867 | goto error_brelse; |
| 868 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 869 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 870 | BTRFS_UUID_SIZE)) |
| 871 | goto error_brelse; |
| 872 | |
| 873 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 874 | if (!latest_dev || |
| 875 | device->generation > latest_dev->generation) |
| 876 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 877 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 878 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 879 | device->writeable = 0; |
| 880 | } else { |
| 881 | device->writeable = !bdev_read_only(bdev); |
| 882 | seeding = 0; |
| 883 | } |
| 884 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 885 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 886 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 887 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 888 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 889 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 890 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 891 | device->mode = flags; |
| 892 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 893 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 894 | fs_devices->rotating = 1; |
| 895 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 896 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 897 | if (device->writeable && |
| 898 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 899 | fs_devices->rw_devices++; |
| 900 | list_add(&device->dev_alloc_list, |
| 901 | &fs_devices->alloc_list); |
| 902 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 903 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 904 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 905 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 906 | error_brelse: |
| 907 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 908 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 909 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 910 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 911 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 912 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 913 | goto out; |
| 914 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 915 | fs_devices->seeding = seeding; |
| 916 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 917 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 918 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 919 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 924 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 925 | { |
| 926 | int ret; |
| 927 | |
| 928 | mutex_lock(&uuid_mutex); |
| 929 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 930 | fs_devices->opened++; |
| 931 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 932 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 933 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 934 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 935 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 936 | return ret; |
| 937 | } |
| 938 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 939 | /* |
| 940 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 941 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 942 | * is read via pagecache |
| 943 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 944 | 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] | 945 | struct btrfs_fs_devices **fs_devices_ret) |
| 946 | { |
| 947 | struct btrfs_super_block *disk_super; |
| 948 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 949 | struct page *page; |
| 950 | void *p; |
| 951 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 952 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 953 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 954 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 955 | u64 bytenr; |
| 956 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 957 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 958 | /* |
| 959 | * we would like to check all the supers, but that would make |
| 960 | * a btrfs mount succeed after a mkfs from a different FS. |
| 961 | * So, we need to add a special mount option to scan for |
| 962 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 963 | */ |
| 964 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 965 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 966 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 967 | |
| 968 | bdev = blkdev_get_by_path(path, flags, holder); |
| 969 | |
| 970 | if (IS_ERR(bdev)) { |
| 971 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 972 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* make sure our super fits in the device */ |
| 976 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 977 | goto error_bdev_put; |
| 978 | |
| 979 | /* make sure our super fits in the page */ |
| 980 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 981 | goto error_bdev_put; |
| 982 | |
| 983 | /* make sure our super doesn't straddle pages on disk */ |
| 984 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 985 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 986 | goto error_bdev_put; |
| 987 | |
| 988 | /* pull in the page with our super */ |
| 989 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 990 | index, GFP_NOFS); |
| 991 | |
| 992 | if (IS_ERR_OR_NULL(page)) |
| 993 | goto error_bdev_put; |
| 994 | |
| 995 | p = kmap(page); |
| 996 | |
| 997 | /* align our pointer to the offset of the super block */ |
| 998 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 999 | |
| 1000 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 1001 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1002 | goto error_unmap; |
| 1003 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1004 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 1005 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1006 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1007 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1008 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 1009 | if (ret > 0) { |
| 1010 | if (disk_super->label[0]) { |
| 1011 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 1012 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 1013 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 1014 | } else { |
| 1015 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 1016 | } |
| 1017 | |
| 1018 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 1019 | ret = 0; |
| 1020 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1021 | if (!ret && fs_devices_ret) |
| 1022 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 1023 | |
| 1024 | error_unmap: |
| 1025 | kunmap(page); |
| 1026 | page_cache_release(page); |
| 1027 | |
| 1028 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 1029 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1030 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1031 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1032 | return ret; |
| 1033 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1034 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1035 | /* helper to account the used device space in the range */ |
| 1036 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 1037 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1038 | { |
| 1039 | struct btrfs_key key; |
| 1040 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1041 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1042 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1043 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1044 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1045 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1046 | struct extent_buffer *l; |
| 1047 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1048 | *length = 0; |
| 1049 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1050 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1051 | return 0; |
| 1052 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1053 | path = btrfs_alloc_path(); |
| 1054 | if (!path) |
| 1055 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1056 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1057 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1058 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1059 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1060 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1061 | |
| 1062 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1063 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1064 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1065 | if (ret > 0) { |
| 1066 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1067 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1068 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1069 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1070 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1071 | while (1) { |
| 1072 | l = path->nodes[0]; |
| 1073 | slot = path->slots[0]; |
| 1074 | if (slot >= btrfs_header_nritems(l)) { |
| 1075 | ret = btrfs_next_leaf(root, path); |
| 1076 | if (ret == 0) |
| 1077 | continue; |
| 1078 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1079 | goto out; |
| 1080 | |
| 1081 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1082 | } |
| 1083 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1084 | |
| 1085 | if (key.objectid < device->devid) |
| 1086 | goto next; |
| 1087 | |
| 1088 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1089 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1090 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1091 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1092 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1093 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1094 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1095 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1096 | dev_extent); |
| 1097 | if (key.offset <= start && extent_end > end) { |
| 1098 | *length = end - start + 1; |
| 1099 | break; |
| 1100 | } else if (key.offset <= start && extent_end > start) |
| 1101 | *length += extent_end - start; |
| 1102 | else if (key.offset > start && extent_end <= end) |
| 1103 | *length += extent_end - key.offset; |
| 1104 | else if (key.offset > start && key.offset <= end) { |
| 1105 | *length += end - key.offset + 1; |
| 1106 | break; |
| 1107 | } else if (key.offset > end) |
| 1108 | break; |
| 1109 | |
| 1110 | next: |
| 1111 | path->slots[0]++; |
| 1112 | } |
| 1113 | ret = 0; |
| 1114 | out: |
| 1115 | btrfs_free_path(path); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1119 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1120 | struct btrfs_device *device, |
| 1121 | u64 *start, u64 len) |
| 1122 | { |
| 1123 | struct extent_map *em; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1124 | struct list_head *search_list = &trans->transaction->pending_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1125 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1126 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1127 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1128 | again: |
| 1129 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1130 | struct map_lookup *map; |
| 1131 | int i; |
| 1132 | |
| 1133 | map = (struct map_lookup *)em->bdev; |
| 1134 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1135 | u64 end; |
| 1136 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1137 | if (map->stripes[i].dev != device) |
| 1138 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1139 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1140 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1141 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1142 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1143 | /* |
| 1144 | * Make sure that while processing the pinned list we do |
| 1145 | * not override our *start with a lower value, because |
| 1146 | * we can have pinned chunks that fall within this |
| 1147 | * device hole and that have lower physical addresses |
| 1148 | * than the pending chunks we processed before. If we |
| 1149 | * do not take this special care we can end up getting |
| 1150 | * 2 pending chunks that start at the same physical |
| 1151 | * device offsets because the end offset of a pinned |
| 1152 | * chunk can be equal to the start offset of some |
| 1153 | * pending chunk. |
| 1154 | */ |
| 1155 | end = map->stripes[i].physical + em->orig_block_len; |
| 1156 | if (end > *start) { |
| 1157 | *start = end; |
| 1158 | ret = 1; |
| 1159 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1160 | } |
| 1161 | } |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1162 | if (search_list == &trans->transaction->pending_chunks) { |
| 1163 | search_list = &trans->root->fs_info->pinned_chunks; |
| 1164 | goto again; |
| 1165 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1166 | |
| 1167 | return ret; |
| 1168 | } |
| 1169 | |
| 1170 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1171 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1172 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1173 | * @device: the device which we search the free space in |
| 1174 | * @num_bytes: the size of the free space that we need |
| 1175 | * @start: store the start of the free space. |
| 1176 | * @len: the size of the free space. that we find, or the size of the max |
| 1177 | * free space if we don't find suitable free space |
| 1178 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1179 | * this uses a pretty simple search, the expectation is that it is |
| 1180 | * called very infrequently and that a given device has a small number |
| 1181 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1182 | * |
| 1183 | * @start is used to store the start of the free space if we find. But if we |
| 1184 | * don't find suitable free space, it will be used to store the start position |
| 1185 | * of the max free space. |
| 1186 | * |
| 1187 | * @len is used to store the size of the free space that we find. |
| 1188 | * But if we don't find suitable free space, it is used to store the size of |
| 1189 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1190 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1191 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1192 | struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1193 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1194 | { |
| 1195 | struct btrfs_key key; |
| 1196 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1197 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1198 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1199 | u64 hole_size; |
| 1200 | u64 max_hole_start; |
| 1201 | u64 max_hole_size; |
| 1202 | u64 extent_end; |
| 1203 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1204 | u64 search_end = device->total_bytes; |
| 1205 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1206 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1207 | struct extent_buffer *l; |
| 1208 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1209 | /* FIXME use last free of some kind */ |
| 1210 | |
| 1211 | /* we don't want to overwrite the superblock on the drive, |
| 1212 | * so we make sure to start at an offset of at least 1MB |
| 1213 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1214 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1215 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1216 | path = btrfs_alloc_path(); |
| 1217 | if (!path) |
| 1218 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1219 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1220 | max_hole_start = search_start; |
| 1221 | max_hole_size = 0; |
| 1222 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1223 | again: |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1224 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1225 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1226 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1229 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1230 | path->search_commit_root = 1; |
| 1231 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1232 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1233 | key.objectid = device->devid; |
| 1234 | key.offset = search_start; |
| 1235 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1236 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1237 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1238 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1239 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1240 | if (ret > 0) { |
| 1241 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1242 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1243 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1244 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1245 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1246 | while (1) { |
| 1247 | l = path->nodes[0]; |
| 1248 | slot = path->slots[0]; |
| 1249 | if (slot >= btrfs_header_nritems(l)) { |
| 1250 | ret = btrfs_next_leaf(root, path); |
| 1251 | if (ret == 0) |
| 1252 | continue; |
| 1253 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1254 | goto out; |
| 1255 | |
| 1256 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1257 | } |
| 1258 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1259 | |
| 1260 | if (key.objectid < device->devid) |
| 1261 | goto next; |
| 1262 | |
| 1263 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1264 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1265 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1266 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1267 | goto next; |
| 1268 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1269 | if (key.offset > search_start) { |
| 1270 | hole_size = key.offset - search_start; |
| 1271 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1272 | /* |
| 1273 | * Have to check before we set max_hole_start, otherwise |
| 1274 | * we could end up sending back this offset anyway. |
| 1275 | */ |
| 1276 | if (contains_pending_extent(trans, device, |
| 1277 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1278 | hole_size)) { |
| 1279 | if (key.offset >= search_start) { |
| 1280 | hole_size = key.offset - search_start; |
| 1281 | } else { |
| 1282 | WARN_ON_ONCE(1); |
| 1283 | hole_size = 0; |
| 1284 | } |
| 1285 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1286 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1287 | if (hole_size > max_hole_size) { |
| 1288 | max_hole_start = search_start; |
| 1289 | max_hole_size = hole_size; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * If this free space is greater than which we need, |
| 1294 | * it must be the max free space that we have found |
| 1295 | * until now, so max_hole_start must point to the start |
| 1296 | * of this free space and the length of this free space |
| 1297 | * is stored in max_hole_size. Thus, we return |
| 1298 | * max_hole_start and max_hole_size and go back to the |
| 1299 | * caller. |
| 1300 | */ |
| 1301 | if (hole_size >= num_bytes) { |
| 1302 | ret = 0; |
| 1303 | goto out; |
| 1304 | } |
| 1305 | } |
| 1306 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1307 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1308 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1309 | dev_extent); |
| 1310 | if (extent_end > search_start) |
| 1311 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1312 | next: |
| 1313 | path->slots[0]++; |
| 1314 | cond_resched(); |
| 1315 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1316 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1317 | /* |
| 1318 | * At this point, search_start should be the end of |
| 1319 | * allocated dev extents, and when shrinking the device, |
| 1320 | * search_end may be smaller than search_start. |
| 1321 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1322 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1323 | hole_size = search_end - search_start; |
| 1324 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1325 | if (contains_pending_extent(trans, device, &search_start, |
| 1326 | hole_size)) { |
| 1327 | btrfs_release_path(path); |
| 1328 | goto again; |
| 1329 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1330 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1331 | if (hole_size > max_hole_size) { |
| 1332 | max_hole_start = search_start; |
| 1333 | max_hole_size = hole_size; |
| 1334 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1337 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1338 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1339 | ret = -ENOSPC; |
| 1340 | else |
| 1341 | ret = 0; |
| 1342 | |
| 1343 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1344 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1345 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1346 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1347 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1348 | return ret; |
| 1349 | } |
| 1350 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1351 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1352 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1353 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1354 | { |
| 1355 | int ret; |
| 1356 | struct btrfs_path *path; |
| 1357 | struct btrfs_root *root = device->dev_root; |
| 1358 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1359 | struct btrfs_key found_key; |
| 1360 | struct extent_buffer *leaf = NULL; |
| 1361 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1362 | |
| 1363 | path = btrfs_alloc_path(); |
| 1364 | if (!path) |
| 1365 | return -ENOMEM; |
| 1366 | |
| 1367 | key.objectid = device->devid; |
| 1368 | key.offset = start; |
| 1369 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1370 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1371 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1372 | if (ret > 0) { |
| 1373 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1374 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1375 | if (ret) |
| 1376 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1377 | leaf = path->nodes[0]; |
| 1378 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1379 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1380 | struct btrfs_dev_extent); |
| 1381 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1382 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1383 | key = found_key; |
| 1384 | btrfs_release_path(path); |
| 1385 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1386 | } else if (ret == 0) { |
| 1387 | leaf = path->nodes[0]; |
| 1388 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1389 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1390 | } else { |
| 1391 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1392 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1393 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1394 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1395 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1396 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1397 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1398 | if (ret) { |
| 1399 | btrfs_error(root->fs_info, ret, |
| 1400 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1401 | } else { |
| 1402 | trans->transaction->have_free_bgs = 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1403 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1404 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1405 | btrfs_free_path(path); |
| 1406 | return ret; |
| 1407 | } |
| 1408 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1409 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1410 | struct btrfs_device *device, |
| 1411 | u64 chunk_tree, u64 chunk_objectid, |
| 1412 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | { |
| 1414 | int ret; |
| 1415 | struct btrfs_path *path; |
| 1416 | struct btrfs_root *root = device->dev_root; |
| 1417 | struct btrfs_dev_extent *extent; |
| 1418 | struct extent_buffer *leaf; |
| 1419 | struct btrfs_key key; |
| 1420 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1421 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1422 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1423 | path = btrfs_alloc_path(); |
| 1424 | if (!path) |
| 1425 | return -ENOMEM; |
| 1426 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1427 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1428 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1429 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1430 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1431 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1432 | if (ret) |
| 1433 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1434 | |
| 1435 | leaf = path->nodes[0]; |
| 1436 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1437 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1438 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1439 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1440 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1441 | |
| 1442 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1443 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1444 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1445 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1446 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1447 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1448 | btrfs_free_path(path); |
| 1449 | return ret; |
| 1450 | } |
| 1451 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1452 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1453 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1454 | struct extent_map_tree *em_tree; |
| 1455 | struct extent_map *em; |
| 1456 | struct rb_node *n; |
| 1457 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1458 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1459 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1460 | read_lock(&em_tree->lock); |
| 1461 | n = rb_last(&em_tree->map); |
| 1462 | if (n) { |
| 1463 | em = rb_entry(n, struct extent_map, rb_node); |
| 1464 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1465 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1466 | read_unlock(&em_tree->lock); |
| 1467 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1468 | return ret; |
| 1469 | } |
| 1470 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1471 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1472 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1473 | { |
| 1474 | int ret; |
| 1475 | struct btrfs_key key; |
| 1476 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1477 | struct btrfs_path *path; |
| 1478 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1479 | path = btrfs_alloc_path(); |
| 1480 | if (!path) |
| 1481 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1482 | |
| 1483 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1484 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1485 | key.offset = (u64)-1; |
| 1486 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1487 | 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] | 1488 | if (ret < 0) |
| 1489 | goto error; |
| 1490 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1491 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1492 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1493 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1494 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1495 | BTRFS_DEV_ITEM_KEY); |
| 1496 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1497 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1498 | } else { |
| 1499 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1500 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1501 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1502 | } |
| 1503 | ret = 0; |
| 1504 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1505 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * the device information is stored in the chunk root |
| 1511 | * the btrfs_device struct should be fully filled in |
| 1512 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1513 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1514 | struct btrfs_root *root, |
| 1515 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1516 | { |
| 1517 | int ret; |
| 1518 | struct btrfs_path *path; |
| 1519 | struct btrfs_dev_item *dev_item; |
| 1520 | struct extent_buffer *leaf; |
| 1521 | struct btrfs_key key; |
| 1522 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1523 | |
| 1524 | root = root->fs_info->chunk_root; |
| 1525 | |
| 1526 | path = btrfs_alloc_path(); |
| 1527 | if (!path) |
| 1528 | return -ENOMEM; |
| 1529 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1530 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1531 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1532 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1533 | |
| 1534 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1535 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1536 | if (ret) |
| 1537 | goto out; |
| 1538 | |
| 1539 | leaf = path->nodes[0]; |
| 1540 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1541 | |
| 1542 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1543 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1544 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1545 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1546 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1547 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1548 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1549 | btrfs_device_get_disk_total_bytes(device)); |
| 1550 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1551 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1552 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1553 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1554 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1555 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1556 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1557 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1558 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1559 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1560 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1561 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1562 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1563 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1564 | out: |
| 1565 | btrfs_free_path(path); |
| 1566 | return ret; |
| 1567 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1568 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1569 | /* |
| 1570 | * Function to update ctime/mtime for a given device path. |
| 1571 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1572 | */ |
| 1573 | static void update_dev_time(char *path_name) |
| 1574 | { |
| 1575 | struct file *filp; |
| 1576 | |
| 1577 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1578 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1579 | return; |
| 1580 | file_update_time(filp); |
| 1581 | filp_close(filp, NULL); |
| 1582 | return; |
| 1583 | } |
| 1584 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1585 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1586 | struct btrfs_device *device) |
| 1587 | { |
| 1588 | int ret; |
| 1589 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1590 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1591 | struct btrfs_trans_handle *trans; |
| 1592 | |
| 1593 | root = root->fs_info->chunk_root; |
| 1594 | |
| 1595 | path = btrfs_alloc_path(); |
| 1596 | if (!path) |
| 1597 | return -ENOMEM; |
| 1598 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1599 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1600 | if (IS_ERR(trans)) { |
| 1601 | btrfs_free_path(path); |
| 1602 | return PTR_ERR(trans); |
| 1603 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1604 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1605 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1606 | key.offset = device->devid; |
| 1607 | |
| 1608 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1609 | if (ret < 0) |
| 1610 | goto out; |
| 1611 | |
| 1612 | if (ret > 0) { |
| 1613 | ret = -ENOENT; |
| 1614 | goto out; |
| 1615 | } |
| 1616 | |
| 1617 | ret = btrfs_del_item(trans, root, path); |
| 1618 | if (ret) |
| 1619 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1620 | out: |
| 1621 | btrfs_free_path(path); |
| 1622 | btrfs_commit_transaction(trans, root); |
| 1623 | return ret; |
| 1624 | } |
| 1625 | |
| 1626 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1627 | { |
| 1628 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1629 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1630 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1631 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1632 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1633 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1634 | u64 all_avail; |
| 1635 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1636 | u64 num_devices; |
| 1637 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1638 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1639 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1640 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1641 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1642 | mutex_lock(&uuid_mutex); |
| 1643 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1644 | do { |
| 1645 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1646 | |
| 1647 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1648 | root->fs_info->avail_system_alloc_bits | |
| 1649 | root->fs_info->avail_metadata_alloc_bits; |
| 1650 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1651 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1652 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1653 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1654 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1655 | WARN_ON(num_devices < 1); |
| 1656 | num_devices--; |
| 1657 | } |
| 1658 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1659 | |
| 1660 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1661 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1662 | goto out; |
| 1663 | } |
| 1664 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1665 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1666 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1667 | goto out; |
| 1668 | } |
| 1669 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1670 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1671 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1672 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1673 | goto out; |
| 1674 | } |
| 1675 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1676 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1677 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1678 | goto out; |
| 1679 | } |
| 1680 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1681 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1682 | struct list_head *devices; |
| 1683 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1684 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1685 | device = NULL; |
| 1686 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1687 | /* |
| 1688 | * It is safe to read the devices since the volume_mutex |
| 1689 | * is held. |
| 1690 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1691 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1692 | if (tmp->in_fs_metadata && |
| 1693 | !tmp->is_tgtdev_for_dev_replace && |
| 1694 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1695 | device = tmp; |
| 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | bdev = NULL; |
| 1700 | bh = NULL; |
| 1701 | disk_super = NULL; |
| 1702 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1703 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1704 | goto out; |
| 1705 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1706 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1707 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1708 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1709 | root->fs_info->bdev_holder, 0, |
| 1710 | &bdev, &bh); |
| 1711 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1712 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1713 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1714 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1715 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1716 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1717 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1718 | if (!device) { |
| 1719 | ret = -ENOENT; |
| 1720 | goto error_brelse; |
| 1721 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1722 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1723 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1724 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1725 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1726 | goto error_brelse; |
| 1727 | } |
| 1728 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1729 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1730 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1731 | goto error_brelse; |
| 1732 | } |
| 1733 | |
| 1734 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1735 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1736 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1737 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1738 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1739 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1740 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1741 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1742 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1743 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1744 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1745 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1746 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1747 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1748 | /* |
| 1749 | * TODO: the superblock still includes this device in its num_devices |
| 1750 | * counter although write_all_supers() is not locked out. This |
| 1751 | * could give a filesystem state which requires a degraded mount. |
| 1752 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1753 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1754 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1755 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1756 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1757 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1758 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1759 | |
| 1760 | /* |
| 1761 | * the device list mutex makes sure that we don't change |
| 1762 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1763 | * the device supers. Whoever is writing all supers, should |
| 1764 | * lock the device list mutex before getting the number of |
| 1765 | * devices in the super block (super_copy). Conversely, |
| 1766 | * whoever updates the number of devices in the super block |
| 1767 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1768 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1769 | |
| 1770 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1771 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1772 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1773 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1774 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1775 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1776 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1777 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1778 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1779 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1780 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1781 | struct btrfs_device, dev_list); |
| 1782 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1783 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1784 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1785 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1786 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1787 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1788 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1789 | /* remove sysfs entry */ |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 1790 | btrfs_kobj_rm_device(root->fs_info->fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1791 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1792 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1793 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1794 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1795 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1796 | 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] | 1797 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1798 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1799 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1800 | struct btrfs_fs_devices *fs_devices; |
| 1801 | fs_devices = root->fs_info->fs_devices; |
| 1802 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1803 | if (fs_devices->seed == cur_devices) { |
| 1804 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1805 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1806 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1807 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1808 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1809 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1810 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1811 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1812 | } |
| 1813 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1814 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1815 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1816 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1817 | /* |
| 1818 | * at this point, the device is zero sized. We want to |
| 1819 | * remove it from the devices list and zero out the old super |
| 1820 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1821 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1822 | u64 bytenr; |
| 1823 | int i; |
| 1824 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1825 | /* make sure this device isn't detected as part of |
| 1826 | * the FS anymore |
| 1827 | */ |
| 1828 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1829 | set_buffer_dirty(bh); |
| 1830 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1831 | |
| 1832 | /* clear the mirror copies of super block on the disk |
| 1833 | * being removed, 0th copy is been taken care above and |
| 1834 | * the below would take of the rest |
| 1835 | */ |
| 1836 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1837 | bytenr = btrfs_sb_offset(i); |
| 1838 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1839 | i_size_read(bdev->bd_inode)) |
| 1840 | break; |
| 1841 | |
| 1842 | brelse(bh); |
| 1843 | bh = __bread(bdev, bytenr / 4096, |
| 1844 | BTRFS_SUPER_INFO_SIZE); |
| 1845 | if (!bh) |
| 1846 | continue; |
| 1847 | |
| 1848 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1849 | |
| 1850 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1851 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1852 | continue; |
| 1853 | } |
| 1854 | memset(&disk_super->magic, 0, |
| 1855 | sizeof(disk_super->magic)); |
| 1856 | set_buffer_dirty(bh); |
| 1857 | sync_dirty_buffer(bh); |
| 1858 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1859 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1860 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1861 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1862 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1863 | if (bdev) { |
| 1864 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1865 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1866 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1867 | /* Update ctime/mtime for device path for libblkid */ |
| 1868 | update_dev_time(device_path); |
| 1869 | } |
| 1870 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1871 | error_brelse: |
| 1872 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1873 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1874 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1875 | out: |
| 1876 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1877 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1878 | error_undo: |
| 1879 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1880 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1881 | list_add(&device->dev_alloc_list, |
| 1882 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1883 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1884 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1885 | } |
| 1886 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1887 | } |
| 1888 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1889 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1890 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1891 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1892 | struct btrfs_fs_devices *fs_devices; |
| 1893 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1894 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1895 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1896 | /* |
| 1897 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1898 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1899 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1900 | * srcdev will have its correct fs_devices in both the cases. |
| 1901 | */ |
| 1902 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1903 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1904 | list_del_rcu(&srcdev->dev_list); |
| 1905 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1906 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1907 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1908 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1909 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1910 | if (srcdev->writeable) { |
| 1911 | fs_devices->rw_devices--; |
| 1912 | /* zero out the old super if it is writable */ |
| 1913 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1914 | } |
| 1915 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1916 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1917 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 1921 | struct btrfs_device *srcdev) |
| 1922 | { |
| 1923 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1924 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1925 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1926 | |
| 1927 | /* |
| 1928 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 1929 | * zero |
| 1930 | */ |
| 1931 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 1932 | |
| 1933 | /* if this is no devs we rather delete the fs_devices */ |
| 1934 | if (!fs_devices->num_devices) { |
| 1935 | struct btrfs_fs_devices *tmp_fs_devices; |
| 1936 | |
| 1937 | tmp_fs_devices = fs_info->fs_devices; |
| 1938 | while (tmp_fs_devices) { |
| 1939 | if (tmp_fs_devices->seed == fs_devices) { |
| 1940 | tmp_fs_devices->seed = fs_devices->seed; |
| 1941 | break; |
| 1942 | } |
| 1943 | tmp_fs_devices = tmp_fs_devices->seed; |
| 1944 | } |
| 1945 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 1946 | __btrfs_close_devices(fs_devices); |
| 1947 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1948 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1952 | struct btrfs_device *tgtdev) |
| 1953 | { |
| 1954 | struct btrfs_device *next_device; |
| 1955 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1956 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1957 | WARN_ON(!tgtdev); |
| 1958 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 1959 | |
| 1960 | btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev); |
| 1961 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1962 | if (tgtdev->bdev) { |
| 1963 | btrfs_scratch_superblock(tgtdev); |
| 1964 | fs_info->fs_devices->open_devices--; |
| 1965 | } |
| 1966 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1967 | |
| 1968 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1969 | struct btrfs_device, dev_list); |
| 1970 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1971 | fs_info->sb->s_bdev = next_device->bdev; |
| 1972 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1973 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1974 | list_del_rcu(&tgtdev->dev_list); |
| 1975 | |
| 1976 | call_rcu(&tgtdev->rcu, free_device); |
| 1977 | |
| 1978 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1979 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1980 | } |
| 1981 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1982 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1983 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1984 | { |
| 1985 | int ret = 0; |
| 1986 | struct btrfs_super_block *disk_super; |
| 1987 | u64 devid; |
| 1988 | u8 *dev_uuid; |
| 1989 | struct block_device *bdev; |
| 1990 | struct buffer_head *bh; |
| 1991 | |
| 1992 | *device = NULL; |
| 1993 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1994 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1995 | if (ret) |
| 1996 | return ret; |
| 1997 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1998 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1999 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2000 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2001 | disk_super->fsid); |
| 2002 | brelse(bh); |
| 2003 | if (!*device) |
| 2004 | ret = -ENOENT; |
| 2005 | blkdev_put(bdev, FMODE_READ); |
| 2006 | return ret; |
| 2007 | } |
| 2008 | |
| 2009 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 2010 | char *device_path, |
| 2011 | struct btrfs_device **device) |
| 2012 | { |
| 2013 | *device = NULL; |
| 2014 | if (strcmp(device_path, "missing") == 0) { |
| 2015 | struct list_head *devices; |
| 2016 | struct btrfs_device *tmp; |
| 2017 | |
| 2018 | devices = &root->fs_info->fs_devices->devices; |
| 2019 | /* |
| 2020 | * It is safe to read the devices since the volume_mutex |
| 2021 | * is held by the caller. |
| 2022 | */ |
| 2023 | list_for_each_entry(tmp, devices, dev_list) { |
| 2024 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 2025 | *device = tmp; |
| 2026 | break; |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2031 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2032 | return -ENOENT; |
| 2033 | } |
| 2034 | |
| 2035 | return 0; |
| 2036 | } else { |
| 2037 | return btrfs_find_device_by_path(root, device_path, device); |
| 2038 | } |
| 2039 | } |
| 2040 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2041 | /* |
| 2042 | * does all the dirty work required for changing file system's UUID. |
| 2043 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2044 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2045 | { |
| 2046 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2047 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2048 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2049 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2050 | struct btrfs_device *device; |
| 2051 | u64 super_flags; |
| 2052 | |
| 2053 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2054 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2055 | return -EINVAL; |
| 2056 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2057 | seed_devices = __alloc_fs_devices(); |
| 2058 | if (IS_ERR(seed_devices)) |
| 2059 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2060 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2061 | old_devices = clone_fs_devices(fs_devices); |
| 2062 | if (IS_ERR(old_devices)) { |
| 2063 | kfree(seed_devices); |
| 2064 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2065 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2066 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2067 | list_add(&old_devices->list, &fs_uuids); |
| 2068 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2069 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2070 | seed_devices->opened = 1; |
| 2071 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2072 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2073 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2074 | |
| 2075 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2076 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2077 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2078 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2079 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2080 | |
| 2081 | lock_chunks(root); |
| 2082 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 2083 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2084 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2085 | fs_devices->seeding = 0; |
| 2086 | fs_devices->num_devices = 0; |
| 2087 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2088 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2089 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2090 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2091 | |
| 2092 | generate_random_uuid(fs_devices->fsid); |
| 2093 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2094 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2095 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2096 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2097 | super_flags = btrfs_super_flags(disk_super) & |
| 2098 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2099 | btrfs_set_super_flags(disk_super, super_flags); |
| 2100 | |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | /* |
| 2105 | * strore the expected generation for seed devices in device items. |
| 2106 | */ |
| 2107 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2108 | struct btrfs_root *root) |
| 2109 | { |
| 2110 | struct btrfs_path *path; |
| 2111 | struct extent_buffer *leaf; |
| 2112 | struct btrfs_dev_item *dev_item; |
| 2113 | struct btrfs_device *device; |
| 2114 | struct btrfs_key key; |
| 2115 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2116 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2117 | u64 devid; |
| 2118 | int ret; |
| 2119 | |
| 2120 | path = btrfs_alloc_path(); |
| 2121 | if (!path) |
| 2122 | return -ENOMEM; |
| 2123 | |
| 2124 | root = root->fs_info->chunk_root; |
| 2125 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2126 | key.offset = 0; |
| 2127 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2128 | |
| 2129 | while (1) { |
| 2130 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2131 | if (ret < 0) |
| 2132 | goto error; |
| 2133 | |
| 2134 | leaf = path->nodes[0]; |
| 2135 | next_slot: |
| 2136 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2137 | ret = btrfs_next_leaf(root, path); |
| 2138 | if (ret > 0) |
| 2139 | break; |
| 2140 | if (ret < 0) |
| 2141 | goto error; |
| 2142 | leaf = path->nodes[0]; |
| 2143 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2144 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2145 | continue; |
| 2146 | } |
| 2147 | |
| 2148 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2149 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2150 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2151 | break; |
| 2152 | |
| 2153 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2154 | struct btrfs_dev_item); |
| 2155 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2156 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2157 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2158 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2159 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2160 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2161 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2162 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2163 | |
| 2164 | if (device->fs_devices->seeding) { |
| 2165 | btrfs_set_device_generation(leaf, dev_item, |
| 2166 | device->generation); |
| 2167 | btrfs_mark_buffer_dirty(leaf); |
| 2168 | } |
| 2169 | |
| 2170 | path->slots[0]++; |
| 2171 | goto next_slot; |
| 2172 | } |
| 2173 | ret = 0; |
| 2174 | error: |
| 2175 | btrfs_free_path(path); |
| 2176 | return ret; |
| 2177 | } |
| 2178 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2179 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2180 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2181 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2182 | struct btrfs_trans_handle *trans; |
| 2183 | struct btrfs_device *device; |
| 2184 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2185 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2186 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2187 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2188 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2189 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2190 | int ret = 0; |
| 2191 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2192 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2193 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2194 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2195 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2196 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2197 | if (IS_ERR(bdev)) |
| 2198 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2199 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2200 | if (root->fs_info->fs_devices->seeding) { |
| 2201 | seeding_dev = 1; |
| 2202 | down_write(&sb->s_umount); |
| 2203 | mutex_lock(&uuid_mutex); |
| 2204 | } |
| 2205 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2206 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2207 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2208 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2209 | |
| 2210 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2211 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2212 | if (device->bdev == bdev) { |
| 2213 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2214 | mutex_unlock( |
| 2215 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2216 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2217 | } |
| 2218 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2219 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2220 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2221 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2222 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2223 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2224 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2225 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2226 | } |
| 2227 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2228 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2229 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2230 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2231 | ret = -ENOMEM; |
| 2232 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2233 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2234 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2235 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2236 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2237 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2238 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2239 | kfree(device); |
| 2240 | ret = PTR_ERR(trans); |
| 2241 | goto error; |
| 2242 | } |
| 2243 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2244 | q = bdev_get_queue(bdev); |
| 2245 | if (blk_queue_discard(q)) |
| 2246 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2247 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2248 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2249 | device->io_width = root->sectorsize; |
| 2250 | device->io_align = root->sectorsize; |
| 2251 | device->sector_size = root->sectorsize; |
| 2252 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2253 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2254 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2255 | device->dev_root = root->fs_info->dev_root; |
| 2256 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2257 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2258 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2259 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2260 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2261 | set_blocksize(device->bdev, 4096); |
| 2262 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2263 | if (seeding_dev) { |
| 2264 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2265 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2266 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2270 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2271 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2272 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2273 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2274 | list_add(&device->dev_alloc_list, |
| 2275 | &root->fs_info->fs_devices->alloc_list); |
| 2276 | root->fs_info->fs_devices->num_devices++; |
| 2277 | root->fs_info->fs_devices->open_devices++; |
| 2278 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2279 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2280 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2281 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2282 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2283 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2284 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2285 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2286 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2287 | root->fs_info->fs_devices->rotating = 1; |
| 2288 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2289 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2290 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2291 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2292 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2293 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2294 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2295 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2296 | |
| 2297 | /* add sysfs device entry */ |
Anand Jain | 1ba4381 | 2015-03-10 06:38:33 +0800 | [diff] [blame] | 2298 | btrfs_kobj_add_device(root->fs_info->fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2299 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2300 | /* |
| 2301 | * we've got more storage, clear any full flags on the space |
| 2302 | * infos |
| 2303 | */ |
| 2304 | btrfs_clear_space_info_full(root->fs_info); |
| 2305 | |
| 2306 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2307 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2308 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2309 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2310 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2311 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2312 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2313 | if (ret) { |
| 2314 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2315 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2316 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | ret = btrfs_add_device(trans, root, device); |
| 2320 | if (ret) { |
| 2321 | btrfs_abort_transaction(trans, root, ret); |
| 2322 | goto error_trans; |
| 2323 | } |
| 2324 | |
| 2325 | if (seeding_dev) { |
| 2326 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2327 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2328 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2329 | if (ret) { |
| 2330 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2331 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2332 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2333 | |
| 2334 | /* Sprouting would change fsid of the mounted root, |
| 2335 | * so rename the fsid on the sysfs |
| 2336 | */ |
| 2337 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2338 | root->fs_info->fsid); |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 2339 | if (kobject_rename(&root->fs_info->fs_devices->super_kobj, |
| 2340 | fsid_buf)) |
Anand Jain | 2421a8c | 2015-03-10 06:38:40 +0800 | [diff] [blame] | 2341 | pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2342 | } |
| 2343 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2344 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2345 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2346 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2347 | |
| 2348 | if (seeding_dev) { |
| 2349 | mutex_unlock(&uuid_mutex); |
| 2350 | up_write(&sb->s_umount); |
| 2351 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2352 | if (ret) /* transaction commit */ |
| 2353 | return ret; |
| 2354 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2355 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2356 | if (ret < 0) |
| 2357 | btrfs_error(root->fs_info, ret, |
| 2358 | "Failed to relocate sys chunks after " |
| 2359 | "device initialization. This can be fixed " |
| 2360 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2361 | trans = btrfs_attach_transaction(root); |
| 2362 | if (IS_ERR(trans)) { |
| 2363 | if (PTR_ERR(trans) == -ENOENT) |
| 2364 | return 0; |
| 2365 | return PTR_ERR(trans); |
| 2366 | } |
| 2367 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2368 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2369 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2370 | /* Update ctime/mtime for libblkid */ |
| 2371 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2372 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2373 | |
| 2374 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2375 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2376 | rcu_string_free(device->name); |
Anand Jain | 6c14a16 | 2015-03-10 06:38:34 +0800 | [diff] [blame] | 2377 | btrfs_kobj_rm_device(root->fs_info->fs_devices, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2378 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2379 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2380 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2381 | if (seeding_dev) { |
| 2382 | mutex_unlock(&uuid_mutex); |
| 2383 | up_write(&sb->s_umount); |
| 2384 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2385 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2386 | } |
| 2387 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2388 | 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] | 2389 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2390 | struct btrfs_device **device_out) |
| 2391 | { |
| 2392 | struct request_queue *q; |
| 2393 | struct btrfs_device *device; |
| 2394 | struct block_device *bdev; |
| 2395 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2396 | struct list_head *devices; |
| 2397 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2398 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2399 | int ret = 0; |
| 2400 | |
| 2401 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2402 | if (fs_info->fs_devices->seeding) { |
| 2403 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2404 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2405 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2406 | |
| 2407 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2408 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2409 | if (IS_ERR(bdev)) { |
| 2410 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2411 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2412 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2413 | |
| 2414 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2415 | |
| 2416 | devices = &fs_info->fs_devices->devices; |
| 2417 | list_for_each_entry(device, devices, dev_list) { |
| 2418 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2419 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2420 | ret = -EEXIST; |
| 2421 | goto error; |
| 2422 | } |
| 2423 | } |
| 2424 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2425 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2426 | if (i_size_read(bdev->bd_inode) < |
| 2427 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2428 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2429 | ret = -EINVAL; |
| 2430 | goto error; |
| 2431 | } |
| 2432 | |
| 2433 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2434 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2435 | if (IS_ERR(device)) { |
| 2436 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2437 | goto error; |
| 2438 | } |
| 2439 | |
| 2440 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2441 | if (!name) { |
| 2442 | kfree(device); |
| 2443 | ret = -ENOMEM; |
| 2444 | goto error; |
| 2445 | } |
| 2446 | rcu_assign_pointer(device->name, name); |
| 2447 | |
| 2448 | q = bdev_get_queue(bdev); |
| 2449 | if (blk_queue_discard(q)) |
| 2450 | device->can_discard = 1; |
| 2451 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2452 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2453 | device->generation = 0; |
| 2454 | device->io_width = root->sectorsize; |
| 2455 | device->io_align = root->sectorsize; |
| 2456 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2457 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2458 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2459 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2460 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2461 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2462 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2463 | device->dev_root = fs_info->dev_root; |
| 2464 | device->bdev = bdev; |
| 2465 | device->in_fs_metadata = 1; |
| 2466 | device->is_tgtdev_for_dev_replace = 1; |
| 2467 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2468 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2469 | set_blocksize(device->bdev, 4096); |
| 2470 | device->fs_devices = fs_info->fs_devices; |
| 2471 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2472 | fs_info->fs_devices->num_devices++; |
| 2473 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2474 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2475 | |
| 2476 | *device_out = device; |
| 2477 | return ret; |
| 2478 | |
| 2479 | error: |
| 2480 | blkdev_put(bdev, FMODE_EXCL); |
| 2481 | return ret; |
| 2482 | } |
| 2483 | |
| 2484 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2485 | struct btrfs_device *tgtdev) |
| 2486 | { |
| 2487 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2488 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2489 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2490 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2491 | tgtdev->dev_root = fs_info->dev_root; |
| 2492 | tgtdev->in_fs_metadata = 1; |
| 2493 | } |
| 2494 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2495 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2496 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2497 | { |
| 2498 | int ret; |
| 2499 | struct btrfs_path *path; |
| 2500 | struct btrfs_root *root; |
| 2501 | struct btrfs_dev_item *dev_item; |
| 2502 | struct extent_buffer *leaf; |
| 2503 | struct btrfs_key key; |
| 2504 | |
| 2505 | root = device->dev_root->fs_info->chunk_root; |
| 2506 | |
| 2507 | path = btrfs_alloc_path(); |
| 2508 | if (!path) |
| 2509 | return -ENOMEM; |
| 2510 | |
| 2511 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2512 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2513 | key.offset = device->devid; |
| 2514 | |
| 2515 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2516 | if (ret < 0) |
| 2517 | goto out; |
| 2518 | |
| 2519 | if (ret > 0) { |
| 2520 | ret = -ENOENT; |
| 2521 | goto out; |
| 2522 | } |
| 2523 | |
| 2524 | leaf = path->nodes[0]; |
| 2525 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2526 | |
| 2527 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2528 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2529 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2530 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2531 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2532 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2533 | btrfs_device_get_disk_total_bytes(device)); |
| 2534 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2535 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2536 | btrfs_mark_buffer_dirty(leaf); |
| 2537 | |
| 2538 | out: |
| 2539 | btrfs_free_path(path); |
| 2540 | return ret; |
| 2541 | } |
| 2542 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2543 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2544 | struct btrfs_device *device, u64 new_size) |
| 2545 | { |
| 2546 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2547 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2548 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2549 | u64 old_total; |
| 2550 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2551 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2552 | if (!device->writeable) |
| 2553 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2554 | |
| 2555 | lock_chunks(device->dev_root); |
| 2556 | old_total = btrfs_super_total_bytes(super_copy); |
| 2557 | diff = new_size - device->total_bytes; |
| 2558 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2559 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2560 | device->is_tgtdev_for_dev_replace) { |
| 2561 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2562 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2563 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2564 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2565 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2566 | |
| 2567 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2568 | device->fs_devices->total_rw_bytes += diff; |
| 2569 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2570 | btrfs_device_set_total_bytes(device, new_size); |
| 2571 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2572 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2573 | if (list_empty(&device->resized_list)) |
| 2574 | list_add_tail(&device->resized_list, |
| 2575 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2576 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2577 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2578 | return btrfs_update_device(trans, device); |
| 2579 | } |
| 2580 | |
| 2581 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2582 | struct btrfs_root *root, u64 chunk_objectid, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2583 | u64 chunk_offset) |
| 2584 | { |
| 2585 | int ret; |
| 2586 | struct btrfs_path *path; |
| 2587 | struct btrfs_key key; |
| 2588 | |
| 2589 | root = root->fs_info->chunk_root; |
| 2590 | path = btrfs_alloc_path(); |
| 2591 | if (!path) |
| 2592 | return -ENOMEM; |
| 2593 | |
| 2594 | key.objectid = chunk_objectid; |
| 2595 | key.offset = chunk_offset; |
| 2596 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2597 | |
| 2598 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2599 | if (ret < 0) |
| 2600 | goto out; |
| 2601 | else if (ret > 0) { /* Logic error or corruption */ |
| 2602 | btrfs_error(root->fs_info, -ENOENT, |
| 2603 | "Failed lookup while freeing chunk."); |
| 2604 | ret = -ENOENT; |
| 2605 | goto out; |
| 2606 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2607 | |
| 2608 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2609 | if (ret < 0) |
| 2610 | btrfs_error(root->fs_info, ret, |
| 2611 | "Failed to delete chunk item."); |
| 2612 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2613 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2614 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2615 | } |
| 2616 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2617 | 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] | 2618 | chunk_offset) |
| 2619 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2620 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2621 | struct btrfs_disk_key *disk_key; |
| 2622 | struct btrfs_chunk *chunk; |
| 2623 | u8 *ptr; |
| 2624 | int ret = 0; |
| 2625 | u32 num_stripes; |
| 2626 | u32 array_size; |
| 2627 | u32 len = 0; |
| 2628 | u32 cur; |
| 2629 | struct btrfs_key key; |
| 2630 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2631 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2632 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2633 | |
| 2634 | ptr = super_copy->sys_chunk_array; |
| 2635 | cur = 0; |
| 2636 | |
| 2637 | while (cur < array_size) { |
| 2638 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2639 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2640 | |
| 2641 | len = sizeof(*disk_key); |
| 2642 | |
| 2643 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2644 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2645 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2646 | len += btrfs_chunk_item_size(num_stripes); |
| 2647 | } else { |
| 2648 | ret = -EIO; |
| 2649 | break; |
| 2650 | } |
| 2651 | if (key.objectid == chunk_objectid && |
| 2652 | key.offset == chunk_offset) { |
| 2653 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2654 | array_size -= len; |
| 2655 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2656 | } else { |
| 2657 | ptr += len; |
| 2658 | cur += len; |
| 2659 | } |
| 2660 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2661 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2662 | return ret; |
| 2663 | } |
| 2664 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2665 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2666 | struct btrfs_root *root, u64 chunk_offset) |
| 2667 | { |
| 2668 | struct extent_map_tree *em_tree; |
| 2669 | struct extent_map *em; |
| 2670 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2671 | struct map_lookup *map; |
| 2672 | u64 dev_extent_len = 0; |
| 2673 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2674 | int i, ret = 0; |
| 2675 | |
| 2676 | /* Just in case */ |
| 2677 | root = root->fs_info->chunk_root; |
| 2678 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2679 | |
| 2680 | read_lock(&em_tree->lock); |
| 2681 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2682 | read_unlock(&em_tree->lock); |
| 2683 | |
| 2684 | if (!em || em->start > chunk_offset || |
| 2685 | em->start + em->len < chunk_offset) { |
| 2686 | /* |
| 2687 | * This is a logic error, but we don't want to just rely on the |
| 2688 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2689 | * do anything we still error out. |
| 2690 | */ |
| 2691 | ASSERT(0); |
| 2692 | if (em) |
| 2693 | free_extent_map(em); |
| 2694 | return -EINVAL; |
| 2695 | } |
| 2696 | map = (struct map_lookup *)em->bdev; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2697 | lock_chunks(root->fs_info->chunk_root); |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 2698 | check_system_chunk(trans, extent_root, map->type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2699 | unlock_chunks(root->fs_info->chunk_root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2700 | |
| 2701 | for (i = 0; i < map->num_stripes; i++) { |
| 2702 | struct btrfs_device *device = map->stripes[i].dev; |
| 2703 | ret = btrfs_free_dev_extent(trans, device, |
| 2704 | map->stripes[i].physical, |
| 2705 | &dev_extent_len); |
| 2706 | if (ret) { |
| 2707 | btrfs_abort_transaction(trans, root, ret); |
| 2708 | goto out; |
| 2709 | } |
| 2710 | |
| 2711 | if (device->bytes_used > 0) { |
| 2712 | lock_chunks(root); |
| 2713 | btrfs_device_set_bytes_used(device, |
| 2714 | device->bytes_used - dev_extent_len); |
| 2715 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2716 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2717 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2718 | btrfs_clear_space_info_full(root->fs_info); |
| 2719 | unlock_chunks(root); |
| 2720 | } |
| 2721 | |
| 2722 | if (map->stripes[i].dev) { |
| 2723 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2724 | if (ret) { |
| 2725 | btrfs_abort_transaction(trans, root, ret); |
| 2726 | goto out; |
| 2727 | } |
| 2728 | } |
| 2729 | } |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2730 | ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2731 | if (ret) { |
| 2732 | btrfs_abort_transaction(trans, root, ret); |
| 2733 | goto out; |
| 2734 | } |
| 2735 | |
| 2736 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2737 | |
| 2738 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2739 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2740 | if (ret) { |
| 2741 | btrfs_abort_transaction(trans, root, ret); |
| 2742 | goto out; |
| 2743 | } |
| 2744 | } |
| 2745 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2746 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2747 | if (ret) { |
| 2748 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2749 | goto out; |
| 2750 | } |
| 2751 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2752 | out: |
| 2753 | /* once for us */ |
| 2754 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2755 | return ret; |
| 2756 | } |
| 2757 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2758 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2759 | u64 chunk_objectid, |
| 2760 | u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2761 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2762 | struct btrfs_root *extent_root; |
| 2763 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2764 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2765 | |
| 2766 | root = root->fs_info->chunk_root; |
| 2767 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2768 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 2769 | /* |
| 2770 | * Prevent races with automatic removal of unused block groups. |
| 2771 | * After we relocate and before we remove the chunk with offset |
| 2772 | * chunk_offset, automatic removal of the block group can kick in, |
| 2773 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2774 | * |
| 2775 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2776 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2777 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2778 | * we release the path used to search the chunk/dev tree and before |
| 2779 | * the current task acquires this mutex and calls us. |
| 2780 | */ |
| 2781 | ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex)); |
| 2782 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2783 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2784 | if (ret) |
| 2785 | return -ENOSPC; |
| 2786 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2787 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2788 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2789 | if (ret) |
| 2790 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2791 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2792 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2793 | if (IS_ERR(trans)) { |
| 2794 | ret = PTR_ERR(trans); |
| 2795 | btrfs_std_error(root->fs_info, ret); |
| 2796 | return ret; |
| 2797 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2798 | |
| 2799 | /* |
| 2800 | * step two, delete the device extents and the |
| 2801 | * chunk tree entries |
| 2802 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2803 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2804 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2805 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2806 | } |
| 2807 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2808 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2809 | { |
| 2810 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2811 | struct btrfs_path *path; |
| 2812 | struct extent_buffer *leaf; |
| 2813 | struct btrfs_chunk *chunk; |
| 2814 | struct btrfs_key key; |
| 2815 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2816 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2817 | bool retried = false; |
| 2818 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2819 | int ret; |
| 2820 | |
| 2821 | path = btrfs_alloc_path(); |
| 2822 | if (!path) |
| 2823 | return -ENOMEM; |
| 2824 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2825 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2826 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2827 | key.offset = (u64)-1; |
| 2828 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2829 | |
| 2830 | while (1) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 2831 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2832 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 2833 | if (ret < 0) { |
| 2834 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2835 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 2836 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2837 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2838 | |
| 2839 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2840 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 2841 | if (ret) |
| 2842 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2843 | if (ret < 0) |
| 2844 | goto error; |
| 2845 | if (ret > 0) |
| 2846 | break; |
| 2847 | |
| 2848 | leaf = path->nodes[0]; |
| 2849 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2850 | |
| 2851 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2852 | struct btrfs_chunk); |
| 2853 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2854 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2855 | |
| 2856 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2857 | ret = btrfs_relocate_chunk(chunk_root, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2858 | found_key.objectid, |
| 2859 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2860 | if (ret == -ENOSPC) |
| 2861 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2862 | else |
| 2863 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2864 | } |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 2865 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2866 | |
| 2867 | if (found_key.offset == 0) |
| 2868 | break; |
| 2869 | key.offset = found_key.offset - 1; |
| 2870 | } |
| 2871 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2872 | if (failed && !retried) { |
| 2873 | failed = 0; |
| 2874 | retried = true; |
| 2875 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2876 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2877 | ret = -ENOSPC; |
| 2878 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2879 | error: |
| 2880 | btrfs_free_path(path); |
| 2881 | return ret; |
| 2882 | } |
| 2883 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2884 | static int insert_balance_item(struct btrfs_root *root, |
| 2885 | struct btrfs_balance_control *bctl) |
| 2886 | { |
| 2887 | struct btrfs_trans_handle *trans; |
| 2888 | struct btrfs_balance_item *item; |
| 2889 | struct btrfs_disk_balance_args disk_bargs; |
| 2890 | struct btrfs_path *path; |
| 2891 | struct extent_buffer *leaf; |
| 2892 | struct btrfs_key key; |
| 2893 | int ret, err; |
| 2894 | |
| 2895 | path = btrfs_alloc_path(); |
| 2896 | if (!path) |
| 2897 | return -ENOMEM; |
| 2898 | |
| 2899 | trans = btrfs_start_transaction(root, 0); |
| 2900 | if (IS_ERR(trans)) { |
| 2901 | btrfs_free_path(path); |
| 2902 | return PTR_ERR(trans); |
| 2903 | } |
| 2904 | |
| 2905 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2906 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2907 | key.offset = 0; |
| 2908 | |
| 2909 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2910 | sizeof(*item)); |
| 2911 | if (ret) |
| 2912 | goto out; |
| 2913 | |
| 2914 | leaf = path->nodes[0]; |
| 2915 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2916 | |
| 2917 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2918 | |
| 2919 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2920 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2921 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2922 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2923 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2924 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2925 | |
| 2926 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2927 | |
| 2928 | btrfs_mark_buffer_dirty(leaf); |
| 2929 | out: |
| 2930 | btrfs_free_path(path); |
| 2931 | err = btrfs_commit_transaction(trans, root); |
| 2932 | if (err && !ret) |
| 2933 | ret = err; |
| 2934 | return ret; |
| 2935 | } |
| 2936 | |
| 2937 | static int del_balance_item(struct btrfs_root *root) |
| 2938 | { |
| 2939 | struct btrfs_trans_handle *trans; |
| 2940 | struct btrfs_path *path; |
| 2941 | struct btrfs_key key; |
| 2942 | int ret, err; |
| 2943 | |
| 2944 | path = btrfs_alloc_path(); |
| 2945 | if (!path) |
| 2946 | return -ENOMEM; |
| 2947 | |
| 2948 | trans = btrfs_start_transaction(root, 0); |
| 2949 | if (IS_ERR(trans)) { |
| 2950 | btrfs_free_path(path); |
| 2951 | return PTR_ERR(trans); |
| 2952 | } |
| 2953 | |
| 2954 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2955 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2956 | key.offset = 0; |
| 2957 | |
| 2958 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2959 | if (ret < 0) |
| 2960 | goto out; |
| 2961 | if (ret > 0) { |
| 2962 | ret = -ENOENT; |
| 2963 | goto out; |
| 2964 | } |
| 2965 | |
| 2966 | ret = btrfs_del_item(trans, root, path); |
| 2967 | out: |
| 2968 | btrfs_free_path(path); |
| 2969 | err = btrfs_commit_transaction(trans, root); |
| 2970 | if (err && !ret) |
| 2971 | ret = err; |
| 2972 | return ret; |
| 2973 | } |
| 2974 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2975 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2976 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2977 | * resume after balance was interrupted. |
| 2978 | */ |
| 2979 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2980 | { |
| 2981 | /* |
| 2982 | * Turn on soft mode for chunk types that were being converted. |
| 2983 | */ |
| 2984 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2985 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2986 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2987 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2988 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2989 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2990 | |
| 2991 | /* |
| 2992 | * Turn on usage filter if is not already used. The idea is |
| 2993 | * that chunks that we have already balanced should be |
| 2994 | * reasonably full. Don't do it for chunks that are being |
| 2995 | * converted - that will keep us from relocating unconverted |
| 2996 | * (albeit full) chunks. |
| 2997 | */ |
| 2998 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2999 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3000 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3001 | bctl->data.usage = 90; |
| 3002 | } |
| 3003 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3004 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3005 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3006 | bctl->sys.usage = 90; |
| 3007 | } |
| 3008 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3009 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3010 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3011 | bctl->meta.usage = 90; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3016 | * Should be called with both balance and volume mutexes held to |
| 3017 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 3018 | * restriper. Same goes for unset_balance_control. |
| 3019 | */ |
| 3020 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 3021 | { |
| 3022 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 3023 | |
| 3024 | BUG_ON(fs_info->balance_ctl); |
| 3025 | |
| 3026 | spin_lock(&fs_info->balance_lock); |
| 3027 | fs_info->balance_ctl = bctl; |
| 3028 | spin_unlock(&fs_info->balance_lock); |
| 3029 | } |
| 3030 | |
| 3031 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 3032 | { |
| 3033 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3034 | |
| 3035 | BUG_ON(!fs_info->balance_ctl); |
| 3036 | |
| 3037 | spin_lock(&fs_info->balance_lock); |
| 3038 | fs_info->balance_ctl = NULL; |
| 3039 | spin_unlock(&fs_info->balance_lock); |
| 3040 | |
| 3041 | kfree(bctl); |
| 3042 | } |
| 3043 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3044 | /* |
| 3045 | * Balance filters. Return 1 if chunk should be filtered out |
| 3046 | * (should not be balanced). |
| 3047 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3048 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3049 | struct btrfs_balance_args *bargs) |
| 3050 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3051 | chunk_type = chunk_to_extended(chunk_type) & |
| 3052 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3053 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3054 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3055 | return 0; |
| 3056 | |
| 3057 | return 1; |
| 3058 | } |
| 3059 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3060 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 3061 | struct btrfs_balance_args *bargs) |
| 3062 | { |
| 3063 | struct btrfs_block_group_cache *cache; |
| 3064 | u64 chunk_used, user_thresh; |
| 3065 | int ret = 1; |
| 3066 | |
| 3067 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3068 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3069 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3070 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3071 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3072 | else if (bargs->usage > 100) |
| 3073 | user_thresh = cache->key.offset; |
| 3074 | else |
| 3075 | user_thresh = div_factor_fine(cache->key.offset, |
| 3076 | bargs->usage); |
| 3077 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3078 | if (chunk_used < user_thresh) |
| 3079 | ret = 0; |
| 3080 | |
| 3081 | btrfs_put_block_group(cache); |
| 3082 | return ret; |
| 3083 | } |
| 3084 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3085 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3086 | struct btrfs_chunk *chunk, |
| 3087 | struct btrfs_balance_args *bargs) |
| 3088 | { |
| 3089 | struct btrfs_stripe *stripe; |
| 3090 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3091 | int i; |
| 3092 | |
| 3093 | for (i = 0; i < num_stripes; i++) { |
| 3094 | stripe = btrfs_stripe_nr(chunk, i); |
| 3095 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3096 | return 0; |
| 3097 | } |
| 3098 | |
| 3099 | return 1; |
| 3100 | } |
| 3101 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3102 | /* [pstart, pend) */ |
| 3103 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3104 | struct btrfs_chunk *chunk, |
| 3105 | u64 chunk_offset, |
| 3106 | struct btrfs_balance_args *bargs) |
| 3107 | { |
| 3108 | struct btrfs_stripe *stripe; |
| 3109 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3110 | u64 stripe_offset; |
| 3111 | u64 stripe_length; |
| 3112 | int factor; |
| 3113 | int i; |
| 3114 | |
| 3115 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3116 | return 0; |
| 3117 | |
| 3118 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3119 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3120 | factor = num_stripes / 2; |
| 3121 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3122 | factor = num_stripes - 1; |
| 3123 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3124 | factor = num_stripes - 2; |
| 3125 | } else { |
| 3126 | factor = num_stripes; |
| 3127 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3128 | |
| 3129 | for (i = 0; i < num_stripes; i++) { |
| 3130 | stripe = btrfs_stripe_nr(chunk, i); |
| 3131 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3132 | continue; |
| 3133 | |
| 3134 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3135 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3136 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3137 | |
| 3138 | if (stripe_offset < bargs->pend && |
| 3139 | stripe_offset + stripe_length > bargs->pstart) |
| 3140 | return 0; |
| 3141 | } |
| 3142 | |
| 3143 | return 1; |
| 3144 | } |
| 3145 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3146 | /* [vstart, vend) */ |
| 3147 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3148 | struct btrfs_chunk *chunk, |
| 3149 | u64 chunk_offset, |
| 3150 | struct btrfs_balance_args *bargs) |
| 3151 | { |
| 3152 | if (chunk_offset < bargs->vend && |
| 3153 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3154 | /* at least part of the chunk is inside this vrange */ |
| 3155 | return 0; |
| 3156 | |
| 3157 | return 1; |
| 3158 | } |
| 3159 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3160 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3161 | struct btrfs_balance_args *bargs) |
| 3162 | { |
| 3163 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3164 | return 0; |
| 3165 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3166 | chunk_type = chunk_to_extended(chunk_type) & |
| 3167 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3168 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3169 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3170 | return 1; |
| 3171 | |
| 3172 | return 0; |
| 3173 | } |
| 3174 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3175 | static int should_balance_chunk(struct btrfs_root *root, |
| 3176 | struct extent_buffer *leaf, |
| 3177 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3178 | { |
| 3179 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3180 | struct btrfs_balance_args *bargs = NULL; |
| 3181 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3182 | |
| 3183 | /* type filter */ |
| 3184 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3185 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3186 | return 0; |
| 3187 | } |
| 3188 | |
| 3189 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3190 | bargs = &bctl->data; |
| 3191 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3192 | bargs = &bctl->sys; |
| 3193 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3194 | bargs = &bctl->meta; |
| 3195 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3196 | /* profiles filter */ |
| 3197 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3198 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3199 | return 0; |
| 3200 | } |
| 3201 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3202 | /* usage filter */ |
| 3203 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3204 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3205 | return 0; |
| 3206 | } |
| 3207 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3208 | /* devid filter */ |
| 3209 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3210 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3211 | return 0; |
| 3212 | } |
| 3213 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3214 | /* drange filter, makes sense only with devid filter */ |
| 3215 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3216 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3217 | return 0; |
| 3218 | } |
| 3219 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3220 | /* vrange filter */ |
| 3221 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3222 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3223 | return 0; |
| 3224 | } |
| 3225 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3226 | /* soft profile changing mode */ |
| 3227 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3228 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3229 | return 0; |
| 3230 | } |
| 3231 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3232 | /* |
| 3233 | * limited by count, must be the last filter |
| 3234 | */ |
| 3235 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3236 | if (bargs->limit == 0) |
| 3237 | return 0; |
| 3238 | else |
| 3239 | bargs->limit--; |
| 3240 | } |
| 3241 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3242 | return 1; |
| 3243 | } |
| 3244 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3245 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3246 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3247 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3248 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3249 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3250 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3251 | struct btrfs_device *device; |
| 3252 | u64 old_size; |
| 3253 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3254 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3255 | struct btrfs_path *path; |
| 3256 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3257 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3258 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3259 | struct extent_buffer *leaf; |
| 3260 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3261 | int ret; |
| 3262 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3263 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3264 | u64 limit_data = bctl->data.limit; |
| 3265 | u64 limit_meta = bctl->meta.limit; |
| 3266 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3267 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3268 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3269 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3270 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3271 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3272 | size_to_free = div_factor(old_size, 1); |
| 3273 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3274 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3275 | btrfs_device_get_total_bytes(device) - |
| 3276 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3277 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3278 | continue; |
| 3279 | |
| 3280 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3281 | if (ret == -ENOSPC) |
| 3282 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3283 | BUG_ON(ret); |
| 3284 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3285 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3286 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3287 | |
| 3288 | ret = btrfs_grow_device(trans, device, old_size); |
| 3289 | BUG_ON(ret); |
| 3290 | |
| 3291 | btrfs_end_transaction(trans, dev_root); |
| 3292 | } |
| 3293 | |
| 3294 | /* step two, relocate all the chunks */ |
| 3295 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3296 | if (!path) { |
| 3297 | ret = -ENOMEM; |
| 3298 | goto error; |
| 3299 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3300 | |
| 3301 | /* zero out stat counters */ |
| 3302 | spin_lock(&fs_info->balance_lock); |
| 3303 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3304 | spin_unlock(&fs_info->balance_lock); |
| 3305 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3306 | if (!counting) { |
| 3307 | bctl->data.limit = limit_data; |
| 3308 | bctl->meta.limit = limit_meta; |
| 3309 | bctl->sys.limit = limit_sys; |
| 3310 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3311 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3312 | key.offset = (u64)-1; |
| 3313 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3314 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3315 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3316 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3317 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3318 | ret = -ECANCELED; |
| 3319 | goto error; |
| 3320 | } |
| 3321 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3322 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3323 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3324 | if (ret < 0) { |
| 3325 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3326 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3327 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3328 | |
| 3329 | /* |
| 3330 | * this shouldn't happen, it means the last relocate |
| 3331 | * failed |
| 3332 | */ |
| 3333 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3334 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3335 | |
| 3336 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3337 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3338 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3339 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3340 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3341 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3342 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3343 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3344 | leaf = path->nodes[0]; |
| 3345 | slot = path->slots[0]; |
| 3346 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3347 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3348 | if (found_key.objectid != key.objectid) { |
| 3349 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3350 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3351 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3352 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3353 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3354 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3355 | if (!counting) { |
| 3356 | spin_lock(&fs_info->balance_lock); |
| 3357 | bctl->stat.considered++; |
| 3358 | spin_unlock(&fs_info->balance_lock); |
| 3359 | } |
| 3360 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3361 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3362 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3363 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3364 | if (!ret) { |
| 3365 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3366 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3367 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3368 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3369 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3370 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3371 | spin_lock(&fs_info->balance_lock); |
| 3372 | bctl->stat.expected++; |
| 3373 | spin_unlock(&fs_info->balance_lock); |
| 3374 | goto loop; |
| 3375 | } |
| 3376 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3377 | ret = btrfs_relocate_chunk(chunk_root, |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3378 | found_key.objectid, |
| 3379 | found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 3380 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3381 | if (ret && ret != -ENOSPC) |
| 3382 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3383 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3384 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3385 | } else { |
| 3386 | spin_lock(&fs_info->balance_lock); |
| 3387 | bctl->stat.completed++; |
| 3388 | spin_unlock(&fs_info->balance_lock); |
| 3389 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3390 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3391 | if (found_key.offset == 0) |
| 3392 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3393 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3394 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3395 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3396 | if (counting) { |
| 3397 | btrfs_release_path(path); |
| 3398 | counting = false; |
| 3399 | goto again; |
| 3400 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3401 | error: |
| 3402 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3403 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3404 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3405 | enospc_errors); |
| 3406 | if (!ret) |
| 3407 | ret = -ENOSPC; |
| 3408 | } |
| 3409 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3410 | return ret; |
| 3411 | } |
| 3412 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3413 | /** |
| 3414 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3415 | * @flags: profile to validate |
| 3416 | * @extended: if true @flags is treated as an extended profile |
| 3417 | */ |
| 3418 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3419 | { |
| 3420 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3421 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3422 | |
| 3423 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3424 | |
| 3425 | /* 1) check that all other bits are zeroed */ |
| 3426 | if (flags & ~mask) |
| 3427 | return 0; |
| 3428 | |
| 3429 | /* 2) see if profile is reduced */ |
| 3430 | if (flags == 0) |
| 3431 | return !extended; /* "0" is valid for usual profiles */ |
| 3432 | |
| 3433 | /* true if exactly one bit set */ |
| 3434 | return (flags & (flags - 1)) == 0; |
| 3435 | } |
| 3436 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3437 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3438 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3439 | /* cancel requested || normal exit path */ |
| 3440 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3441 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3442 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3443 | } |
| 3444 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3445 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3446 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3447 | int ret; |
| 3448 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3449 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3450 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3451 | if (ret) |
| 3452 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3453 | |
| 3454 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3455 | } |
| 3456 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3457 | /* |
| 3458 | * Should be called with both balance and volume mutexes held |
| 3459 | */ |
| 3460 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3461 | struct btrfs_ioctl_balance_args *bargs) |
| 3462 | { |
| 3463 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3464 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3465 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3466 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3467 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3468 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3469 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3470 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3471 | atomic_read(&fs_info->balance_pause_req) || |
| 3472 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3473 | ret = -EINVAL; |
| 3474 | goto out; |
| 3475 | } |
| 3476 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3477 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3478 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3479 | mixed = 1; |
| 3480 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3481 | /* |
| 3482 | * In case of mixed groups both data and meta should be picked, |
| 3483 | * and identical options should be given for both of them. |
| 3484 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3485 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3486 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3487 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3488 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3489 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3490 | btrfs_err(fs_info, "with mixed groups data and " |
| 3491 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3492 | ret = -EINVAL; |
| 3493 | goto out; |
| 3494 | } |
| 3495 | } |
| 3496 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3497 | num_devices = fs_info->fs_devices->num_devices; |
| 3498 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3499 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3500 | BUG_ON(num_devices < 1); |
| 3501 | num_devices--; |
| 3502 | } |
| 3503 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3504 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3505 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3506 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3507 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3508 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3509 | if (num_devices > 2) |
| 3510 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3511 | if (num_devices > 3) |
| 3512 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3513 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3514 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3515 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3516 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3517 | btrfs_err(fs_info, "unable to start balance with target " |
| 3518 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3519 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3520 | ret = -EINVAL; |
| 3521 | goto out; |
| 3522 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3523 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3524 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3525 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3526 | btrfs_err(fs_info, |
| 3527 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3528 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3529 | ret = -EINVAL; |
| 3530 | goto out; |
| 3531 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3532 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3533 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3534 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3535 | btrfs_err(fs_info, |
| 3536 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3537 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3538 | ret = -EINVAL; |
| 3539 | goto out; |
| 3540 | } |
| 3541 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3542 | /* allow dup'ed data chunks only in mixed mode */ |
| 3543 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3544 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3545 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3546 | ret = -EINVAL; |
| 3547 | goto out; |
| 3548 | } |
| 3549 | |
| 3550 | /* allow to reduce meta or sys integrity only if force set */ |
| 3551 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3552 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3553 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3554 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3555 | do { |
| 3556 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3557 | |
| 3558 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3559 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3560 | !(bctl->sys.target & allowed)) || |
| 3561 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3562 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3563 | !(bctl->meta.target & allowed))) { |
| 3564 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3565 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3566 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3567 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3568 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3569 | ret = -EINVAL; |
| 3570 | goto out; |
| 3571 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3572 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3573 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3574 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3575 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3576 | int num_tolerated_disk_barrier_failures; |
| 3577 | u64 target = bctl->sys.target; |
| 3578 | |
| 3579 | num_tolerated_disk_barrier_failures = |
| 3580 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3581 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3582 | (target & |
| 3583 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3584 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3585 | num_tolerated_disk_barrier_failures = 0; |
| 3586 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3587 | (target & |
| 3588 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3589 | num_tolerated_disk_barrier_failures = 1; |
| 3590 | |
| 3591 | fs_info->num_tolerated_disk_barrier_failures = |
| 3592 | num_tolerated_disk_barrier_failures; |
| 3593 | } |
| 3594 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3595 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3596 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3597 | goto out; |
| 3598 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3599 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3600 | BUG_ON(ret == -EEXIST); |
| 3601 | set_balance_control(bctl); |
| 3602 | } else { |
| 3603 | BUG_ON(ret != -EEXIST); |
| 3604 | spin_lock(&fs_info->balance_lock); |
| 3605 | update_balance_args(bctl); |
| 3606 | spin_unlock(&fs_info->balance_lock); |
| 3607 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3608 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3609 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3610 | mutex_unlock(&fs_info->balance_mutex); |
| 3611 | |
| 3612 | ret = __btrfs_balance(fs_info); |
| 3613 | |
| 3614 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3615 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3616 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3617 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3618 | fs_info->num_tolerated_disk_barrier_failures = |
| 3619 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3620 | } |
| 3621 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3622 | if (bargs) { |
| 3623 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3624 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3625 | } |
| 3626 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3627 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3628 | balance_need_close(fs_info)) { |
| 3629 | __cancel_balance(fs_info); |
| 3630 | } |
| 3631 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3632 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3633 | |
| 3634 | return ret; |
| 3635 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3636 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3637 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3638 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3639 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3640 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3641 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3642 | return ret; |
| 3643 | } |
| 3644 | |
| 3645 | static int balance_kthread(void *data) |
| 3646 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3647 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3648 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3649 | |
| 3650 | mutex_lock(&fs_info->volume_mutex); |
| 3651 | mutex_lock(&fs_info->balance_mutex); |
| 3652 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3653 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3654 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3655 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3656 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3657 | |
| 3658 | mutex_unlock(&fs_info->balance_mutex); |
| 3659 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3660 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3661 | return ret; |
| 3662 | } |
| 3663 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3664 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3665 | { |
| 3666 | struct task_struct *tsk; |
| 3667 | |
| 3668 | spin_lock(&fs_info->balance_lock); |
| 3669 | if (!fs_info->balance_ctl) { |
| 3670 | spin_unlock(&fs_info->balance_lock); |
| 3671 | return 0; |
| 3672 | } |
| 3673 | spin_unlock(&fs_info->balance_lock); |
| 3674 | |
| 3675 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3676 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3677 | return 0; |
| 3678 | } |
| 3679 | |
| 3680 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3681 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3682 | } |
| 3683 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3684 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3685 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3686 | struct btrfs_balance_control *bctl; |
| 3687 | struct btrfs_balance_item *item; |
| 3688 | struct btrfs_disk_balance_args disk_bargs; |
| 3689 | struct btrfs_path *path; |
| 3690 | struct extent_buffer *leaf; |
| 3691 | struct btrfs_key key; |
| 3692 | int ret; |
| 3693 | |
| 3694 | path = btrfs_alloc_path(); |
| 3695 | if (!path) |
| 3696 | return -ENOMEM; |
| 3697 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3698 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3699 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3700 | key.offset = 0; |
| 3701 | |
| 3702 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3703 | if (ret < 0) |
| 3704 | goto out; |
| 3705 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3706 | ret = 0; |
| 3707 | goto out; |
| 3708 | } |
| 3709 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3710 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3711 | if (!bctl) { |
| 3712 | ret = -ENOMEM; |
| 3713 | goto out; |
| 3714 | } |
| 3715 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3716 | leaf = path->nodes[0]; |
| 3717 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3718 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3719 | bctl->fs_info = fs_info; |
| 3720 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3721 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3722 | |
| 3723 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3724 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3725 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3726 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3727 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3728 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3729 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3730 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3731 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3732 | mutex_lock(&fs_info->volume_mutex); |
| 3733 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3734 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3735 | set_balance_control(bctl); |
| 3736 | |
| 3737 | mutex_unlock(&fs_info->balance_mutex); |
| 3738 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3739 | out: |
| 3740 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3741 | return ret; |
| 3742 | } |
| 3743 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3744 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3745 | { |
| 3746 | int ret = 0; |
| 3747 | |
| 3748 | mutex_lock(&fs_info->balance_mutex); |
| 3749 | if (!fs_info->balance_ctl) { |
| 3750 | mutex_unlock(&fs_info->balance_mutex); |
| 3751 | return -ENOTCONN; |
| 3752 | } |
| 3753 | |
| 3754 | if (atomic_read(&fs_info->balance_running)) { |
| 3755 | atomic_inc(&fs_info->balance_pause_req); |
| 3756 | mutex_unlock(&fs_info->balance_mutex); |
| 3757 | |
| 3758 | wait_event(fs_info->balance_wait_q, |
| 3759 | atomic_read(&fs_info->balance_running) == 0); |
| 3760 | |
| 3761 | mutex_lock(&fs_info->balance_mutex); |
| 3762 | /* we are good with balance_ctl ripped off from under us */ |
| 3763 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3764 | atomic_dec(&fs_info->balance_pause_req); |
| 3765 | } else { |
| 3766 | ret = -ENOTCONN; |
| 3767 | } |
| 3768 | |
| 3769 | mutex_unlock(&fs_info->balance_mutex); |
| 3770 | return ret; |
| 3771 | } |
| 3772 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3773 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3774 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3775 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3776 | return -EROFS; |
| 3777 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3778 | mutex_lock(&fs_info->balance_mutex); |
| 3779 | if (!fs_info->balance_ctl) { |
| 3780 | mutex_unlock(&fs_info->balance_mutex); |
| 3781 | return -ENOTCONN; |
| 3782 | } |
| 3783 | |
| 3784 | atomic_inc(&fs_info->balance_cancel_req); |
| 3785 | /* |
| 3786 | * if we are running just wait and return, balance item is |
| 3787 | * deleted in btrfs_balance in this case |
| 3788 | */ |
| 3789 | if (atomic_read(&fs_info->balance_running)) { |
| 3790 | mutex_unlock(&fs_info->balance_mutex); |
| 3791 | wait_event(fs_info->balance_wait_q, |
| 3792 | atomic_read(&fs_info->balance_running) == 0); |
| 3793 | mutex_lock(&fs_info->balance_mutex); |
| 3794 | } else { |
| 3795 | /* __cancel_balance needs volume_mutex */ |
| 3796 | mutex_unlock(&fs_info->balance_mutex); |
| 3797 | mutex_lock(&fs_info->volume_mutex); |
| 3798 | mutex_lock(&fs_info->balance_mutex); |
| 3799 | |
| 3800 | if (fs_info->balance_ctl) |
| 3801 | __cancel_balance(fs_info); |
| 3802 | |
| 3803 | mutex_unlock(&fs_info->volume_mutex); |
| 3804 | } |
| 3805 | |
| 3806 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3807 | atomic_dec(&fs_info->balance_cancel_req); |
| 3808 | mutex_unlock(&fs_info->balance_mutex); |
| 3809 | return 0; |
| 3810 | } |
| 3811 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3812 | static int btrfs_uuid_scan_kthread(void *data) |
| 3813 | { |
| 3814 | struct btrfs_fs_info *fs_info = data; |
| 3815 | struct btrfs_root *root = fs_info->tree_root; |
| 3816 | struct btrfs_key key; |
| 3817 | struct btrfs_key max_key; |
| 3818 | struct btrfs_path *path = NULL; |
| 3819 | int ret = 0; |
| 3820 | struct extent_buffer *eb; |
| 3821 | int slot; |
| 3822 | struct btrfs_root_item root_item; |
| 3823 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3824 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3825 | |
| 3826 | path = btrfs_alloc_path(); |
| 3827 | if (!path) { |
| 3828 | ret = -ENOMEM; |
| 3829 | goto out; |
| 3830 | } |
| 3831 | |
| 3832 | key.objectid = 0; |
| 3833 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3834 | key.offset = 0; |
| 3835 | |
| 3836 | max_key.objectid = (u64)-1; |
| 3837 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3838 | max_key.offset = (u64)-1; |
| 3839 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3840 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3841 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3842 | if (ret) { |
| 3843 | if (ret > 0) |
| 3844 | ret = 0; |
| 3845 | break; |
| 3846 | } |
| 3847 | |
| 3848 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3849 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3850 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3851 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3852 | goto skip; |
| 3853 | |
| 3854 | eb = path->nodes[0]; |
| 3855 | slot = path->slots[0]; |
| 3856 | item_size = btrfs_item_size_nr(eb, slot); |
| 3857 | if (item_size < sizeof(root_item)) |
| 3858 | goto skip; |
| 3859 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3860 | read_extent_buffer(eb, &root_item, |
| 3861 | btrfs_item_ptr_offset(eb, slot), |
| 3862 | (int)sizeof(root_item)); |
| 3863 | if (btrfs_root_refs(&root_item) == 0) |
| 3864 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3865 | |
| 3866 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3867 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3868 | if (trans) |
| 3869 | goto update_tree; |
| 3870 | |
| 3871 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3872 | /* |
| 3873 | * 1 - subvol uuid item |
| 3874 | * 1 - received_subvol uuid item |
| 3875 | */ |
| 3876 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3877 | if (IS_ERR(trans)) { |
| 3878 | ret = PTR_ERR(trans); |
| 3879 | break; |
| 3880 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3881 | continue; |
| 3882 | } else { |
| 3883 | goto skip; |
| 3884 | } |
| 3885 | update_tree: |
| 3886 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3887 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3888 | root_item.uuid, |
| 3889 | BTRFS_UUID_KEY_SUBVOL, |
| 3890 | key.objectid); |
| 3891 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3892 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3893 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3894 | break; |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3899 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3900 | root_item.received_uuid, |
| 3901 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3902 | key.objectid); |
| 3903 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3904 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3905 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3906 | break; |
| 3907 | } |
| 3908 | } |
| 3909 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3910 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3911 | if (trans) { |
| 3912 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3913 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3914 | if (ret) |
| 3915 | break; |
| 3916 | } |
| 3917 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3918 | btrfs_release_path(path); |
| 3919 | if (key.offset < (u64)-1) { |
| 3920 | key.offset++; |
| 3921 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3922 | key.offset = 0; |
| 3923 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3924 | } else if (key.objectid < (u64)-1) { |
| 3925 | key.offset = 0; |
| 3926 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3927 | key.objectid++; |
| 3928 | } else { |
| 3929 | break; |
| 3930 | } |
| 3931 | cond_resched(); |
| 3932 | } |
| 3933 | |
| 3934 | out: |
| 3935 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3936 | if (trans && !IS_ERR(trans)) |
| 3937 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3938 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3939 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3940 | else |
| 3941 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3942 | up(&fs_info->uuid_tree_rescan_sem); |
| 3943 | return 0; |
| 3944 | } |
| 3945 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3946 | /* |
| 3947 | * Callback for btrfs_uuid_tree_iterate(). |
| 3948 | * returns: |
| 3949 | * 0 check succeeded, the entry is not outdated. |
| 3950 | * < 0 if an error occured. |
| 3951 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3952 | */ |
| 3953 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3954 | u8 *uuid, u8 type, u64 subid) |
| 3955 | { |
| 3956 | struct btrfs_key key; |
| 3957 | int ret = 0; |
| 3958 | struct btrfs_root *subvol_root; |
| 3959 | |
| 3960 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3961 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3962 | goto out; |
| 3963 | |
| 3964 | key.objectid = subid; |
| 3965 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3966 | key.offset = (u64)-1; |
| 3967 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3968 | if (IS_ERR(subvol_root)) { |
| 3969 | ret = PTR_ERR(subvol_root); |
| 3970 | if (ret == -ENOENT) |
| 3971 | ret = 1; |
| 3972 | goto out; |
| 3973 | } |
| 3974 | |
| 3975 | switch (type) { |
| 3976 | case BTRFS_UUID_KEY_SUBVOL: |
| 3977 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3978 | ret = 1; |
| 3979 | break; |
| 3980 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3981 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3982 | BTRFS_UUID_SIZE)) |
| 3983 | ret = 1; |
| 3984 | break; |
| 3985 | } |
| 3986 | |
| 3987 | out: |
| 3988 | return ret; |
| 3989 | } |
| 3990 | |
| 3991 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3992 | { |
| 3993 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3994 | int ret; |
| 3995 | |
| 3996 | /* |
| 3997 | * 1st step is to iterate through the existing UUID tree and |
| 3998 | * to delete all entries that contain outdated data. |
| 3999 | * 2nd step is to add all missing entries to the UUID tree. |
| 4000 | */ |
| 4001 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4002 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4003 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4004 | up(&fs_info->uuid_tree_rescan_sem); |
| 4005 | return ret; |
| 4006 | } |
| 4007 | return btrfs_uuid_scan_kthread(data); |
| 4008 | } |
| 4009 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4010 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4011 | { |
| 4012 | struct btrfs_trans_handle *trans; |
| 4013 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4014 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4015 | struct task_struct *task; |
| 4016 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4017 | |
| 4018 | /* |
| 4019 | * 1 - root node |
| 4020 | * 1 - root item |
| 4021 | */ |
| 4022 | trans = btrfs_start_transaction(tree_root, 2); |
| 4023 | if (IS_ERR(trans)) |
| 4024 | return PTR_ERR(trans); |
| 4025 | |
| 4026 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4027 | BTRFS_UUID_TREE_OBJECTID); |
| 4028 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4029 | ret = PTR_ERR(uuid_root); |
| 4030 | btrfs_abort_transaction(trans, tree_root, ret); |
| 4031 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | fs_info->uuid_root = uuid_root; |
| 4035 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4036 | ret = btrfs_commit_transaction(trans, tree_root); |
| 4037 | if (ret) |
| 4038 | return ret; |
| 4039 | |
| 4040 | down(&fs_info->uuid_tree_rescan_sem); |
| 4041 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4042 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4043 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4044 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4045 | up(&fs_info->uuid_tree_rescan_sem); |
| 4046 | return PTR_ERR(task); |
| 4047 | } |
| 4048 | |
| 4049 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4050 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4051 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4052 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4053 | { |
| 4054 | struct task_struct *task; |
| 4055 | |
| 4056 | down(&fs_info->uuid_tree_rescan_sem); |
| 4057 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4058 | if (IS_ERR(task)) { |
| 4059 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4060 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4061 | up(&fs_info->uuid_tree_rescan_sem); |
| 4062 | return PTR_ERR(task); |
| 4063 | } |
| 4064 | |
| 4065 | return 0; |
| 4066 | } |
| 4067 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4068 | /* |
| 4069 | * shrinking a device means finding all of the device extents past |
| 4070 | * the new size, and then following the back refs to the chunks. |
| 4071 | * The chunk relocation code actually frees the device extent |
| 4072 | */ |
| 4073 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4074 | { |
| 4075 | struct btrfs_trans_handle *trans; |
| 4076 | struct btrfs_root *root = device->dev_root; |
| 4077 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4078 | struct btrfs_path *path; |
| 4079 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4080 | u64 chunk_objectid; |
| 4081 | u64 chunk_offset; |
| 4082 | int ret; |
| 4083 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4084 | int failed = 0; |
| 4085 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4086 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4087 | struct extent_buffer *l; |
| 4088 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4089 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4090 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4091 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 4092 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4093 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4094 | if (device->is_tgtdev_for_dev_replace) |
| 4095 | return -EINVAL; |
| 4096 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4097 | path = btrfs_alloc_path(); |
| 4098 | if (!path) |
| 4099 | return -ENOMEM; |
| 4100 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4101 | path->reada = 2; |
| 4102 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4103 | lock_chunks(root); |
| 4104 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4105 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4106 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4107 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4108 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4109 | root->fs_info->free_chunk_space -= diff; |
| 4110 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4111 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4112 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4113 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4114 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4115 | key.objectid = device->devid; |
| 4116 | key.offset = (u64)-1; |
| 4117 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4118 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4119 | do { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 4120 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4121 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 4122 | if (ret < 0) { |
| 4123 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4124 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 4125 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4126 | |
| 4127 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 4128 | if (ret) |
| 4129 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4130 | if (ret < 0) |
| 4131 | goto done; |
| 4132 | if (ret) { |
| 4133 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4134 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4135 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | l = path->nodes[0]; |
| 4139 | slot = path->slots[0]; |
| 4140 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4141 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4142 | if (key.objectid != device->devid) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 4143 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4144 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4145 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4146 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4147 | |
| 4148 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4149 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4150 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4151 | if (key.offset + length <= new_size) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 4152 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4153 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4154 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4155 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4156 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4157 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 4158 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4159 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4160 | |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 4161 | ret = btrfs_relocate_chunk(root, chunk_objectid, chunk_offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame^] | 4162 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4163 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4164 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4165 | if (ret == -ENOSPC) |
| 4166 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4167 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4168 | |
| 4169 | if (failed && !retried) { |
| 4170 | failed = 0; |
| 4171 | retried = true; |
| 4172 | goto again; |
| 4173 | } else if (failed && retried) { |
| 4174 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4175 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4176 | } |
| 4177 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4178 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4179 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4180 | if (IS_ERR(trans)) { |
| 4181 | ret = PTR_ERR(trans); |
| 4182 | goto done; |
| 4183 | } |
| 4184 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4185 | lock_chunks(root); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4186 | |
| 4187 | /* |
| 4188 | * We checked in the above loop all device extents that were already in |
| 4189 | * the device tree. However before we have updated the device's |
| 4190 | * total_bytes to the new size, we might have had chunk allocations that |
| 4191 | * have not complete yet (new block groups attached to transaction |
| 4192 | * handles), and therefore their device extents were not yet in the |
| 4193 | * device tree and we missed them in the loop above. So if we have any |
| 4194 | * pending chunk using a device extent that overlaps the device range |
| 4195 | * that we can not use anymore, commit the current transaction and |
| 4196 | * repeat the search on the device tree - this way we guarantee we will |
| 4197 | * not have chunks using device extents that end beyond 'new_size'. |
| 4198 | */ |
| 4199 | if (!checked_pending_chunks) { |
| 4200 | u64 start = new_size; |
| 4201 | u64 len = old_size - new_size; |
| 4202 | |
| 4203 | if (contains_pending_extent(trans, device, &start, len)) { |
| 4204 | unlock_chunks(root); |
| 4205 | checked_pending_chunks = true; |
| 4206 | failed = 0; |
| 4207 | retried = false; |
| 4208 | ret = btrfs_commit_transaction(trans, root); |
| 4209 | if (ret) |
| 4210 | goto done; |
| 4211 | goto again; |
| 4212 | } |
| 4213 | } |
| 4214 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4215 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4216 | if (list_empty(&device->resized_list)) |
| 4217 | list_add_tail(&device->resized_list, |
| 4218 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4219 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4220 | WARN_ON(diff > old_total); |
| 4221 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4222 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4223 | |
| 4224 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4225 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4226 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4227 | done: |
| 4228 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4229 | if (ret) { |
| 4230 | lock_chunks(root); |
| 4231 | btrfs_device_set_total_bytes(device, old_size); |
| 4232 | if (device->writeable) |
| 4233 | device->fs_devices->total_rw_bytes += diff; |
| 4234 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4235 | root->fs_info->free_chunk_space += diff; |
| 4236 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4237 | unlock_chunks(root); |
| 4238 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4239 | return ret; |
| 4240 | } |
| 4241 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4242 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4243 | struct btrfs_key *key, |
| 4244 | struct btrfs_chunk *chunk, int item_size) |
| 4245 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4246 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4247 | struct btrfs_disk_key disk_key; |
| 4248 | u32 array_size; |
| 4249 | u8 *ptr; |
| 4250 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4251 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4252 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4253 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4254 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4255 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4256 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4257 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4258 | |
| 4259 | ptr = super_copy->sys_chunk_array + array_size; |
| 4260 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4261 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4262 | ptr += sizeof(disk_key); |
| 4263 | memcpy(ptr, chunk, item_size); |
| 4264 | item_size += sizeof(disk_key); |
| 4265 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4266 | unlock_chunks(root); |
| 4267 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4268 | return 0; |
| 4269 | } |
| 4270 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4271 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4272 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4273 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4274 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4275 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4276 | const struct btrfs_device_info *di_a = a; |
| 4277 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4278 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4279 | if (di_a->max_avail > di_b->max_avail) |
| 4280 | return -1; |
| 4281 | if (di_a->max_avail < di_b->max_avail) |
| 4282 | return 1; |
| 4283 | if (di_a->total_avail > di_b->total_avail) |
| 4284 | return -1; |
| 4285 | if (di_a->total_avail < di_b->total_avail) |
| 4286 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4287 | return 0; |
| 4288 | } |
| 4289 | |
David Sterba | e8c9f18 | 2015-01-02 18:23:10 +0100 | [diff] [blame] | 4290 | static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 4291 | [BTRFS_RAID_RAID10] = { |
| 4292 | .sub_stripes = 2, |
| 4293 | .dev_stripes = 1, |
| 4294 | .devs_max = 0, /* 0 == as many as possible */ |
| 4295 | .devs_min = 4, |
| 4296 | .devs_increment = 2, |
| 4297 | .ncopies = 2, |
| 4298 | }, |
| 4299 | [BTRFS_RAID_RAID1] = { |
| 4300 | .sub_stripes = 1, |
| 4301 | .dev_stripes = 1, |
| 4302 | .devs_max = 2, |
| 4303 | .devs_min = 2, |
| 4304 | .devs_increment = 2, |
| 4305 | .ncopies = 2, |
| 4306 | }, |
| 4307 | [BTRFS_RAID_DUP] = { |
| 4308 | .sub_stripes = 1, |
| 4309 | .dev_stripes = 2, |
| 4310 | .devs_max = 1, |
| 4311 | .devs_min = 1, |
| 4312 | .devs_increment = 1, |
| 4313 | .ncopies = 2, |
| 4314 | }, |
| 4315 | [BTRFS_RAID_RAID0] = { |
| 4316 | .sub_stripes = 1, |
| 4317 | .dev_stripes = 1, |
| 4318 | .devs_max = 0, |
| 4319 | .devs_min = 2, |
| 4320 | .devs_increment = 1, |
| 4321 | .ncopies = 1, |
| 4322 | }, |
| 4323 | [BTRFS_RAID_SINGLE] = { |
| 4324 | .sub_stripes = 1, |
| 4325 | .dev_stripes = 1, |
| 4326 | .devs_max = 1, |
| 4327 | .devs_min = 1, |
| 4328 | .devs_increment = 1, |
| 4329 | .ncopies = 1, |
| 4330 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4331 | [BTRFS_RAID_RAID5] = { |
| 4332 | .sub_stripes = 1, |
| 4333 | .dev_stripes = 1, |
| 4334 | .devs_max = 0, |
| 4335 | .devs_min = 2, |
| 4336 | .devs_increment = 1, |
| 4337 | .ncopies = 2, |
| 4338 | }, |
| 4339 | [BTRFS_RAID_RAID6] = { |
| 4340 | .sub_stripes = 1, |
| 4341 | .dev_stripes = 1, |
| 4342 | .devs_max = 0, |
| 4343 | .devs_min = 3, |
| 4344 | .devs_increment = 1, |
| 4345 | .ncopies = 3, |
| 4346 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4347 | }; |
| 4348 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4349 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4350 | { |
| 4351 | /* TODO allow them to set a preferred stripe size */ |
| 4352 | return 64 * 1024; |
| 4353 | } |
| 4354 | |
| 4355 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4356 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4357 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4358 | return; |
| 4359 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4360 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4361 | } |
| 4362 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4363 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4364 | - sizeof(struct btrfs_item) \ |
| 4365 | - sizeof(struct btrfs_chunk)) \ |
| 4366 | / sizeof(struct btrfs_stripe) + 1) |
| 4367 | |
| 4368 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4369 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4370 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4371 | / sizeof(struct btrfs_stripe) + 1) |
| 4372 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4373 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4374 | struct btrfs_root *extent_root, u64 start, |
| 4375 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4376 | { |
| 4377 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4378 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4379 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4380 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4381 | struct extent_map_tree *em_tree; |
| 4382 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4383 | struct btrfs_device_info *devices_info = NULL; |
| 4384 | u64 total_avail; |
| 4385 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4386 | int data_stripes; /* number of stripes that count for |
| 4387 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4388 | int sub_stripes; /* sub_stripes info for map */ |
| 4389 | int dev_stripes; /* stripes per dev */ |
| 4390 | int devs_max; /* max devs to use */ |
| 4391 | int devs_min; /* min devs needed */ |
| 4392 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4393 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4394 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4395 | u64 max_stripe_size; |
| 4396 | u64 max_chunk_size; |
| 4397 | u64 stripe_size; |
| 4398 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4399 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4400 | int ndevs; |
| 4401 | int i; |
| 4402 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4403 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4404 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4405 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4406 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4407 | if (list_empty(&fs_devices->alloc_list)) |
| 4408 | return -ENOSPC; |
| 4409 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4410 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4411 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4412 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4413 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4414 | devs_max = btrfs_raid_array[index].devs_max; |
| 4415 | devs_min = btrfs_raid_array[index].devs_min; |
| 4416 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4417 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4418 | |
| 4419 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4420 | max_stripe_size = 1024 * 1024 * 1024; |
| 4421 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4422 | if (!devs_max) |
| 4423 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4424 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4425 | /* for larger filesystems, use larger metadata chunks */ |
| 4426 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4427 | max_stripe_size = 1024 * 1024 * 1024; |
| 4428 | else |
| 4429 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4430 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4431 | if (!devs_max) |
| 4432 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4433 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4434 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4435 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4436 | if (!devs_max) |
| 4437 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4438 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4439 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4440 | type); |
| 4441 | BUG_ON(1); |
| 4442 | } |
| 4443 | |
| 4444 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4445 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4446 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4447 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4448 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4449 | GFP_NOFS); |
| 4450 | if (!devices_info) |
| 4451 | return -ENOMEM; |
| 4452 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4453 | cur = fs_devices->alloc_list.next; |
| 4454 | |
| 4455 | /* |
| 4456 | * in the first pass through the devices list, we gather information |
| 4457 | * about the available holes on each device. |
| 4458 | */ |
| 4459 | ndevs = 0; |
| 4460 | while (cur != &fs_devices->alloc_list) { |
| 4461 | struct btrfs_device *device; |
| 4462 | u64 max_avail; |
| 4463 | u64 dev_offset; |
| 4464 | |
| 4465 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4466 | |
| 4467 | cur = cur->next; |
| 4468 | |
| 4469 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4470 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4471 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4472 | continue; |
| 4473 | } |
| 4474 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4475 | if (!device->in_fs_metadata || |
| 4476 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4477 | continue; |
| 4478 | |
| 4479 | if (device->total_bytes > device->bytes_used) |
| 4480 | total_avail = device->total_bytes - device->bytes_used; |
| 4481 | else |
| 4482 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4483 | |
| 4484 | /* If there is no space on this device, skip it. */ |
| 4485 | if (total_avail == 0) |
| 4486 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4487 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4488 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4489 | max_stripe_size * dev_stripes, |
| 4490 | &dev_offset, &max_avail); |
| 4491 | if (ret && ret != -ENOSPC) |
| 4492 | goto error; |
| 4493 | |
| 4494 | if (ret == 0) |
| 4495 | max_avail = max_stripe_size * dev_stripes; |
| 4496 | |
| 4497 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4498 | continue; |
| 4499 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4500 | if (ndevs == fs_devices->rw_devices) { |
| 4501 | WARN(1, "%s: found more than %llu devices\n", |
| 4502 | __func__, fs_devices->rw_devices); |
| 4503 | break; |
| 4504 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4505 | devices_info[ndevs].dev_offset = dev_offset; |
| 4506 | devices_info[ndevs].max_avail = max_avail; |
| 4507 | devices_info[ndevs].total_avail = total_avail; |
| 4508 | devices_info[ndevs].dev = device; |
| 4509 | ++ndevs; |
| 4510 | } |
| 4511 | |
| 4512 | /* |
| 4513 | * now sort the devices by hole size / available space |
| 4514 | */ |
| 4515 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4516 | btrfs_cmp_device_info, NULL); |
| 4517 | |
| 4518 | /* round down to number of usable stripes */ |
| 4519 | ndevs -= ndevs % devs_increment; |
| 4520 | |
| 4521 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4522 | ret = -ENOSPC; |
| 4523 | goto error; |
| 4524 | } |
| 4525 | |
| 4526 | if (devs_max && ndevs > devs_max) |
| 4527 | ndevs = devs_max; |
| 4528 | /* |
| 4529 | * the primary goal is to maximize the number of stripes, so use as many |
| 4530 | * devices as possible, even if the stripes are not maximum sized. |
| 4531 | */ |
| 4532 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4533 | num_stripes = ndevs * dev_stripes; |
| 4534 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4535 | /* |
| 4536 | * this will have to be fixed for RAID1 and RAID10 over |
| 4537 | * more drives |
| 4538 | */ |
| 4539 | data_stripes = num_stripes / ncopies; |
| 4540 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4541 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4542 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4543 | btrfs_super_stripesize(info->super_copy)); |
| 4544 | data_stripes = num_stripes - 1; |
| 4545 | } |
| 4546 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4547 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4548 | btrfs_super_stripesize(info->super_copy)); |
| 4549 | data_stripes = num_stripes - 2; |
| 4550 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4551 | |
| 4552 | /* |
| 4553 | * Use the number of data stripes to figure out how big this chunk |
| 4554 | * is really going to be in terms of logical address space, |
| 4555 | * and compare that answer with the max chunk size |
| 4556 | */ |
| 4557 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4558 | u64 mask = (1ULL << 24) - 1; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4559 | |
| 4560 | stripe_size = div_u64(max_chunk_size, data_stripes); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4561 | |
| 4562 | /* bump the answer up to a 16MB boundary */ |
| 4563 | stripe_size = (stripe_size + mask) & ~mask; |
| 4564 | |
| 4565 | /* but don't go higher than the limits we found |
| 4566 | * while searching for free extents |
| 4567 | */ |
| 4568 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4569 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4570 | } |
| 4571 | |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4572 | stripe_size = div_u64(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4573 | |
| 4574 | /* align to BTRFS_STRIPE_LEN */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4575 | stripe_size = div_u64(stripe_size, raid_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4576 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4577 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4578 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4579 | if (!map) { |
| 4580 | ret = -ENOMEM; |
| 4581 | goto error; |
| 4582 | } |
| 4583 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4584 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4585 | for (i = 0; i < ndevs; ++i) { |
| 4586 | for (j = 0; j < dev_stripes; ++j) { |
| 4587 | int s = i * dev_stripes + j; |
| 4588 | map->stripes[s].dev = devices_info[i].dev; |
| 4589 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4590 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4591 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4592 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4593 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4594 | map->stripe_len = raid_stripe_len; |
| 4595 | map->io_align = raid_stripe_len; |
| 4596 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4597 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4598 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4599 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4600 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4601 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4602 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4603 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4604 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4605 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4606 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4607 | ret = -ENOMEM; |
| 4608 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4609 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4610 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4611 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4612 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4613 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4614 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4615 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4616 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4617 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4618 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4619 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4620 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4621 | if (!ret) { |
| 4622 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4623 | atomic_inc(&em->refs); |
| 4624 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4625 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4626 | if (ret) { |
| 4627 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4628 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4629 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4630 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4631 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4632 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4633 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4634 | if (ret) |
| 4635 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4636 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4637 | for (i = 0; i < map->num_stripes; i++) { |
| 4638 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4639 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4640 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4641 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4642 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4643 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4644 | map->num_stripes); |
| 4645 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4646 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4647 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4648 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4649 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4650 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4651 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4652 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4653 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4654 | write_lock(&em_tree->lock); |
| 4655 | remove_extent_mapping(em_tree, em); |
| 4656 | write_unlock(&em_tree->lock); |
| 4657 | |
| 4658 | /* One for our allocation */ |
| 4659 | free_extent_map(em); |
| 4660 | /* One for the tree reference */ |
| 4661 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4662 | /* One for the pending_chunks list reference */ |
| 4663 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4664 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4665 | kfree(devices_info); |
| 4666 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4667 | } |
| 4668 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4669 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4670 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4671 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4672 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4673 | struct btrfs_key key; |
| 4674 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4675 | struct btrfs_device *device; |
| 4676 | struct btrfs_chunk *chunk; |
| 4677 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4678 | struct extent_map_tree *em_tree; |
| 4679 | struct extent_map *em; |
| 4680 | struct map_lookup *map; |
| 4681 | size_t item_size; |
| 4682 | u64 dev_offset; |
| 4683 | u64 stripe_size; |
| 4684 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4685 | int ret; |
| 4686 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4687 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4688 | read_lock(&em_tree->lock); |
| 4689 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4690 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4691 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4692 | if (!em) { |
| 4693 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4694 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4695 | return -EINVAL; |
| 4696 | } |
| 4697 | |
| 4698 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4699 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4700 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4701 | chunk_size, em->start, em->len); |
| 4702 | free_extent_map(em); |
| 4703 | return -EINVAL; |
| 4704 | } |
| 4705 | |
| 4706 | map = (struct map_lookup *)em->bdev; |
| 4707 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4708 | stripe_size = em->orig_block_len; |
| 4709 | |
| 4710 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4711 | if (!chunk) { |
| 4712 | ret = -ENOMEM; |
| 4713 | goto out; |
| 4714 | } |
| 4715 | |
| 4716 | for (i = 0; i < map->num_stripes; i++) { |
| 4717 | device = map->stripes[i].dev; |
| 4718 | dev_offset = map->stripes[i].physical; |
| 4719 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4720 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4721 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4722 | goto out; |
| 4723 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4724 | chunk_root->root_key.objectid, |
| 4725 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4726 | chunk_offset, dev_offset, |
| 4727 | stripe_size); |
| 4728 | if (ret) |
| 4729 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4730 | } |
| 4731 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4732 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4733 | for (i = 0; i < map->num_stripes; i++) { |
| 4734 | device = map->stripes[i].dev; |
| 4735 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4736 | |
| 4737 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4738 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4739 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4740 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4744 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4745 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4746 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4747 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4748 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4749 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4750 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4751 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4752 | |
| 4753 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4754 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4755 | key.offset = chunk_offset; |
| 4756 | |
| 4757 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4758 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4759 | /* |
| 4760 | * TODO: Cleanup of inserted chunk root in case of |
| 4761 | * failure. |
| 4762 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4763 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4764 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4765 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4766 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4767 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4768 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4769 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4770 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4771 | } |
| 4772 | |
| 4773 | /* |
| 4774 | * Chunk allocation falls into two parts. The first part does works |
| 4775 | * that make the new allocated chunk useable, but not do any operation |
| 4776 | * that modifies the chunk tree. The second part does the works that |
| 4777 | * require modifying the chunk tree. This division is important for the |
| 4778 | * bootstrap process of adding storage to a seed btrfs. |
| 4779 | */ |
| 4780 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4781 | struct btrfs_root *extent_root, u64 type) |
| 4782 | { |
| 4783 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4784 | |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 4785 | ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex)); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4786 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4787 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4788 | } |
| 4789 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4790 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4791 | struct btrfs_root *root, |
| 4792 | struct btrfs_device *device) |
| 4793 | { |
| 4794 | u64 chunk_offset; |
| 4795 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4796 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4797 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4798 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4799 | int ret; |
| 4800 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4801 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4802 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4803 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4804 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4805 | if (ret) |
| 4806 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4807 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4808 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4809 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4810 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4811 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4812 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4813 | } |
| 4814 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4815 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4816 | { |
| 4817 | int max_errors; |
| 4818 | |
| 4819 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4820 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4821 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4822 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4823 | max_errors = 1; |
| 4824 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4825 | max_errors = 2; |
| 4826 | } else { |
| 4827 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4828 | } |
| 4829 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4830 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4834 | { |
| 4835 | struct extent_map *em; |
| 4836 | struct map_lookup *map; |
| 4837 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4838 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4839 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4840 | int i; |
| 4841 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4842 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4843 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4844 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4845 | if (!em) |
| 4846 | return 1; |
| 4847 | |
| 4848 | map = (struct map_lookup *)em->bdev; |
| 4849 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4850 | if (map->stripes[i].dev->missing) { |
| 4851 | miss_ndevs++; |
| 4852 | continue; |
| 4853 | } |
| 4854 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4855 | if (!map->stripes[i].dev->writeable) { |
| 4856 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4857 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4858 | } |
| 4859 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4860 | |
| 4861 | /* |
| 4862 | * If the number of missing devices is larger than max errors, |
| 4863 | * we can not write the data into that chunk successfully, so |
| 4864 | * set it readonly. |
| 4865 | */ |
| 4866 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4867 | readonly = 1; |
| 4868 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4869 | free_extent_map(em); |
| 4870 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4874 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4875 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4879 | { |
| 4880 | struct extent_map *em; |
| 4881 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4882 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4883 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4884 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4885 | if (em) |
| 4886 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4887 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4888 | if (!em) |
| 4889 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4890 | /* once for us */ |
| 4891 | free_extent_map(em); |
| 4892 | /* once for the tree */ |
| 4893 | free_extent_map(em); |
| 4894 | } |
| 4895 | } |
| 4896 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4897 | 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] | 4898 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4899 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4900 | struct extent_map *em; |
| 4901 | struct map_lookup *map; |
| 4902 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4903 | int ret; |
| 4904 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4905 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4906 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4907 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4908 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4909 | /* |
| 4910 | * We could return errors for these cases, but that could get ugly and |
| 4911 | * we'd probably do the same thing which is just not do anything else |
| 4912 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4913 | */ |
| 4914 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4915 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4916 | logical+len); |
| 4917 | return 1; |
| 4918 | } |
| 4919 | |
| 4920 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4921 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4922 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4923 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4924 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4925 | return 1; |
| 4926 | } |
| 4927 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4928 | map = (struct map_lookup *)em->bdev; |
| 4929 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4930 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4931 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4932 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4933 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4934 | ret = 2; |
| 4935 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4936 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4937 | else |
| 4938 | ret = 1; |
| 4939 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4940 | |
| 4941 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4942 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4943 | ret++; |
| 4944 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4945 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4946 | return ret; |
| 4947 | } |
| 4948 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4949 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4950 | struct btrfs_mapping_tree *map_tree, |
| 4951 | u64 logical) |
| 4952 | { |
| 4953 | struct extent_map *em; |
| 4954 | struct map_lookup *map; |
| 4955 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4956 | unsigned long len = root->sectorsize; |
| 4957 | |
| 4958 | read_lock(&em_tree->lock); |
| 4959 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4960 | read_unlock(&em_tree->lock); |
| 4961 | BUG_ON(!em); |
| 4962 | |
| 4963 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4964 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4965 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4966 | len = map->stripe_len * nr_data_stripes(map); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4967 | free_extent_map(em); |
| 4968 | return len; |
| 4969 | } |
| 4970 | |
| 4971 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4972 | u64 logical, u64 len, int mirror_num) |
| 4973 | { |
| 4974 | struct extent_map *em; |
| 4975 | struct map_lookup *map; |
| 4976 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4977 | int ret = 0; |
| 4978 | |
| 4979 | read_lock(&em_tree->lock); |
| 4980 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4981 | read_unlock(&em_tree->lock); |
| 4982 | BUG_ON(!em); |
| 4983 | |
| 4984 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4985 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4986 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4987 | ret = 1; |
| 4988 | free_extent_map(em); |
| 4989 | return ret; |
| 4990 | } |
| 4991 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4992 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4993 | struct map_lookup *map, int first, int num, |
| 4994 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4995 | { |
| 4996 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4997 | int tolerance; |
| 4998 | struct btrfs_device *srcdev; |
| 4999 | |
| 5000 | if (dev_replace_is_ongoing && |
| 5001 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5002 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5003 | srcdev = fs_info->dev_replace.srcdev; |
| 5004 | else |
| 5005 | srcdev = NULL; |
| 5006 | |
| 5007 | /* |
| 5008 | * try to avoid the drive that is the source drive for a |
| 5009 | * dev-replace procedure, only choose it if no other non-missing |
| 5010 | * mirror is available |
| 5011 | */ |
| 5012 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 5013 | if (map->stripes[optimal].dev->bdev && |
| 5014 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 5015 | return optimal; |
| 5016 | for (i = first; i < first + num; i++) { |
| 5017 | if (map->stripes[i].dev->bdev && |
| 5018 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5019 | return i; |
| 5020 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5021 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5022 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5023 | /* we couldn't find one that doesn't fail. Just return something |
| 5024 | * and the io error handling code will clean up eventually |
| 5025 | */ |
| 5026 | return optimal; |
| 5027 | } |
| 5028 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5029 | static inline int parity_smaller(u64 a, u64 b) |
| 5030 | { |
| 5031 | return a > b; |
| 5032 | } |
| 5033 | |
| 5034 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5035 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5036 | { |
| 5037 | struct btrfs_bio_stripe s; |
| 5038 | int i; |
| 5039 | u64 l; |
| 5040 | int again = 1; |
| 5041 | |
| 5042 | while (again) { |
| 5043 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5044 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5045 | if (parity_smaller(bbio->raid_map[i], |
| 5046 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5047 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5048 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5049 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5050 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5051 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5052 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5053 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5054 | again = 1; |
| 5055 | } |
| 5056 | } |
| 5057 | } |
| 5058 | } |
| 5059 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5060 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5061 | { |
| 5062 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5063 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5064 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5065 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5066 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5067 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5068 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5069 | /* |
| 5070 | * plus the raid_map, which includes both the tgt dev |
| 5071 | * and the stripes |
| 5072 | */ |
| 5073 | sizeof(u64) * (total_stripes), |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5074 | GFP_NOFS); |
| 5075 | if (!bbio) |
| 5076 | return NULL; |
| 5077 | |
| 5078 | atomic_set(&bbio->error, 0); |
| 5079 | atomic_set(&bbio->refs, 1); |
| 5080 | |
| 5081 | return bbio; |
| 5082 | } |
| 5083 | |
| 5084 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5085 | { |
| 5086 | WARN_ON(!atomic_read(&bbio->refs)); |
| 5087 | atomic_inc(&bbio->refs); |
| 5088 | } |
| 5089 | |
| 5090 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5091 | { |
| 5092 | if (!bbio) |
| 5093 | return; |
| 5094 | if (atomic_dec_and_test(&bbio->refs)) |
| 5095 | kfree(bbio); |
| 5096 | } |
| 5097 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5098 | 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] | 5099 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5100 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5101 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5102 | { |
| 5103 | struct extent_map *em; |
| 5104 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5105 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5106 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5107 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5108 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5109 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5110 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5111 | u64 stripe_nr_orig; |
| 5112 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5113 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5114 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5115 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5116 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5117 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5118 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5119 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5120 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5121 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5122 | int dev_replace_is_ongoing = 0; |
| 5123 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5124 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5125 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5126 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5127 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5128 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5129 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5130 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5131 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5132 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5133 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5134 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5135 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5136 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5137 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5138 | if (em->start > logical || em->start + em->len < logical) { |
| 5139 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5140 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5141 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 5142 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5143 | return -EINVAL; |
| 5144 | } |
| 5145 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5146 | map = (struct map_lookup *)em->bdev; |
| 5147 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5148 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5149 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5150 | stripe_nr = offset; |
| 5151 | /* |
| 5152 | * stripe_nr counts the total number of stripes we have to stride |
| 5153 | * to get to this block |
| 5154 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5155 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5156 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5157 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5158 | BUG_ON(offset < stripe_offset); |
| 5159 | |
| 5160 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5161 | stripe_offset = offset - stripe_offset; |
| 5162 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5163 | /* 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] | 5164 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5165 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5166 | raid56_full_stripe_start = offset; |
| 5167 | |
| 5168 | /* allow a write of a full stripe, but make sure we don't |
| 5169 | * allow straddling of stripes |
| 5170 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5171 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5172 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5173 | raid56_full_stripe_start *= full_stripe_len; |
| 5174 | } |
| 5175 | |
| 5176 | if (rw & REQ_DISCARD) { |
| 5177 | /* we don't discard raid56 yet */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5178 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5179 | ret = -EOPNOTSUPP; |
| 5180 | goto out; |
| 5181 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5182 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5183 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5184 | u64 max_len; |
| 5185 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5186 | For other RAID types and for RAID[56] reads, just allow a single |
| 5187 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5188 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5189 | (rw & REQ_WRITE)) { |
| 5190 | max_len = stripe_len * nr_data_stripes(map) - |
| 5191 | (offset - raid56_full_stripe_start); |
| 5192 | } else { |
| 5193 | /* we limit the length of each bio to what fits in a stripe */ |
| 5194 | max_len = stripe_len - stripe_offset; |
| 5195 | } |
| 5196 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5197 | } else { |
| 5198 | *length = em->len - offset; |
| 5199 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5200 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5201 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5202 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5203 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5204 | goto out; |
| 5205 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5206 | btrfs_dev_replace_lock(dev_replace); |
| 5207 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5208 | if (!dev_replace_is_ongoing) |
| 5209 | btrfs_dev_replace_unlock(dev_replace); |
| 5210 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5211 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5212 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5213 | dev_replace->tgtdev != NULL) { |
| 5214 | /* |
| 5215 | * in dev-replace case, for repair case (that's the only |
| 5216 | * case where the mirror is selected explicitly when |
| 5217 | * calling btrfs_map_block), blocks left of the left cursor |
| 5218 | * can also be read from the target drive. |
| 5219 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5220 | * the last one to the array of stripes. For READ, it also |
| 5221 | * needs to be supported using the same mirror number. |
| 5222 | * If the requested block is not left of the left cursor, |
| 5223 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5224 | * returns one more in the dev-replace case. |
| 5225 | */ |
| 5226 | u64 tmp_length = *length; |
| 5227 | struct btrfs_bio *tmp_bbio = NULL; |
| 5228 | int tmp_num_stripes; |
| 5229 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5230 | int index_srcdev = 0; |
| 5231 | int found = 0; |
| 5232 | u64 physical_of_found = 0; |
| 5233 | |
| 5234 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5235 | logical, &tmp_length, &tmp_bbio, 0, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5236 | if (ret) { |
| 5237 | WARN_ON(tmp_bbio != NULL); |
| 5238 | goto out; |
| 5239 | } |
| 5240 | |
| 5241 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5242 | if (mirror_num > tmp_num_stripes) { |
| 5243 | /* |
| 5244 | * REQ_GET_READ_MIRRORS does not contain this |
| 5245 | * mirror, that means that the requested area |
| 5246 | * is not left of the left cursor |
| 5247 | */ |
| 5248 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5249 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5250 | goto out; |
| 5251 | } |
| 5252 | |
| 5253 | /* |
| 5254 | * process the rest of the function using the mirror_num |
| 5255 | * of the source drive. Therefore look it up first. |
| 5256 | * At the end, patch the device pointer to the one of the |
| 5257 | * target drive. |
| 5258 | */ |
| 5259 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5260 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5261 | /* |
| 5262 | * In case of DUP, in order to keep it |
| 5263 | * simple, only add the mirror with the |
| 5264 | * lowest physical address |
| 5265 | */ |
| 5266 | if (found && |
| 5267 | physical_of_found <= |
| 5268 | tmp_bbio->stripes[i].physical) |
| 5269 | continue; |
| 5270 | index_srcdev = i; |
| 5271 | found = 1; |
| 5272 | physical_of_found = |
| 5273 | tmp_bbio->stripes[i].physical; |
| 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | if (found) { |
| 5278 | mirror_num = index_srcdev + 1; |
| 5279 | patch_the_first_stripe_for_dev_replace = 1; |
| 5280 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5281 | } else { |
| 5282 | WARN_ON(1); |
| 5283 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5284 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5285 | goto out; |
| 5286 | } |
| 5287 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5288 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5289 | } else if (mirror_num > map->num_stripes) { |
| 5290 | mirror_num = 0; |
| 5291 | } |
| 5292 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5293 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5294 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5295 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5296 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5297 | stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5298 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5299 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5300 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5301 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5302 | if (rw & REQ_DISCARD) |
| 5303 | num_stripes = min_t(u64, map->num_stripes, |
| 5304 | stripe_nr_end - stripe_nr_orig); |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5305 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5306 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5307 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5308 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5309 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5310 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5311 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5312 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5313 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5314 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5315 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5316 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5317 | current->pid % map->num_stripes, |
| 5318 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5319 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5320 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5321 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5322 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5323 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5324 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5325 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5326 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5327 | } else { |
| 5328 | mirror_num = 1; |
| 5329 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5330 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5331 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5332 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5333 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5334 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5335 | stripe_index *= map->sub_stripes; |
| 5336 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5337 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5338 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5339 | else if (rw & REQ_DISCARD) |
| 5340 | num_stripes = min_t(u64, map->sub_stripes * |
| 5341 | (stripe_nr_end - stripe_nr_orig), |
| 5342 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5343 | else if (mirror_num) |
| 5344 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5345 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5346 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5347 | stripe_index = find_live_mirror(fs_info, map, |
| 5348 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5349 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5350 | current->pid % map->sub_stripes, |
| 5351 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5352 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5353 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5354 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5355 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5356 | if (need_raid_map && |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5357 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5358 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5359 | /* push stripe_nr back to the start of the full stripe */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5360 | stripe_nr = div_u64(raid56_full_stripe_start, |
| 5361 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5362 | |
| 5363 | /* RAID[56] write or recovery. Return all stripes */ |
| 5364 | num_stripes = map->num_stripes; |
| 5365 | max_errors = nr_parity_stripes(map); |
| 5366 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5367 | *length = map->stripe_len; |
| 5368 | stripe_index = 0; |
| 5369 | stripe_offset = 0; |
| 5370 | } else { |
| 5371 | /* |
| 5372 | * Mirror #0 or #1 means the original data block. |
| 5373 | * Mirror #2 is RAID5 parity block. |
| 5374 | * Mirror #3 is RAID6 Q block. |
| 5375 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5376 | stripe_nr = div_u64_rem(stripe_nr, |
| 5377 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5378 | if (mirror_num > 1) |
| 5379 | stripe_index = nr_data_stripes(map) + |
| 5380 | mirror_num - 2; |
| 5381 | |
| 5382 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5383 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5384 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5385 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5386 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5387 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5388 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5389 | } else { |
| 5390 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5391 | * after this, stripe_nr is the number of stripes on this |
| 5392 | * device we have to walk to find the data, and stripe_index is |
| 5393 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5394 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5395 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5396 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5397 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5398 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5399 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5400 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5401 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5402 | if (dev_replace_is_ongoing) { |
| 5403 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5404 | num_alloc_stripes <<= 1; |
| 5405 | if (rw & REQ_GET_READ_MIRRORS) |
| 5406 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5407 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5408 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5409 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5410 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5411 | if (!bbio) { |
| 5412 | ret = -ENOMEM; |
| 5413 | goto out; |
| 5414 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5415 | if (dev_replace_is_ongoing) |
| 5416 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5417 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5418 | /* build raid_map */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5419 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5420 | need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5421 | mirror_num > 1)) { |
| 5422 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5423 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5424 | |
| 5425 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5426 | sizeof(struct btrfs_bio_stripe) * |
| 5427 | num_alloc_stripes + |
| 5428 | sizeof(int) * tgtdev_indexes); |
| 5429 | |
| 5430 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5431 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5432 | |
| 5433 | /* Fill in the logical address of each stripe */ |
| 5434 | tmp = stripe_nr * nr_data_stripes(map); |
| 5435 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5436 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5437 | em->start + (tmp + i) * map->stripe_len; |
| 5438 | |
| 5439 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5440 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5441 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5442 | RAID6_Q_STRIPE; |
| 5443 | } |
| 5444 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5445 | if (rw & REQ_DISCARD) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5446 | u32 factor = 0; |
| 5447 | u32 sub_stripes = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5448 | u64 stripes_per_dev = 0; |
| 5449 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5450 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5451 | |
| 5452 | if (map->type & |
| 5453 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5454 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5455 | sub_stripes = 1; |
| 5456 | else |
| 5457 | sub_stripes = map->sub_stripes; |
| 5458 | |
| 5459 | factor = map->num_stripes / sub_stripes; |
| 5460 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5461 | stripe_nr_orig, |
| 5462 | factor, |
| 5463 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5464 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5465 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5466 | } |
| 5467 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5468 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5469 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5470 | map->stripes[stripe_index].physical + |
| 5471 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5472 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5473 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5474 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5475 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5476 | bbio->stripes[i].length = stripes_per_dev * |
| 5477 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5478 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5479 | if (i / sub_stripes < remaining_stripes) |
| 5480 | bbio->stripes[i].length += |
| 5481 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5482 | |
| 5483 | /* |
| 5484 | * Special for the first stripe and |
| 5485 | * the last stripe: |
| 5486 | * |
| 5487 | * |-------|...|-------| |
| 5488 | * |----------| |
| 5489 | * off end_off |
| 5490 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5491 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5492 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5493 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5494 | |
| 5495 | if (stripe_index >= last_stripe && |
| 5496 | stripe_index <= (last_stripe + |
| 5497 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5498 | bbio->stripes[i].length -= |
| 5499 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5500 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5501 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5502 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5503 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5504 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5505 | |
| 5506 | stripe_index++; |
| 5507 | if (stripe_index == map->num_stripes) { |
| 5508 | /* This could only happen for RAID0/10 */ |
| 5509 | stripe_index = 0; |
| 5510 | stripe_nr++; |
| 5511 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5512 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5513 | } else { |
| 5514 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5515 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5516 | map->stripes[stripe_index].physical + |
| 5517 | stripe_offset + |
| 5518 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5519 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5520 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5521 | stripe_index++; |
| 5522 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5523 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5524 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5525 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5526 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5527 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5528 | if (bbio->raid_map) |
| 5529 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5530 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5531 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5532 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5533 | dev_replace->tgtdev != NULL) { |
| 5534 | int index_where_to_add; |
| 5535 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5536 | |
| 5537 | /* |
| 5538 | * duplicate the write operations while the dev replace |
| 5539 | * procedure is running. Since the copying of the old disk |
| 5540 | * to the new disk takes place at run time while the |
| 5541 | * filesystem is mounted writable, the regular write |
| 5542 | * operations to the old disk have to be duplicated to go |
| 5543 | * to the new disk as well. |
| 5544 | * Note that device->missing is handled by the caller, and |
| 5545 | * that the write to the old disk is already set up in the |
| 5546 | * stripes array. |
| 5547 | */ |
| 5548 | index_where_to_add = num_stripes; |
| 5549 | for (i = 0; i < num_stripes; i++) { |
| 5550 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5551 | /* write to new disk, too */ |
| 5552 | struct btrfs_bio_stripe *new = |
| 5553 | bbio->stripes + index_where_to_add; |
| 5554 | struct btrfs_bio_stripe *old = |
| 5555 | bbio->stripes + i; |
| 5556 | |
| 5557 | new->physical = old->physical; |
| 5558 | new->length = old->length; |
| 5559 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5560 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5561 | index_where_to_add++; |
| 5562 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5563 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5564 | } |
| 5565 | } |
| 5566 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5567 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5568 | dev_replace->tgtdev != NULL) { |
| 5569 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5570 | int index_srcdev = 0; |
| 5571 | int found = 0; |
| 5572 | u64 physical_of_found = 0; |
| 5573 | |
| 5574 | /* |
| 5575 | * During the dev-replace procedure, the target drive can |
| 5576 | * also be used to read data in case it is needed to repair |
| 5577 | * a corrupt block elsewhere. This is possible if the |
| 5578 | * requested area is left of the left cursor. In this area, |
| 5579 | * the target drive is a full copy of the source drive. |
| 5580 | */ |
| 5581 | for (i = 0; i < num_stripes; i++) { |
| 5582 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5583 | /* |
| 5584 | * In case of DUP, in order to keep it |
| 5585 | * simple, only add the mirror with the |
| 5586 | * lowest physical address |
| 5587 | */ |
| 5588 | if (found && |
| 5589 | physical_of_found <= |
| 5590 | bbio->stripes[i].physical) |
| 5591 | continue; |
| 5592 | index_srcdev = i; |
| 5593 | found = 1; |
| 5594 | physical_of_found = bbio->stripes[i].physical; |
| 5595 | } |
| 5596 | } |
| 5597 | if (found) { |
David Sterba | 258ece0 | 2015-02-24 19:45:15 +0100 | [diff] [blame] | 5598 | if (physical_of_found + map->stripe_len <= |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5599 | dev_replace->cursor_left) { |
| 5600 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5601 | bbio->stripes + num_stripes; |
| 5602 | |
| 5603 | tgtdev_stripe->physical = physical_of_found; |
| 5604 | tgtdev_stripe->length = |
| 5605 | bbio->stripes[index_srcdev].length; |
| 5606 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5607 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5608 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5609 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5610 | num_stripes++; |
| 5611 | } |
| 5612 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5613 | } |
| 5614 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5615 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5616 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5617 | bbio->num_stripes = num_stripes; |
| 5618 | bbio->max_errors = max_errors; |
| 5619 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5620 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5621 | |
| 5622 | /* |
| 5623 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5624 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5625 | * available as a mirror |
| 5626 | */ |
| 5627 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5628 | WARN_ON(num_stripes > 1); |
| 5629 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5630 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5631 | bbio->mirror_num = map->num_stripes + 1; |
| 5632 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5633 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5634 | if (dev_replace_is_ongoing) |
| 5635 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5636 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5637 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5638 | } |
| 5639 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5640 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5641 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5642 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5643 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5644 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5645 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5646 | } |
| 5647 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5648 | /* For Scrub/replace */ |
| 5649 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5650 | u64 logical, u64 *length, |
| 5651 | struct btrfs_bio **bbio_ret, int mirror_num, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5652 | int need_raid_map) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5653 | { |
| 5654 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5655 | mirror_num, need_raid_map); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5656 | } |
| 5657 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5658 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5659 | u64 chunk_start, u64 physical, u64 devid, |
| 5660 | u64 **logical, int *naddrs, int *stripe_len) |
| 5661 | { |
| 5662 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5663 | struct extent_map *em; |
| 5664 | struct map_lookup *map; |
| 5665 | u64 *buf; |
| 5666 | u64 bytenr; |
| 5667 | u64 length; |
| 5668 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5669 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5670 | int i, j, nr = 0; |
| 5671 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5672 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5673 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5674 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5675 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5676 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5677 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5678 | chunk_start); |
| 5679 | return -EIO; |
| 5680 | } |
| 5681 | |
| 5682 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5683 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5684 | em->start, chunk_start); |
| 5685 | free_extent_map(em); |
| 5686 | return -EIO; |
| 5687 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5688 | map = (struct map_lookup *)em->bdev; |
| 5689 | |
| 5690 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5691 | rmap_len = map->stripe_len; |
| 5692 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5693 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5694 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5695 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5696 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5697 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5698 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5699 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5700 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5701 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5702 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5703 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5704 | |
| 5705 | for (i = 0; i < map->num_stripes; i++) { |
| 5706 | if (devid && map->stripes[i].dev->devid != devid) |
| 5707 | continue; |
| 5708 | if (map->stripes[i].physical > physical || |
| 5709 | map->stripes[i].physical + length <= physical) |
| 5710 | continue; |
| 5711 | |
| 5712 | stripe_nr = physical - map->stripes[i].physical; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5713 | stripe_nr = div_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5714 | |
| 5715 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5716 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5717 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5718 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5719 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5720 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5721 | * Alternatively, just use rmap_len below instead of |
| 5722 | * map->stripe_len */ |
| 5723 | |
| 5724 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5725 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5726 | for (j = 0; j < nr; j++) { |
| 5727 | if (buf[j] == bytenr) |
| 5728 | break; |
| 5729 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5730 | if (j == nr) { |
| 5731 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5732 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5733 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5734 | } |
| 5735 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5736 | *logical = buf; |
| 5737 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5738 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5739 | |
| 5740 | free_extent_map(em); |
| 5741 | return 0; |
| 5742 | } |
| 5743 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5744 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5745 | { |
| 5746 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5747 | bio_endio_nodec(bio, err); |
| 5748 | else |
| 5749 | bio_endio(bio, err); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5750 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5751 | } |
| 5752 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5753 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5754 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5755 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5756 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5757 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5758 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5759 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5760 | if (err == -EIO || err == -EREMOTEIO) { |
| 5761 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5762 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 5763 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5764 | |
| 5765 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5766 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5767 | if (dev->bdev) { |
| 5768 | if (bio->bi_rw & WRITE) |
| 5769 | btrfs_dev_stat_inc(dev, |
| 5770 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5771 | else |
| 5772 | btrfs_dev_stat_inc(dev, |
| 5773 | BTRFS_DEV_STAT_READ_ERRS); |
| 5774 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5775 | btrfs_dev_stat_inc(dev, |
| 5776 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5777 | btrfs_dev_stat_print_on_error(dev); |
| 5778 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5779 | } |
| 5780 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5781 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5782 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5783 | is_orig_bio = 1; |
| 5784 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5785 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5786 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5787 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5788 | if (!is_orig_bio) { |
| 5789 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5790 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5791 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5792 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5793 | bio->bi_private = bbio->private; |
| 5794 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5795 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5796 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5797 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5798 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5799 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5800 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5801 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5802 | /* |
| 5803 | * this bio is actually up to date, we didn't |
| 5804 | * go over the max number of errors |
| 5805 | */ |
| 5806 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5807 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5808 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5809 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5810 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5811 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5812 | bio_put(bio); |
| 5813 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5814 | } |
| 5815 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5816 | /* |
| 5817 | * see run_scheduled_bios for a description of why bios are collected for |
| 5818 | * async submit. |
| 5819 | * |
| 5820 | * This will add one bio to the pending list for a device and make sure |
| 5821 | * the work struct is scheduled. |
| 5822 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5823 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5824 | struct btrfs_device *device, |
| 5825 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5826 | { |
| 5827 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5828 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5829 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5830 | if (device->missing || !device->bdev) { |
| 5831 | bio_endio(bio, -EIO); |
| 5832 | return; |
| 5833 | } |
| 5834 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5835 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5836 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5837 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5838 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5839 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5840 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5844 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5845 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5846 | * made progress against dirty pages when we've really just put it |
| 5847 | * on a queue for later |
| 5848 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5849 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5850 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5851 | bio->bi_next = NULL; |
| 5852 | bio->bi_rw |= rw; |
| 5853 | |
| 5854 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5855 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5856 | pending_bios = &device->pending_sync_bios; |
| 5857 | else |
| 5858 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5859 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5860 | if (pending_bios->tail) |
| 5861 | pending_bios->tail->bi_next = bio; |
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 | pending_bios->tail = bio; |
| 5864 | if (!pending_bios->head) |
| 5865 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5866 | if (device->running_pending) |
| 5867 | should_queue = 0; |
| 5868 | |
| 5869 | spin_unlock(&device->io_lock); |
| 5870 | |
| 5871 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5872 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5873 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5874 | } |
| 5875 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5876 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5877 | sector_t sector) |
| 5878 | { |
| 5879 | struct bio_vec *prev; |
| 5880 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5881 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5882 | struct bvec_merge_data bvm = { |
| 5883 | .bi_bdev = bdev, |
| 5884 | .bi_sector = sector, |
| 5885 | .bi_rw = bio->bi_rw, |
| 5886 | }; |
| 5887 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5888 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5889 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5890 | |
| 5891 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5892 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5893 | return 0; |
| 5894 | |
| 5895 | if (!q->merge_bvec_fn) |
| 5896 | return 1; |
| 5897 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5898 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5899 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5900 | return 0; |
| 5901 | return 1; |
| 5902 | } |
| 5903 | |
| 5904 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5905 | struct bio *bio, u64 physical, int dev_nr, |
| 5906 | int rw, int async) |
| 5907 | { |
| 5908 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5909 | |
| 5910 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5911 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5912 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5913 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5914 | #ifdef DEBUG |
| 5915 | { |
| 5916 | struct rcu_string *name; |
| 5917 | |
| 5918 | rcu_read_lock(); |
| 5919 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5920 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5921 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5922 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5923 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5924 | rcu_read_unlock(); |
| 5925 | } |
| 5926 | #endif |
| 5927 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5928 | |
| 5929 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5930 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5931 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5932 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5933 | else |
| 5934 | btrfsic_submit_bio(rw, bio); |
| 5935 | } |
| 5936 | |
| 5937 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5938 | struct bio *first_bio, struct btrfs_device *dev, |
| 5939 | int dev_nr, int rw, int async) |
| 5940 | { |
| 5941 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5942 | struct bio *bio; |
| 5943 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5944 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5945 | |
| 5946 | again: |
| 5947 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5948 | if (!bio) |
| 5949 | return -ENOMEM; |
| 5950 | |
| 5951 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5952 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5953 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5954 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5955 | |
| 5956 | atomic_inc(&bbio->stripes_pending); |
| 5957 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5958 | rw, async); |
| 5959 | physical += len; |
| 5960 | goto again; |
| 5961 | } |
| 5962 | bvec++; |
| 5963 | } |
| 5964 | |
| 5965 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5966 | return 0; |
| 5967 | } |
| 5968 | |
| 5969 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5970 | { |
| 5971 | atomic_inc(&bbio->error); |
| 5972 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5973 | /* Shoud be the original bio. */ |
| 5974 | WARN_ON(bio != bbio->orig_bio); |
| 5975 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5976 | bio->bi_private = bbio->private; |
| 5977 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5978 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5979 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5980 | |
| 5981 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5982 | } |
| 5983 | } |
| 5984 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5985 | 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] | 5986 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5987 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5988 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5989 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5990 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5991 | u64 length = 0; |
| 5992 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5993 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5994 | int dev_nr; |
| 5995 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5996 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5997 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5998 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5999 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6000 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6001 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6002 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6003 | mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6004 | if (ret) { |
| 6005 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6006 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6007 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6008 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6009 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6010 | bbio->orig_bio = first_bio; |
| 6011 | bbio->private = first_bio->bi_private; |
| 6012 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6013 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6014 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 6015 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6016 | if (bbio->raid_map) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6017 | /* In this case, map_length has been set to the length of |
| 6018 | a single stripe; not the whole write */ |
| 6019 | if (rw & WRITE) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6020 | ret = raid56_parity_write(root, bio, bbio, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6021 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 6022 | ret = raid56_parity_recover(root, bio, bbio, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6023 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6024 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 6025 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6026 | btrfs_bio_counter_dec(root->fs_info); |
| 6027 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6028 | } |
| 6029 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6030 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6031 | 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] | 6032 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 6033 | BUG(); |
| 6034 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6035 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 6036 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6037 | dev = bbio->stripes[dev_nr].dev; |
| 6038 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 6039 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6040 | continue; |
| 6041 | } |
| 6042 | |
| 6043 | /* |
| 6044 | * Check and see if we're ok with this bio based on it's size |
| 6045 | * and offset with the given device. |
| 6046 | */ |
| 6047 | if (!bio_size_ok(dev->bdev, first_bio, |
| 6048 | bbio->stripes[dev_nr].physical >> 9)) { |
| 6049 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 6050 | dev_nr, rw, async_submit); |
| 6051 | BUG_ON(ret); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6052 | continue; |
| 6053 | } |
| 6054 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6055 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 6056 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6057 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 6058 | } else { |
| 6059 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 6060 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 6061 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6062 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 6063 | submit_stripe_bio(root, bbio, bio, |
| 6064 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 6065 | async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6066 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 6067 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6068 | return 0; |
| 6069 | } |
| 6070 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6071 | 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] | 6072 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6073 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6074 | struct btrfs_device *device; |
| 6075 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6076 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6077 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6078 | while (cur_devices) { |
| 6079 | if (!fsid || |
| 6080 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 6081 | device = __find_device(&cur_devices->devices, |
| 6082 | devid, uuid); |
| 6083 | if (device) |
| 6084 | return device; |
| 6085 | } |
| 6086 | cur_devices = cur_devices->seed; |
| 6087 | } |
| 6088 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6089 | } |
| 6090 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6091 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6092 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6093 | u64 devid, u8 *dev_uuid) |
| 6094 | { |
| 6095 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6096 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6097 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6098 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 6099 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6100 | |
| 6101 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6102 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6103 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6104 | |
| 6105 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6106 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6107 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6108 | return device; |
| 6109 | } |
| 6110 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6111 | /** |
| 6112 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6113 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6114 | * devid is provided (i.e. @devid != NULL). |
| 6115 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6116 | * is generated. |
| 6117 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6118 | * is generated. |
| 6119 | * |
| 6120 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 6121 | * on error. Returned struct is not linked onto any lists and can be |
| 6122 | * destroyed with kfree() right away. |
| 6123 | */ |
| 6124 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6125 | const u64 *devid, |
| 6126 | const u8 *uuid) |
| 6127 | { |
| 6128 | struct btrfs_device *dev; |
| 6129 | u64 tmp; |
| 6130 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6131 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6132 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6133 | |
| 6134 | dev = __alloc_device(); |
| 6135 | if (IS_ERR(dev)) |
| 6136 | return dev; |
| 6137 | |
| 6138 | if (devid) |
| 6139 | tmp = *devid; |
| 6140 | else { |
| 6141 | int ret; |
| 6142 | |
| 6143 | ret = find_next_devid(fs_info, &tmp); |
| 6144 | if (ret) { |
| 6145 | kfree(dev); |
| 6146 | return ERR_PTR(ret); |
| 6147 | } |
| 6148 | } |
| 6149 | dev->devid = tmp; |
| 6150 | |
| 6151 | if (uuid) |
| 6152 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6153 | else |
| 6154 | generate_random_uuid(dev->uuid); |
| 6155 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6156 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6157 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6158 | |
| 6159 | return dev; |
| 6160 | } |
| 6161 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6162 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 6163 | struct extent_buffer *leaf, |
| 6164 | struct btrfs_chunk *chunk) |
| 6165 | { |
| 6166 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6167 | struct map_lookup *map; |
| 6168 | struct extent_map *em; |
| 6169 | u64 logical; |
| 6170 | u64 length; |
| 6171 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6172 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6173 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6174 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6175 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6176 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6177 | logical = key->offset; |
| 6178 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6179 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6180 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6181 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6182 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6183 | |
| 6184 | /* already mapped? */ |
| 6185 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6186 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6187 | return 0; |
| 6188 | } else if (em) { |
| 6189 | free_extent_map(em); |
| 6190 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6191 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6192 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6193 | if (!em) |
| 6194 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6195 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6196 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6197 | if (!map) { |
| 6198 | free_extent_map(em); |
| 6199 | return -ENOMEM; |
| 6200 | } |
| 6201 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6202 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6203 | em->bdev = (struct block_device *)map; |
| 6204 | em->start = logical; |
| 6205 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6206 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6207 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6208 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6209 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6210 | map->num_stripes = num_stripes; |
| 6211 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6212 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6213 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6214 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6215 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6216 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6217 | for (i = 0; i < num_stripes; i++) { |
| 6218 | map->stripes[i].physical = |
| 6219 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6220 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6221 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6222 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6223 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6224 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6225 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6226 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6227 | free_extent_map(em); |
| 6228 | return -EIO; |
| 6229 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6230 | if (!map->stripes[i].dev) { |
| 6231 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6232 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6233 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6234 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6235 | free_extent_map(em); |
| 6236 | return -EIO; |
| 6237 | } |
Anand Jain | 816fceb | 2015-04-27 12:46:18 +0800 | [diff] [blame] | 6238 | btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing", |
| 6239 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6240 | } |
| 6241 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6242 | } |
| 6243 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6244 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6245 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6246 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6247 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6248 | free_extent_map(em); |
| 6249 | |
| 6250 | return 0; |
| 6251 | } |
| 6252 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6253 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6254 | struct btrfs_dev_item *dev_item, |
| 6255 | struct btrfs_device *device) |
| 6256 | { |
| 6257 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6258 | |
| 6259 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6260 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6261 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6262 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6263 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6264 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6265 | device->type = btrfs_device_type(leaf, dev_item); |
| 6266 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6267 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6268 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6269 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6270 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6271 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6272 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6273 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6274 | } |
| 6275 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6276 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6277 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6278 | { |
| 6279 | struct btrfs_fs_devices *fs_devices; |
| 6280 | int ret; |
| 6281 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6282 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6283 | |
| 6284 | fs_devices = root->fs_info->fs_devices->seed; |
| 6285 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6286 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6287 | return fs_devices; |
| 6288 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6289 | fs_devices = fs_devices->seed; |
| 6290 | } |
| 6291 | |
| 6292 | fs_devices = find_fsid(fsid); |
| 6293 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6294 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6295 | return ERR_PTR(-ENOENT); |
| 6296 | |
| 6297 | fs_devices = alloc_fs_devices(fsid); |
| 6298 | if (IS_ERR(fs_devices)) |
| 6299 | return fs_devices; |
| 6300 | |
| 6301 | fs_devices->seeding = 1; |
| 6302 | fs_devices->opened = 1; |
| 6303 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6304 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6305 | |
| 6306 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6307 | if (IS_ERR(fs_devices)) |
| 6308 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6309 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6310 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6311 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6312 | if (ret) { |
| 6313 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6314 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6315 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6316 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6317 | |
| 6318 | if (!fs_devices->seeding) { |
| 6319 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6320 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6321 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6322 | goto out; |
| 6323 | } |
| 6324 | |
| 6325 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6326 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6327 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6328 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6329 | } |
| 6330 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6331 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6332 | struct extent_buffer *leaf, |
| 6333 | struct btrfs_dev_item *dev_item) |
| 6334 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6335 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6336 | struct btrfs_device *device; |
| 6337 | u64 devid; |
| 6338 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6339 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6340 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6341 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6342 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6343 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6344 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6345 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6346 | BTRFS_UUID_SIZE); |
| 6347 | |
| 6348 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6349 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6350 | if (IS_ERR(fs_devices)) |
| 6351 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6352 | } |
| 6353 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6354 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6355 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6356 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6357 | return -EIO; |
| 6358 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6359 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6360 | if (!device) |
| 6361 | return -ENOMEM; |
Anand Jain | 33b97e4 | 2015-05-08 04:34:35 +0800 | [diff] [blame] | 6362 | btrfs_warn(root->fs_info, "devid %llu uuid %pU missing", |
| 6363 | devid, dev_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6364 | } else { |
| 6365 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6366 | return -EIO; |
| 6367 | |
| 6368 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6369 | /* |
| 6370 | * this happens when a device that was properly setup |
| 6371 | * in the device info lists suddenly goes bad. |
| 6372 | * device->bdev is NULL, and so we have to set |
| 6373 | * device->missing to one here |
| 6374 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6375 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6376 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6377 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6378 | |
| 6379 | /* Move the device to its own fs_devices */ |
| 6380 | if (device->fs_devices != fs_devices) { |
| 6381 | ASSERT(device->missing); |
| 6382 | |
| 6383 | list_move(&device->dev_list, &fs_devices->devices); |
| 6384 | device->fs_devices->num_devices--; |
| 6385 | fs_devices->num_devices++; |
| 6386 | |
| 6387 | device->fs_devices->missing_devices--; |
| 6388 | fs_devices->missing_devices++; |
| 6389 | |
| 6390 | device->fs_devices = fs_devices; |
| 6391 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6395 | BUG_ON(device->writeable); |
| 6396 | if (device->generation != |
| 6397 | btrfs_device_generation(leaf, dev_item)) |
| 6398 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6399 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6400 | |
| 6401 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6402 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6403 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6404 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6405 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6406 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6407 | device->bytes_used; |
| 6408 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6409 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6410 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6411 | return ret; |
| 6412 | } |
| 6413 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6414 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6415 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6416 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6417 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6418 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6419 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6420 | u8 *array_ptr; |
| 6421 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6422 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6423 | u32 num_stripes; |
| 6424 | u32 array_size; |
| 6425 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6426 | u32 cur_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6427 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6428 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6429 | ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize); |
| 6430 | /* |
| 6431 | * This will create extent buffer of nodesize, superblock size is |
| 6432 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6433 | * overallocate but we can keep it as-is, only the first page is used. |
| 6434 | */ |
| 6435 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6436 | if (!sb) |
| 6437 | return -ENOMEM; |
| 6438 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6439 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6440 | /* |
| 6441 | * The sb extent buffer is artifical and just used to read the system array. |
| 6442 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6443 | * pages up-to-date when the page is larger: extent does not cover the |
| 6444 | * whole page and consequently check_page_uptodate does not find all |
| 6445 | * the page's extents up-to-date (the hole beyond sb), |
| 6446 | * write_extent_buffer then triggers a WARN_ON. |
| 6447 | * |
| 6448 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6449 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6450 | * to silence the warning eg. on PowerPC 64. |
| 6451 | */ |
| 6452 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6453 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6454 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6455 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6456 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6457 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6458 | array_ptr = super_copy->sys_chunk_array; |
| 6459 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6460 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6461 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6462 | while (cur_offset < array_size) { |
| 6463 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6464 | len = sizeof(*disk_key); |
| 6465 | if (cur_offset + len > array_size) |
| 6466 | goto out_short_read; |
| 6467 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6468 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6469 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6470 | array_ptr += len; |
| 6471 | sb_array_offset += len; |
| 6472 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6473 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6474 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6475 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6476 | /* |
| 6477 | * At least one btrfs_chunk with one stripe must be |
| 6478 | * present, exact stripe count check comes afterwards |
| 6479 | */ |
| 6480 | len = btrfs_chunk_item_size(1); |
| 6481 | if (cur_offset + len > array_size) |
| 6482 | goto out_short_read; |
| 6483 | |
| 6484 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6485 | len = btrfs_chunk_item_size(num_stripes); |
| 6486 | if (cur_offset + len > array_size) |
| 6487 | goto out_short_read; |
| 6488 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6489 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6490 | if (ret) |
| 6491 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6492 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6493 | ret = -EIO; |
| 6494 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6495 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6496 | array_ptr += len; |
| 6497 | sb_array_offset += len; |
| 6498 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6499 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6500 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6501 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6502 | |
| 6503 | out_short_read: |
| 6504 | printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n", |
| 6505 | len, cur_offset); |
| 6506 | free_extent_buffer(sb); |
| 6507 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6508 | } |
| 6509 | |
| 6510 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6511 | { |
| 6512 | struct btrfs_path *path; |
| 6513 | struct extent_buffer *leaf; |
| 6514 | struct btrfs_key key; |
| 6515 | struct btrfs_key found_key; |
| 6516 | int ret; |
| 6517 | int slot; |
| 6518 | |
| 6519 | root = root->fs_info->chunk_root; |
| 6520 | |
| 6521 | path = btrfs_alloc_path(); |
| 6522 | if (!path) |
| 6523 | return -ENOMEM; |
| 6524 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6525 | mutex_lock(&uuid_mutex); |
| 6526 | lock_chunks(root); |
| 6527 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6528 | /* |
| 6529 | * Read all device items, and then all the chunk items. All |
| 6530 | * device items are found before any chunk item (their object id |
| 6531 | * is smaller than the lowest possible object id for a chunk |
| 6532 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6533 | */ |
| 6534 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6535 | key.offset = 0; |
| 6536 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6537 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6538 | if (ret < 0) |
| 6539 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6540 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6541 | leaf = path->nodes[0]; |
| 6542 | slot = path->slots[0]; |
| 6543 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6544 | ret = btrfs_next_leaf(root, path); |
| 6545 | if (ret == 0) |
| 6546 | continue; |
| 6547 | if (ret < 0) |
| 6548 | goto error; |
| 6549 | break; |
| 6550 | } |
| 6551 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6552 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6553 | struct btrfs_dev_item *dev_item; |
| 6554 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6555 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6556 | ret = read_one_dev(root, leaf, dev_item); |
| 6557 | if (ret) |
| 6558 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6559 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6560 | struct btrfs_chunk *chunk; |
| 6561 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6562 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6563 | if (ret) |
| 6564 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6565 | } |
| 6566 | path->slots[0]++; |
| 6567 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6568 | ret = 0; |
| 6569 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6570 | unlock_chunks(root); |
| 6571 | mutex_unlock(&uuid_mutex); |
| 6572 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6573 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6574 | return ret; |
| 6575 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6576 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6577 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6578 | { |
| 6579 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6580 | struct btrfs_device *device; |
| 6581 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6582 | while (fs_devices) { |
| 6583 | mutex_lock(&fs_devices->device_list_mutex); |
| 6584 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6585 | device->dev_root = fs_info->dev_root; |
| 6586 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6587 | |
| 6588 | fs_devices = fs_devices->seed; |
| 6589 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6590 | } |
| 6591 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6592 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6593 | { |
| 6594 | int i; |
| 6595 | |
| 6596 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6597 | btrfs_dev_stat_reset(dev, i); |
| 6598 | } |
| 6599 | |
| 6600 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6601 | { |
| 6602 | struct btrfs_key key; |
| 6603 | struct btrfs_key found_key; |
| 6604 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6605 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6606 | struct extent_buffer *eb; |
| 6607 | int slot; |
| 6608 | int ret = 0; |
| 6609 | struct btrfs_device *device; |
| 6610 | struct btrfs_path *path = NULL; |
| 6611 | int i; |
| 6612 | |
| 6613 | path = btrfs_alloc_path(); |
| 6614 | if (!path) { |
| 6615 | ret = -ENOMEM; |
| 6616 | goto out; |
| 6617 | } |
| 6618 | |
| 6619 | mutex_lock(&fs_devices->device_list_mutex); |
| 6620 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6621 | int item_size; |
| 6622 | struct btrfs_dev_stats_item *ptr; |
| 6623 | |
| 6624 | key.objectid = 0; |
| 6625 | key.type = BTRFS_DEV_STATS_KEY; |
| 6626 | key.offset = device->devid; |
| 6627 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6628 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6629 | __btrfs_reset_dev_stats(device); |
| 6630 | device->dev_stats_valid = 1; |
| 6631 | btrfs_release_path(path); |
| 6632 | continue; |
| 6633 | } |
| 6634 | slot = path->slots[0]; |
| 6635 | eb = path->nodes[0]; |
| 6636 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6637 | item_size = btrfs_item_size_nr(eb, slot); |
| 6638 | |
| 6639 | ptr = btrfs_item_ptr(eb, slot, |
| 6640 | struct btrfs_dev_stats_item); |
| 6641 | |
| 6642 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6643 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6644 | btrfs_dev_stat_set(device, i, |
| 6645 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6646 | else |
| 6647 | btrfs_dev_stat_reset(device, i); |
| 6648 | } |
| 6649 | |
| 6650 | device->dev_stats_valid = 1; |
| 6651 | btrfs_dev_stat_print_on_load(device); |
| 6652 | btrfs_release_path(path); |
| 6653 | } |
| 6654 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6655 | |
| 6656 | out: |
| 6657 | btrfs_free_path(path); |
| 6658 | return ret < 0 ? ret : 0; |
| 6659 | } |
| 6660 | |
| 6661 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6662 | struct btrfs_root *dev_root, |
| 6663 | struct btrfs_device *device) |
| 6664 | { |
| 6665 | struct btrfs_path *path; |
| 6666 | struct btrfs_key key; |
| 6667 | struct extent_buffer *eb; |
| 6668 | struct btrfs_dev_stats_item *ptr; |
| 6669 | int ret; |
| 6670 | int i; |
| 6671 | |
| 6672 | key.objectid = 0; |
| 6673 | key.type = BTRFS_DEV_STATS_KEY; |
| 6674 | key.offset = device->devid; |
| 6675 | |
| 6676 | path = btrfs_alloc_path(); |
| 6677 | BUG_ON(!path); |
| 6678 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6679 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6680 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6681 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6682 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6683 | goto out; |
| 6684 | } |
| 6685 | |
| 6686 | if (ret == 0 && |
| 6687 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6688 | /* need to delete old one and insert a new one */ |
| 6689 | ret = btrfs_del_item(trans, dev_root, path); |
| 6690 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6691 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6692 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6693 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6694 | goto out; |
| 6695 | } |
| 6696 | ret = 1; |
| 6697 | } |
| 6698 | |
| 6699 | if (ret == 1) { |
| 6700 | /* need to insert a new item */ |
| 6701 | btrfs_release_path(path); |
| 6702 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6703 | &key, sizeof(*ptr)); |
| 6704 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6705 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6706 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6707 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6708 | goto out; |
| 6709 | } |
| 6710 | } |
| 6711 | |
| 6712 | eb = path->nodes[0]; |
| 6713 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6714 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6715 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6716 | btrfs_dev_stat_read(device, i)); |
| 6717 | btrfs_mark_buffer_dirty(eb); |
| 6718 | |
| 6719 | out: |
| 6720 | btrfs_free_path(path); |
| 6721 | return ret; |
| 6722 | } |
| 6723 | |
| 6724 | /* |
| 6725 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6726 | */ |
| 6727 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6728 | struct btrfs_fs_info *fs_info) |
| 6729 | { |
| 6730 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6731 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6732 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6733 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6734 | int ret = 0; |
| 6735 | |
| 6736 | mutex_lock(&fs_devices->device_list_mutex); |
| 6737 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6738 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6739 | continue; |
| 6740 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6741 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6742 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6743 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6744 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6745 | } |
| 6746 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6747 | |
| 6748 | return ret; |
| 6749 | } |
| 6750 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6751 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6752 | { |
| 6753 | btrfs_dev_stat_inc(dev, index); |
| 6754 | btrfs_dev_stat_print_on_error(dev); |
| 6755 | } |
| 6756 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6757 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6758 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6759 | if (!dev->dev_stats_valid) |
| 6760 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6761 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6762 | "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] | 6763 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6764 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6765 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6766 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6767 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6768 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6769 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6770 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6771 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6772 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6773 | int i; |
| 6774 | |
| 6775 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6776 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6777 | break; |
| 6778 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6779 | return; /* all values == 0, suppress message */ |
| 6780 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6781 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6782 | "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] | 6783 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6784 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6785 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6786 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6787 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6788 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6789 | } |
| 6790 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6791 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6792 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6793 | { |
| 6794 | struct btrfs_device *dev; |
| 6795 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6796 | int i; |
| 6797 | |
| 6798 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6799 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6800 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6801 | |
| 6802 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6803 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6804 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6805 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6806 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6807 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6808 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6809 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6810 | if (stats->nr_items > i) |
| 6811 | stats->values[i] = |
| 6812 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6813 | else |
| 6814 | btrfs_dev_stat_reset(dev, i); |
| 6815 | } |
| 6816 | } else { |
| 6817 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6818 | if (stats->nr_items > i) |
| 6819 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6820 | } |
| 6821 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6822 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6823 | return 0; |
| 6824 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6825 | |
| 6826 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6827 | { |
| 6828 | struct buffer_head *bh; |
| 6829 | struct btrfs_super_block *disk_super; |
| 6830 | |
| 6831 | bh = btrfs_read_dev_super(device->bdev); |
| 6832 | if (!bh) |
| 6833 | return -EINVAL; |
| 6834 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6835 | |
| 6836 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6837 | set_buffer_dirty(bh); |
| 6838 | sync_dirty_buffer(bh); |
| 6839 | brelse(bh); |
| 6840 | |
| 6841 | return 0; |
| 6842 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6843 | |
| 6844 | /* |
| 6845 | * Update the size of all devices, which is used for writing out the |
| 6846 | * super blocks. |
| 6847 | */ |
| 6848 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6849 | { |
| 6850 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6851 | struct btrfs_device *curr, *next; |
| 6852 | |
| 6853 | if (list_empty(&fs_devices->resized_devices)) |
| 6854 | return; |
| 6855 | |
| 6856 | mutex_lock(&fs_devices->device_list_mutex); |
| 6857 | lock_chunks(fs_info->dev_root); |
| 6858 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6859 | resized_list) { |
| 6860 | list_del_init(&curr->resized_list); |
| 6861 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6862 | } |
| 6863 | unlock_chunks(fs_info->dev_root); |
| 6864 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6865 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6866 | |
| 6867 | /* Must be invoked during the transaction commit */ |
| 6868 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6869 | struct btrfs_transaction *transaction) |
| 6870 | { |
| 6871 | struct extent_map *em; |
| 6872 | struct map_lookup *map; |
| 6873 | struct btrfs_device *dev; |
| 6874 | int i; |
| 6875 | |
| 6876 | if (list_empty(&transaction->pending_chunks)) |
| 6877 | return; |
| 6878 | |
| 6879 | /* In order to kick the device replace finish process */ |
| 6880 | lock_chunks(root); |
| 6881 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6882 | map = (struct map_lookup *)em->bdev; |
| 6883 | |
| 6884 | for (i = 0; i < map->num_stripes; i++) { |
| 6885 | dev = map->stripes[i].dev; |
| 6886 | dev->commit_bytes_used = dev->bytes_used; |
| 6887 | } |
| 6888 | } |
| 6889 | unlock_chunks(root); |
| 6890 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 6891 | |
| 6892 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6893 | { |
| 6894 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6895 | while (fs_devices) { |
| 6896 | fs_devices->fs_info = fs_info; |
| 6897 | fs_devices = fs_devices->seed; |
| 6898 | } |
| 6899 | } |
| 6900 | |
| 6901 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6902 | { |
| 6903 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6904 | while (fs_devices) { |
| 6905 | fs_devices->fs_info = NULL; |
| 6906 | fs_devices = fs_devices->seed; |
| 6907 | } |
| 6908 | } |