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); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 214 | if (IS_ERR(*bh)) { |
| 215 | ret = PTR_ERR(*bh); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 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 | |
Jens Axboe | dac5621 | 2015-04-17 16:23:59 -0600 | [diff] [blame] | 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 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1119 | static int contains_pending_extent(struct btrfs_transaction *transaction, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1120 | struct btrfs_device *device, |
| 1121 | u64 *start, u64 len) |
| 1122 | { |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1123 | struct btrfs_fs_info *fs_info = device->dev_root->fs_info; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1124 | struct extent_map *em; |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1125 | struct list_head *search_list = &fs_info->pinned_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1126 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1127 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1128 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1129 | if (transaction) |
| 1130 | search_list = &transaction->pending_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1131 | again: |
| 1132 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1133 | struct map_lookup *map; |
| 1134 | int i; |
| 1135 | |
| 1136 | map = (struct map_lookup *)em->bdev; |
| 1137 | for (i = 0; i < map->num_stripes; i++) { |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1138 | u64 end; |
| 1139 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1140 | if (map->stripes[i].dev != device) |
| 1141 | continue; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1142 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1143 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1144 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1145 | continue; |
Filipe Manana | c152b63 | 2015-05-14 10:46:03 +0100 | [diff] [blame] | 1146 | /* |
| 1147 | * Make sure that while processing the pinned list we do |
| 1148 | * not override our *start with a lower value, because |
| 1149 | * we can have pinned chunks that fall within this |
| 1150 | * device hole and that have lower physical addresses |
| 1151 | * than the pending chunks we processed before. If we |
| 1152 | * do not take this special care we can end up getting |
| 1153 | * 2 pending chunks that start at the same physical |
| 1154 | * device offsets because the end offset of a pinned |
| 1155 | * chunk can be equal to the start offset of some |
| 1156 | * pending chunk. |
| 1157 | */ |
| 1158 | end = map->stripes[i].physical + em->orig_block_len; |
| 1159 | if (end > *start) { |
| 1160 | *start = end; |
| 1161 | ret = 1; |
| 1162 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1163 | } |
| 1164 | } |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1165 | if (search_list != &fs_info->pinned_chunks) { |
| 1166 | search_list = &fs_info->pinned_chunks; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1167 | goto again; |
| 1168 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1169 | |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
| 1173 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1174 | /* |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1175 | * find_free_dev_extent_start - find free space in the specified device |
| 1176 | * @device: the device which we search the free space in |
| 1177 | * @num_bytes: the size of the free space that we need |
| 1178 | * @search_start: the position from which to begin the search |
| 1179 | * @start: store the start of the free space. |
| 1180 | * @len: the size of the free space. that we find, or the size |
| 1181 | * of the max free space if we don't find suitable free space |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1182 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1183 | * this uses a pretty simple search, the expectation is that it is |
| 1184 | * called very infrequently and that a given device has a small number |
| 1185 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1186 | * |
| 1187 | * @start is used to store the start of the free space if we find. But if we |
| 1188 | * don't find suitable free space, it will be used to store the start position |
| 1189 | * of the max free space. |
| 1190 | * |
| 1191 | * @len is used to store the size of the free space that we find. |
| 1192 | * But if we don't find suitable free space, it is used to store the size of |
| 1193 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1194 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1195 | int find_free_dev_extent_start(struct btrfs_transaction *transaction, |
| 1196 | struct btrfs_device *device, u64 num_bytes, |
| 1197 | u64 search_start, u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1198 | { |
| 1199 | struct btrfs_key key; |
| 1200 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1201 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1202 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1203 | u64 hole_size; |
| 1204 | u64 max_hole_start; |
| 1205 | u64 max_hole_size; |
| 1206 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1207 | u64 search_end = device->total_bytes; |
| 1208 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1209 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1210 | struct extent_buffer *l; |
| 1211 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1212 | path = btrfs_alloc_path(); |
| 1213 | if (!path) |
| 1214 | return -ENOMEM; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1215 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1216 | max_hole_start = search_start; |
| 1217 | max_hole_size = 0; |
| 1218 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1219 | again: |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1220 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1221 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1222 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1225 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1226 | path->search_commit_root = 1; |
| 1227 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1228 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1229 | key.objectid = device->devid; |
| 1230 | key.offset = search_start; |
| 1231 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1232 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1233 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1234 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1235 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1236 | if (ret > 0) { |
| 1237 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 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 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1241 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1242 | while (1) { |
| 1243 | l = path->nodes[0]; |
| 1244 | slot = path->slots[0]; |
| 1245 | if (slot >= btrfs_header_nritems(l)) { |
| 1246 | ret = btrfs_next_leaf(root, path); |
| 1247 | if (ret == 0) |
| 1248 | continue; |
| 1249 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1250 | goto out; |
| 1251 | |
| 1252 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1253 | } |
| 1254 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1255 | |
| 1256 | if (key.objectid < device->devid) |
| 1257 | goto next; |
| 1258 | |
| 1259 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1260 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1261 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1262 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1263 | goto next; |
| 1264 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1265 | if (key.offset > search_start) { |
| 1266 | hole_size = key.offset - search_start; |
| 1267 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1268 | /* |
| 1269 | * Have to check before we set max_hole_start, otherwise |
| 1270 | * we could end up sending back this offset anyway. |
| 1271 | */ |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1272 | if (contains_pending_extent(transaction, device, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1273 | &search_start, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1274 | hole_size)) { |
| 1275 | if (key.offset >= search_start) { |
| 1276 | hole_size = key.offset - search_start; |
| 1277 | } else { |
| 1278 | WARN_ON_ONCE(1); |
| 1279 | hole_size = 0; |
| 1280 | } |
| 1281 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1282 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1283 | if (hole_size > max_hole_size) { |
| 1284 | max_hole_start = search_start; |
| 1285 | max_hole_size = hole_size; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * If this free space is greater than which we need, |
| 1290 | * it must be the max free space that we have found |
| 1291 | * until now, so max_hole_start must point to the start |
| 1292 | * of this free space and the length of this free space |
| 1293 | * is stored in max_hole_size. Thus, we return |
| 1294 | * max_hole_start and max_hole_size and go back to the |
| 1295 | * caller. |
| 1296 | */ |
| 1297 | if (hole_size >= num_bytes) { |
| 1298 | ret = 0; |
| 1299 | goto out; |
| 1300 | } |
| 1301 | } |
| 1302 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1303 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1304 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1305 | dev_extent); |
| 1306 | if (extent_end > search_start) |
| 1307 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1308 | next: |
| 1309 | path->slots[0]++; |
| 1310 | cond_resched(); |
| 1311 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1312 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1313 | /* |
| 1314 | * At this point, search_start should be the end of |
| 1315 | * allocated dev extents, and when shrinking the device, |
| 1316 | * search_end may be smaller than search_start. |
| 1317 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1318 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1319 | hole_size = search_end - search_start; |
| 1320 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1321 | if (contains_pending_extent(transaction, device, &search_start, |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1322 | hole_size)) { |
| 1323 | btrfs_release_path(path); |
| 1324 | goto again; |
| 1325 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1326 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1327 | if (hole_size > max_hole_size) { |
| 1328 | max_hole_start = search_start; |
| 1329 | max_hole_size = hole_size; |
| 1330 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1331 | } |
| 1332 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1333 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1334 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1335 | ret = -ENOSPC; |
| 1336 | else |
| 1337 | ret = 0; |
| 1338 | |
| 1339 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1340 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1341 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1342 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1343 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1344 | return ret; |
| 1345 | } |
| 1346 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 1347 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1348 | struct btrfs_device *device, u64 num_bytes, |
| 1349 | u64 *start, u64 *len) |
| 1350 | { |
| 1351 | struct btrfs_root *root = device->dev_root; |
| 1352 | u64 search_start; |
| 1353 | |
| 1354 | /* FIXME use last free of some kind */ |
| 1355 | |
| 1356 | /* |
| 1357 | * we don't want to overwrite the superblock on the drive, |
| 1358 | * so we make sure to start at an offset of at least 1MB |
| 1359 | */ |
| 1360 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
| 1361 | return find_free_dev_extent_start(trans->transaction, device, |
| 1362 | num_bytes, search_start, start, len); |
| 1363 | } |
| 1364 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1365 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1366 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1367 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1368 | { |
| 1369 | int ret; |
| 1370 | struct btrfs_path *path; |
| 1371 | struct btrfs_root *root = device->dev_root; |
| 1372 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1373 | struct btrfs_key found_key; |
| 1374 | struct extent_buffer *leaf = NULL; |
| 1375 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1376 | |
| 1377 | path = btrfs_alloc_path(); |
| 1378 | if (!path) |
| 1379 | return -ENOMEM; |
| 1380 | |
| 1381 | key.objectid = device->devid; |
| 1382 | key.offset = start; |
| 1383 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1384 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1385 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1386 | if (ret > 0) { |
| 1387 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1388 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1389 | if (ret) |
| 1390 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1391 | leaf = path->nodes[0]; |
| 1392 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1393 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1394 | struct btrfs_dev_extent); |
| 1395 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1396 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1397 | key = found_key; |
| 1398 | btrfs_release_path(path); |
| 1399 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1400 | } else if (ret == 0) { |
| 1401 | leaf = path->nodes[0]; |
| 1402 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1403 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1404 | } else { |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame^] | 1405 | btrfs_std_error(root->fs_info, ret, "Slot search failed"); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1406 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1407 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1408 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1409 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1410 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1411 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1412 | if (ret) { |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame^] | 1413 | btrfs_std_error(root->fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1414 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1415 | } else { |
| 1416 | trans->transaction->have_free_bgs = 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1417 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1418 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1419 | btrfs_free_path(path); |
| 1420 | return ret; |
| 1421 | } |
| 1422 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1423 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1424 | struct btrfs_device *device, |
| 1425 | u64 chunk_tree, u64 chunk_objectid, |
| 1426 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1427 | { |
| 1428 | int ret; |
| 1429 | struct btrfs_path *path; |
| 1430 | struct btrfs_root *root = device->dev_root; |
| 1431 | struct btrfs_dev_extent *extent; |
| 1432 | struct extent_buffer *leaf; |
| 1433 | struct btrfs_key key; |
| 1434 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1435 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1436 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1437 | path = btrfs_alloc_path(); |
| 1438 | if (!path) |
| 1439 | return -ENOMEM; |
| 1440 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1441 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1442 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1443 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1444 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1445 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1446 | if (ret) |
| 1447 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1448 | |
| 1449 | leaf = path->nodes[0]; |
| 1450 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1451 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1452 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1453 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1454 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1455 | |
| 1456 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1457 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1458 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1459 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1460 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1461 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1462 | btrfs_free_path(path); |
| 1463 | return ret; |
| 1464 | } |
| 1465 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1466 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1467 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1468 | struct extent_map_tree *em_tree; |
| 1469 | struct extent_map *em; |
| 1470 | struct rb_node *n; |
| 1471 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1472 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1473 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1474 | read_lock(&em_tree->lock); |
| 1475 | n = rb_last(&em_tree->map); |
| 1476 | if (n) { |
| 1477 | em = rb_entry(n, struct extent_map, rb_node); |
| 1478 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1479 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1480 | read_unlock(&em_tree->lock); |
| 1481 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1482 | return ret; |
| 1483 | } |
| 1484 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1485 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1486 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1487 | { |
| 1488 | int ret; |
| 1489 | struct btrfs_key key; |
| 1490 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1491 | struct btrfs_path *path; |
| 1492 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1493 | path = btrfs_alloc_path(); |
| 1494 | if (!path) |
| 1495 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1496 | |
| 1497 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1498 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1499 | key.offset = (u64)-1; |
| 1500 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1501 | 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] | 1502 | if (ret < 0) |
| 1503 | goto error; |
| 1504 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1505 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1506 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1507 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1508 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1509 | BTRFS_DEV_ITEM_KEY); |
| 1510 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1511 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1512 | } else { |
| 1513 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1514 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1515 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1516 | } |
| 1517 | ret = 0; |
| 1518 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1519 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1520 | return ret; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | * the device information is stored in the chunk root |
| 1525 | * the btrfs_device struct should be fully filled in |
| 1526 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1527 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1528 | struct btrfs_root *root, |
| 1529 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1530 | { |
| 1531 | int ret; |
| 1532 | struct btrfs_path *path; |
| 1533 | struct btrfs_dev_item *dev_item; |
| 1534 | struct extent_buffer *leaf; |
| 1535 | struct btrfs_key key; |
| 1536 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1537 | |
| 1538 | root = root->fs_info->chunk_root; |
| 1539 | |
| 1540 | path = btrfs_alloc_path(); |
| 1541 | if (!path) |
| 1542 | return -ENOMEM; |
| 1543 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1544 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1545 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1546 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1547 | |
| 1548 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1549 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1550 | if (ret) |
| 1551 | goto out; |
| 1552 | |
| 1553 | leaf = path->nodes[0]; |
| 1554 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1555 | |
| 1556 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1557 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1558 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1559 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1560 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1561 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1562 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1563 | btrfs_device_get_disk_total_bytes(device)); |
| 1564 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1565 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1566 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1567 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1568 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1569 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1570 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1571 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1572 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1573 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1574 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1575 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1576 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1577 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1578 | out: |
| 1579 | btrfs_free_path(path); |
| 1580 | return ret; |
| 1581 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1582 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1583 | /* |
| 1584 | * Function to update ctime/mtime for a given device path. |
| 1585 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1586 | */ |
| 1587 | static void update_dev_time(char *path_name) |
| 1588 | { |
| 1589 | struct file *filp; |
| 1590 | |
| 1591 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1592 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1593 | return; |
| 1594 | file_update_time(filp); |
| 1595 | filp_close(filp, NULL); |
| 1596 | return; |
| 1597 | } |
| 1598 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1599 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1600 | struct btrfs_device *device) |
| 1601 | { |
| 1602 | int ret; |
| 1603 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1604 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1605 | struct btrfs_trans_handle *trans; |
| 1606 | |
| 1607 | root = root->fs_info->chunk_root; |
| 1608 | |
| 1609 | path = btrfs_alloc_path(); |
| 1610 | if (!path) |
| 1611 | return -ENOMEM; |
| 1612 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1613 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1614 | if (IS_ERR(trans)) { |
| 1615 | btrfs_free_path(path); |
| 1616 | return PTR_ERR(trans); |
| 1617 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1618 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1619 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1620 | key.offset = device->devid; |
| 1621 | |
| 1622 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1623 | if (ret < 0) |
| 1624 | goto out; |
| 1625 | |
| 1626 | if (ret > 0) { |
| 1627 | ret = -ENOENT; |
| 1628 | goto out; |
| 1629 | } |
| 1630 | |
| 1631 | ret = btrfs_del_item(trans, root, path); |
| 1632 | if (ret) |
| 1633 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1634 | out: |
| 1635 | btrfs_free_path(path); |
| 1636 | btrfs_commit_transaction(trans, root); |
| 1637 | return ret; |
| 1638 | } |
| 1639 | |
| 1640 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1641 | { |
| 1642 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1643 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1644 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1645 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1646 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1647 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1648 | u64 all_avail; |
| 1649 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1650 | u64 num_devices; |
| 1651 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1652 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1653 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1654 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1655 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1656 | mutex_lock(&uuid_mutex); |
| 1657 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1658 | do { |
| 1659 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1660 | |
| 1661 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1662 | root->fs_info->avail_system_alloc_bits | |
| 1663 | root->fs_info->avail_metadata_alloc_bits; |
| 1664 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1665 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1666 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1667 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1668 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1669 | WARN_ON(num_devices < 1); |
| 1670 | num_devices--; |
| 1671 | } |
| 1672 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1673 | |
| 1674 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1675 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1676 | goto out; |
| 1677 | } |
| 1678 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1679 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1680 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1681 | goto out; |
| 1682 | } |
| 1683 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1684 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1685 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1686 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1687 | goto out; |
| 1688 | } |
| 1689 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1690 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1691 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1692 | goto out; |
| 1693 | } |
| 1694 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1695 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1696 | struct list_head *devices; |
| 1697 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1698 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1699 | device = NULL; |
| 1700 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1701 | /* |
| 1702 | * It is safe to read the devices since the volume_mutex |
| 1703 | * is held. |
| 1704 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1705 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1706 | if (tmp->in_fs_metadata && |
| 1707 | !tmp->is_tgtdev_for_dev_replace && |
| 1708 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1709 | device = tmp; |
| 1710 | break; |
| 1711 | } |
| 1712 | } |
| 1713 | bdev = NULL; |
| 1714 | bh = NULL; |
| 1715 | disk_super = NULL; |
| 1716 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1717 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1718 | goto out; |
| 1719 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1720 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1721 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1722 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1723 | root->fs_info->bdev_holder, 0, |
| 1724 | &bdev, &bh); |
| 1725 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1726 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1727 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1728 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1729 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1730 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1731 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1732 | if (!device) { |
| 1733 | ret = -ENOENT; |
| 1734 | goto error_brelse; |
| 1735 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1736 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1737 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1738 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1739 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1740 | goto error_brelse; |
| 1741 | } |
| 1742 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1743 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1744 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1745 | goto error_brelse; |
| 1746 | } |
| 1747 | |
| 1748 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1749 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1750 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1751 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1752 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1753 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1754 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1755 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1756 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1757 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1758 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1759 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1760 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1761 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1762 | /* |
| 1763 | * TODO: the superblock still includes this device in its num_devices |
| 1764 | * counter although write_all_supers() is not locked out. This |
| 1765 | * could give a filesystem state which requires a degraded mount. |
| 1766 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1767 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1768 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1769 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1770 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1771 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1772 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1773 | |
| 1774 | /* |
| 1775 | * the device list mutex makes sure that we don't change |
| 1776 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1777 | * the device supers. Whoever is writing all supers, should |
| 1778 | * lock the device list mutex before getting the number of |
| 1779 | * devices in the super block (super_copy). Conversely, |
| 1780 | * whoever updates the number of devices in the super block |
| 1781 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1782 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1783 | |
| 1784 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1785 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1786 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1787 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1788 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1789 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1790 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1791 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1792 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1793 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1794 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1795 | struct btrfs_device, dev_list); |
| 1796 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1797 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1798 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1799 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1800 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1801 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1802 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1803 | /* remove sysfs entry */ |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 1804 | btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1805 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1806 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1807 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1808 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1809 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1810 | 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] | 1811 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1812 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1813 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1814 | struct btrfs_fs_devices *fs_devices; |
| 1815 | fs_devices = root->fs_info->fs_devices; |
| 1816 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1817 | if (fs_devices->seed == cur_devices) { |
| 1818 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1819 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1820 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1821 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1822 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1823 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1824 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1825 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1826 | } |
| 1827 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1828 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1829 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1830 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1831 | /* |
| 1832 | * at this point, the device is zero sized. We want to |
| 1833 | * remove it from the devices list and zero out the old super |
| 1834 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1835 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1836 | u64 bytenr; |
| 1837 | int i; |
| 1838 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1839 | /* make sure this device isn't detected as part of |
| 1840 | * the FS anymore |
| 1841 | */ |
| 1842 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1843 | set_buffer_dirty(bh); |
| 1844 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1845 | |
| 1846 | /* clear the mirror copies of super block on the disk |
| 1847 | * being removed, 0th copy is been taken care above and |
| 1848 | * the below would take of the rest |
| 1849 | */ |
| 1850 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1851 | bytenr = btrfs_sb_offset(i); |
| 1852 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1853 | i_size_read(bdev->bd_inode)) |
| 1854 | break; |
| 1855 | |
| 1856 | brelse(bh); |
| 1857 | bh = __bread(bdev, bytenr / 4096, |
| 1858 | BTRFS_SUPER_INFO_SIZE); |
| 1859 | if (!bh) |
| 1860 | continue; |
| 1861 | |
| 1862 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1863 | |
| 1864 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1865 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1866 | continue; |
| 1867 | } |
| 1868 | memset(&disk_super->magic, 0, |
| 1869 | sizeof(disk_super->magic)); |
| 1870 | set_buffer_dirty(bh); |
| 1871 | sync_dirty_buffer(bh); |
| 1872 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1873 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1874 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1875 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1876 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1877 | if (bdev) { |
| 1878 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1879 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1880 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1881 | /* Update ctime/mtime for device path for libblkid */ |
| 1882 | update_dev_time(device_path); |
| 1883 | } |
| 1884 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1885 | error_brelse: |
| 1886 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1887 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1888 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1889 | out: |
| 1890 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1891 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1892 | error_undo: |
| 1893 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1894 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1895 | list_add(&device->dev_alloc_list, |
| 1896 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1897 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1898 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1899 | } |
| 1900 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1901 | } |
| 1902 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1903 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1904 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1905 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1906 | struct btrfs_fs_devices *fs_devices; |
| 1907 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1908 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1909 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1910 | /* |
| 1911 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1912 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1913 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1914 | * srcdev will have its correct fs_devices in both the cases. |
| 1915 | */ |
| 1916 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1917 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1918 | list_del_rcu(&srcdev->dev_list); |
| 1919 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1920 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1921 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1922 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1923 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1924 | if (srcdev->writeable) { |
| 1925 | fs_devices->rw_devices--; |
| 1926 | /* zero out the old super if it is writable */ |
| 1927 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1928 | } |
| 1929 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1930 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1931 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 1935 | struct btrfs_device *srcdev) |
| 1936 | { |
| 1937 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1938 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1939 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1940 | |
| 1941 | /* |
| 1942 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 1943 | * zero |
| 1944 | */ |
| 1945 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 1946 | |
| 1947 | /* if this is no devs we rather delete the fs_devices */ |
| 1948 | if (!fs_devices->num_devices) { |
| 1949 | struct btrfs_fs_devices *tmp_fs_devices; |
| 1950 | |
| 1951 | tmp_fs_devices = fs_info->fs_devices; |
| 1952 | while (tmp_fs_devices) { |
| 1953 | if (tmp_fs_devices->seed == fs_devices) { |
| 1954 | tmp_fs_devices->seed = fs_devices->seed; |
| 1955 | break; |
| 1956 | } |
| 1957 | tmp_fs_devices = tmp_fs_devices->seed; |
| 1958 | } |
| 1959 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 1960 | __btrfs_close_devices(fs_devices); |
| 1961 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1962 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1966 | struct btrfs_device *tgtdev) |
| 1967 | { |
| 1968 | struct btrfs_device *next_device; |
| 1969 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1970 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1971 | WARN_ON(!tgtdev); |
| 1972 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 1973 | |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 1974 | btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev); |
Anand Jain | d2ff1b2 | 2015-03-10 06:38:42 +0800 | [diff] [blame] | 1975 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1976 | if (tgtdev->bdev) { |
| 1977 | btrfs_scratch_superblock(tgtdev); |
| 1978 | fs_info->fs_devices->open_devices--; |
| 1979 | } |
| 1980 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1981 | |
| 1982 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1983 | struct btrfs_device, dev_list); |
| 1984 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1985 | fs_info->sb->s_bdev = next_device->bdev; |
| 1986 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1987 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1988 | list_del_rcu(&tgtdev->dev_list); |
| 1989 | |
| 1990 | call_rcu(&tgtdev->rcu, free_device); |
| 1991 | |
| 1992 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1993 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1994 | } |
| 1995 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1996 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1997 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1998 | { |
| 1999 | int ret = 0; |
| 2000 | struct btrfs_super_block *disk_super; |
| 2001 | u64 devid; |
| 2002 | u8 *dev_uuid; |
| 2003 | struct block_device *bdev; |
| 2004 | struct buffer_head *bh; |
| 2005 | |
| 2006 | *device = NULL; |
| 2007 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 2008 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 2009 | if (ret) |
| 2010 | return ret; |
| 2011 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 2012 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 2013 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2014 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2015 | disk_super->fsid); |
| 2016 | brelse(bh); |
| 2017 | if (!*device) |
| 2018 | ret = -ENOENT; |
| 2019 | blkdev_put(bdev, FMODE_READ); |
| 2020 | return ret; |
| 2021 | } |
| 2022 | |
| 2023 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 2024 | char *device_path, |
| 2025 | struct btrfs_device **device) |
| 2026 | { |
| 2027 | *device = NULL; |
| 2028 | if (strcmp(device_path, "missing") == 0) { |
| 2029 | struct list_head *devices; |
| 2030 | struct btrfs_device *tmp; |
| 2031 | |
| 2032 | devices = &root->fs_info->fs_devices->devices; |
| 2033 | /* |
| 2034 | * It is safe to read the devices since the volume_mutex |
| 2035 | * is held by the caller. |
| 2036 | */ |
| 2037 | list_for_each_entry(tmp, devices, dev_list) { |
| 2038 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 2039 | *device = tmp; |
| 2040 | break; |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 2045 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 2046 | return -ENOENT; |
| 2047 | } |
| 2048 | |
| 2049 | return 0; |
| 2050 | } else { |
| 2051 | return btrfs_find_device_by_path(root, device_path, device); |
| 2052 | } |
| 2053 | } |
| 2054 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2055 | /* |
| 2056 | * does all the dirty work required for changing file system's UUID. |
| 2057 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2058 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2059 | { |
| 2060 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 2061 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2062 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2063 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2064 | struct btrfs_device *device; |
| 2065 | u64 super_flags; |
| 2066 | |
| 2067 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2068 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2069 | return -EINVAL; |
| 2070 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 2071 | seed_devices = __alloc_fs_devices(); |
| 2072 | if (IS_ERR(seed_devices)) |
| 2073 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2074 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2075 | old_devices = clone_fs_devices(fs_devices); |
| 2076 | if (IS_ERR(old_devices)) { |
| 2077 | kfree(seed_devices); |
| 2078 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2079 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2080 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2081 | list_add(&old_devices->list, &fs_uuids); |
| 2082 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2083 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 2084 | seed_devices->opened = 1; |
| 2085 | INIT_LIST_HEAD(&seed_devices->devices); |
| 2086 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2087 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 2088 | |
| 2089 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2090 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 2091 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2092 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2093 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2094 | |
| 2095 | lock_chunks(root); |
| 2096 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 2097 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2098 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2099 | fs_devices->seeding = 0; |
| 2100 | fs_devices->num_devices = 0; |
| 2101 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2102 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2103 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2104 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2105 | |
| 2106 | generate_random_uuid(fs_devices->fsid); |
| 2107 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2108 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2109 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2110 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2111 | super_flags = btrfs_super_flags(disk_super) & |
| 2112 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2113 | btrfs_set_super_flags(disk_super, super_flags); |
| 2114 | |
| 2115 | return 0; |
| 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | * strore the expected generation for seed devices in device items. |
| 2120 | */ |
| 2121 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2122 | struct btrfs_root *root) |
| 2123 | { |
| 2124 | struct btrfs_path *path; |
| 2125 | struct extent_buffer *leaf; |
| 2126 | struct btrfs_dev_item *dev_item; |
| 2127 | struct btrfs_device *device; |
| 2128 | struct btrfs_key key; |
| 2129 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2130 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2131 | u64 devid; |
| 2132 | int ret; |
| 2133 | |
| 2134 | path = btrfs_alloc_path(); |
| 2135 | if (!path) |
| 2136 | return -ENOMEM; |
| 2137 | |
| 2138 | root = root->fs_info->chunk_root; |
| 2139 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2140 | key.offset = 0; |
| 2141 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2142 | |
| 2143 | while (1) { |
| 2144 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2145 | if (ret < 0) |
| 2146 | goto error; |
| 2147 | |
| 2148 | leaf = path->nodes[0]; |
| 2149 | next_slot: |
| 2150 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2151 | ret = btrfs_next_leaf(root, path); |
| 2152 | if (ret > 0) |
| 2153 | break; |
| 2154 | if (ret < 0) |
| 2155 | goto error; |
| 2156 | leaf = path->nodes[0]; |
| 2157 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2158 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2159 | continue; |
| 2160 | } |
| 2161 | |
| 2162 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2163 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2164 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2165 | break; |
| 2166 | |
| 2167 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2168 | struct btrfs_dev_item); |
| 2169 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2170 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2171 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2172 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2173 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2174 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2175 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2176 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2177 | |
| 2178 | if (device->fs_devices->seeding) { |
| 2179 | btrfs_set_device_generation(leaf, dev_item, |
| 2180 | device->generation); |
| 2181 | btrfs_mark_buffer_dirty(leaf); |
| 2182 | } |
| 2183 | |
| 2184 | path->slots[0]++; |
| 2185 | goto next_slot; |
| 2186 | } |
| 2187 | ret = 0; |
| 2188 | error: |
| 2189 | btrfs_free_path(path); |
| 2190 | return ret; |
| 2191 | } |
| 2192 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2193 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2194 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2195 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2196 | struct btrfs_trans_handle *trans; |
| 2197 | struct btrfs_device *device; |
| 2198 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2199 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2200 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2201 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2202 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2203 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2204 | int ret = 0; |
| 2205 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2206 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2207 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2208 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2209 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2210 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2211 | if (IS_ERR(bdev)) |
| 2212 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2213 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2214 | if (root->fs_info->fs_devices->seeding) { |
| 2215 | seeding_dev = 1; |
| 2216 | down_write(&sb->s_umount); |
| 2217 | mutex_lock(&uuid_mutex); |
| 2218 | } |
| 2219 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2220 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2221 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2222 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2223 | |
| 2224 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2225 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2226 | if (device->bdev == bdev) { |
| 2227 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2228 | mutex_unlock( |
| 2229 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2230 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2231 | } |
| 2232 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2233 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2234 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2235 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2236 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2237 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2238 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2239 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2240 | } |
| 2241 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2242 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2243 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2244 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2245 | ret = -ENOMEM; |
| 2246 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2247 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2248 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2249 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2250 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2251 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2252 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2253 | kfree(device); |
| 2254 | ret = PTR_ERR(trans); |
| 2255 | goto error; |
| 2256 | } |
| 2257 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2258 | q = bdev_get_queue(bdev); |
| 2259 | if (blk_queue_discard(q)) |
| 2260 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2261 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2262 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2263 | device->io_width = root->sectorsize; |
| 2264 | device->io_align = root->sectorsize; |
| 2265 | device->sector_size = root->sectorsize; |
| 2266 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2267 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2268 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2269 | device->dev_root = root->fs_info->dev_root; |
| 2270 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2271 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2272 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2273 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2274 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2275 | set_blocksize(device->bdev, 4096); |
| 2276 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2277 | if (seeding_dev) { |
| 2278 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2279 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2280 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2284 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2285 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2286 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2287 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2288 | list_add(&device->dev_alloc_list, |
| 2289 | &root->fs_info->fs_devices->alloc_list); |
| 2290 | root->fs_info->fs_devices->num_devices++; |
| 2291 | root->fs_info->fs_devices->open_devices++; |
| 2292 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2293 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2294 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2295 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2296 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2297 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2298 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2299 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2300 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2301 | root->fs_info->fs_devices->rotating = 1; |
| 2302 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2303 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2304 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2305 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2306 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2307 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2308 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2309 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2310 | |
| 2311 | /* add sysfs device entry */ |
Anand Jain | e3bd697 | 2015-08-14 18:32:48 +0800 | [diff] [blame] | 2312 | btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2313 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2314 | /* |
| 2315 | * we've got more storage, clear any full flags on the space |
| 2316 | * infos |
| 2317 | */ |
| 2318 | btrfs_clear_space_info_full(root->fs_info); |
| 2319 | |
| 2320 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2321 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2322 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2323 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2324 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2325 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2326 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2327 | if (ret) { |
| 2328 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2329 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2330 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | ret = btrfs_add_device(trans, root, device); |
| 2334 | if (ret) { |
| 2335 | btrfs_abort_transaction(trans, root, ret); |
| 2336 | goto error_trans; |
| 2337 | } |
| 2338 | |
| 2339 | if (seeding_dev) { |
| 2340 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2341 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2342 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2343 | if (ret) { |
| 2344 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2345 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2346 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2347 | |
| 2348 | /* Sprouting would change fsid of the mounted root, |
| 2349 | * so rename the fsid on the sysfs |
| 2350 | */ |
| 2351 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2352 | root->fs_info->fsid); |
Anand Jain | c1b7e47 | 2015-08-14 18:32:50 +0800 | [diff] [blame] | 2353 | if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj, |
Anand Jain | 2e7910d | 2015-03-10 06:38:29 +0800 | [diff] [blame] | 2354 | fsid_buf)) |
Anand Jain | 2421a8c | 2015-03-10 06:38:40 +0800 | [diff] [blame] | 2355 | pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n"); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2356 | } |
| 2357 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2358 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2359 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2360 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2361 | |
| 2362 | if (seeding_dev) { |
| 2363 | mutex_unlock(&uuid_mutex); |
| 2364 | up_write(&sb->s_umount); |
| 2365 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2366 | if (ret) /* transaction commit */ |
| 2367 | return ret; |
| 2368 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2369 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2370 | if (ret < 0) |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame^] | 2371 | btrfs_std_error(root->fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2372 | "Failed to relocate sys chunks after " |
| 2373 | "device initialization. This can be fixed " |
| 2374 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2375 | trans = btrfs_attach_transaction(root); |
| 2376 | if (IS_ERR(trans)) { |
| 2377 | if (PTR_ERR(trans) == -ENOENT) |
| 2378 | return 0; |
| 2379 | return PTR_ERR(trans); |
| 2380 | } |
| 2381 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2382 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2383 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2384 | /* Update ctime/mtime for libblkid */ |
| 2385 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2386 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2387 | |
| 2388 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2389 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2390 | rcu_string_free(device->name); |
Anand Jain | 3257604 | 2015-08-14 18:32:49 +0800 | [diff] [blame] | 2391 | btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2392 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2393 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2394 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2395 | if (seeding_dev) { |
| 2396 | mutex_unlock(&uuid_mutex); |
| 2397 | up_write(&sb->s_umount); |
| 2398 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2399 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2400 | } |
| 2401 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2402 | 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] | 2403 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2404 | struct btrfs_device **device_out) |
| 2405 | { |
| 2406 | struct request_queue *q; |
| 2407 | struct btrfs_device *device; |
| 2408 | struct block_device *bdev; |
| 2409 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2410 | struct list_head *devices; |
| 2411 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2412 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2413 | int ret = 0; |
| 2414 | |
| 2415 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2416 | if (fs_info->fs_devices->seeding) { |
| 2417 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2418 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2419 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2420 | |
| 2421 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2422 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2423 | if (IS_ERR(bdev)) { |
| 2424 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2425 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2426 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2427 | |
| 2428 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2429 | |
| 2430 | devices = &fs_info->fs_devices->devices; |
| 2431 | list_for_each_entry(device, devices, dev_list) { |
| 2432 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2433 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2434 | ret = -EEXIST; |
| 2435 | goto error; |
| 2436 | } |
| 2437 | } |
| 2438 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2439 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2440 | if (i_size_read(bdev->bd_inode) < |
| 2441 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2442 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2443 | ret = -EINVAL; |
| 2444 | goto error; |
| 2445 | } |
| 2446 | |
| 2447 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2448 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2449 | if (IS_ERR(device)) { |
| 2450 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2451 | goto error; |
| 2452 | } |
| 2453 | |
| 2454 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2455 | if (!name) { |
| 2456 | kfree(device); |
| 2457 | ret = -ENOMEM; |
| 2458 | goto error; |
| 2459 | } |
| 2460 | rcu_assign_pointer(device->name, name); |
| 2461 | |
| 2462 | q = bdev_get_queue(bdev); |
| 2463 | if (blk_queue_discard(q)) |
| 2464 | device->can_discard = 1; |
| 2465 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2466 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2467 | device->generation = 0; |
| 2468 | device->io_width = root->sectorsize; |
| 2469 | device->io_align = root->sectorsize; |
| 2470 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2471 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2472 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2473 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2474 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2475 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2476 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2477 | device->dev_root = fs_info->dev_root; |
| 2478 | device->bdev = bdev; |
| 2479 | device->in_fs_metadata = 1; |
| 2480 | device->is_tgtdev_for_dev_replace = 1; |
| 2481 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2482 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2483 | set_blocksize(device->bdev, 4096); |
| 2484 | device->fs_devices = fs_info->fs_devices; |
| 2485 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2486 | fs_info->fs_devices->num_devices++; |
| 2487 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2488 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2489 | |
| 2490 | *device_out = device; |
| 2491 | return ret; |
| 2492 | |
| 2493 | error: |
| 2494 | blkdev_put(bdev, FMODE_EXCL); |
| 2495 | return ret; |
| 2496 | } |
| 2497 | |
| 2498 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2499 | struct btrfs_device *tgtdev) |
| 2500 | { |
| 2501 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2502 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2503 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2504 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2505 | tgtdev->dev_root = fs_info->dev_root; |
| 2506 | tgtdev->in_fs_metadata = 1; |
| 2507 | } |
| 2508 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2509 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2510 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2511 | { |
| 2512 | int ret; |
| 2513 | struct btrfs_path *path; |
| 2514 | struct btrfs_root *root; |
| 2515 | struct btrfs_dev_item *dev_item; |
| 2516 | struct extent_buffer *leaf; |
| 2517 | struct btrfs_key key; |
| 2518 | |
| 2519 | root = device->dev_root->fs_info->chunk_root; |
| 2520 | |
| 2521 | path = btrfs_alloc_path(); |
| 2522 | if (!path) |
| 2523 | return -ENOMEM; |
| 2524 | |
| 2525 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2526 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2527 | key.offset = device->devid; |
| 2528 | |
| 2529 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2530 | if (ret < 0) |
| 2531 | goto out; |
| 2532 | |
| 2533 | if (ret > 0) { |
| 2534 | ret = -ENOENT; |
| 2535 | goto out; |
| 2536 | } |
| 2537 | |
| 2538 | leaf = path->nodes[0]; |
| 2539 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2540 | |
| 2541 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2542 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2543 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2544 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2545 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2546 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2547 | btrfs_device_get_disk_total_bytes(device)); |
| 2548 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2549 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2550 | btrfs_mark_buffer_dirty(leaf); |
| 2551 | |
| 2552 | out: |
| 2553 | btrfs_free_path(path); |
| 2554 | return ret; |
| 2555 | } |
| 2556 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2557 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2558 | struct btrfs_device *device, u64 new_size) |
| 2559 | { |
| 2560 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2561 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2562 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2563 | u64 old_total; |
| 2564 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2565 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2566 | if (!device->writeable) |
| 2567 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2568 | |
| 2569 | lock_chunks(device->dev_root); |
| 2570 | old_total = btrfs_super_total_bytes(super_copy); |
| 2571 | diff = new_size - device->total_bytes; |
| 2572 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2573 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2574 | device->is_tgtdev_for_dev_replace) { |
| 2575 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2576 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2577 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2578 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2579 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2580 | |
| 2581 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2582 | device->fs_devices->total_rw_bytes += diff; |
| 2583 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2584 | btrfs_device_set_total_bytes(device, new_size); |
| 2585 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2586 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2587 | if (list_empty(&device->resized_list)) |
| 2588 | list_add_tail(&device->resized_list, |
| 2589 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2590 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2591 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2592 | return btrfs_update_device(trans, device); |
| 2593 | } |
| 2594 | |
| 2595 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2596 | struct btrfs_root *root, u64 chunk_objectid, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2597 | u64 chunk_offset) |
| 2598 | { |
| 2599 | int ret; |
| 2600 | struct btrfs_path *path; |
| 2601 | struct btrfs_key key; |
| 2602 | |
| 2603 | root = root->fs_info->chunk_root; |
| 2604 | path = btrfs_alloc_path(); |
| 2605 | if (!path) |
| 2606 | return -ENOMEM; |
| 2607 | |
| 2608 | key.objectid = chunk_objectid; |
| 2609 | key.offset = chunk_offset; |
| 2610 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2611 | |
| 2612 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2613 | if (ret < 0) |
| 2614 | goto out; |
| 2615 | else if (ret > 0) { /* Logic error or corruption */ |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame^] | 2616 | btrfs_std_error(root->fs_info, -ENOENT, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2617 | "Failed lookup while freeing chunk."); |
| 2618 | ret = -ENOENT; |
| 2619 | goto out; |
| 2620 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2621 | |
| 2622 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2623 | if (ret < 0) |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame^] | 2624 | btrfs_std_error(root->fs_info, ret, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2625 | "Failed to delete chunk item."); |
| 2626 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2627 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2628 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2629 | } |
| 2630 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2631 | 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] | 2632 | chunk_offset) |
| 2633 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2634 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2635 | struct btrfs_disk_key *disk_key; |
| 2636 | struct btrfs_chunk *chunk; |
| 2637 | u8 *ptr; |
| 2638 | int ret = 0; |
| 2639 | u32 num_stripes; |
| 2640 | u32 array_size; |
| 2641 | u32 len = 0; |
| 2642 | u32 cur; |
| 2643 | struct btrfs_key key; |
| 2644 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2645 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2646 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2647 | |
| 2648 | ptr = super_copy->sys_chunk_array; |
| 2649 | cur = 0; |
| 2650 | |
| 2651 | while (cur < array_size) { |
| 2652 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2653 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2654 | |
| 2655 | len = sizeof(*disk_key); |
| 2656 | |
| 2657 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2658 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2659 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2660 | len += btrfs_chunk_item_size(num_stripes); |
| 2661 | } else { |
| 2662 | ret = -EIO; |
| 2663 | break; |
| 2664 | } |
| 2665 | if (key.objectid == chunk_objectid && |
| 2666 | key.offset == chunk_offset) { |
| 2667 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2668 | array_size -= len; |
| 2669 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2670 | } else { |
| 2671 | ptr += len; |
| 2672 | cur += len; |
| 2673 | } |
| 2674 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2675 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2676 | return ret; |
| 2677 | } |
| 2678 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2679 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2680 | struct btrfs_root *root, u64 chunk_offset) |
| 2681 | { |
| 2682 | struct extent_map_tree *em_tree; |
| 2683 | struct extent_map *em; |
| 2684 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2685 | struct map_lookup *map; |
| 2686 | u64 dev_extent_len = 0; |
| 2687 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2688 | int i, ret = 0; |
| 2689 | |
| 2690 | /* Just in case */ |
| 2691 | root = root->fs_info->chunk_root; |
| 2692 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2693 | |
| 2694 | read_lock(&em_tree->lock); |
| 2695 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2696 | read_unlock(&em_tree->lock); |
| 2697 | |
| 2698 | if (!em || em->start > chunk_offset || |
| 2699 | em->start + em->len < chunk_offset) { |
| 2700 | /* |
| 2701 | * This is a logic error, but we don't want to just rely on the |
| 2702 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2703 | * do anything we still error out. |
| 2704 | */ |
| 2705 | ASSERT(0); |
| 2706 | if (em) |
| 2707 | free_extent_map(em); |
| 2708 | return -EINVAL; |
| 2709 | } |
| 2710 | map = (struct map_lookup *)em->bdev; |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2711 | lock_chunks(root->fs_info->chunk_root); |
Filipe Manana | 4617ea3 | 2015-06-09 17:48:21 +0100 | [diff] [blame] | 2712 | check_system_chunk(trans, extent_root, map->type); |
Filipe Manana | 39c2d7f | 2015-05-20 14:01:55 +0100 | [diff] [blame] | 2713 | unlock_chunks(root->fs_info->chunk_root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2714 | |
| 2715 | for (i = 0; i < map->num_stripes; i++) { |
| 2716 | struct btrfs_device *device = map->stripes[i].dev; |
| 2717 | ret = btrfs_free_dev_extent(trans, device, |
| 2718 | map->stripes[i].physical, |
| 2719 | &dev_extent_len); |
| 2720 | if (ret) { |
| 2721 | btrfs_abort_transaction(trans, root, ret); |
| 2722 | goto out; |
| 2723 | } |
| 2724 | |
| 2725 | if (device->bytes_used > 0) { |
| 2726 | lock_chunks(root); |
| 2727 | btrfs_device_set_bytes_used(device, |
| 2728 | device->bytes_used - dev_extent_len); |
| 2729 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2730 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2731 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2732 | btrfs_clear_space_info_full(root->fs_info); |
| 2733 | unlock_chunks(root); |
| 2734 | } |
| 2735 | |
| 2736 | if (map->stripes[i].dev) { |
| 2737 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2738 | if (ret) { |
| 2739 | btrfs_abort_transaction(trans, root, ret); |
| 2740 | goto out; |
| 2741 | } |
| 2742 | } |
| 2743 | } |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2744 | ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2745 | if (ret) { |
| 2746 | btrfs_abort_transaction(trans, root, ret); |
| 2747 | goto out; |
| 2748 | } |
| 2749 | |
| 2750 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2751 | |
| 2752 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2753 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2754 | if (ret) { |
| 2755 | btrfs_abort_transaction(trans, root, ret); |
| 2756 | goto out; |
| 2757 | } |
| 2758 | } |
| 2759 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2760 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2761 | if (ret) { |
| 2762 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2763 | goto out; |
| 2764 | } |
| 2765 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2766 | out: |
| 2767 | /* once for us */ |
| 2768 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2769 | return ret; |
| 2770 | } |
| 2771 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 2772 | static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2773 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2774 | struct btrfs_root *extent_root; |
| 2775 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2776 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2777 | |
| 2778 | root = root->fs_info->chunk_root; |
| 2779 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2780 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2781 | /* |
| 2782 | * Prevent races with automatic removal of unused block groups. |
| 2783 | * After we relocate and before we remove the chunk with offset |
| 2784 | * chunk_offset, automatic removal of the block group can kick in, |
| 2785 | * resulting in a failure when calling btrfs_remove_chunk() below. |
| 2786 | * |
| 2787 | * Make sure to acquire this mutex before doing a tree search (dev |
| 2788 | * or chunk trees) to find chunks. Otherwise the cleaner kthread might |
| 2789 | * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after |
| 2790 | * we release the path used to search the chunk/dev tree and before |
| 2791 | * the current task acquires this mutex and calls us. |
| 2792 | */ |
| 2793 | ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex)); |
| 2794 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2795 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2796 | if (ret) |
| 2797 | return -ENOSPC; |
| 2798 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2799 | /* step one, relocate all the extents inside this chunk */ |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2800 | btrfs_scrub_pause(root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2801 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Zhaolei | 55e3a60 | 2015-08-05 16:43:30 +0800 | [diff] [blame] | 2802 | btrfs_scrub_continue(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2803 | if (ret) |
| 2804 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2805 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2806 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2807 | if (IS_ERR(trans)) { |
| 2808 | ret = PTR_ERR(trans); |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame^] | 2809 | btrfs_std_error(root->fs_info, ret, NULL); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2810 | return ret; |
| 2811 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2812 | |
| 2813 | /* |
| 2814 | * step two, delete the device extents and the |
| 2815 | * chunk tree entries |
| 2816 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2817 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2818 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2819 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2820 | } |
| 2821 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2822 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2823 | { |
| 2824 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2825 | struct btrfs_path *path; |
| 2826 | struct extent_buffer *leaf; |
| 2827 | struct btrfs_chunk *chunk; |
| 2828 | struct btrfs_key key; |
| 2829 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2830 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2831 | bool retried = false; |
| 2832 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2833 | int ret; |
| 2834 | |
| 2835 | path = btrfs_alloc_path(); |
| 2836 | if (!path) |
| 2837 | return -ENOMEM; |
| 2838 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2839 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2840 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2841 | key.offset = (u64)-1; |
| 2842 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2843 | |
| 2844 | while (1) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2845 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2846 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2847 | if (ret < 0) { |
| 2848 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2849 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2850 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2851 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2852 | |
| 2853 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2854 | key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2855 | if (ret) |
| 2856 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2857 | if (ret < 0) |
| 2858 | goto error; |
| 2859 | if (ret > 0) |
| 2860 | break; |
| 2861 | |
| 2862 | leaf = path->nodes[0]; |
| 2863 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2864 | |
| 2865 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2866 | struct btrfs_chunk); |
| 2867 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2868 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2869 | |
| 2870 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2871 | ret = btrfs_relocate_chunk(chunk_root, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2872 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2873 | if (ret == -ENOSPC) |
| 2874 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2875 | else |
| 2876 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2877 | } |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 2878 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2879 | |
| 2880 | if (found_key.offset == 0) |
| 2881 | break; |
| 2882 | key.offset = found_key.offset - 1; |
| 2883 | } |
| 2884 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2885 | if (failed && !retried) { |
| 2886 | failed = 0; |
| 2887 | retried = true; |
| 2888 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2889 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2890 | ret = -ENOSPC; |
| 2891 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2892 | error: |
| 2893 | btrfs_free_path(path); |
| 2894 | return ret; |
| 2895 | } |
| 2896 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2897 | static int insert_balance_item(struct btrfs_root *root, |
| 2898 | struct btrfs_balance_control *bctl) |
| 2899 | { |
| 2900 | struct btrfs_trans_handle *trans; |
| 2901 | struct btrfs_balance_item *item; |
| 2902 | struct btrfs_disk_balance_args disk_bargs; |
| 2903 | struct btrfs_path *path; |
| 2904 | struct extent_buffer *leaf; |
| 2905 | struct btrfs_key key; |
| 2906 | int ret, err; |
| 2907 | |
| 2908 | path = btrfs_alloc_path(); |
| 2909 | if (!path) |
| 2910 | return -ENOMEM; |
| 2911 | |
| 2912 | trans = btrfs_start_transaction(root, 0); |
| 2913 | if (IS_ERR(trans)) { |
| 2914 | btrfs_free_path(path); |
| 2915 | return PTR_ERR(trans); |
| 2916 | } |
| 2917 | |
| 2918 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2919 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2920 | key.offset = 0; |
| 2921 | |
| 2922 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2923 | sizeof(*item)); |
| 2924 | if (ret) |
| 2925 | goto out; |
| 2926 | |
| 2927 | leaf = path->nodes[0]; |
| 2928 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2929 | |
| 2930 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2931 | |
| 2932 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2933 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2934 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2935 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2936 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2937 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2938 | |
| 2939 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2940 | |
| 2941 | btrfs_mark_buffer_dirty(leaf); |
| 2942 | out: |
| 2943 | btrfs_free_path(path); |
| 2944 | err = btrfs_commit_transaction(trans, root); |
| 2945 | if (err && !ret) |
| 2946 | ret = err; |
| 2947 | return ret; |
| 2948 | } |
| 2949 | |
| 2950 | static int del_balance_item(struct btrfs_root *root) |
| 2951 | { |
| 2952 | struct btrfs_trans_handle *trans; |
| 2953 | struct btrfs_path *path; |
| 2954 | struct btrfs_key key; |
| 2955 | int ret, err; |
| 2956 | |
| 2957 | path = btrfs_alloc_path(); |
| 2958 | if (!path) |
| 2959 | return -ENOMEM; |
| 2960 | |
| 2961 | trans = btrfs_start_transaction(root, 0); |
| 2962 | if (IS_ERR(trans)) { |
| 2963 | btrfs_free_path(path); |
| 2964 | return PTR_ERR(trans); |
| 2965 | } |
| 2966 | |
| 2967 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2968 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2969 | key.offset = 0; |
| 2970 | |
| 2971 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2972 | if (ret < 0) |
| 2973 | goto out; |
| 2974 | if (ret > 0) { |
| 2975 | ret = -ENOENT; |
| 2976 | goto out; |
| 2977 | } |
| 2978 | |
| 2979 | ret = btrfs_del_item(trans, root, path); |
| 2980 | out: |
| 2981 | btrfs_free_path(path); |
| 2982 | err = btrfs_commit_transaction(trans, root); |
| 2983 | if (err && !ret) |
| 2984 | ret = err; |
| 2985 | return ret; |
| 2986 | } |
| 2987 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2988 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2989 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2990 | * resume after balance was interrupted. |
| 2991 | */ |
| 2992 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2993 | { |
| 2994 | /* |
| 2995 | * Turn on soft mode for chunk types that were being converted. |
| 2996 | */ |
| 2997 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2998 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2999 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3000 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3001 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 3002 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 3003 | |
| 3004 | /* |
| 3005 | * Turn on usage filter if is not already used. The idea is |
| 3006 | * that chunks that we have already balanced should be |
| 3007 | * reasonably full. Don't do it for chunks that are being |
| 3008 | * converted - that will keep us from relocating unconverted |
| 3009 | * (albeit full) chunks. |
| 3010 | */ |
| 3011 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3012 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3013 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3014 | bctl->data.usage = 90; |
| 3015 | } |
| 3016 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3017 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3018 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3019 | bctl->sys.usage = 90; |
| 3020 | } |
| 3021 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3022 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 3023 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 3024 | bctl->meta.usage = 90; |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3029 | * Should be called with both balance and volume mutexes held to |
| 3030 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 3031 | * restriper. Same goes for unset_balance_control. |
| 3032 | */ |
| 3033 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 3034 | { |
| 3035 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 3036 | |
| 3037 | BUG_ON(fs_info->balance_ctl); |
| 3038 | |
| 3039 | spin_lock(&fs_info->balance_lock); |
| 3040 | fs_info->balance_ctl = bctl; |
| 3041 | spin_unlock(&fs_info->balance_lock); |
| 3042 | } |
| 3043 | |
| 3044 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 3045 | { |
| 3046 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3047 | |
| 3048 | BUG_ON(!fs_info->balance_ctl); |
| 3049 | |
| 3050 | spin_lock(&fs_info->balance_lock); |
| 3051 | fs_info->balance_ctl = NULL; |
| 3052 | spin_unlock(&fs_info->balance_lock); |
| 3053 | |
| 3054 | kfree(bctl); |
| 3055 | } |
| 3056 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3057 | /* |
| 3058 | * Balance filters. Return 1 if chunk should be filtered out |
| 3059 | * (should not be balanced). |
| 3060 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3061 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3062 | struct btrfs_balance_args *bargs) |
| 3063 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3064 | chunk_type = chunk_to_extended(chunk_type) & |
| 3065 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3066 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3067 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3068 | return 0; |
| 3069 | |
| 3070 | return 1; |
| 3071 | } |
| 3072 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3073 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 3074 | struct btrfs_balance_args *bargs) |
| 3075 | { |
| 3076 | struct btrfs_block_group_cache *cache; |
| 3077 | u64 chunk_used, user_thresh; |
| 3078 | int ret = 1; |
| 3079 | |
| 3080 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 3081 | chunk_used = btrfs_block_group_used(&cache->item); |
| 3082 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3083 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 3084 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 3085 | else if (bargs->usage > 100) |
| 3086 | user_thresh = cache->key.offset; |
| 3087 | else |
| 3088 | user_thresh = div_factor_fine(cache->key.offset, |
| 3089 | bargs->usage); |
| 3090 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3091 | if (chunk_used < user_thresh) |
| 3092 | ret = 0; |
| 3093 | |
| 3094 | btrfs_put_block_group(cache); |
| 3095 | return ret; |
| 3096 | } |
| 3097 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3098 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 3099 | struct btrfs_chunk *chunk, |
| 3100 | struct btrfs_balance_args *bargs) |
| 3101 | { |
| 3102 | struct btrfs_stripe *stripe; |
| 3103 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3104 | int i; |
| 3105 | |
| 3106 | for (i = 0; i < num_stripes; i++) { |
| 3107 | stripe = btrfs_stripe_nr(chunk, i); |
| 3108 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 3109 | return 0; |
| 3110 | } |
| 3111 | |
| 3112 | return 1; |
| 3113 | } |
| 3114 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3115 | /* [pstart, pend) */ |
| 3116 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 3117 | struct btrfs_chunk *chunk, |
| 3118 | u64 chunk_offset, |
| 3119 | struct btrfs_balance_args *bargs) |
| 3120 | { |
| 3121 | struct btrfs_stripe *stripe; |
| 3122 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3123 | u64 stripe_offset; |
| 3124 | u64 stripe_length; |
| 3125 | int factor; |
| 3126 | int i; |
| 3127 | |
| 3128 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3129 | return 0; |
| 3130 | |
| 3131 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3132 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3133 | factor = num_stripes / 2; |
| 3134 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3135 | factor = num_stripes - 1; |
| 3136 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3137 | factor = num_stripes - 2; |
| 3138 | } else { |
| 3139 | factor = num_stripes; |
| 3140 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3141 | |
| 3142 | for (i = 0; i < num_stripes; i++) { |
| 3143 | stripe = btrfs_stripe_nr(chunk, i); |
| 3144 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3145 | continue; |
| 3146 | |
| 3147 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3148 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3149 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3150 | |
| 3151 | if (stripe_offset < bargs->pend && |
| 3152 | stripe_offset + stripe_length > bargs->pstart) |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | return 1; |
| 3157 | } |
| 3158 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3159 | /* [vstart, vend) */ |
| 3160 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3161 | struct btrfs_chunk *chunk, |
| 3162 | u64 chunk_offset, |
| 3163 | struct btrfs_balance_args *bargs) |
| 3164 | { |
| 3165 | if (chunk_offset < bargs->vend && |
| 3166 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3167 | /* at least part of the chunk is inside this vrange */ |
| 3168 | return 0; |
| 3169 | |
| 3170 | return 1; |
| 3171 | } |
| 3172 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3173 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3174 | struct btrfs_balance_args *bargs) |
| 3175 | { |
| 3176 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3177 | return 0; |
| 3178 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3179 | chunk_type = chunk_to_extended(chunk_type) & |
| 3180 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3181 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3182 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3183 | return 1; |
| 3184 | |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3188 | static int should_balance_chunk(struct btrfs_root *root, |
| 3189 | struct extent_buffer *leaf, |
| 3190 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3191 | { |
| 3192 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3193 | struct btrfs_balance_args *bargs = NULL; |
| 3194 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3195 | |
| 3196 | /* type filter */ |
| 3197 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3198 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3199 | return 0; |
| 3200 | } |
| 3201 | |
| 3202 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3203 | bargs = &bctl->data; |
| 3204 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3205 | bargs = &bctl->sys; |
| 3206 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3207 | bargs = &bctl->meta; |
| 3208 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3209 | /* profiles filter */ |
| 3210 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3211 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3215 | /* usage filter */ |
| 3216 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3217 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3218 | return 0; |
| 3219 | } |
| 3220 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3221 | /* devid filter */ |
| 3222 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3223 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3224 | return 0; |
| 3225 | } |
| 3226 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3227 | /* drange filter, makes sense only with devid filter */ |
| 3228 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3229 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3230 | return 0; |
| 3231 | } |
| 3232 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3233 | /* vrange filter */ |
| 3234 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3235 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3236 | return 0; |
| 3237 | } |
| 3238 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3239 | /* soft profile changing mode */ |
| 3240 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3241 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3242 | return 0; |
| 3243 | } |
| 3244 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3245 | /* |
| 3246 | * limited by count, must be the last filter |
| 3247 | */ |
| 3248 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3249 | if (bargs->limit == 0) |
| 3250 | return 0; |
| 3251 | else |
| 3252 | bargs->limit--; |
| 3253 | } |
| 3254 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3255 | return 1; |
| 3256 | } |
| 3257 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3258 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3259 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3260 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3261 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3262 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3263 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3264 | struct btrfs_device *device; |
| 3265 | u64 old_size; |
| 3266 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3267 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3268 | struct btrfs_path *path; |
| 3269 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3270 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3271 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3272 | struct extent_buffer *leaf; |
| 3273 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3274 | int ret; |
| 3275 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3276 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3277 | u64 limit_data = bctl->data.limit; |
| 3278 | u64 limit_meta = bctl->meta.limit; |
| 3279 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3280 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3281 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3282 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3283 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3284 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3285 | size_to_free = div_factor(old_size, 1); |
| 3286 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3287 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3288 | btrfs_device_get_total_bytes(device) - |
| 3289 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3290 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3291 | continue; |
| 3292 | |
| 3293 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3294 | if (ret == -ENOSPC) |
| 3295 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3296 | BUG_ON(ret); |
| 3297 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3298 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3299 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3300 | |
| 3301 | ret = btrfs_grow_device(trans, device, old_size); |
| 3302 | BUG_ON(ret); |
| 3303 | |
| 3304 | btrfs_end_transaction(trans, dev_root); |
| 3305 | } |
| 3306 | |
| 3307 | /* step two, relocate all the chunks */ |
| 3308 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3309 | if (!path) { |
| 3310 | ret = -ENOMEM; |
| 3311 | goto error; |
| 3312 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3313 | |
| 3314 | /* zero out stat counters */ |
| 3315 | spin_lock(&fs_info->balance_lock); |
| 3316 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3317 | spin_unlock(&fs_info->balance_lock); |
| 3318 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3319 | if (!counting) { |
| 3320 | bctl->data.limit = limit_data; |
| 3321 | bctl->meta.limit = limit_meta; |
| 3322 | bctl->sys.limit = limit_sys; |
| 3323 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3324 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3325 | key.offset = (u64)-1; |
| 3326 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3327 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3328 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3329 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3330 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3331 | ret = -ECANCELED; |
| 3332 | goto error; |
| 3333 | } |
| 3334 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3335 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3336 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3337 | if (ret < 0) { |
| 3338 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3339 | goto error; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3340 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3341 | |
| 3342 | /* |
| 3343 | * this shouldn't happen, it means the last relocate |
| 3344 | * failed |
| 3345 | */ |
| 3346 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3347 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3348 | |
| 3349 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3350 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3351 | if (ret) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3352 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3353 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3354 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3355 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3356 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3357 | leaf = path->nodes[0]; |
| 3358 | slot = path->slots[0]; |
| 3359 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3360 | |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3361 | if (found_key.objectid != key.objectid) { |
| 3362 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3363 | break; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3364 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3365 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3366 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3367 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3368 | if (!counting) { |
| 3369 | spin_lock(&fs_info->balance_lock); |
| 3370 | bctl->stat.considered++; |
| 3371 | spin_unlock(&fs_info->balance_lock); |
| 3372 | } |
| 3373 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3374 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3375 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3376 | btrfs_release_path(path); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3377 | if (!ret) { |
| 3378 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3379 | goto loop; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3380 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3381 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3382 | if (counting) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3383 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3384 | spin_lock(&fs_info->balance_lock); |
| 3385 | bctl->stat.expected++; |
| 3386 | spin_unlock(&fs_info->balance_lock); |
| 3387 | goto loop; |
| 3388 | } |
| 3389 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3390 | ret = btrfs_relocate_chunk(chunk_root, |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3391 | found_key.offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 3392 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3393 | if (ret && ret != -ENOSPC) |
| 3394 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3395 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3396 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3397 | } else { |
| 3398 | spin_lock(&fs_info->balance_lock); |
| 3399 | bctl->stat.completed++; |
| 3400 | spin_unlock(&fs_info->balance_lock); |
| 3401 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3402 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3403 | if (found_key.offset == 0) |
| 3404 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3405 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3406 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3407 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3408 | if (counting) { |
| 3409 | btrfs_release_path(path); |
| 3410 | counting = false; |
| 3411 | goto again; |
| 3412 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3413 | error: |
| 3414 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3415 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3416 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3417 | enospc_errors); |
| 3418 | if (!ret) |
| 3419 | ret = -ENOSPC; |
| 3420 | } |
| 3421 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3422 | return ret; |
| 3423 | } |
| 3424 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3425 | /** |
| 3426 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3427 | * @flags: profile to validate |
| 3428 | * @extended: if true @flags is treated as an extended profile |
| 3429 | */ |
| 3430 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3431 | { |
| 3432 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3433 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3434 | |
| 3435 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3436 | |
| 3437 | /* 1) check that all other bits are zeroed */ |
| 3438 | if (flags & ~mask) |
| 3439 | return 0; |
| 3440 | |
| 3441 | /* 2) see if profile is reduced */ |
| 3442 | if (flags == 0) |
| 3443 | return !extended; /* "0" is valid for usual profiles */ |
| 3444 | |
| 3445 | /* true if exactly one bit set */ |
| 3446 | return (flags & (flags - 1)) == 0; |
| 3447 | } |
| 3448 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3449 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3450 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3451 | /* cancel requested || normal exit path */ |
| 3452 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3453 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3454 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3455 | } |
| 3456 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3457 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3458 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3459 | int ret; |
| 3460 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3461 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3462 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3463 | if (ret) |
Anand Jain | a4553fe | 2015-09-25 14:43:01 +0800 | [diff] [blame^] | 3464 | btrfs_std_error(fs_info, ret, NULL); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3465 | |
| 3466 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3467 | } |
| 3468 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3469 | /* |
| 3470 | * Should be called with both balance and volume mutexes held |
| 3471 | */ |
| 3472 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3473 | struct btrfs_ioctl_balance_args *bargs) |
| 3474 | { |
| 3475 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3476 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3477 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3478 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3479 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3480 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3481 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3482 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3483 | atomic_read(&fs_info->balance_pause_req) || |
| 3484 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3485 | ret = -EINVAL; |
| 3486 | goto out; |
| 3487 | } |
| 3488 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3489 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3490 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3491 | mixed = 1; |
| 3492 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3493 | /* |
| 3494 | * In case of mixed groups both data and meta should be picked, |
| 3495 | * and identical options should be given for both of them. |
| 3496 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3497 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3498 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3499 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3500 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3501 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3502 | btrfs_err(fs_info, "with mixed groups data and " |
| 3503 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3504 | ret = -EINVAL; |
| 3505 | goto out; |
| 3506 | } |
| 3507 | } |
| 3508 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3509 | num_devices = fs_info->fs_devices->num_devices; |
| 3510 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3511 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3512 | BUG_ON(num_devices < 1); |
| 3513 | num_devices--; |
| 3514 | } |
| 3515 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3516 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3517 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3518 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3519 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3520 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3521 | if (num_devices > 2) |
| 3522 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3523 | if (num_devices > 3) |
| 3524 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3525 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3526 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3527 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3528 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3529 | btrfs_err(fs_info, "unable to start balance with target " |
| 3530 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3531 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3532 | ret = -EINVAL; |
| 3533 | goto out; |
| 3534 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3535 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3536 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3537 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3538 | btrfs_err(fs_info, |
| 3539 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3540 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3541 | ret = -EINVAL; |
| 3542 | goto out; |
| 3543 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3544 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3545 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3546 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3547 | btrfs_err(fs_info, |
| 3548 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3549 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3550 | ret = -EINVAL; |
| 3551 | goto out; |
| 3552 | } |
| 3553 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3554 | /* allow dup'ed data chunks only in mixed mode */ |
| 3555 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3556 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3557 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3558 | ret = -EINVAL; |
| 3559 | goto out; |
| 3560 | } |
| 3561 | |
| 3562 | /* allow to reduce meta or sys integrity only if force set */ |
| 3563 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3564 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3565 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3566 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3567 | do { |
| 3568 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3569 | |
| 3570 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3571 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3572 | !(bctl->sys.target & allowed)) || |
| 3573 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3574 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3575 | !(bctl->meta.target & allowed))) { |
| 3576 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3577 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3578 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3579 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3580 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3581 | ret = -EINVAL; |
| 3582 | goto out; |
| 3583 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3584 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3585 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3586 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3587 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
Zhao Lei | 943c6e9 | 2015-08-19 15:54:15 +0800 | [diff] [blame] | 3588 | fs_info->num_tolerated_disk_barrier_failures = min( |
| 3589 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info), |
| 3590 | btrfs_get_num_tolerated_disk_barrier_failures( |
| 3591 | bctl->sys.target)); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3592 | } |
| 3593 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3594 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3595 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3596 | goto out; |
| 3597 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3598 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3599 | BUG_ON(ret == -EEXIST); |
| 3600 | set_balance_control(bctl); |
| 3601 | } else { |
| 3602 | BUG_ON(ret != -EEXIST); |
| 3603 | spin_lock(&fs_info->balance_lock); |
| 3604 | update_balance_args(bctl); |
| 3605 | spin_unlock(&fs_info->balance_lock); |
| 3606 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3607 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3608 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3609 | mutex_unlock(&fs_info->balance_mutex); |
| 3610 | |
| 3611 | ret = __btrfs_balance(fs_info); |
| 3612 | |
| 3613 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3614 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3615 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3616 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3617 | fs_info->num_tolerated_disk_barrier_failures = |
| 3618 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3619 | } |
| 3620 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3621 | if (bargs) { |
| 3622 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3623 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3626 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3627 | balance_need_close(fs_info)) { |
| 3628 | __cancel_balance(fs_info); |
| 3629 | } |
| 3630 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3631 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3632 | |
| 3633 | return ret; |
| 3634 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3635 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3636 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3637 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3638 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3639 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3640 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3641 | return ret; |
| 3642 | } |
| 3643 | |
| 3644 | static int balance_kthread(void *data) |
| 3645 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3646 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3647 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3648 | |
| 3649 | mutex_lock(&fs_info->volume_mutex); |
| 3650 | mutex_lock(&fs_info->balance_mutex); |
| 3651 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3652 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3653 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3654 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3655 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3656 | |
| 3657 | mutex_unlock(&fs_info->balance_mutex); |
| 3658 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3659 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3660 | return ret; |
| 3661 | } |
| 3662 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3663 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3664 | { |
| 3665 | struct task_struct *tsk; |
| 3666 | |
| 3667 | spin_lock(&fs_info->balance_lock); |
| 3668 | if (!fs_info->balance_ctl) { |
| 3669 | spin_unlock(&fs_info->balance_lock); |
| 3670 | return 0; |
| 3671 | } |
| 3672 | spin_unlock(&fs_info->balance_lock); |
| 3673 | |
| 3674 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3675 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3676 | return 0; |
| 3677 | } |
| 3678 | |
| 3679 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3680 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3681 | } |
| 3682 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3683 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3684 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3685 | struct btrfs_balance_control *bctl; |
| 3686 | struct btrfs_balance_item *item; |
| 3687 | struct btrfs_disk_balance_args disk_bargs; |
| 3688 | struct btrfs_path *path; |
| 3689 | struct extent_buffer *leaf; |
| 3690 | struct btrfs_key key; |
| 3691 | int ret; |
| 3692 | |
| 3693 | path = btrfs_alloc_path(); |
| 3694 | if (!path) |
| 3695 | return -ENOMEM; |
| 3696 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3697 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3698 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3699 | key.offset = 0; |
| 3700 | |
| 3701 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3702 | if (ret < 0) |
| 3703 | goto out; |
| 3704 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3705 | ret = 0; |
| 3706 | goto out; |
| 3707 | } |
| 3708 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3709 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3710 | if (!bctl) { |
| 3711 | ret = -ENOMEM; |
| 3712 | goto out; |
| 3713 | } |
| 3714 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3715 | leaf = path->nodes[0]; |
| 3716 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3717 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3718 | bctl->fs_info = fs_info; |
| 3719 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3720 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3721 | |
| 3722 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3723 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3724 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3725 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3726 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3727 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3728 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3729 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3730 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3731 | mutex_lock(&fs_info->volume_mutex); |
| 3732 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3733 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3734 | set_balance_control(bctl); |
| 3735 | |
| 3736 | mutex_unlock(&fs_info->balance_mutex); |
| 3737 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3738 | out: |
| 3739 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3740 | return ret; |
| 3741 | } |
| 3742 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3743 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3744 | { |
| 3745 | int ret = 0; |
| 3746 | |
| 3747 | mutex_lock(&fs_info->balance_mutex); |
| 3748 | if (!fs_info->balance_ctl) { |
| 3749 | mutex_unlock(&fs_info->balance_mutex); |
| 3750 | return -ENOTCONN; |
| 3751 | } |
| 3752 | |
| 3753 | if (atomic_read(&fs_info->balance_running)) { |
| 3754 | atomic_inc(&fs_info->balance_pause_req); |
| 3755 | mutex_unlock(&fs_info->balance_mutex); |
| 3756 | |
| 3757 | wait_event(fs_info->balance_wait_q, |
| 3758 | atomic_read(&fs_info->balance_running) == 0); |
| 3759 | |
| 3760 | mutex_lock(&fs_info->balance_mutex); |
| 3761 | /* we are good with balance_ctl ripped off from under us */ |
| 3762 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3763 | atomic_dec(&fs_info->balance_pause_req); |
| 3764 | } else { |
| 3765 | ret = -ENOTCONN; |
| 3766 | } |
| 3767 | |
| 3768 | mutex_unlock(&fs_info->balance_mutex); |
| 3769 | return ret; |
| 3770 | } |
| 3771 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3772 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3773 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3774 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3775 | return -EROFS; |
| 3776 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3777 | mutex_lock(&fs_info->balance_mutex); |
| 3778 | if (!fs_info->balance_ctl) { |
| 3779 | mutex_unlock(&fs_info->balance_mutex); |
| 3780 | return -ENOTCONN; |
| 3781 | } |
| 3782 | |
| 3783 | atomic_inc(&fs_info->balance_cancel_req); |
| 3784 | /* |
| 3785 | * if we are running just wait and return, balance item is |
| 3786 | * deleted in btrfs_balance in this case |
| 3787 | */ |
| 3788 | if (atomic_read(&fs_info->balance_running)) { |
| 3789 | mutex_unlock(&fs_info->balance_mutex); |
| 3790 | wait_event(fs_info->balance_wait_q, |
| 3791 | atomic_read(&fs_info->balance_running) == 0); |
| 3792 | mutex_lock(&fs_info->balance_mutex); |
| 3793 | } else { |
| 3794 | /* __cancel_balance needs volume_mutex */ |
| 3795 | mutex_unlock(&fs_info->balance_mutex); |
| 3796 | mutex_lock(&fs_info->volume_mutex); |
| 3797 | mutex_lock(&fs_info->balance_mutex); |
| 3798 | |
| 3799 | if (fs_info->balance_ctl) |
| 3800 | __cancel_balance(fs_info); |
| 3801 | |
| 3802 | mutex_unlock(&fs_info->volume_mutex); |
| 3803 | } |
| 3804 | |
| 3805 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3806 | atomic_dec(&fs_info->balance_cancel_req); |
| 3807 | mutex_unlock(&fs_info->balance_mutex); |
| 3808 | return 0; |
| 3809 | } |
| 3810 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3811 | static int btrfs_uuid_scan_kthread(void *data) |
| 3812 | { |
| 3813 | struct btrfs_fs_info *fs_info = data; |
| 3814 | struct btrfs_root *root = fs_info->tree_root; |
| 3815 | struct btrfs_key key; |
| 3816 | struct btrfs_key max_key; |
| 3817 | struct btrfs_path *path = NULL; |
| 3818 | int ret = 0; |
| 3819 | struct extent_buffer *eb; |
| 3820 | int slot; |
| 3821 | struct btrfs_root_item root_item; |
| 3822 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3823 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3824 | |
| 3825 | path = btrfs_alloc_path(); |
| 3826 | if (!path) { |
| 3827 | ret = -ENOMEM; |
| 3828 | goto out; |
| 3829 | } |
| 3830 | |
| 3831 | key.objectid = 0; |
| 3832 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3833 | key.offset = 0; |
| 3834 | |
| 3835 | max_key.objectid = (u64)-1; |
| 3836 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3837 | max_key.offset = (u64)-1; |
| 3838 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3839 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3840 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3841 | if (ret) { |
| 3842 | if (ret > 0) |
| 3843 | ret = 0; |
| 3844 | break; |
| 3845 | } |
| 3846 | |
| 3847 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3848 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3849 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3850 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3851 | goto skip; |
| 3852 | |
| 3853 | eb = path->nodes[0]; |
| 3854 | slot = path->slots[0]; |
| 3855 | item_size = btrfs_item_size_nr(eb, slot); |
| 3856 | if (item_size < sizeof(root_item)) |
| 3857 | goto skip; |
| 3858 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3859 | read_extent_buffer(eb, &root_item, |
| 3860 | btrfs_item_ptr_offset(eb, slot), |
| 3861 | (int)sizeof(root_item)); |
| 3862 | if (btrfs_root_refs(&root_item) == 0) |
| 3863 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3864 | |
| 3865 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3866 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3867 | if (trans) |
| 3868 | goto update_tree; |
| 3869 | |
| 3870 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3871 | /* |
| 3872 | * 1 - subvol uuid item |
| 3873 | * 1 - received_subvol uuid item |
| 3874 | */ |
| 3875 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3876 | if (IS_ERR(trans)) { |
| 3877 | ret = PTR_ERR(trans); |
| 3878 | break; |
| 3879 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3880 | continue; |
| 3881 | } else { |
| 3882 | goto skip; |
| 3883 | } |
| 3884 | update_tree: |
| 3885 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3886 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3887 | root_item.uuid, |
| 3888 | BTRFS_UUID_KEY_SUBVOL, |
| 3889 | key.objectid); |
| 3890 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3891 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3892 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3893 | break; |
| 3894 | } |
| 3895 | } |
| 3896 | |
| 3897 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3898 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3899 | root_item.received_uuid, |
| 3900 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3901 | key.objectid); |
| 3902 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3903 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3904 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3905 | break; |
| 3906 | } |
| 3907 | } |
| 3908 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3909 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3910 | if (trans) { |
| 3911 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3912 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3913 | if (ret) |
| 3914 | break; |
| 3915 | } |
| 3916 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3917 | btrfs_release_path(path); |
| 3918 | if (key.offset < (u64)-1) { |
| 3919 | key.offset++; |
| 3920 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3921 | key.offset = 0; |
| 3922 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3923 | } else if (key.objectid < (u64)-1) { |
| 3924 | key.offset = 0; |
| 3925 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3926 | key.objectid++; |
| 3927 | } else { |
| 3928 | break; |
| 3929 | } |
| 3930 | cond_resched(); |
| 3931 | } |
| 3932 | |
| 3933 | out: |
| 3934 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3935 | if (trans && !IS_ERR(trans)) |
| 3936 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3937 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3938 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3939 | else |
| 3940 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3941 | up(&fs_info->uuid_tree_rescan_sem); |
| 3942 | return 0; |
| 3943 | } |
| 3944 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3945 | /* |
| 3946 | * Callback for btrfs_uuid_tree_iterate(). |
| 3947 | * returns: |
| 3948 | * 0 check succeeded, the entry is not outdated. |
| 3949 | * < 0 if an error occured. |
| 3950 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3951 | */ |
| 3952 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3953 | u8 *uuid, u8 type, u64 subid) |
| 3954 | { |
| 3955 | struct btrfs_key key; |
| 3956 | int ret = 0; |
| 3957 | struct btrfs_root *subvol_root; |
| 3958 | |
| 3959 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3960 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3961 | goto out; |
| 3962 | |
| 3963 | key.objectid = subid; |
| 3964 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3965 | key.offset = (u64)-1; |
| 3966 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3967 | if (IS_ERR(subvol_root)) { |
| 3968 | ret = PTR_ERR(subvol_root); |
| 3969 | if (ret == -ENOENT) |
| 3970 | ret = 1; |
| 3971 | goto out; |
| 3972 | } |
| 3973 | |
| 3974 | switch (type) { |
| 3975 | case BTRFS_UUID_KEY_SUBVOL: |
| 3976 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3977 | ret = 1; |
| 3978 | break; |
| 3979 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3980 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3981 | BTRFS_UUID_SIZE)) |
| 3982 | ret = 1; |
| 3983 | break; |
| 3984 | } |
| 3985 | |
| 3986 | out: |
| 3987 | return ret; |
| 3988 | } |
| 3989 | |
| 3990 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3991 | { |
| 3992 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3993 | int ret; |
| 3994 | |
| 3995 | /* |
| 3996 | * 1st step is to iterate through the existing UUID tree and |
| 3997 | * to delete all entries that contain outdated data. |
| 3998 | * 2nd step is to add all missing entries to the UUID tree. |
| 3999 | */ |
| 4000 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 4001 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4002 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4003 | up(&fs_info->uuid_tree_rescan_sem); |
| 4004 | return ret; |
| 4005 | } |
| 4006 | return btrfs_uuid_scan_kthread(data); |
| 4007 | } |
| 4008 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4009 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4010 | { |
| 4011 | struct btrfs_trans_handle *trans; |
| 4012 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 4013 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4014 | struct task_struct *task; |
| 4015 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4016 | |
| 4017 | /* |
| 4018 | * 1 - root node |
| 4019 | * 1 - root item |
| 4020 | */ |
| 4021 | trans = btrfs_start_transaction(tree_root, 2); |
| 4022 | if (IS_ERR(trans)) |
| 4023 | return PTR_ERR(trans); |
| 4024 | |
| 4025 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 4026 | BTRFS_UUID_TREE_OBJECTID); |
| 4027 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame] | 4028 | ret = PTR_ERR(uuid_root); |
| 4029 | btrfs_abort_transaction(trans, tree_root, ret); |
| 4030 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | fs_info->uuid_root = uuid_root; |
| 4034 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4035 | ret = btrfs_commit_transaction(trans, tree_root); |
| 4036 | if (ret) |
| 4037 | return ret; |
| 4038 | |
| 4039 | down(&fs_info->uuid_tree_rescan_sem); |
| 4040 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 4041 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4042 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4043 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4044 | up(&fs_info->uuid_tree_rescan_sem); |
| 4045 | return PTR_ERR(task); |
| 4046 | } |
| 4047 | |
| 4048 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4049 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 4050 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4051 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 4052 | { |
| 4053 | struct task_struct *task; |
| 4054 | |
| 4055 | down(&fs_info->uuid_tree_rescan_sem); |
| 4056 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 4057 | if (IS_ERR(task)) { |
| 4058 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4059 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 4060 | up(&fs_info->uuid_tree_rescan_sem); |
| 4061 | return PTR_ERR(task); |
| 4062 | } |
| 4063 | |
| 4064 | return 0; |
| 4065 | } |
| 4066 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4067 | /* |
| 4068 | * shrinking a device means finding all of the device extents past |
| 4069 | * the new size, and then following the back refs to the chunks. |
| 4070 | * The chunk relocation code actually frees the device extent |
| 4071 | */ |
| 4072 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 4073 | { |
| 4074 | struct btrfs_trans_handle *trans; |
| 4075 | struct btrfs_root *root = device->dev_root; |
| 4076 | struct btrfs_dev_extent *dev_extent = NULL; |
| 4077 | struct btrfs_path *path; |
| 4078 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4079 | u64 chunk_offset; |
| 4080 | int ret; |
| 4081 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4082 | int failed = 0; |
| 4083 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4084 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4085 | struct extent_buffer *l; |
| 4086 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4087 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4088 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4089 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 4090 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4091 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4092 | if (device->is_tgtdev_for_dev_replace) |
| 4093 | return -EINVAL; |
| 4094 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4095 | path = btrfs_alloc_path(); |
| 4096 | if (!path) |
| 4097 | return -ENOMEM; |
| 4098 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4099 | path->reada = 2; |
| 4100 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4101 | lock_chunks(root); |
| 4102 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4103 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4104 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4105 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4106 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4107 | root->fs_info->free_chunk_space -= diff; |
| 4108 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4109 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4110 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4111 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4112 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4113 | key.objectid = device->devid; |
| 4114 | key.offset = (u64)-1; |
| 4115 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4116 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4117 | do { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4118 | mutex_lock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4119 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4120 | if (ret < 0) { |
| 4121 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4122 | goto done; |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4123 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4124 | |
| 4125 | ret = btrfs_previous_item(root, path, 0, key.type); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4126 | if (ret) |
| 4127 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4128 | if (ret < 0) |
| 4129 | goto done; |
| 4130 | if (ret) { |
| 4131 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4132 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4133 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | l = path->nodes[0]; |
| 4137 | slot = path->slots[0]; |
| 4138 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4139 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4140 | if (key.objectid != device->devid) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4141 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4142 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4143 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4144 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4145 | |
| 4146 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4147 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4148 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4149 | if (key.offset + length <= new_size) { |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4150 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4151 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4152 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4153 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4154 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4155 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4156 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4157 | |
Zhaolei | dc2ee4e | 2015-08-05 18:00:04 +0800 | [diff] [blame] | 4158 | ret = btrfs_relocate_chunk(root, chunk_offset); |
Filipe Manana | 67c5e7d | 2015-06-11 00:58:53 +0100 | [diff] [blame] | 4159 | mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4160 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4161 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4162 | if (ret == -ENOSPC) |
| 4163 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4164 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4165 | |
| 4166 | if (failed && !retried) { |
| 4167 | failed = 0; |
| 4168 | retried = true; |
| 4169 | goto again; |
| 4170 | } else if (failed && retried) { |
| 4171 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4172 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4173 | } |
| 4174 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4175 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4176 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4177 | if (IS_ERR(trans)) { |
| 4178 | ret = PTR_ERR(trans); |
| 4179 | goto done; |
| 4180 | } |
| 4181 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4182 | lock_chunks(root); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4183 | |
| 4184 | /* |
| 4185 | * We checked in the above loop all device extents that were already in |
| 4186 | * the device tree. However before we have updated the device's |
| 4187 | * total_bytes to the new size, we might have had chunk allocations that |
| 4188 | * have not complete yet (new block groups attached to transaction |
| 4189 | * handles), and therefore their device extents were not yet in the |
| 4190 | * device tree and we missed them in the loop above. So if we have any |
| 4191 | * pending chunk using a device extent that overlaps the device range |
| 4192 | * that we can not use anymore, commit the current transaction and |
| 4193 | * repeat the search on the device tree - this way we guarantee we will |
| 4194 | * not have chunks using device extents that end beyond 'new_size'. |
| 4195 | */ |
| 4196 | if (!checked_pending_chunks) { |
| 4197 | u64 start = new_size; |
| 4198 | u64 len = old_size - new_size; |
| 4199 | |
Jeff Mahoney | 499f377 | 2015-06-15 09:41:17 -0400 | [diff] [blame] | 4200 | if (contains_pending_extent(trans->transaction, device, |
| 4201 | &start, len)) { |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4202 | unlock_chunks(root); |
| 4203 | checked_pending_chunks = true; |
| 4204 | failed = 0; |
| 4205 | retried = false; |
| 4206 | ret = btrfs_commit_transaction(trans, root); |
| 4207 | if (ret) |
| 4208 | goto done; |
| 4209 | goto again; |
| 4210 | } |
| 4211 | } |
| 4212 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4213 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4214 | if (list_empty(&device->resized_list)) |
| 4215 | list_add_tail(&device->resized_list, |
| 4216 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4217 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4218 | WARN_ON(diff > old_total); |
| 4219 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4220 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4221 | |
| 4222 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4223 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4224 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4225 | done: |
| 4226 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4227 | if (ret) { |
| 4228 | lock_chunks(root); |
| 4229 | btrfs_device_set_total_bytes(device, old_size); |
| 4230 | if (device->writeable) |
| 4231 | device->fs_devices->total_rw_bytes += diff; |
| 4232 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4233 | root->fs_info->free_chunk_space += diff; |
| 4234 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4235 | unlock_chunks(root); |
| 4236 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4237 | return ret; |
| 4238 | } |
| 4239 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4240 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4241 | struct btrfs_key *key, |
| 4242 | struct btrfs_chunk *chunk, int item_size) |
| 4243 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4244 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4245 | struct btrfs_disk_key disk_key; |
| 4246 | u32 array_size; |
| 4247 | u8 *ptr; |
| 4248 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4249 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4250 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4251 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4252 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4253 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4254 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4255 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4256 | |
| 4257 | ptr = super_copy->sys_chunk_array + array_size; |
| 4258 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4259 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4260 | ptr += sizeof(disk_key); |
| 4261 | memcpy(ptr, chunk, item_size); |
| 4262 | item_size += sizeof(disk_key); |
| 4263 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4264 | unlock_chunks(root); |
| 4265 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4266 | return 0; |
| 4267 | } |
| 4268 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4269 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4270 | * sort the devices in descending order by max_avail, total_avail |
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 | static int btrfs_cmp_device_info(const void *a, const void *b) |
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 | const struct btrfs_device_info *di_a = a; |
| 4275 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4276 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4277 | if (di_a->max_avail > di_b->max_avail) |
| 4278 | return -1; |
| 4279 | if (di_a->max_avail < di_b->max_avail) |
| 4280 | return 1; |
| 4281 | if (di_a->total_avail > di_b->total_avail) |
| 4282 | return -1; |
| 4283 | if (di_a->total_avail < di_b->total_avail) |
| 4284 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4285 | return 0; |
| 4286 | } |
| 4287 | |
David Sterba | e8c9f18 | 2015-01-02 18:23:10 +0100 | [diff] [blame] | 4288 | 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] | 4289 | [BTRFS_RAID_RAID10] = { |
| 4290 | .sub_stripes = 2, |
| 4291 | .dev_stripes = 1, |
| 4292 | .devs_max = 0, /* 0 == as many as possible */ |
| 4293 | .devs_min = 4, |
| 4294 | .devs_increment = 2, |
| 4295 | .ncopies = 2, |
| 4296 | }, |
| 4297 | [BTRFS_RAID_RAID1] = { |
| 4298 | .sub_stripes = 1, |
| 4299 | .dev_stripes = 1, |
| 4300 | .devs_max = 2, |
| 4301 | .devs_min = 2, |
| 4302 | .devs_increment = 2, |
| 4303 | .ncopies = 2, |
| 4304 | }, |
| 4305 | [BTRFS_RAID_DUP] = { |
| 4306 | .sub_stripes = 1, |
| 4307 | .dev_stripes = 2, |
| 4308 | .devs_max = 1, |
| 4309 | .devs_min = 1, |
| 4310 | .devs_increment = 1, |
| 4311 | .ncopies = 2, |
| 4312 | }, |
| 4313 | [BTRFS_RAID_RAID0] = { |
| 4314 | .sub_stripes = 1, |
| 4315 | .dev_stripes = 1, |
| 4316 | .devs_max = 0, |
| 4317 | .devs_min = 2, |
| 4318 | .devs_increment = 1, |
| 4319 | .ncopies = 1, |
| 4320 | }, |
| 4321 | [BTRFS_RAID_SINGLE] = { |
| 4322 | .sub_stripes = 1, |
| 4323 | .dev_stripes = 1, |
| 4324 | .devs_max = 1, |
| 4325 | .devs_min = 1, |
| 4326 | .devs_increment = 1, |
| 4327 | .ncopies = 1, |
| 4328 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4329 | [BTRFS_RAID_RAID5] = { |
| 4330 | .sub_stripes = 1, |
| 4331 | .dev_stripes = 1, |
| 4332 | .devs_max = 0, |
| 4333 | .devs_min = 2, |
| 4334 | .devs_increment = 1, |
| 4335 | .ncopies = 2, |
| 4336 | }, |
| 4337 | [BTRFS_RAID_RAID6] = { |
| 4338 | .sub_stripes = 1, |
| 4339 | .dev_stripes = 1, |
| 4340 | .devs_max = 0, |
| 4341 | .devs_min = 3, |
| 4342 | .devs_increment = 1, |
| 4343 | .ncopies = 3, |
| 4344 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4345 | }; |
| 4346 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4347 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4348 | { |
| 4349 | /* TODO allow them to set a preferred stripe size */ |
| 4350 | return 64 * 1024; |
| 4351 | } |
| 4352 | |
| 4353 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4354 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4355 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4356 | return; |
| 4357 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4358 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4359 | } |
| 4360 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4361 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4362 | - sizeof(struct btrfs_item) \ |
| 4363 | - sizeof(struct btrfs_chunk)) \ |
| 4364 | / sizeof(struct btrfs_stripe) + 1) |
| 4365 | |
| 4366 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4367 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4368 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4369 | / sizeof(struct btrfs_stripe) + 1) |
| 4370 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4371 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4372 | struct btrfs_root *extent_root, u64 start, |
| 4373 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4374 | { |
| 4375 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4376 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4377 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4378 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4379 | struct extent_map_tree *em_tree; |
| 4380 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4381 | struct btrfs_device_info *devices_info = NULL; |
| 4382 | u64 total_avail; |
| 4383 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4384 | int data_stripes; /* number of stripes that count for |
| 4385 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4386 | int sub_stripes; /* sub_stripes info for map */ |
| 4387 | int dev_stripes; /* stripes per dev */ |
| 4388 | int devs_max; /* max devs to use */ |
| 4389 | int devs_min; /* min devs needed */ |
| 4390 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4391 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4392 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4393 | u64 max_stripe_size; |
| 4394 | u64 max_chunk_size; |
| 4395 | u64 stripe_size; |
| 4396 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4397 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4398 | int ndevs; |
| 4399 | int i; |
| 4400 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4401 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4402 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4403 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4404 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4405 | if (list_empty(&fs_devices->alloc_list)) |
| 4406 | return -ENOSPC; |
| 4407 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4408 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4409 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4410 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4411 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4412 | devs_max = btrfs_raid_array[index].devs_max; |
| 4413 | devs_min = btrfs_raid_array[index].devs_min; |
| 4414 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4415 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4416 | |
| 4417 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4418 | max_stripe_size = 1024 * 1024 * 1024; |
| 4419 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4420 | if (!devs_max) |
| 4421 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4422 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4423 | /* for larger filesystems, use larger metadata chunks */ |
| 4424 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4425 | max_stripe_size = 1024 * 1024 * 1024; |
| 4426 | else |
| 4427 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4428 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4429 | if (!devs_max) |
| 4430 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4431 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4432 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4433 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4434 | if (!devs_max) |
| 4435 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4436 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4437 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4438 | type); |
| 4439 | BUG_ON(1); |
| 4440 | } |
| 4441 | |
| 4442 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4443 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4444 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4445 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4446 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4447 | GFP_NOFS); |
| 4448 | if (!devices_info) |
| 4449 | return -ENOMEM; |
| 4450 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4451 | cur = fs_devices->alloc_list.next; |
| 4452 | |
| 4453 | /* |
| 4454 | * in the first pass through the devices list, we gather information |
| 4455 | * about the available holes on each device. |
| 4456 | */ |
| 4457 | ndevs = 0; |
| 4458 | while (cur != &fs_devices->alloc_list) { |
| 4459 | struct btrfs_device *device; |
| 4460 | u64 max_avail; |
| 4461 | u64 dev_offset; |
| 4462 | |
| 4463 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4464 | |
| 4465 | cur = cur->next; |
| 4466 | |
| 4467 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4468 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4469 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4470 | continue; |
| 4471 | } |
| 4472 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4473 | if (!device->in_fs_metadata || |
| 4474 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4475 | continue; |
| 4476 | |
| 4477 | if (device->total_bytes > device->bytes_used) |
| 4478 | total_avail = device->total_bytes - device->bytes_used; |
| 4479 | else |
| 4480 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4481 | |
| 4482 | /* If there is no space on this device, skip it. */ |
| 4483 | if (total_avail == 0) |
| 4484 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4485 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4486 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4487 | max_stripe_size * dev_stripes, |
| 4488 | &dev_offset, &max_avail); |
| 4489 | if (ret && ret != -ENOSPC) |
| 4490 | goto error; |
| 4491 | |
| 4492 | if (ret == 0) |
| 4493 | max_avail = max_stripe_size * dev_stripes; |
| 4494 | |
| 4495 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4496 | continue; |
| 4497 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4498 | if (ndevs == fs_devices->rw_devices) { |
| 4499 | WARN(1, "%s: found more than %llu devices\n", |
| 4500 | __func__, fs_devices->rw_devices); |
| 4501 | break; |
| 4502 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4503 | devices_info[ndevs].dev_offset = dev_offset; |
| 4504 | devices_info[ndevs].max_avail = max_avail; |
| 4505 | devices_info[ndevs].total_avail = total_avail; |
| 4506 | devices_info[ndevs].dev = device; |
| 4507 | ++ndevs; |
| 4508 | } |
| 4509 | |
| 4510 | /* |
| 4511 | * now sort the devices by hole size / available space |
| 4512 | */ |
| 4513 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4514 | btrfs_cmp_device_info, NULL); |
| 4515 | |
| 4516 | /* round down to number of usable stripes */ |
| 4517 | ndevs -= ndevs % devs_increment; |
| 4518 | |
| 4519 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4520 | ret = -ENOSPC; |
| 4521 | goto error; |
| 4522 | } |
| 4523 | |
| 4524 | if (devs_max && ndevs > devs_max) |
| 4525 | ndevs = devs_max; |
| 4526 | /* |
| 4527 | * the primary goal is to maximize the number of stripes, so use as many |
| 4528 | * devices as possible, even if the stripes are not maximum sized. |
| 4529 | */ |
| 4530 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4531 | num_stripes = ndevs * dev_stripes; |
| 4532 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4533 | /* |
| 4534 | * this will have to be fixed for RAID1 and RAID10 over |
| 4535 | * more drives |
| 4536 | */ |
| 4537 | data_stripes = num_stripes / ncopies; |
| 4538 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4539 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4540 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4541 | btrfs_super_stripesize(info->super_copy)); |
| 4542 | data_stripes = num_stripes - 1; |
| 4543 | } |
| 4544 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4545 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4546 | btrfs_super_stripesize(info->super_copy)); |
| 4547 | data_stripes = num_stripes - 2; |
| 4548 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4549 | |
| 4550 | /* |
| 4551 | * Use the number of data stripes to figure out how big this chunk |
| 4552 | * is really going to be in terms of logical address space, |
| 4553 | * and compare that answer with the max chunk size |
| 4554 | */ |
| 4555 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4556 | u64 mask = (1ULL << 24) - 1; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4557 | |
| 4558 | stripe_size = div_u64(max_chunk_size, data_stripes); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4559 | |
| 4560 | /* bump the answer up to a 16MB boundary */ |
| 4561 | stripe_size = (stripe_size + mask) & ~mask; |
| 4562 | |
| 4563 | /* but don't go higher than the limits we found |
| 4564 | * while searching for free extents |
| 4565 | */ |
| 4566 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4567 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4568 | } |
| 4569 | |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4570 | stripe_size = div_u64(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4571 | |
| 4572 | /* align to BTRFS_STRIPE_LEN */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4573 | stripe_size = div_u64(stripe_size, raid_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4574 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4575 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4576 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4577 | if (!map) { |
| 4578 | ret = -ENOMEM; |
| 4579 | goto error; |
| 4580 | } |
| 4581 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4582 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4583 | for (i = 0; i < ndevs; ++i) { |
| 4584 | for (j = 0; j < dev_stripes; ++j) { |
| 4585 | int s = i * dev_stripes + j; |
| 4586 | map->stripes[s].dev = devices_info[i].dev; |
| 4587 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4588 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4589 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4590 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4591 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4592 | map->stripe_len = raid_stripe_len; |
| 4593 | map->io_align = raid_stripe_len; |
| 4594 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4595 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4596 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4597 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4598 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4599 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4600 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4601 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4602 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4603 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4604 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4605 | ret = -ENOMEM; |
| 4606 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4607 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4608 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4609 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4610 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4611 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4612 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4613 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4614 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4615 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4616 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4617 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4618 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4619 | if (!ret) { |
| 4620 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4621 | atomic_inc(&em->refs); |
| 4622 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4623 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4624 | if (ret) { |
| 4625 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4626 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4627 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4628 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4629 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4630 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4631 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4632 | if (ret) |
| 4633 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4634 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4635 | for (i = 0; i < map->num_stripes; i++) { |
| 4636 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4637 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4638 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4639 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4640 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4641 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4642 | map->num_stripes); |
| 4643 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4644 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4645 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4646 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4647 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4648 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4649 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4650 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4651 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4652 | write_lock(&em_tree->lock); |
| 4653 | remove_extent_mapping(em_tree, em); |
| 4654 | write_unlock(&em_tree->lock); |
| 4655 | |
| 4656 | /* One for our allocation */ |
| 4657 | free_extent_map(em); |
| 4658 | /* One for the tree reference */ |
| 4659 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4660 | /* One for the pending_chunks list reference */ |
| 4661 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4662 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4663 | kfree(devices_info); |
| 4664 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4665 | } |
| 4666 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4667 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4668 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4669 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4670 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4671 | struct btrfs_key key; |
| 4672 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4673 | struct btrfs_device *device; |
| 4674 | struct btrfs_chunk *chunk; |
| 4675 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4676 | struct extent_map_tree *em_tree; |
| 4677 | struct extent_map *em; |
| 4678 | struct map_lookup *map; |
| 4679 | size_t item_size; |
| 4680 | u64 dev_offset; |
| 4681 | u64 stripe_size; |
| 4682 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4683 | int ret; |
| 4684 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4685 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4686 | read_lock(&em_tree->lock); |
| 4687 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4688 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4689 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4690 | if (!em) { |
| 4691 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4692 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4693 | return -EINVAL; |
| 4694 | } |
| 4695 | |
| 4696 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4697 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4698 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4699 | chunk_size, em->start, em->len); |
| 4700 | free_extent_map(em); |
| 4701 | return -EINVAL; |
| 4702 | } |
| 4703 | |
| 4704 | map = (struct map_lookup *)em->bdev; |
| 4705 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4706 | stripe_size = em->orig_block_len; |
| 4707 | |
| 4708 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4709 | if (!chunk) { |
| 4710 | ret = -ENOMEM; |
| 4711 | goto out; |
| 4712 | } |
| 4713 | |
| 4714 | for (i = 0; i < map->num_stripes; i++) { |
| 4715 | device = map->stripes[i].dev; |
| 4716 | dev_offset = map->stripes[i].physical; |
| 4717 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4718 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4719 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4720 | goto out; |
| 4721 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4722 | chunk_root->root_key.objectid, |
| 4723 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4724 | chunk_offset, dev_offset, |
| 4725 | stripe_size); |
| 4726 | if (ret) |
| 4727 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4728 | } |
| 4729 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4730 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4731 | for (i = 0; i < map->num_stripes; i++) { |
| 4732 | device = map->stripes[i].dev; |
| 4733 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4734 | |
| 4735 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4736 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4737 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4738 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4742 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4743 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4744 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4745 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4746 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4747 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4748 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4749 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4750 | |
| 4751 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4752 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4753 | key.offset = chunk_offset; |
| 4754 | |
| 4755 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4756 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4757 | /* |
| 4758 | * TODO: Cleanup of inserted chunk root in case of |
| 4759 | * failure. |
| 4760 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4761 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4762 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4763 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4764 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4765 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4766 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4767 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4768 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | /* |
| 4772 | * Chunk allocation falls into two parts. The first part does works |
| 4773 | * that make the new allocated chunk useable, but not do any operation |
| 4774 | * that modifies the chunk tree. The second part does the works that |
| 4775 | * require modifying the chunk tree. This division is important for the |
| 4776 | * bootstrap process of adding storage to a seed btrfs. |
| 4777 | */ |
| 4778 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4779 | struct btrfs_root *extent_root, u64 type) |
| 4780 | { |
| 4781 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4782 | |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 4783 | ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex)); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4784 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4785 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4786 | } |
| 4787 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4788 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4789 | struct btrfs_root *root, |
| 4790 | struct btrfs_device *device) |
| 4791 | { |
| 4792 | u64 chunk_offset; |
| 4793 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4794 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4795 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4796 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4797 | int ret; |
| 4798 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4799 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4800 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4801 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4802 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4803 | if (ret) |
| 4804 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4805 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4806 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4807 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4808 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4809 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4810 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4811 | } |
| 4812 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4813 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4814 | { |
| 4815 | int max_errors; |
| 4816 | |
| 4817 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4818 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4819 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4820 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4821 | max_errors = 1; |
| 4822 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4823 | max_errors = 2; |
| 4824 | } else { |
| 4825 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4826 | } |
| 4827 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4828 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4832 | { |
| 4833 | struct extent_map *em; |
| 4834 | struct map_lookup *map; |
| 4835 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4836 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4837 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4838 | int i; |
| 4839 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4840 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4841 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4842 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4843 | if (!em) |
| 4844 | return 1; |
| 4845 | |
| 4846 | map = (struct map_lookup *)em->bdev; |
| 4847 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4848 | if (map->stripes[i].dev->missing) { |
| 4849 | miss_ndevs++; |
| 4850 | continue; |
| 4851 | } |
| 4852 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4853 | if (!map->stripes[i].dev->writeable) { |
| 4854 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4855 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4856 | } |
| 4857 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4858 | |
| 4859 | /* |
| 4860 | * If the number of missing devices is larger than max errors, |
| 4861 | * we can not write the data into that chunk successfully, so |
| 4862 | * set it readonly. |
| 4863 | */ |
| 4864 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4865 | readonly = 1; |
| 4866 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4867 | free_extent_map(em); |
| 4868 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4872 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4873 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4877 | { |
| 4878 | struct extent_map *em; |
| 4879 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4880 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4881 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4882 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4883 | if (em) |
| 4884 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4885 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4886 | if (!em) |
| 4887 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4888 | /* once for us */ |
| 4889 | free_extent_map(em); |
| 4890 | /* once for the tree */ |
| 4891 | free_extent_map(em); |
| 4892 | } |
| 4893 | } |
| 4894 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4895 | 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] | 4896 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4897 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4898 | struct extent_map *em; |
| 4899 | struct map_lookup *map; |
| 4900 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4901 | int ret; |
| 4902 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4903 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4904 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4905 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4906 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4907 | /* |
| 4908 | * We could return errors for these cases, but that could get ugly and |
| 4909 | * we'd probably do the same thing which is just not do anything else |
| 4910 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4911 | */ |
| 4912 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4913 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4914 | logical+len); |
| 4915 | return 1; |
| 4916 | } |
| 4917 | |
| 4918 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4919 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4920 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4921 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4922 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4923 | return 1; |
| 4924 | } |
| 4925 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4926 | map = (struct map_lookup *)em->bdev; |
| 4927 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4928 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4929 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4930 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4931 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4932 | ret = 2; |
| 4933 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4934 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4935 | else |
| 4936 | ret = 1; |
| 4937 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4938 | |
| 4939 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4940 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4941 | ret++; |
| 4942 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4943 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4944 | return ret; |
| 4945 | } |
| 4946 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4947 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4948 | struct btrfs_mapping_tree *map_tree, |
| 4949 | u64 logical) |
| 4950 | { |
| 4951 | struct extent_map *em; |
| 4952 | struct map_lookup *map; |
| 4953 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4954 | unsigned long len = root->sectorsize; |
| 4955 | |
| 4956 | read_lock(&em_tree->lock); |
| 4957 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4958 | read_unlock(&em_tree->lock); |
| 4959 | BUG_ON(!em); |
| 4960 | |
| 4961 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4962 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4963 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4964 | len = map->stripe_len * nr_data_stripes(map); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4965 | free_extent_map(em); |
| 4966 | return len; |
| 4967 | } |
| 4968 | |
| 4969 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4970 | u64 logical, u64 len, int mirror_num) |
| 4971 | { |
| 4972 | struct extent_map *em; |
| 4973 | struct map_lookup *map; |
| 4974 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4975 | int ret = 0; |
| 4976 | |
| 4977 | read_lock(&em_tree->lock); |
| 4978 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4979 | read_unlock(&em_tree->lock); |
| 4980 | BUG_ON(!em); |
| 4981 | |
| 4982 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4983 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4984 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4985 | ret = 1; |
| 4986 | free_extent_map(em); |
| 4987 | return ret; |
| 4988 | } |
| 4989 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4990 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4991 | struct map_lookup *map, int first, int num, |
| 4992 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4993 | { |
| 4994 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4995 | int tolerance; |
| 4996 | struct btrfs_device *srcdev; |
| 4997 | |
| 4998 | if (dev_replace_is_ongoing && |
| 4999 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 5000 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 5001 | srcdev = fs_info->dev_replace.srcdev; |
| 5002 | else |
| 5003 | srcdev = NULL; |
| 5004 | |
| 5005 | /* |
| 5006 | * try to avoid the drive that is the source drive for a |
| 5007 | * dev-replace procedure, only choose it if no other non-missing |
| 5008 | * mirror is available |
| 5009 | */ |
| 5010 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 5011 | if (map->stripes[optimal].dev->bdev && |
| 5012 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 5013 | return optimal; |
| 5014 | for (i = first; i < first + num; i++) { |
| 5015 | if (map->stripes[i].dev->bdev && |
| 5016 | (tolerance || map->stripes[i].dev != srcdev)) |
| 5017 | return i; |
| 5018 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5019 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5020 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5021 | /* we couldn't find one that doesn't fail. Just return something |
| 5022 | * and the io error handling code will clean up eventually |
| 5023 | */ |
| 5024 | return optimal; |
| 5025 | } |
| 5026 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5027 | static inline int parity_smaller(u64 a, u64 b) |
| 5028 | { |
| 5029 | return a > b; |
| 5030 | } |
| 5031 | |
| 5032 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5033 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5034 | { |
| 5035 | struct btrfs_bio_stripe s; |
| 5036 | int i; |
| 5037 | u64 l; |
| 5038 | int again = 1; |
| 5039 | |
| 5040 | while (again) { |
| 5041 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5042 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5043 | if (parity_smaller(bbio->raid_map[i], |
| 5044 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5045 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5046 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5047 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5048 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5049 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5050 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5051 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5052 | again = 1; |
| 5053 | } |
| 5054 | } |
| 5055 | } |
| 5056 | } |
| 5057 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5058 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 5059 | { |
| 5060 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5061 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5062 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5063 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5064 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5065 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5066 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 5067 | /* |
| 5068 | * plus the raid_map, which includes both the tgt dev |
| 5069 | * and the stripes |
| 5070 | */ |
| 5071 | sizeof(u64) * (total_stripes), |
Michal Hocko | 277fb5f | 2015-08-19 14:17:41 +0200 | [diff] [blame] | 5072 | GFP_NOFS|__GFP_NOFAIL); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5073 | |
| 5074 | atomic_set(&bbio->error, 0); |
| 5075 | atomic_set(&bbio->refs, 1); |
| 5076 | |
| 5077 | return bbio; |
| 5078 | } |
| 5079 | |
| 5080 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 5081 | { |
| 5082 | WARN_ON(!atomic_read(&bbio->refs)); |
| 5083 | atomic_inc(&bbio->refs); |
| 5084 | } |
| 5085 | |
| 5086 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 5087 | { |
| 5088 | if (!bbio) |
| 5089 | return; |
| 5090 | if (atomic_dec_and_test(&bbio->refs)) |
| 5091 | kfree(bbio); |
| 5092 | } |
| 5093 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5094 | 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] | 5095 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5096 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5097 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5098 | { |
| 5099 | struct extent_map *em; |
| 5100 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5101 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5102 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5103 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5104 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5105 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5106 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5107 | u64 stripe_nr_orig; |
| 5108 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5109 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5110 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5111 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5112 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5113 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5114 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5115 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5116 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5117 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 5118 | int dev_replace_is_ongoing = 0; |
| 5119 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5120 | int patch_the_first_stripe_for_dev_replace = 0; |
| 5121 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5122 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5123 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5124 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5125 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5126 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5127 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5128 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5129 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5130 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5131 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5132 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5133 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5134 | if (em->start > logical || em->start + em->len < logical) { |
| 5135 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5136 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5137 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 5138 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5139 | return -EINVAL; |
| 5140 | } |
| 5141 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5142 | map = (struct map_lookup *)em->bdev; |
| 5143 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5144 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5145 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5146 | stripe_nr = offset; |
| 5147 | /* |
| 5148 | * stripe_nr counts the total number of stripes we have to stride |
| 5149 | * to get to this block |
| 5150 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5151 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5152 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5153 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5154 | BUG_ON(offset < stripe_offset); |
| 5155 | |
| 5156 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5157 | stripe_offset = offset - stripe_offset; |
| 5158 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5159 | /* 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] | 5160 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5161 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5162 | raid56_full_stripe_start = offset; |
| 5163 | |
| 5164 | /* allow a write of a full stripe, but make sure we don't |
| 5165 | * allow straddling of stripes |
| 5166 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5167 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5168 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5169 | raid56_full_stripe_start *= full_stripe_len; |
| 5170 | } |
| 5171 | |
| 5172 | if (rw & REQ_DISCARD) { |
| 5173 | /* we don't discard raid56 yet */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5174 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5175 | ret = -EOPNOTSUPP; |
| 5176 | goto out; |
| 5177 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5178 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5179 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5180 | u64 max_len; |
| 5181 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5182 | For other RAID types and for RAID[56] reads, just allow a single |
| 5183 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5184 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5185 | (rw & REQ_WRITE)) { |
| 5186 | max_len = stripe_len * nr_data_stripes(map) - |
| 5187 | (offset - raid56_full_stripe_start); |
| 5188 | } else { |
| 5189 | /* we limit the length of each bio to what fits in a stripe */ |
| 5190 | max_len = stripe_len - stripe_offset; |
| 5191 | } |
| 5192 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5193 | } else { |
| 5194 | *length = em->len - offset; |
| 5195 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5196 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5197 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5198 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5199 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5200 | goto out; |
| 5201 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5202 | btrfs_dev_replace_lock(dev_replace); |
| 5203 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5204 | if (!dev_replace_is_ongoing) |
| 5205 | btrfs_dev_replace_unlock(dev_replace); |
| 5206 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5207 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5208 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5209 | dev_replace->tgtdev != NULL) { |
| 5210 | /* |
| 5211 | * in dev-replace case, for repair case (that's the only |
| 5212 | * case where the mirror is selected explicitly when |
| 5213 | * calling btrfs_map_block), blocks left of the left cursor |
| 5214 | * can also be read from the target drive. |
| 5215 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5216 | * the last one to the array of stripes. For READ, it also |
| 5217 | * needs to be supported using the same mirror number. |
| 5218 | * If the requested block is not left of the left cursor, |
| 5219 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5220 | * returns one more in the dev-replace case. |
| 5221 | */ |
| 5222 | u64 tmp_length = *length; |
| 5223 | struct btrfs_bio *tmp_bbio = NULL; |
| 5224 | int tmp_num_stripes; |
| 5225 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5226 | int index_srcdev = 0; |
| 5227 | int found = 0; |
| 5228 | u64 physical_of_found = 0; |
| 5229 | |
| 5230 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5231 | logical, &tmp_length, &tmp_bbio, 0, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5232 | if (ret) { |
| 5233 | WARN_ON(tmp_bbio != NULL); |
| 5234 | goto out; |
| 5235 | } |
| 5236 | |
| 5237 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5238 | if (mirror_num > tmp_num_stripes) { |
| 5239 | /* |
| 5240 | * REQ_GET_READ_MIRRORS does not contain this |
| 5241 | * mirror, that means that the requested area |
| 5242 | * is not left of the left cursor |
| 5243 | */ |
| 5244 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5245 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5246 | goto out; |
| 5247 | } |
| 5248 | |
| 5249 | /* |
| 5250 | * process the rest of the function using the mirror_num |
| 5251 | * of the source drive. Therefore look it up first. |
| 5252 | * At the end, patch the device pointer to the one of the |
| 5253 | * target drive. |
| 5254 | */ |
| 5255 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5256 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5257 | /* |
| 5258 | * In case of DUP, in order to keep it |
| 5259 | * simple, only add the mirror with the |
| 5260 | * lowest physical address |
| 5261 | */ |
| 5262 | if (found && |
| 5263 | physical_of_found <= |
| 5264 | tmp_bbio->stripes[i].physical) |
| 5265 | continue; |
| 5266 | index_srcdev = i; |
| 5267 | found = 1; |
| 5268 | physical_of_found = |
| 5269 | tmp_bbio->stripes[i].physical; |
| 5270 | } |
| 5271 | } |
| 5272 | |
| 5273 | if (found) { |
| 5274 | mirror_num = index_srcdev + 1; |
| 5275 | patch_the_first_stripe_for_dev_replace = 1; |
| 5276 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5277 | } else { |
| 5278 | WARN_ON(1); |
| 5279 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5280 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5281 | goto out; |
| 5282 | } |
| 5283 | |
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 | } else if (mirror_num > map->num_stripes) { |
| 5286 | mirror_num = 0; |
| 5287 | } |
| 5288 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5289 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5290 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5291 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5292 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5293 | stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5294 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5295 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5296 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5297 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5298 | if (rw & REQ_DISCARD) |
| 5299 | num_stripes = min_t(u64, map->num_stripes, |
| 5300 | stripe_nr_end - stripe_nr_orig); |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5301 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5302 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5303 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5304 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5305 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5306 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5307 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5308 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5309 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5310 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5311 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5312 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5313 | current->pid % map->num_stripes, |
| 5314 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5315 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5316 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5317 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5318 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5319 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5320 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5321 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5322 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5323 | } else { |
| 5324 | mirror_num = 1; |
| 5325 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5326 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5327 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5328 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5329 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5330 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5331 | stripe_index *= map->sub_stripes; |
| 5332 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5333 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5334 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5335 | else if (rw & REQ_DISCARD) |
| 5336 | num_stripes = min_t(u64, map->sub_stripes * |
| 5337 | (stripe_nr_end - stripe_nr_orig), |
| 5338 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5339 | else if (mirror_num) |
| 5340 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5341 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5342 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5343 | stripe_index = find_live_mirror(fs_info, map, |
| 5344 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5345 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5346 | current->pid % map->sub_stripes, |
| 5347 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5348 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5349 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5350 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5351 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5352 | if (need_raid_map && |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5353 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5354 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5355 | /* push stripe_nr back to the start of the full stripe */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5356 | stripe_nr = div_u64(raid56_full_stripe_start, |
| 5357 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5358 | |
| 5359 | /* RAID[56] write or recovery. Return all stripes */ |
| 5360 | num_stripes = map->num_stripes; |
| 5361 | max_errors = nr_parity_stripes(map); |
| 5362 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5363 | *length = map->stripe_len; |
| 5364 | stripe_index = 0; |
| 5365 | stripe_offset = 0; |
| 5366 | } else { |
| 5367 | /* |
| 5368 | * Mirror #0 or #1 means the original data block. |
| 5369 | * Mirror #2 is RAID5 parity block. |
| 5370 | * Mirror #3 is RAID6 Q block. |
| 5371 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5372 | stripe_nr = div_u64_rem(stripe_nr, |
| 5373 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5374 | if (mirror_num > 1) |
| 5375 | stripe_index = nr_data_stripes(map) + |
| 5376 | mirror_num - 2; |
| 5377 | |
| 5378 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5379 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5380 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5381 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5382 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5383 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5384 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5385 | } else { |
| 5386 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5387 | * after this, stripe_nr is the number of stripes on this |
| 5388 | * device we have to walk to find the data, and stripe_index is |
| 5389 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5390 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5391 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5392 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5393 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5394 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5395 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5396 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5397 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5398 | if (dev_replace_is_ongoing) { |
| 5399 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5400 | num_alloc_stripes <<= 1; |
| 5401 | if (rw & REQ_GET_READ_MIRRORS) |
| 5402 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5403 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5404 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5405 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5406 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5407 | if (!bbio) { |
| 5408 | ret = -ENOMEM; |
| 5409 | goto out; |
| 5410 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5411 | if (dev_replace_is_ongoing) |
| 5412 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5413 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5414 | /* build raid_map */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5415 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5416 | need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5417 | mirror_num > 1)) { |
| 5418 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5419 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5420 | |
| 5421 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5422 | sizeof(struct btrfs_bio_stripe) * |
| 5423 | num_alloc_stripes + |
| 5424 | sizeof(int) * tgtdev_indexes); |
| 5425 | |
| 5426 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5427 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5428 | |
| 5429 | /* Fill in the logical address of each stripe */ |
| 5430 | tmp = stripe_nr * nr_data_stripes(map); |
| 5431 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5432 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5433 | em->start + (tmp + i) * map->stripe_len; |
| 5434 | |
| 5435 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5436 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5437 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5438 | RAID6_Q_STRIPE; |
| 5439 | } |
| 5440 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5441 | if (rw & REQ_DISCARD) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5442 | u32 factor = 0; |
| 5443 | u32 sub_stripes = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5444 | u64 stripes_per_dev = 0; |
| 5445 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5446 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5447 | |
| 5448 | if (map->type & |
| 5449 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5450 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5451 | sub_stripes = 1; |
| 5452 | else |
| 5453 | sub_stripes = map->sub_stripes; |
| 5454 | |
| 5455 | factor = map->num_stripes / sub_stripes; |
| 5456 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5457 | stripe_nr_orig, |
| 5458 | factor, |
| 5459 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5460 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5461 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5462 | } |
| 5463 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5464 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5465 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5466 | map->stripes[stripe_index].physical + |
| 5467 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5468 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5469 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5470 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5471 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5472 | bbio->stripes[i].length = stripes_per_dev * |
| 5473 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5474 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5475 | if (i / sub_stripes < remaining_stripes) |
| 5476 | bbio->stripes[i].length += |
| 5477 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5478 | |
| 5479 | /* |
| 5480 | * Special for the first stripe and |
| 5481 | * the last stripe: |
| 5482 | * |
| 5483 | * |-------|...|-------| |
| 5484 | * |----------| |
| 5485 | * off end_off |
| 5486 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5487 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5488 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5489 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5490 | |
| 5491 | if (stripe_index >= last_stripe && |
| 5492 | stripe_index <= (last_stripe + |
| 5493 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5494 | bbio->stripes[i].length -= |
| 5495 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5496 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5497 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5498 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5499 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5500 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5501 | |
| 5502 | stripe_index++; |
| 5503 | if (stripe_index == map->num_stripes) { |
| 5504 | /* This could only happen for RAID0/10 */ |
| 5505 | stripe_index = 0; |
| 5506 | stripe_nr++; |
| 5507 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5508 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5509 | } else { |
| 5510 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5511 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5512 | map->stripes[stripe_index].physical + |
| 5513 | stripe_offset + |
| 5514 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5515 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5516 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5517 | stripe_index++; |
| 5518 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5519 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5520 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5521 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5522 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5523 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5524 | if (bbio->raid_map) |
| 5525 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5526 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5527 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5528 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5529 | dev_replace->tgtdev != NULL) { |
| 5530 | int index_where_to_add; |
| 5531 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5532 | |
| 5533 | /* |
| 5534 | * duplicate the write operations while the dev replace |
| 5535 | * procedure is running. Since the copying of the old disk |
| 5536 | * to the new disk takes place at run time while the |
| 5537 | * filesystem is mounted writable, the regular write |
| 5538 | * operations to the old disk have to be duplicated to go |
| 5539 | * to the new disk as well. |
| 5540 | * Note that device->missing is handled by the caller, and |
| 5541 | * that the write to the old disk is already set up in the |
| 5542 | * stripes array. |
| 5543 | */ |
| 5544 | index_where_to_add = num_stripes; |
| 5545 | for (i = 0; i < num_stripes; i++) { |
| 5546 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5547 | /* write to new disk, too */ |
| 5548 | struct btrfs_bio_stripe *new = |
| 5549 | bbio->stripes + index_where_to_add; |
| 5550 | struct btrfs_bio_stripe *old = |
| 5551 | bbio->stripes + i; |
| 5552 | |
| 5553 | new->physical = old->physical; |
| 5554 | new->length = old->length; |
| 5555 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5556 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5557 | index_where_to_add++; |
| 5558 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5559 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5560 | } |
| 5561 | } |
| 5562 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5563 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5564 | dev_replace->tgtdev != NULL) { |
| 5565 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5566 | int index_srcdev = 0; |
| 5567 | int found = 0; |
| 5568 | u64 physical_of_found = 0; |
| 5569 | |
| 5570 | /* |
| 5571 | * During the dev-replace procedure, the target drive can |
| 5572 | * also be used to read data in case it is needed to repair |
| 5573 | * a corrupt block elsewhere. This is possible if the |
| 5574 | * requested area is left of the left cursor. In this area, |
| 5575 | * the target drive is a full copy of the source drive. |
| 5576 | */ |
| 5577 | for (i = 0; i < num_stripes; i++) { |
| 5578 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5579 | /* |
| 5580 | * In case of DUP, in order to keep it |
| 5581 | * simple, only add the mirror with the |
| 5582 | * lowest physical address |
| 5583 | */ |
| 5584 | if (found && |
| 5585 | physical_of_found <= |
| 5586 | bbio->stripes[i].physical) |
| 5587 | continue; |
| 5588 | index_srcdev = i; |
| 5589 | found = 1; |
| 5590 | physical_of_found = bbio->stripes[i].physical; |
| 5591 | } |
| 5592 | } |
| 5593 | if (found) { |
David Sterba | 258ece0 | 2015-02-24 19:45:15 +0100 | [diff] [blame] | 5594 | if (physical_of_found + map->stripe_len <= |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5595 | dev_replace->cursor_left) { |
| 5596 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5597 | bbio->stripes + num_stripes; |
| 5598 | |
| 5599 | tgtdev_stripe->physical = physical_of_found; |
| 5600 | tgtdev_stripe->length = |
| 5601 | bbio->stripes[index_srcdev].length; |
| 5602 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5603 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5604 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5605 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5606 | num_stripes++; |
| 5607 | } |
| 5608 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5609 | } |
| 5610 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5611 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5612 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5613 | bbio->num_stripes = num_stripes; |
| 5614 | bbio->max_errors = max_errors; |
| 5615 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5616 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5617 | |
| 5618 | /* |
| 5619 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5620 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5621 | * available as a mirror |
| 5622 | */ |
| 5623 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5624 | WARN_ON(num_stripes > 1); |
| 5625 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5626 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5627 | bbio->mirror_num = map->num_stripes + 1; |
| 5628 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5629 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5630 | if (dev_replace_is_ongoing) |
| 5631 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5632 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5633 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5634 | } |
| 5635 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5636 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5637 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5638 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5639 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5640 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5641 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5642 | } |
| 5643 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5644 | /* For Scrub/replace */ |
| 5645 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5646 | u64 logical, u64 *length, |
| 5647 | struct btrfs_bio **bbio_ret, int mirror_num, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5648 | int need_raid_map) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5649 | { |
| 5650 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5651 | mirror_num, need_raid_map); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5652 | } |
| 5653 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5654 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5655 | u64 chunk_start, u64 physical, u64 devid, |
| 5656 | u64 **logical, int *naddrs, int *stripe_len) |
| 5657 | { |
| 5658 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5659 | struct extent_map *em; |
| 5660 | struct map_lookup *map; |
| 5661 | u64 *buf; |
| 5662 | u64 bytenr; |
| 5663 | u64 length; |
| 5664 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5665 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5666 | int i, j, nr = 0; |
| 5667 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5668 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5669 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5670 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5671 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5672 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5673 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5674 | chunk_start); |
| 5675 | return -EIO; |
| 5676 | } |
| 5677 | |
| 5678 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5679 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5680 | em->start, chunk_start); |
| 5681 | free_extent_map(em); |
| 5682 | return -EIO; |
| 5683 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5684 | map = (struct map_lookup *)em->bdev; |
| 5685 | |
| 5686 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5687 | rmap_len = map->stripe_len; |
| 5688 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5689 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5690 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5691 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5692 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5693 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5694 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5695 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5696 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5697 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5698 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5699 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5700 | |
| 5701 | for (i = 0; i < map->num_stripes; i++) { |
| 5702 | if (devid && map->stripes[i].dev->devid != devid) |
| 5703 | continue; |
| 5704 | if (map->stripes[i].physical > physical || |
| 5705 | map->stripes[i].physical + length <= physical) |
| 5706 | continue; |
| 5707 | |
| 5708 | stripe_nr = physical - map->stripes[i].physical; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5709 | stripe_nr = div_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5710 | |
| 5711 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5712 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5713 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5714 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5715 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5716 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5717 | * Alternatively, just use rmap_len below instead of |
| 5718 | * map->stripe_len */ |
| 5719 | |
| 5720 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5721 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5722 | for (j = 0; j < nr; j++) { |
| 5723 | if (buf[j] == bytenr) |
| 5724 | break; |
| 5725 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5726 | if (j == nr) { |
| 5727 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5728 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5729 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5730 | } |
| 5731 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5732 | *logical = buf; |
| 5733 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5734 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5735 | |
| 5736 | free_extent_map(em); |
| 5737 | return 0; |
| 5738 | } |
| 5739 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5740 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio) |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5741 | { |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5742 | bio->bi_private = bbio->private; |
| 5743 | bio->bi_end_io = bbio->end_io; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5744 | bio_endio(bio); |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5745 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5746 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5747 | } |
| 5748 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5749 | static void btrfs_end_bio(struct bio *bio) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5750 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5751 | struct btrfs_bio *bbio = bio->bi_private; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5752 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5753 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5754 | if (bio->bi_error) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5755 | atomic_inc(&bbio->error); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5756 | if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) { |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5757 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5758 | btrfs_io_bio(bio)->stripe_index; |
Zhao Lei | 65f5333 | 2015-06-08 20:05:50 +0800 | [diff] [blame] | 5759 | struct btrfs_device *dev; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5760 | |
| 5761 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5762 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5763 | if (dev->bdev) { |
| 5764 | if (bio->bi_rw & WRITE) |
| 5765 | btrfs_dev_stat_inc(dev, |
| 5766 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5767 | else |
| 5768 | btrfs_dev_stat_inc(dev, |
| 5769 | BTRFS_DEV_STAT_READ_ERRS); |
| 5770 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5771 | btrfs_dev_stat_inc(dev, |
| 5772 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5773 | btrfs_dev_stat_print_on_error(dev); |
| 5774 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5775 | } |
| 5776 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5777 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5778 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5779 | is_orig_bio = 1; |
| 5780 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5781 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5782 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5783 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5784 | if (!is_orig_bio) { |
| 5785 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5786 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5787 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5788 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5789 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5790 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5791 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5792 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5793 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5794 | bio->bi_error = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5795 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5796 | /* |
| 5797 | * this bio is actually up to date, we didn't |
| 5798 | * go over the max number of errors |
| 5799 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5800 | bio->bi_error = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5801 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5802 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5803 | btrfs_end_bbio(bbio, bio); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5804 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5805 | bio_put(bio); |
| 5806 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5807 | } |
| 5808 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5809 | /* |
| 5810 | * see run_scheduled_bios for a description of why bios are collected for |
| 5811 | * async submit. |
| 5812 | * |
| 5813 | * This will add one bio to the pending list for a device and make sure |
| 5814 | * the work struct is scheduled. |
| 5815 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5816 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5817 | struct btrfs_device *device, |
| 5818 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5819 | { |
| 5820 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5821 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5822 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5823 | if (device->missing || !device->bdev) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5824 | bio_io_error(bio); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5825 | return; |
| 5826 | } |
| 5827 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5828 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5829 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5830 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5831 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5832 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5833 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5837 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5838 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5839 | * made progress against dirty pages when we've really just put it |
| 5840 | * on a queue for later |
| 5841 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5842 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5843 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5844 | bio->bi_next = NULL; |
| 5845 | bio->bi_rw |= rw; |
| 5846 | |
| 5847 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5848 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5849 | pending_bios = &device->pending_sync_bios; |
| 5850 | else |
| 5851 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5852 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5853 | if (pending_bios->tail) |
| 5854 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5855 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5856 | pending_bios->tail = bio; |
| 5857 | if (!pending_bios->head) |
| 5858 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5859 | if (device->running_pending) |
| 5860 | should_queue = 0; |
| 5861 | |
| 5862 | spin_unlock(&device->io_lock); |
| 5863 | |
| 5864 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5865 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5866 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5867 | } |
| 5868 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5869 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5870 | struct bio *bio, u64 physical, int dev_nr, |
| 5871 | int rw, int async) |
| 5872 | { |
| 5873 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5874 | |
| 5875 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5876 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5877 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5878 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5879 | #ifdef DEBUG |
| 5880 | { |
| 5881 | struct rcu_string *name; |
| 5882 | |
| 5883 | rcu_read_lock(); |
| 5884 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5885 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5886 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5887 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5888 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5889 | rcu_read_unlock(); |
| 5890 | } |
| 5891 | #endif |
| 5892 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5893 | |
| 5894 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5895 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5896 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5897 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5898 | else |
| 5899 | btrfsic_submit_bio(rw, bio); |
| 5900 | } |
| 5901 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5902 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5903 | { |
| 5904 | atomic_inc(&bbio->error); |
| 5905 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5906 | /* Shoud be the original bio. */ |
| 5907 | WARN_ON(bio != bbio->orig_bio); |
| 5908 | |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5909 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5910 | bio->bi_iter.bi_sector = logical >> 9; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5911 | bio->bi_error = -EIO; |
| 5912 | btrfs_end_bbio(bbio, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5913 | } |
| 5914 | } |
| 5915 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5916 | 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] | 5917 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5918 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5919 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5920 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5921 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5922 | u64 length = 0; |
| 5923 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5924 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5925 | int dev_nr; |
| 5926 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5927 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5928 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5929 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5930 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5931 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5932 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5933 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5934 | mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5935 | if (ret) { |
| 5936 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5937 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5938 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5939 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5940 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5941 | bbio->orig_bio = first_bio; |
| 5942 | bbio->private = first_bio->bi_private; |
| 5943 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5944 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5945 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5946 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5947 | if (bbio->raid_map) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5948 | /* In this case, map_length has been set to the length of |
| 5949 | a single stripe; not the whole write */ |
| 5950 | if (rw & WRITE) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5951 | ret = raid56_parity_write(root, bio, bbio, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5952 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5953 | ret = raid56_parity_recover(root, bio, bbio, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5954 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5955 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5956 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5957 | btrfs_bio_counter_dec(root->fs_info); |
| 5958 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5959 | } |
| 5960 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5961 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5962 | 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] | 5963 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5964 | BUG(); |
| 5965 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5966 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5967 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5968 | dev = bbio->stripes[dev_nr].dev; |
| 5969 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5970 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5971 | continue; |
| 5972 | } |
| 5973 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5974 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5975 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5976 | BUG_ON(!bio); /* -ENOMEM */ |
Mike Snitzer | 326e1db | 2015-05-22 09:14:03 -0400 | [diff] [blame] | 5977 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5978 | bio = first_bio; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5979 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5980 | submit_stripe_bio(root, bbio, bio, |
| 5981 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5982 | async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5983 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5984 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5985 | return 0; |
| 5986 | } |
| 5987 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5988 | 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] | 5989 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5990 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5991 | struct btrfs_device *device; |
| 5992 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5993 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5994 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5995 | while (cur_devices) { |
| 5996 | if (!fsid || |
| 5997 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5998 | device = __find_device(&cur_devices->devices, |
| 5999 | devid, uuid); |
| 6000 | if (device) |
| 6001 | return device; |
| 6002 | } |
| 6003 | cur_devices = cur_devices->seed; |
| 6004 | } |
| 6005 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6006 | } |
| 6007 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6008 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6009 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6010 | u64 devid, u8 *dev_uuid) |
| 6011 | { |
| 6012 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6013 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6014 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 6015 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 6016 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6017 | |
| 6018 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6019 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6020 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6021 | |
| 6022 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6023 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6024 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6025 | return device; |
| 6026 | } |
| 6027 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6028 | /** |
| 6029 | * btrfs_alloc_device - allocate struct btrfs_device |
| 6030 | * @fs_info: used only for generating a new devid, can be NULL if |
| 6031 | * devid is provided (i.e. @devid != NULL). |
| 6032 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 6033 | * is generated. |
| 6034 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 6035 | * is generated. |
| 6036 | * |
| 6037 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 6038 | * on error. Returned struct is not linked onto any lists and can be |
| 6039 | * destroyed with kfree() right away. |
| 6040 | */ |
| 6041 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 6042 | const u64 *devid, |
| 6043 | const u8 *uuid) |
| 6044 | { |
| 6045 | struct btrfs_device *dev; |
| 6046 | u64 tmp; |
| 6047 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6048 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6049 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6050 | |
| 6051 | dev = __alloc_device(); |
| 6052 | if (IS_ERR(dev)) |
| 6053 | return dev; |
| 6054 | |
| 6055 | if (devid) |
| 6056 | tmp = *devid; |
| 6057 | else { |
| 6058 | int ret; |
| 6059 | |
| 6060 | ret = find_next_devid(fs_info, &tmp); |
| 6061 | if (ret) { |
| 6062 | kfree(dev); |
| 6063 | return ERR_PTR(ret); |
| 6064 | } |
| 6065 | } |
| 6066 | dev->devid = tmp; |
| 6067 | |
| 6068 | if (uuid) |
| 6069 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6070 | else |
| 6071 | generate_random_uuid(dev->uuid); |
| 6072 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6073 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6074 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6075 | |
| 6076 | return dev; |
| 6077 | } |
| 6078 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6079 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 6080 | struct extent_buffer *leaf, |
| 6081 | struct btrfs_chunk *chunk) |
| 6082 | { |
| 6083 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6084 | struct map_lookup *map; |
| 6085 | struct extent_map *em; |
| 6086 | u64 logical; |
| 6087 | u64 length; |
| 6088 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6089 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6090 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6091 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6092 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6093 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6094 | logical = key->offset; |
| 6095 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6096 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6097 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6098 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6099 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6100 | |
| 6101 | /* already mapped? */ |
| 6102 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6103 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6104 | return 0; |
| 6105 | } else if (em) { |
| 6106 | free_extent_map(em); |
| 6107 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6108 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6109 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6110 | if (!em) |
| 6111 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6112 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6113 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6114 | if (!map) { |
| 6115 | free_extent_map(em); |
| 6116 | return -ENOMEM; |
| 6117 | } |
| 6118 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6119 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6120 | em->bdev = (struct block_device *)map; |
| 6121 | em->start = logical; |
| 6122 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6123 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6124 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6125 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6126 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6127 | map->num_stripes = num_stripes; |
| 6128 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6129 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6130 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6131 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6132 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6133 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6134 | for (i = 0; i < num_stripes; i++) { |
| 6135 | map->stripes[i].physical = |
| 6136 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6137 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6138 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6139 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6140 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6141 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6142 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6143 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6144 | free_extent_map(em); |
| 6145 | return -EIO; |
| 6146 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6147 | if (!map->stripes[i].dev) { |
| 6148 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6149 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6150 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6151 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6152 | free_extent_map(em); |
| 6153 | return -EIO; |
| 6154 | } |
Anand Jain | 816fceb | 2015-04-27 12:46:18 +0800 | [diff] [blame] | 6155 | btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing", |
| 6156 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6157 | } |
| 6158 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6159 | } |
| 6160 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6161 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6162 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6163 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6164 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6165 | free_extent_map(em); |
| 6166 | |
| 6167 | return 0; |
| 6168 | } |
| 6169 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6170 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6171 | struct btrfs_dev_item *dev_item, |
| 6172 | struct btrfs_device *device) |
| 6173 | { |
| 6174 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6175 | |
| 6176 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6177 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6178 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6179 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6180 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6181 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6182 | device->type = btrfs_device_type(leaf, dev_item); |
| 6183 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6184 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6185 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6186 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6187 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6188 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6189 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6190 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6191 | } |
| 6192 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6193 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6194 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6195 | { |
| 6196 | struct btrfs_fs_devices *fs_devices; |
| 6197 | int ret; |
| 6198 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6199 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6200 | |
| 6201 | fs_devices = root->fs_info->fs_devices->seed; |
| 6202 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6203 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6204 | return fs_devices; |
| 6205 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6206 | fs_devices = fs_devices->seed; |
| 6207 | } |
| 6208 | |
| 6209 | fs_devices = find_fsid(fsid); |
| 6210 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6211 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6212 | return ERR_PTR(-ENOENT); |
| 6213 | |
| 6214 | fs_devices = alloc_fs_devices(fsid); |
| 6215 | if (IS_ERR(fs_devices)) |
| 6216 | return fs_devices; |
| 6217 | |
| 6218 | fs_devices->seeding = 1; |
| 6219 | fs_devices->opened = 1; |
| 6220 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6221 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6222 | |
| 6223 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6224 | if (IS_ERR(fs_devices)) |
| 6225 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6226 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6227 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6228 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6229 | if (ret) { |
| 6230 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6231 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6232 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6233 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6234 | |
| 6235 | if (!fs_devices->seeding) { |
| 6236 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6237 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6238 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6239 | goto out; |
| 6240 | } |
| 6241 | |
| 6242 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6243 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6244 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6245 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6246 | } |
| 6247 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6248 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6249 | struct extent_buffer *leaf, |
| 6250 | struct btrfs_dev_item *dev_item) |
| 6251 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6252 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6253 | struct btrfs_device *device; |
| 6254 | u64 devid; |
| 6255 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6256 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6257 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6258 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6259 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6260 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6261 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6262 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6263 | BTRFS_UUID_SIZE); |
| 6264 | |
| 6265 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6266 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6267 | if (IS_ERR(fs_devices)) |
| 6268 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6269 | } |
| 6270 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6271 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6272 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6273 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6274 | return -EIO; |
| 6275 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6276 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6277 | if (!device) |
| 6278 | return -ENOMEM; |
Anand Jain | 33b97e4 | 2015-05-08 04:34:35 +0800 | [diff] [blame] | 6279 | btrfs_warn(root->fs_info, "devid %llu uuid %pU missing", |
| 6280 | devid, dev_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6281 | } else { |
| 6282 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6283 | return -EIO; |
| 6284 | |
| 6285 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6286 | /* |
| 6287 | * this happens when a device that was properly setup |
| 6288 | * in the device info lists suddenly goes bad. |
| 6289 | * device->bdev is NULL, and so we have to set |
| 6290 | * device->missing to one here |
| 6291 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6292 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6293 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6294 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6295 | |
| 6296 | /* Move the device to its own fs_devices */ |
| 6297 | if (device->fs_devices != fs_devices) { |
| 6298 | ASSERT(device->missing); |
| 6299 | |
| 6300 | list_move(&device->dev_list, &fs_devices->devices); |
| 6301 | device->fs_devices->num_devices--; |
| 6302 | fs_devices->num_devices++; |
| 6303 | |
| 6304 | device->fs_devices->missing_devices--; |
| 6305 | fs_devices->missing_devices++; |
| 6306 | |
| 6307 | device->fs_devices = fs_devices; |
| 6308 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6309 | } |
| 6310 | |
| 6311 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6312 | BUG_ON(device->writeable); |
| 6313 | if (device->generation != |
| 6314 | btrfs_device_generation(leaf, dev_item)) |
| 6315 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6316 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6317 | |
| 6318 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6319 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6320 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6321 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6322 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6323 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6324 | device->bytes_used; |
| 6325 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6326 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6327 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6328 | return ret; |
| 6329 | } |
| 6330 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6331 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6332 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6333 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6334 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6335 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6336 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6337 | u8 *array_ptr; |
| 6338 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6339 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6340 | u32 num_stripes; |
| 6341 | u32 array_size; |
| 6342 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6343 | u32 cur_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6344 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6345 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6346 | ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize); |
| 6347 | /* |
| 6348 | * This will create extent buffer of nodesize, superblock size is |
| 6349 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6350 | * overallocate but we can keep it as-is, only the first page is used. |
| 6351 | */ |
| 6352 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6353 | if (!sb) |
| 6354 | return -ENOMEM; |
| 6355 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6356 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6357 | /* |
| 6358 | * The sb extent buffer is artifical and just used to read the system array. |
| 6359 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6360 | * pages up-to-date when the page is larger: extent does not cover the |
| 6361 | * whole page and consequently check_page_uptodate does not find all |
| 6362 | * the page's extents up-to-date (the hole beyond sb), |
| 6363 | * write_extent_buffer then triggers a WARN_ON. |
| 6364 | * |
| 6365 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6366 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6367 | * to silence the warning eg. on PowerPC 64. |
| 6368 | */ |
| 6369 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6370 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6371 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6372 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6373 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6374 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6375 | array_ptr = super_copy->sys_chunk_array; |
| 6376 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6377 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6378 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6379 | while (cur_offset < array_size) { |
| 6380 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6381 | len = sizeof(*disk_key); |
| 6382 | if (cur_offset + len > array_size) |
| 6383 | goto out_short_read; |
| 6384 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6385 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6386 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6387 | array_ptr += len; |
| 6388 | sb_array_offset += len; |
| 6389 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6390 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6391 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6392 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6393 | /* |
| 6394 | * At least one btrfs_chunk with one stripe must be |
| 6395 | * present, exact stripe count check comes afterwards |
| 6396 | */ |
| 6397 | len = btrfs_chunk_item_size(1); |
| 6398 | if (cur_offset + len > array_size) |
| 6399 | goto out_short_read; |
| 6400 | |
| 6401 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6402 | len = btrfs_chunk_item_size(num_stripes); |
| 6403 | if (cur_offset + len > array_size) |
| 6404 | goto out_short_read; |
| 6405 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6406 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6407 | if (ret) |
| 6408 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6409 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6410 | ret = -EIO; |
| 6411 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6412 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6413 | array_ptr += len; |
| 6414 | sb_array_offset += len; |
| 6415 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6416 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6417 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6418 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6419 | |
| 6420 | out_short_read: |
| 6421 | printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n", |
| 6422 | len, cur_offset); |
| 6423 | free_extent_buffer(sb); |
| 6424 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6425 | } |
| 6426 | |
| 6427 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6428 | { |
| 6429 | struct btrfs_path *path; |
| 6430 | struct extent_buffer *leaf; |
| 6431 | struct btrfs_key key; |
| 6432 | struct btrfs_key found_key; |
| 6433 | int ret; |
| 6434 | int slot; |
| 6435 | |
| 6436 | root = root->fs_info->chunk_root; |
| 6437 | |
| 6438 | path = btrfs_alloc_path(); |
| 6439 | if (!path) |
| 6440 | return -ENOMEM; |
| 6441 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6442 | mutex_lock(&uuid_mutex); |
| 6443 | lock_chunks(root); |
| 6444 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6445 | /* |
| 6446 | * Read all device items, and then all the chunk items. All |
| 6447 | * device items are found before any chunk item (their object id |
| 6448 | * is smaller than the lowest possible object id for a chunk |
| 6449 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6450 | */ |
| 6451 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6452 | key.offset = 0; |
| 6453 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6454 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6455 | if (ret < 0) |
| 6456 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6457 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6458 | leaf = path->nodes[0]; |
| 6459 | slot = path->slots[0]; |
| 6460 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6461 | ret = btrfs_next_leaf(root, path); |
| 6462 | if (ret == 0) |
| 6463 | continue; |
| 6464 | if (ret < 0) |
| 6465 | goto error; |
| 6466 | break; |
| 6467 | } |
| 6468 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6469 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6470 | struct btrfs_dev_item *dev_item; |
| 6471 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6472 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6473 | ret = read_one_dev(root, leaf, dev_item); |
| 6474 | if (ret) |
| 6475 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6476 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6477 | struct btrfs_chunk *chunk; |
| 6478 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6479 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6480 | if (ret) |
| 6481 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6482 | } |
| 6483 | path->slots[0]++; |
| 6484 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6485 | ret = 0; |
| 6486 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6487 | unlock_chunks(root); |
| 6488 | mutex_unlock(&uuid_mutex); |
| 6489 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6490 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6491 | return ret; |
| 6492 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6493 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6494 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6495 | { |
| 6496 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6497 | struct btrfs_device *device; |
| 6498 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6499 | while (fs_devices) { |
| 6500 | mutex_lock(&fs_devices->device_list_mutex); |
| 6501 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6502 | device->dev_root = fs_info->dev_root; |
| 6503 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6504 | |
| 6505 | fs_devices = fs_devices->seed; |
| 6506 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6509 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6510 | { |
| 6511 | int i; |
| 6512 | |
| 6513 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6514 | btrfs_dev_stat_reset(dev, i); |
| 6515 | } |
| 6516 | |
| 6517 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6518 | { |
| 6519 | struct btrfs_key key; |
| 6520 | struct btrfs_key found_key; |
| 6521 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6522 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6523 | struct extent_buffer *eb; |
| 6524 | int slot; |
| 6525 | int ret = 0; |
| 6526 | struct btrfs_device *device; |
| 6527 | struct btrfs_path *path = NULL; |
| 6528 | int i; |
| 6529 | |
| 6530 | path = btrfs_alloc_path(); |
| 6531 | if (!path) { |
| 6532 | ret = -ENOMEM; |
| 6533 | goto out; |
| 6534 | } |
| 6535 | |
| 6536 | mutex_lock(&fs_devices->device_list_mutex); |
| 6537 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6538 | int item_size; |
| 6539 | struct btrfs_dev_stats_item *ptr; |
| 6540 | |
| 6541 | key.objectid = 0; |
| 6542 | key.type = BTRFS_DEV_STATS_KEY; |
| 6543 | key.offset = device->devid; |
| 6544 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6545 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6546 | __btrfs_reset_dev_stats(device); |
| 6547 | device->dev_stats_valid = 1; |
| 6548 | btrfs_release_path(path); |
| 6549 | continue; |
| 6550 | } |
| 6551 | slot = path->slots[0]; |
| 6552 | eb = path->nodes[0]; |
| 6553 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6554 | item_size = btrfs_item_size_nr(eb, slot); |
| 6555 | |
| 6556 | ptr = btrfs_item_ptr(eb, slot, |
| 6557 | struct btrfs_dev_stats_item); |
| 6558 | |
| 6559 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6560 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6561 | btrfs_dev_stat_set(device, i, |
| 6562 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6563 | else |
| 6564 | btrfs_dev_stat_reset(device, i); |
| 6565 | } |
| 6566 | |
| 6567 | device->dev_stats_valid = 1; |
| 6568 | btrfs_dev_stat_print_on_load(device); |
| 6569 | btrfs_release_path(path); |
| 6570 | } |
| 6571 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6572 | |
| 6573 | out: |
| 6574 | btrfs_free_path(path); |
| 6575 | return ret < 0 ? ret : 0; |
| 6576 | } |
| 6577 | |
| 6578 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6579 | struct btrfs_root *dev_root, |
| 6580 | struct btrfs_device *device) |
| 6581 | { |
| 6582 | struct btrfs_path *path; |
| 6583 | struct btrfs_key key; |
| 6584 | struct extent_buffer *eb; |
| 6585 | struct btrfs_dev_stats_item *ptr; |
| 6586 | int ret; |
| 6587 | int i; |
| 6588 | |
| 6589 | key.objectid = 0; |
| 6590 | key.type = BTRFS_DEV_STATS_KEY; |
| 6591 | key.offset = device->devid; |
| 6592 | |
| 6593 | path = btrfs_alloc_path(); |
| 6594 | BUG_ON(!path); |
| 6595 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6596 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6597 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6598 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6599 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6600 | goto out; |
| 6601 | } |
| 6602 | |
| 6603 | if (ret == 0 && |
| 6604 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6605 | /* need to delete old one and insert a new one */ |
| 6606 | ret = btrfs_del_item(trans, dev_root, path); |
| 6607 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6608 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6609 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6610 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6611 | goto out; |
| 6612 | } |
| 6613 | ret = 1; |
| 6614 | } |
| 6615 | |
| 6616 | if (ret == 1) { |
| 6617 | /* need to insert a new item */ |
| 6618 | btrfs_release_path(path); |
| 6619 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6620 | &key, sizeof(*ptr)); |
| 6621 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6622 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6623 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6624 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6625 | goto out; |
| 6626 | } |
| 6627 | } |
| 6628 | |
| 6629 | eb = path->nodes[0]; |
| 6630 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6631 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6632 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6633 | btrfs_dev_stat_read(device, i)); |
| 6634 | btrfs_mark_buffer_dirty(eb); |
| 6635 | |
| 6636 | out: |
| 6637 | btrfs_free_path(path); |
| 6638 | return ret; |
| 6639 | } |
| 6640 | |
| 6641 | /* |
| 6642 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6643 | */ |
| 6644 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6645 | struct btrfs_fs_info *fs_info) |
| 6646 | { |
| 6647 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6648 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6649 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6650 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6651 | int ret = 0; |
| 6652 | |
| 6653 | mutex_lock(&fs_devices->device_list_mutex); |
| 6654 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6655 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6656 | continue; |
| 6657 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6658 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6659 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6660 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6661 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6662 | } |
| 6663 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6664 | |
| 6665 | return ret; |
| 6666 | } |
| 6667 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6668 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6669 | { |
| 6670 | btrfs_dev_stat_inc(dev, index); |
| 6671 | btrfs_dev_stat_print_on_error(dev); |
| 6672 | } |
| 6673 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6674 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6675 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6676 | if (!dev->dev_stats_valid) |
| 6677 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6678 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6679 | "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] | 6680 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6681 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6682 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6683 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6684 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6685 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6686 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6687 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6688 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6689 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6690 | int i; |
| 6691 | |
| 6692 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6693 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6694 | break; |
| 6695 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6696 | return; /* all values == 0, suppress message */ |
| 6697 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6698 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6699 | "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] | 6700 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6701 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6702 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6703 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6704 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6705 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6706 | } |
| 6707 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6708 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6709 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6710 | { |
| 6711 | struct btrfs_device *dev; |
| 6712 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6713 | int i; |
| 6714 | |
| 6715 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6716 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6717 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6718 | |
| 6719 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6720 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6721 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6722 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6723 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6724 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6725 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6726 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6727 | if (stats->nr_items > i) |
| 6728 | stats->values[i] = |
| 6729 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6730 | else |
| 6731 | btrfs_dev_stat_reset(dev, i); |
| 6732 | } |
| 6733 | } else { |
| 6734 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6735 | if (stats->nr_items > i) |
| 6736 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6737 | } |
| 6738 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6739 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6740 | return 0; |
| 6741 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6742 | |
| 6743 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6744 | { |
| 6745 | struct buffer_head *bh; |
| 6746 | struct btrfs_super_block *disk_super; |
| 6747 | |
| 6748 | bh = btrfs_read_dev_super(device->bdev); |
Anand Jain | 92fc03f | 2015-08-14 18:32:51 +0800 | [diff] [blame] | 6749 | if (IS_ERR(bh)) |
| 6750 | return PTR_ERR(bh); |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6751 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6752 | |
| 6753 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6754 | set_buffer_dirty(bh); |
| 6755 | sync_dirty_buffer(bh); |
| 6756 | brelse(bh); |
| 6757 | |
| 6758 | return 0; |
| 6759 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6760 | |
| 6761 | /* |
| 6762 | * Update the size of all devices, which is used for writing out the |
| 6763 | * super blocks. |
| 6764 | */ |
| 6765 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6766 | { |
| 6767 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6768 | struct btrfs_device *curr, *next; |
| 6769 | |
| 6770 | if (list_empty(&fs_devices->resized_devices)) |
| 6771 | return; |
| 6772 | |
| 6773 | mutex_lock(&fs_devices->device_list_mutex); |
| 6774 | lock_chunks(fs_info->dev_root); |
| 6775 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6776 | resized_list) { |
| 6777 | list_del_init(&curr->resized_list); |
| 6778 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6779 | } |
| 6780 | unlock_chunks(fs_info->dev_root); |
| 6781 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6782 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6783 | |
| 6784 | /* Must be invoked during the transaction commit */ |
| 6785 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6786 | struct btrfs_transaction *transaction) |
| 6787 | { |
| 6788 | struct extent_map *em; |
| 6789 | struct map_lookup *map; |
| 6790 | struct btrfs_device *dev; |
| 6791 | int i; |
| 6792 | |
| 6793 | if (list_empty(&transaction->pending_chunks)) |
| 6794 | return; |
| 6795 | |
| 6796 | /* In order to kick the device replace finish process */ |
| 6797 | lock_chunks(root); |
| 6798 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6799 | map = (struct map_lookup *)em->bdev; |
| 6800 | |
| 6801 | for (i = 0; i < map->num_stripes; i++) { |
| 6802 | dev = map->stripes[i].dev; |
| 6803 | dev->commit_bytes_used = dev->bytes_used; |
| 6804 | } |
| 6805 | } |
| 6806 | unlock_chunks(root); |
| 6807 | } |
Anand Jain | 5a13f43 | 2015-03-10 06:38:31 +0800 | [diff] [blame] | 6808 | |
| 6809 | void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6810 | { |
| 6811 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6812 | while (fs_devices) { |
| 6813 | fs_devices->fs_info = fs_info; |
| 6814 | fs_devices = fs_devices->seed; |
| 6815 | } |
| 6816 | } |
| 6817 | |
| 6818 | void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info) |
| 6819 | { |
| 6820 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6821 | while (fs_devices) { |
| 6822 | fs_devices->fs_info = NULL; |
| 6823 | fs_devices = fs_devices->seed; |
| 6824 | } |
| 6825 | } |