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); |
| 55 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 56 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 57 | { |
| 58 | struct btrfs_fs_devices *fs_devs; |
| 59 | |
| 60 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 61 | if (!fs_devs) |
| 62 | return ERR_PTR(-ENOMEM); |
| 63 | |
| 64 | mutex_init(&fs_devs->device_list_mutex); |
| 65 | |
| 66 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 67 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 68 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 69 | INIT_LIST_HEAD(&fs_devs->list); |
| 70 | |
| 71 | return fs_devs; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 76 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 77 | * generated. |
| 78 | * |
| 79 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 80 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 81 | * can be destroyed with kfree() right away. |
| 82 | */ |
| 83 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 84 | { |
| 85 | struct btrfs_fs_devices *fs_devs; |
| 86 | |
| 87 | fs_devs = __alloc_fs_devices(); |
| 88 | if (IS_ERR(fs_devs)) |
| 89 | return fs_devs; |
| 90 | |
| 91 | if (fsid) |
| 92 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 93 | else |
| 94 | generate_random_uuid(fs_devs->fsid); |
| 95 | |
| 96 | return fs_devs; |
| 97 | } |
| 98 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 99 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 100 | { |
| 101 | struct btrfs_device *device; |
| 102 | WARN_ON(fs_devices->opened); |
| 103 | while (!list_empty(&fs_devices->devices)) { |
| 104 | device = list_entry(fs_devices->devices.next, |
| 105 | struct btrfs_device, dev_list); |
| 106 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 107 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 108 | kfree(device); |
| 109 | } |
| 110 | kfree(fs_devices); |
| 111 | } |
| 112 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 113 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 114 | enum kobject_action action) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 119 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 120 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 121 | action, |
| 122 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 123 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 124 | } |
| 125 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 126 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 127 | { |
| 128 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 129 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 130 | while (!list_empty(&fs_uuids)) { |
| 131 | fs_devices = list_entry(fs_uuids.next, |
| 132 | struct btrfs_fs_devices, list); |
| 133 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 134 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 135 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 138 | static struct btrfs_device *__alloc_device(void) |
| 139 | { |
| 140 | struct btrfs_device *dev; |
| 141 | |
| 142 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 143 | if (!dev) |
| 144 | return ERR_PTR(-ENOMEM); |
| 145 | |
| 146 | INIT_LIST_HEAD(&dev->dev_list); |
| 147 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 148 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 149 | |
| 150 | spin_lock_init(&dev->io_lock); |
| 151 | |
| 152 | spin_lock_init(&dev->reada_lock); |
| 153 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 154 | atomic_set(&dev->dev_stats_ccnt, 0); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 155 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 156 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 157 | |
| 158 | return dev; |
| 159 | } |
| 160 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 161 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 162 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 163 | { |
| 164 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 165 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 166 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 167 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 168 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 169 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 170 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 171 | } |
| 172 | return NULL; |
| 173 | } |
| 174 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 175 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 176 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 177 | struct btrfs_fs_devices *fs_devices; |
| 178 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 179 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 180 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 181 | return fs_devices; |
| 182 | } |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 186 | static int |
| 187 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 188 | int flush, struct block_device **bdev, |
| 189 | struct buffer_head **bh) |
| 190 | { |
| 191 | int ret; |
| 192 | |
| 193 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 194 | |
| 195 | if (IS_ERR(*bdev)) { |
| 196 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 197 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 198 | goto error; |
| 199 | } |
| 200 | |
| 201 | if (flush) |
| 202 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 203 | ret = set_blocksize(*bdev, 4096); |
| 204 | if (ret) { |
| 205 | blkdev_put(*bdev, flags); |
| 206 | goto error; |
| 207 | } |
| 208 | invalidate_bdev(*bdev); |
| 209 | *bh = btrfs_read_dev_super(*bdev); |
| 210 | if (!*bh) { |
| 211 | ret = -EINVAL; |
| 212 | blkdev_put(*bdev, flags); |
| 213 | goto error; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | |
| 218 | error: |
| 219 | *bdev = NULL; |
| 220 | *bh = NULL; |
| 221 | return ret; |
| 222 | } |
| 223 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 224 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 225 | struct bio *head, struct bio *tail) |
| 226 | { |
| 227 | |
| 228 | struct bio *old_head; |
| 229 | |
| 230 | old_head = pending_bios->head; |
| 231 | pending_bios->head = head; |
| 232 | if (pending_bios->tail) |
| 233 | tail->bi_next = old_head; |
| 234 | else |
| 235 | pending_bios->tail = tail; |
| 236 | } |
| 237 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 238 | /* |
| 239 | * we try to collect pending bios for a device so we don't get a large |
| 240 | * number of procs sending bios down to the same device. This greatly |
| 241 | * improves the schedulers ability to collect and merge the bios. |
| 242 | * |
| 243 | * But, it also turns into a long list of bios to process and that is sure |
| 244 | * to eventually make the worker thread block. The solution here is to |
| 245 | * make some progress and then put this work struct back at the end of |
| 246 | * the list if the block device is congested. This way, multiple devices |
| 247 | * can make progress from a single worker thread. |
| 248 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 249 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 250 | { |
| 251 | struct bio *pending; |
| 252 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 253 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 254 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 255 | struct bio *tail; |
| 256 | struct bio *cur; |
| 257 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 258 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 259 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 260 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 261 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 262 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 263 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 264 | struct blk_plug plug; |
| 265 | |
| 266 | /* |
| 267 | * this function runs all the bios we've collected for |
| 268 | * a particular device. We don't want to wander off to |
| 269 | * another device without first sending all of these down. |
| 270 | * So, setup a plug here and finish it off before we return |
| 271 | */ |
| 272 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 273 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 274 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 275 | fs_info = device->dev_root->fs_info; |
| 276 | limit = btrfs_async_submit_limit(fs_info); |
| 277 | limit = limit * 2 / 3; |
| 278 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 279 | loop: |
| 280 | spin_lock(&device->io_lock); |
| 281 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 282 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 283 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 284 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 285 | /* take all the bios off the list at once and process them |
| 286 | * later on (without the lock held). But, remember the |
| 287 | * tail and other pointers so the bios can be properly reinserted |
| 288 | * into the list if we hit congestion |
| 289 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 290 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 291 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 292 | force_reg = 1; |
| 293 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 294 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 295 | force_reg = 0; |
| 296 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 297 | |
| 298 | pending = pending_bios->head; |
| 299 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 300 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * if pending was null this time around, no bios need processing |
| 304 | * at all and we can stop. Otherwise it'll loop back up again |
| 305 | * and do an additional check so no bios are missed. |
| 306 | * |
| 307 | * device->running_pending is used to synchronize with the |
| 308 | * schedule_bio code. |
| 309 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 310 | if (device->pending_sync_bios.head == NULL && |
| 311 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 312 | again = 0; |
| 313 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 314 | } else { |
| 315 | again = 1; |
| 316 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 317 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 318 | |
| 319 | pending_bios->head = NULL; |
| 320 | pending_bios->tail = NULL; |
| 321 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 322 | spin_unlock(&device->io_lock); |
| 323 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 324 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 325 | |
| 326 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 327 | /* we want to work on both lists, but do more bios on the |
| 328 | * sync list than the regular list |
| 329 | */ |
| 330 | if ((num_run > 32 && |
| 331 | pending_bios != &device->pending_sync_bios && |
| 332 | device->pending_sync_bios.head) || |
| 333 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 334 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 335 | spin_lock(&device->io_lock); |
| 336 | requeue_list(pending_bios, pending, tail); |
| 337 | goto loop_lock; |
| 338 | } |
| 339 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 340 | cur = pending; |
| 341 | pending = pending->bi_next; |
| 342 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 343 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 344 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 345 | waitqueue_active(&fs_info->async_submit_wait)) |
| 346 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 347 | |
| 348 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 349 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 350 | /* |
| 351 | * if we're doing the sync list, record that our |
| 352 | * plug has some sync requests on it |
| 353 | * |
| 354 | * If we're doing the regular list and there are |
| 355 | * sync requests sitting around, unplug before |
| 356 | * we add more |
| 357 | */ |
| 358 | if (pending_bios == &device->pending_sync_bios) { |
| 359 | sync_pending = 1; |
| 360 | } else if (sync_pending) { |
| 361 | blk_finish_plug(&plug); |
| 362 | blk_start_plug(&plug); |
| 363 | sync_pending = 0; |
| 364 | } |
| 365 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 366 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 367 | num_run++; |
| 368 | batch_run++; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 369 | |
| 370 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * we made progress, there is more work to do and the bdi |
| 374 | * is now congested. Back off and let other work structs |
| 375 | * run instead |
| 376 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 377 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 378 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 379 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 380 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 381 | ioc = current->io_context; |
| 382 | |
| 383 | /* |
| 384 | * the main goal here is that we don't want to |
| 385 | * block if we're going to be able to submit |
| 386 | * more requests without blocking. |
| 387 | * |
| 388 | * This code does two great things, it pokes into |
| 389 | * the elevator code from a filesystem _and_ |
| 390 | * it makes assumptions about how batching works. |
| 391 | */ |
| 392 | if (ioc && ioc->nr_batch_requests > 0 && |
| 393 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 394 | (last_waited == 0 || |
| 395 | ioc->last_waited == last_waited)) { |
| 396 | /* |
| 397 | * we want to go through our batch of |
| 398 | * requests and stop. So, we copy out |
| 399 | * the ioc->last_waited time and test |
| 400 | * against it before looping |
| 401 | */ |
| 402 | last_waited = ioc->last_waited; |
David Sterba | 853d8ec | 2015-01-08 15:15:19 +0100 | [diff] [blame] | 403 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 404 | continue; |
| 405 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 406 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 407 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 408 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 409 | |
| 410 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 411 | btrfs_queue_work(fs_info->submit_workers, |
| 412 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 413 | goto done; |
| 414 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 415 | /* unplug every 64 requests just for good measure */ |
| 416 | if (batch_run % 64 == 0) { |
| 417 | blk_finish_plug(&plug); |
| 418 | blk_start_plug(&plug); |
| 419 | sync_pending = 0; |
| 420 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 421 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 422 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 423 | cond_resched(); |
| 424 | if (again) |
| 425 | goto loop; |
| 426 | |
| 427 | spin_lock(&device->io_lock); |
| 428 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 429 | goto loop_lock; |
| 430 | spin_unlock(&device->io_lock); |
| 431 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 432 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 433 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 436 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 437 | { |
| 438 | struct btrfs_device *device; |
| 439 | |
| 440 | device = container_of(work, struct btrfs_device, work); |
| 441 | run_scheduled_bios(device); |
| 442 | } |
| 443 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 444 | /* |
| 445 | * Add new device to list of registered devices |
| 446 | * |
| 447 | * Returns: |
| 448 | * 1 - first time device is seen |
| 449 | * 0 - device already known |
| 450 | * < 0 - error |
| 451 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 452 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 453 | struct btrfs_super_block *disk_super, |
| 454 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 455 | { |
| 456 | struct btrfs_device *device; |
| 457 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 458 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 459 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 460 | u64 found_transid = btrfs_super_generation(disk_super); |
| 461 | |
| 462 | fs_devices = find_fsid(disk_super->fsid); |
| 463 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 464 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 465 | if (IS_ERR(fs_devices)) |
| 466 | return PTR_ERR(fs_devices); |
| 467 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 468 | list_add(&fs_devices->list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 469 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 470 | device = NULL; |
| 471 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 472 | device = __find_device(&fs_devices->devices, devid, |
| 473 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 474 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 475 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 476 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 477 | if (fs_devices->opened) |
| 478 | return -EBUSY; |
| 479 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 480 | device = btrfs_alloc_device(NULL, &devid, |
| 481 | disk_super->dev_item.uuid); |
| 482 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 483 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 484 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 485 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 486 | |
| 487 | name = rcu_string_strdup(path, GFP_NOFS); |
| 488 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 489 | kfree(device); |
| 490 | return -ENOMEM; |
| 491 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 492 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 493 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 494 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 495 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 496 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 497 | mutex_unlock(&fs_devices->device_list_mutex); |
| 498 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 499 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 500 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 501 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 502 | /* |
| 503 | * When FS is already mounted. |
| 504 | * 1. If you are here and if the device->name is NULL that |
| 505 | * means this device was missing at time of FS mount. |
| 506 | * 2. If you are here and if the device->name is different |
| 507 | * from 'path' that means either |
| 508 | * a. The same device disappeared and reappeared with |
| 509 | * different name. or |
| 510 | * b. The missing-disk-which-was-replaced, has |
| 511 | * reappeared now. |
| 512 | * |
| 513 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 514 | * and unintentional. |
| 515 | * |
| 516 | * Further in case of 1 and 2a above, the disk at 'path' |
| 517 | * would have missed some transaction when it was away and |
| 518 | * in case of 2a the stale bdev has to be updated as well. |
| 519 | * 2b must not be allowed at all time. |
| 520 | */ |
| 521 | |
| 522 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 523 | * For now, we do allow update to btrfs_fs_device through the |
| 524 | * btrfs dev scan cli after FS has been mounted. We're still |
| 525 | * tracking a problem where systems fail mount by subvolume id |
| 526 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 527 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 528 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 529 | /* |
| 530 | * That is if the FS is _not_ mounted and if you |
| 531 | * are here, that means there is more than one |
| 532 | * disk with same uuid and devid.We keep the one |
| 533 | * with larger generation number or the last-in if |
| 534 | * generation are equal. |
| 535 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 536 | return -EEXIST; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 537 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 538 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 539 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 540 | if (!name) |
| 541 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 542 | rcu_string_free(device->name); |
| 543 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 544 | if (device->missing) { |
| 545 | fs_devices->missing_devices--; |
| 546 | device->missing = 0; |
| 547 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 550 | /* |
| 551 | * Unmount does not free the btrfs_device struct but would zero |
| 552 | * generation along with most of the other members. So just update |
| 553 | * it back. We need it to pick the disk with largest generation |
| 554 | * (as above). |
| 555 | */ |
| 556 | if (!fs_devices->opened) |
| 557 | device->generation = found_transid; |
| 558 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 559 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 560 | |
| 561 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 564 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 565 | { |
| 566 | struct btrfs_fs_devices *fs_devices; |
| 567 | struct btrfs_device *device; |
| 568 | struct btrfs_device *orig_dev; |
| 569 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 570 | fs_devices = alloc_fs_devices(orig->fsid); |
| 571 | if (IS_ERR(fs_devices)) |
| 572 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 573 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 574 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 575 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 576 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 577 | /* 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] | 578 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 579 | struct rcu_string *name; |
| 580 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 581 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 582 | orig_dev->uuid); |
| 583 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 584 | goto error; |
| 585 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 586 | /* |
| 587 | * This is ok to do without rcu read locked because we hold the |
| 588 | * uuid mutex so nothing we touch in here is going to disappear. |
| 589 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 590 | if (orig_dev->name) { |
| 591 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 592 | if (!name) { |
| 593 | kfree(device); |
| 594 | goto error; |
| 595 | } |
| 596 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 597 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 598 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 599 | list_add(&device->dev_list, &fs_devices->devices); |
| 600 | device->fs_devices = fs_devices; |
| 601 | fs_devices->num_devices++; |
| 602 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 603 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 604 | return fs_devices; |
| 605 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 606 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 607 | free_fs_devices(fs_devices); |
| 608 | return ERR_PTR(-ENOMEM); |
| 609 | } |
| 610 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 611 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 612 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 613 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 614 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 615 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 616 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 617 | mutex_lock(&uuid_mutex); |
| 618 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 619 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 620 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 621 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 622 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 623 | (!latest_dev || |
| 624 | device->generation > latest_dev->generation)) { |
| 625 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 626 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 627 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 628 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 629 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 630 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 631 | /* |
| 632 | * In the first step, keep the device which has |
| 633 | * the correct fsid and the devid that is used |
| 634 | * for the dev_replace procedure. |
| 635 | * In the second step, the dev_replace state is |
| 636 | * read from the device tree and it is known |
| 637 | * whether the procedure is really active or |
| 638 | * not, which means whether this device is |
| 639 | * used or whether it should be removed. |
| 640 | */ |
| 641 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 642 | continue; |
| 643 | } |
| 644 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 645 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 646 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 647 | device->bdev = NULL; |
| 648 | fs_devices->open_devices--; |
| 649 | } |
| 650 | if (device->writeable) { |
| 651 | list_del_init(&device->dev_alloc_list); |
| 652 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 653 | if (!device->is_tgtdev_for_dev_replace) |
| 654 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 655 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 656 | list_del_init(&device->dev_list); |
| 657 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 658 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 659 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 660 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 661 | |
| 662 | if (fs_devices->seed) { |
| 663 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 664 | goto again; |
| 665 | } |
| 666 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 667 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 668 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 669 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 670 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 671 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 672 | static void __free_device(struct work_struct *work) |
| 673 | { |
| 674 | struct btrfs_device *device; |
| 675 | |
| 676 | device = container_of(work, struct btrfs_device, rcu_work); |
| 677 | |
| 678 | if (device->bdev) |
| 679 | blkdev_put(device->bdev, device->mode); |
| 680 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 681 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 682 | kfree(device); |
| 683 | } |
| 684 | |
| 685 | static void free_device(struct rcu_head *head) |
| 686 | { |
| 687 | struct btrfs_device *device; |
| 688 | |
| 689 | device = container_of(head, struct btrfs_device, rcu); |
| 690 | |
| 691 | INIT_WORK(&device->rcu_work, __free_device); |
| 692 | schedule_work(&device->rcu_work); |
| 693 | } |
| 694 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 695 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 696 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 697 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 698 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 699 | if (--fs_devices->opened > 0) |
| 700 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 701 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 702 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 703 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 704 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 705 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 706 | |
| 707 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 708 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 709 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 710 | if (device->writeable && |
| 711 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 712 | list_del_init(&device->dev_alloc_list); |
| 713 | fs_devices->rw_devices--; |
| 714 | } |
| 715 | |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 716 | if (device->missing) |
| 717 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 718 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 719 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 720 | device->uuid); |
| 721 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 722 | |
| 723 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 724 | if (device->name) { |
| 725 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 726 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 727 | rcu_assign_pointer(new_device->name, name); |
| 728 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 729 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 730 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 731 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 732 | |
| 733 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 734 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 735 | mutex_unlock(&fs_devices->device_list_mutex); |
| 736 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 737 | WARN_ON(fs_devices->open_devices); |
| 738 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 739 | fs_devices->opened = 0; |
| 740 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 741 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 745 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 746 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 747 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 748 | int ret; |
| 749 | |
| 750 | mutex_lock(&uuid_mutex); |
| 751 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 752 | if (!fs_devices->opened) { |
| 753 | seed_devices = fs_devices->seed; |
| 754 | fs_devices->seed = NULL; |
| 755 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 756 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 757 | |
| 758 | while (seed_devices) { |
| 759 | fs_devices = seed_devices; |
| 760 | seed_devices = fs_devices->seed; |
| 761 | __btrfs_close_devices(fs_devices); |
| 762 | free_fs_devices(fs_devices); |
| 763 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 764 | /* |
| 765 | * Wait for rcu kworkers under __btrfs_close_devices |
| 766 | * to finish all blkdev_puts so device is really |
| 767 | * free when umount is done. |
| 768 | */ |
| 769 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 770 | return ret; |
| 771 | } |
| 772 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 773 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 774 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 775 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 776 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 777 | struct block_device *bdev; |
| 778 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 779 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 780 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 781 | struct buffer_head *bh; |
| 782 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 783 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 784 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 785 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 786 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 787 | flags |= FMODE_EXCL; |
| 788 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 789 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 790 | if (device->bdev) |
| 791 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 792 | if (!device->name) |
| 793 | continue; |
| 794 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 795 | /* Just open everything we can; ignore failures here */ |
| 796 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 797 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 798 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 799 | |
| 800 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 801 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 802 | if (devid != device->devid) |
| 803 | goto error_brelse; |
| 804 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 805 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 806 | BTRFS_UUID_SIZE)) |
| 807 | goto error_brelse; |
| 808 | |
| 809 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 810 | if (!latest_dev || |
| 811 | device->generation > latest_dev->generation) |
| 812 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 813 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 814 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 815 | device->writeable = 0; |
| 816 | } else { |
| 817 | device->writeable = !bdev_read_only(bdev); |
| 818 | seeding = 0; |
| 819 | } |
| 820 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 821 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 822 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 823 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 824 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 825 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 826 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 827 | device->mode = flags; |
| 828 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 829 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 830 | fs_devices->rotating = 1; |
| 831 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 832 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 833 | if (device->writeable && |
| 834 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 835 | fs_devices->rw_devices++; |
| 836 | list_add(&device->dev_alloc_list, |
| 837 | &fs_devices->alloc_list); |
| 838 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 839 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 840 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 841 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 842 | error_brelse: |
| 843 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 844 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 845 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 846 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 847 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 848 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 849 | goto out; |
| 850 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 851 | fs_devices->seeding = seeding; |
| 852 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 853 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 854 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 855 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 860 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 861 | { |
| 862 | int ret; |
| 863 | |
| 864 | mutex_lock(&uuid_mutex); |
| 865 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 866 | fs_devices->opened++; |
| 867 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 868 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 869 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 870 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 871 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 872 | return ret; |
| 873 | } |
| 874 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 875 | /* |
| 876 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 877 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 878 | * is read via pagecache |
| 879 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 880 | 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] | 881 | struct btrfs_fs_devices **fs_devices_ret) |
| 882 | { |
| 883 | struct btrfs_super_block *disk_super; |
| 884 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 885 | struct page *page; |
| 886 | void *p; |
| 887 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 888 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 889 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 890 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 891 | u64 bytenr; |
| 892 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 893 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 894 | /* |
| 895 | * we would like to check all the supers, but that would make |
| 896 | * a btrfs mount succeed after a mkfs from a different FS. |
| 897 | * So, we need to add a special mount option to scan for |
| 898 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 899 | */ |
| 900 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 901 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 902 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 903 | |
| 904 | bdev = blkdev_get_by_path(path, flags, holder); |
| 905 | |
| 906 | if (IS_ERR(bdev)) { |
| 907 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 908 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* make sure our super fits in the device */ |
| 912 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 913 | goto error_bdev_put; |
| 914 | |
| 915 | /* make sure our super fits in the page */ |
| 916 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 917 | goto error_bdev_put; |
| 918 | |
| 919 | /* make sure our super doesn't straddle pages on disk */ |
| 920 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 921 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 922 | goto error_bdev_put; |
| 923 | |
| 924 | /* pull in the page with our super */ |
| 925 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 926 | index, GFP_NOFS); |
| 927 | |
| 928 | if (IS_ERR_OR_NULL(page)) |
| 929 | goto error_bdev_put; |
| 930 | |
| 931 | p = kmap(page); |
| 932 | |
| 933 | /* align our pointer to the offset of the super block */ |
| 934 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 935 | |
| 936 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 937 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 938 | goto error_unmap; |
| 939 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 940 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 941 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 942 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 943 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 944 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 945 | if (ret > 0) { |
| 946 | if (disk_super->label[0]) { |
| 947 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 948 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 949 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 950 | } else { |
| 951 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 952 | } |
| 953 | |
| 954 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 955 | ret = 0; |
| 956 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 957 | if (!ret && fs_devices_ret) |
| 958 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 959 | |
| 960 | error_unmap: |
| 961 | kunmap(page); |
| 962 | page_cache_release(page); |
| 963 | |
| 964 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 965 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 966 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 967 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 968 | return ret; |
| 969 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 970 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 971 | /* helper to account the used device space in the range */ |
| 972 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 973 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 974 | { |
| 975 | struct btrfs_key key; |
| 976 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 977 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 978 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 979 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 980 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 981 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 982 | struct extent_buffer *l; |
| 983 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 984 | *length = 0; |
| 985 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 986 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 987 | return 0; |
| 988 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 989 | path = btrfs_alloc_path(); |
| 990 | if (!path) |
| 991 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 992 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 993 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 994 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 995 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 996 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 997 | |
| 998 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 999 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1000 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1001 | if (ret > 0) { |
| 1002 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1003 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1004 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1005 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1006 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1007 | while (1) { |
| 1008 | l = path->nodes[0]; |
| 1009 | slot = path->slots[0]; |
| 1010 | if (slot >= btrfs_header_nritems(l)) { |
| 1011 | ret = btrfs_next_leaf(root, path); |
| 1012 | if (ret == 0) |
| 1013 | continue; |
| 1014 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1015 | goto out; |
| 1016 | |
| 1017 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1018 | } |
| 1019 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1020 | |
| 1021 | if (key.objectid < device->devid) |
| 1022 | goto next; |
| 1023 | |
| 1024 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1025 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1026 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1027 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1028 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1029 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1030 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1031 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1032 | dev_extent); |
| 1033 | if (key.offset <= start && extent_end > end) { |
| 1034 | *length = end - start + 1; |
| 1035 | break; |
| 1036 | } else if (key.offset <= start && extent_end > start) |
| 1037 | *length += extent_end - start; |
| 1038 | else if (key.offset > start && extent_end <= end) |
| 1039 | *length += extent_end - key.offset; |
| 1040 | else if (key.offset > start && key.offset <= end) { |
| 1041 | *length += end - key.offset + 1; |
| 1042 | break; |
| 1043 | } else if (key.offset > end) |
| 1044 | break; |
| 1045 | |
| 1046 | next: |
| 1047 | path->slots[0]++; |
| 1048 | } |
| 1049 | ret = 0; |
| 1050 | out: |
| 1051 | btrfs_free_path(path); |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1055 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1056 | struct btrfs_device *device, |
| 1057 | u64 *start, u64 len) |
| 1058 | { |
| 1059 | struct extent_map *em; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1060 | struct list_head *search_list = &trans->transaction->pending_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1061 | int ret = 0; |
| 1062 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1063 | again: |
| 1064 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1065 | struct map_lookup *map; |
| 1066 | int i; |
| 1067 | |
| 1068 | map = (struct map_lookup *)em->bdev; |
| 1069 | for (i = 0; i < map->num_stripes; i++) { |
| 1070 | if (map->stripes[i].dev != device) |
| 1071 | continue; |
| 1072 | if (map->stripes[i].physical >= *start + len || |
| 1073 | map->stripes[i].physical + em->orig_block_len <= |
| 1074 | *start) |
| 1075 | continue; |
| 1076 | *start = map->stripes[i].physical + |
| 1077 | em->orig_block_len; |
| 1078 | ret = 1; |
| 1079 | } |
| 1080 | } |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1081 | if (search_list == &trans->transaction->pending_chunks) { |
| 1082 | search_list = &trans->root->fs_info->pinned_chunks; |
| 1083 | goto again; |
| 1084 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1085 | |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
| 1089 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1090 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1091 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1092 | * @device: the device which we search the free space in |
| 1093 | * @num_bytes: the size of the free space that we need |
| 1094 | * @start: store the start of the free space. |
| 1095 | * @len: the size of the free space. that we find, or the size of the max |
| 1096 | * free space if we don't find suitable free space |
| 1097 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1098 | * this uses a pretty simple search, the expectation is that it is |
| 1099 | * called very infrequently and that a given device has a small number |
| 1100 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1101 | * |
| 1102 | * @start is used to store the start of the free space if we find. But if we |
| 1103 | * don't find suitable free space, it will be used to store the start position |
| 1104 | * of the max free space. |
| 1105 | * |
| 1106 | * @len is used to store the size of the free space that we find. |
| 1107 | * But if we don't find suitable free space, it is used to store the size of |
| 1108 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1109 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1110 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1111 | struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1112 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1113 | { |
| 1114 | struct btrfs_key key; |
| 1115 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1116 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1117 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1118 | u64 hole_size; |
| 1119 | u64 max_hole_start; |
| 1120 | u64 max_hole_size; |
| 1121 | u64 extent_end; |
| 1122 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1123 | u64 search_end = device->total_bytes; |
| 1124 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1125 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1126 | struct extent_buffer *l; |
| 1127 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1128 | /* FIXME use last free of some kind */ |
| 1129 | |
| 1130 | /* we don't want to overwrite the superblock on the drive, |
| 1131 | * so we make sure to start at an offset of at least 1MB |
| 1132 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1133 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1134 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1135 | path = btrfs_alloc_path(); |
| 1136 | if (!path) |
| 1137 | return -ENOMEM; |
| 1138 | again: |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1139 | max_hole_start = search_start; |
| 1140 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1141 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1142 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1143 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1144 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1145 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1148 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1149 | path->search_commit_root = 1; |
| 1150 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1151 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1152 | key.objectid = device->devid; |
| 1153 | key.offset = search_start; |
| 1154 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1155 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1156 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1157 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1158 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1159 | if (ret > 0) { |
| 1160 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1161 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1162 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1163 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1164 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1165 | while (1) { |
| 1166 | l = path->nodes[0]; |
| 1167 | slot = path->slots[0]; |
| 1168 | if (slot >= btrfs_header_nritems(l)) { |
| 1169 | ret = btrfs_next_leaf(root, path); |
| 1170 | if (ret == 0) |
| 1171 | continue; |
| 1172 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1173 | goto out; |
| 1174 | |
| 1175 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1176 | } |
| 1177 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1178 | |
| 1179 | if (key.objectid < device->devid) |
| 1180 | goto next; |
| 1181 | |
| 1182 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1183 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1184 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1185 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1186 | goto next; |
| 1187 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1188 | if (key.offset > search_start) { |
| 1189 | hole_size = key.offset - search_start; |
| 1190 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1191 | /* |
| 1192 | * Have to check before we set max_hole_start, otherwise |
| 1193 | * we could end up sending back this offset anyway. |
| 1194 | */ |
| 1195 | if (contains_pending_extent(trans, device, |
| 1196 | &search_start, |
| 1197 | hole_size)) |
| 1198 | hole_size = 0; |
| 1199 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1200 | if (hole_size > max_hole_size) { |
| 1201 | max_hole_start = search_start; |
| 1202 | max_hole_size = hole_size; |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * If this free space is greater than which we need, |
| 1207 | * it must be the max free space that we have found |
| 1208 | * until now, so max_hole_start must point to the start |
| 1209 | * of this free space and the length of this free space |
| 1210 | * is stored in max_hole_size. Thus, we return |
| 1211 | * max_hole_start and max_hole_size and go back to the |
| 1212 | * caller. |
| 1213 | */ |
| 1214 | if (hole_size >= num_bytes) { |
| 1215 | ret = 0; |
| 1216 | goto out; |
| 1217 | } |
| 1218 | } |
| 1219 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1220 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1221 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1222 | dev_extent); |
| 1223 | if (extent_end > search_start) |
| 1224 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1225 | next: |
| 1226 | path->slots[0]++; |
| 1227 | cond_resched(); |
| 1228 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1229 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1230 | /* |
| 1231 | * At this point, search_start should be the end of |
| 1232 | * allocated dev extents, and when shrinking the device, |
| 1233 | * search_end may be smaller than search_start. |
| 1234 | */ |
| 1235 | if (search_end > search_start) |
| 1236 | hole_size = search_end - search_start; |
| 1237 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1238 | if (hole_size > max_hole_size) { |
| 1239 | max_hole_start = search_start; |
| 1240 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1241 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1242 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1243 | if (contains_pending_extent(trans, device, &search_start, hole_size)) { |
| 1244 | btrfs_release_path(path); |
| 1245 | goto again; |
| 1246 | } |
| 1247 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1248 | /* See above. */ |
| 1249 | if (hole_size < num_bytes) |
| 1250 | ret = -ENOSPC; |
| 1251 | else |
| 1252 | ret = 0; |
| 1253 | |
| 1254 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1255 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1256 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1257 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1258 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1259 | return ret; |
| 1260 | } |
| 1261 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1262 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1263 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1264 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1265 | { |
| 1266 | int ret; |
| 1267 | struct btrfs_path *path; |
| 1268 | struct btrfs_root *root = device->dev_root; |
| 1269 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1270 | struct btrfs_key found_key; |
| 1271 | struct extent_buffer *leaf = NULL; |
| 1272 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1273 | |
| 1274 | path = btrfs_alloc_path(); |
| 1275 | if (!path) |
| 1276 | return -ENOMEM; |
| 1277 | |
| 1278 | key.objectid = device->devid; |
| 1279 | key.offset = start; |
| 1280 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1281 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1282 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1283 | if (ret > 0) { |
| 1284 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1285 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1286 | if (ret) |
| 1287 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1288 | leaf = path->nodes[0]; |
| 1289 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1290 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1291 | struct btrfs_dev_extent); |
| 1292 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1293 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1294 | key = found_key; |
| 1295 | btrfs_release_path(path); |
| 1296 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1297 | } else if (ret == 0) { |
| 1298 | leaf = path->nodes[0]; |
| 1299 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1300 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1301 | } else { |
| 1302 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1303 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1304 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1305 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1306 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1307 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1308 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1309 | if (ret) { |
| 1310 | btrfs_error(root->fs_info, ret, |
| 1311 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1312 | } else { |
| 1313 | trans->transaction->have_free_bgs = 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1314 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1315 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1316 | btrfs_free_path(path); |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1320 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1321 | struct btrfs_device *device, |
| 1322 | u64 chunk_tree, u64 chunk_objectid, |
| 1323 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1324 | { |
| 1325 | int ret; |
| 1326 | struct btrfs_path *path; |
| 1327 | struct btrfs_root *root = device->dev_root; |
| 1328 | struct btrfs_dev_extent *extent; |
| 1329 | struct extent_buffer *leaf; |
| 1330 | struct btrfs_key key; |
| 1331 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1332 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1333 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1334 | path = btrfs_alloc_path(); |
| 1335 | if (!path) |
| 1336 | return -ENOMEM; |
| 1337 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1338 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1339 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1340 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1341 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1342 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1343 | if (ret) |
| 1344 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1345 | |
| 1346 | leaf = path->nodes[0]; |
| 1347 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1348 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1349 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1350 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1351 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1352 | |
| 1353 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1354 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1355 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1356 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1357 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1358 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1359 | btrfs_free_path(path); |
| 1360 | return ret; |
| 1361 | } |
| 1362 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1363 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1364 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1365 | struct extent_map_tree *em_tree; |
| 1366 | struct extent_map *em; |
| 1367 | struct rb_node *n; |
| 1368 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1369 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1370 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1371 | read_lock(&em_tree->lock); |
| 1372 | n = rb_last(&em_tree->map); |
| 1373 | if (n) { |
| 1374 | em = rb_entry(n, struct extent_map, rb_node); |
| 1375 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1376 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1377 | read_unlock(&em_tree->lock); |
| 1378 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1379 | return ret; |
| 1380 | } |
| 1381 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1382 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1383 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1384 | { |
| 1385 | int ret; |
| 1386 | struct btrfs_key key; |
| 1387 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1388 | struct btrfs_path *path; |
| 1389 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1390 | path = btrfs_alloc_path(); |
| 1391 | if (!path) |
| 1392 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1393 | |
| 1394 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1395 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1396 | key.offset = (u64)-1; |
| 1397 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1398 | 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] | 1399 | if (ret < 0) |
| 1400 | goto error; |
| 1401 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1402 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1403 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1404 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1405 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1406 | BTRFS_DEV_ITEM_KEY); |
| 1407 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1408 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1409 | } else { |
| 1410 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1411 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1412 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | } |
| 1414 | ret = 0; |
| 1415 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1416 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1417 | return ret; |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * the device information is stored in the chunk root |
| 1422 | * the btrfs_device struct should be fully filled in |
| 1423 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1424 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1425 | struct btrfs_root *root, |
| 1426 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1427 | { |
| 1428 | int ret; |
| 1429 | struct btrfs_path *path; |
| 1430 | struct btrfs_dev_item *dev_item; |
| 1431 | struct extent_buffer *leaf; |
| 1432 | struct btrfs_key key; |
| 1433 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1434 | |
| 1435 | root = root->fs_info->chunk_root; |
| 1436 | |
| 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 = BTRFS_DEV_ITEMS_OBJECTID; |
| 1442 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1443 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1444 | |
| 1445 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1446 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1447 | if (ret) |
| 1448 | goto out; |
| 1449 | |
| 1450 | leaf = path->nodes[0]; |
| 1451 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1452 | |
| 1453 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1454 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1455 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1456 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1457 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1458 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1459 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1460 | btrfs_device_get_disk_total_bytes(device)); |
| 1461 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1462 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1463 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1464 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1465 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1466 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1467 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1468 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1469 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1470 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1471 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1472 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1473 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1474 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1475 | out: |
| 1476 | btrfs_free_path(path); |
| 1477 | return ret; |
| 1478 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1479 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1480 | /* |
| 1481 | * Function to update ctime/mtime for a given device path. |
| 1482 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1483 | */ |
| 1484 | static void update_dev_time(char *path_name) |
| 1485 | { |
| 1486 | struct file *filp; |
| 1487 | |
| 1488 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1489 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1490 | return; |
| 1491 | file_update_time(filp); |
| 1492 | filp_close(filp, NULL); |
| 1493 | return; |
| 1494 | } |
| 1495 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1496 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1497 | struct btrfs_device *device) |
| 1498 | { |
| 1499 | int ret; |
| 1500 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1501 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1502 | struct btrfs_trans_handle *trans; |
| 1503 | |
| 1504 | root = root->fs_info->chunk_root; |
| 1505 | |
| 1506 | path = btrfs_alloc_path(); |
| 1507 | if (!path) |
| 1508 | return -ENOMEM; |
| 1509 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1510 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1511 | if (IS_ERR(trans)) { |
| 1512 | btrfs_free_path(path); |
| 1513 | return PTR_ERR(trans); |
| 1514 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1515 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1516 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1517 | key.offset = device->devid; |
| 1518 | |
| 1519 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1520 | if (ret < 0) |
| 1521 | goto out; |
| 1522 | |
| 1523 | if (ret > 0) { |
| 1524 | ret = -ENOENT; |
| 1525 | goto out; |
| 1526 | } |
| 1527 | |
| 1528 | ret = btrfs_del_item(trans, root, path); |
| 1529 | if (ret) |
| 1530 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1531 | out: |
| 1532 | btrfs_free_path(path); |
| 1533 | btrfs_commit_transaction(trans, root); |
| 1534 | return ret; |
| 1535 | } |
| 1536 | |
| 1537 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1538 | { |
| 1539 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1540 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1541 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1542 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1543 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1544 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1545 | u64 all_avail; |
| 1546 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1547 | u64 num_devices; |
| 1548 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1549 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1550 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1551 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1552 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1553 | mutex_lock(&uuid_mutex); |
| 1554 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1555 | do { |
| 1556 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1557 | |
| 1558 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1559 | root->fs_info->avail_system_alloc_bits | |
| 1560 | root->fs_info->avail_metadata_alloc_bits; |
| 1561 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1562 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1563 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1564 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1565 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1566 | WARN_ON(num_devices < 1); |
| 1567 | num_devices--; |
| 1568 | } |
| 1569 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1570 | |
| 1571 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1572 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1573 | goto out; |
| 1574 | } |
| 1575 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1576 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1577 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1578 | goto out; |
| 1579 | } |
| 1580 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1581 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1582 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1583 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1584 | goto out; |
| 1585 | } |
| 1586 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1587 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1588 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1589 | goto out; |
| 1590 | } |
| 1591 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1592 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1593 | struct list_head *devices; |
| 1594 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1595 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1596 | device = NULL; |
| 1597 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1598 | /* |
| 1599 | * It is safe to read the devices since the volume_mutex |
| 1600 | * is held. |
| 1601 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1602 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1603 | if (tmp->in_fs_metadata && |
| 1604 | !tmp->is_tgtdev_for_dev_replace && |
| 1605 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1606 | device = tmp; |
| 1607 | break; |
| 1608 | } |
| 1609 | } |
| 1610 | bdev = NULL; |
| 1611 | bh = NULL; |
| 1612 | disk_super = NULL; |
| 1613 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1614 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1615 | goto out; |
| 1616 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1617 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1618 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1619 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1620 | root->fs_info->bdev_holder, 0, |
| 1621 | &bdev, &bh); |
| 1622 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1623 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1624 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1625 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1626 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1627 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1628 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1629 | if (!device) { |
| 1630 | ret = -ENOENT; |
| 1631 | goto error_brelse; |
| 1632 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1633 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1634 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1635 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1636 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1637 | goto error_brelse; |
| 1638 | } |
| 1639 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1640 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1641 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1642 | goto error_brelse; |
| 1643 | } |
| 1644 | |
| 1645 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1646 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1647 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1648 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1649 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1650 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1651 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1652 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1653 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1654 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1655 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1656 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1657 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1658 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1659 | /* |
| 1660 | * TODO: the superblock still includes this device in its num_devices |
| 1661 | * counter although write_all_supers() is not locked out. This |
| 1662 | * could give a filesystem state which requires a degraded mount. |
| 1663 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1664 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1665 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1666 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1667 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1668 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1669 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1670 | |
| 1671 | /* |
| 1672 | * the device list mutex makes sure that we don't change |
| 1673 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1674 | * the device supers. Whoever is writing all supers, should |
| 1675 | * lock the device list mutex before getting the number of |
| 1676 | * devices in the super block (super_copy). Conversely, |
| 1677 | * whoever updates the number of devices in the super block |
| 1678 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1679 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1680 | |
| 1681 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1682 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1683 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1684 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1685 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1686 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1687 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1688 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1689 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1690 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1691 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1692 | struct btrfs_device, dev_list); |
| 1693 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1694 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1695 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1696 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1697 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1698 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1699 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1700 | /* remove sysfs entry */ |
| 1701 | btrfs_kobj_rm_device(root->fs_info, device); |
| 1702 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1703 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1704 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1705 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1706 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1707 | 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] | 1708 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1709 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1710 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1711 | struct btrfs_fs_devices *fs_devices; |
| 1712 | fs_devices = root->fs_info->fs_devices; |
| 1713 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1714 | if (fs_devices->seed == cur_devices) { |
| 1715 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1716 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1717 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1718 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1719 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1720 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1721 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1722 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1723 | } |
| 1724 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1725 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1726 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1727 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1728 | /* |
| 1729 | * at this point, the device is zero sized. We want to |
| 1730 | * remove it from the devices list and zero out the old super |
| 1731 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1732 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1733 | u64 bytenr; |
| 1734 | int i; |
| 1735 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1736 | /* make sure this device isn't detected as part of |
| 1737 | * the FS anymore |
| 1738 | */ |
| 1739 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1740 | set_buffer_dirty(bh); |
| 1741 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1742 | |
| 1743 | /* clear the mirror copies of super block on the disk |
| 1744 | * being removed, 0th copy is been taken care above and |
| 1745 | * the below would take of the rest |
| 1746 | */ |
| 1747 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1748 | bytenr = btrfs_sb_offset(i); |
| 1749 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1750 | i_size_read(bdev->bd_inode)) |
| 1751 | break; |
| 1752 | |
| 1753 | brelse(bh); |
| 1754 | bh = __bread(bdev, bytenr / 4096, |
| 1755 | BTRFS_SUPER_INFO_SIZE); |
| 1756 | if (!bh) |
| 1757 | continue; |
| 1758 | |
| 1759 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1760 | |
| 1761 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1762 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1763 | continue; |
| 1764 | } |
| 1765 | memset(&disk_super->magic, 0, |
| 1766 | sizeof(disk_super->magic)); |
| 1767 | set_buffer_dirty(bh); |
| 1768 | sync_dirty_buffer(bh); |
| 1769 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1770 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1771 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1772 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1773 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1774 | if (bdev) { |
| 1775 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1776 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1777 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1778 | /* Update ctime/mtime for device path for libblkid */ |
| 1779 | update_dev_time(device_path); |
| 1780 | } |
| 1781 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1782 | error_brelse: |
| 1783 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1784 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1785 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1786 | out: |
| 1787 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1788 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1789 | error_undo: |
| 1790 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1791 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1792 | list_add(&device->dev_alloc_list, |
| 1793 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1794 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1795 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1796 | } |
| 1797 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1798 | } |
| 1799 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1800 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1801 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1802 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1803 | struct btrfs_fs_devices *fs_devices; |
| 1804 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1805 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1806 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1807 | /* |
| 1808 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1809 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1810 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1811 | * srcdev will have its correct fs_devices in both the cases. |
| 1812 | */ |
| 1813 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1814 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1815 | list_del_rcu(&srcdev->dev_list); |
| 1816 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1817 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1818 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1819 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1820 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1821 | if (srcdev->writeable) { |
| 1822 | fs_devices->rw_devices--; |
| 1823 | /* zero out the old super if it is writable */ |
| 1824 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1825 | } |
| 1826 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1827 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1828 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 1832 | struct btrfs_device *srcdev) |
| 1833 | { |
| 1834 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1835 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1836 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1837 | |
| 1838 | /* |
| 1839 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 1840 | * zero |
| 1841 | */ |
| 1842 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 1843 | |
| 1844 | /* if this is no devs we rather delete the fs_devices */ |
| 1845 | if (!fs_devices->num_devices) { |
| 1846 | struct btrfs_fs_devices *tmp_fs_devices; |
| 1847 | |
| 1848 | tmp_fs_devices = fs_info->fs_devices; |
| 1849 | while (tmp_fs_devices) { |
| 1850 | if (tmp_fs_devices->seed == fs_devices) { |
| 1851 | tmp_fs_devices->seed = fs_devices->seed; |
| 1852 | break; |
| 1853 | } |
| 1854 | tmp_fs_devices = tmp_fs_devices->seed; |
| 1855 | } |
| 1856 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 1857 | __btrfs_close_devices(fs_devices); |
| 1858 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1859 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1863 | struct btrfs_device *tgtdev) |
| 1864 | { |
| 1865 | struct btrfs_device *next_device; |
| 1866 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1867 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1868 | WARN_ON(!tgtdev); |
| 1869 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1870 | if (tgtdev->bdev) { |
| 1871 | btrfs_scratch_superblock(tgtdev); |
| 1872 | fs_info->fs_devices->open_devices--; |
| 1873 | } |
| 1874 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1875 | |
| 1876 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1877 | struct btrfs_device, dev_list); |
| 1878 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1879 | fs_info->sb->s_bdev = next_device->bdev; |
| 1880 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1881 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1882 | list_del_rcu(&tgtdev->dev_list); |
| 1883 | |
| 1884 | call_rcu(&tgtdev->rcu, free_device); |
| 1885 | |
| 1886 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1887 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1888 | } |
| 1889 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1890 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1891 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1892 | { |
| 1893 | int ret = 0; |
| 1894 | struct btrfs_super_block *disk_super; |
| 1895 | u64 devid; |
| 1896 | u8 *dev_uuid; |
| 1897 | struct block_device *bdev; |
| 1898 | struct buffer_head *bh; |
| 1899 | |
| 1900 | *device = NULL; |
| 1901 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1902 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1903 | if (ret) |
| 1904 | return ret; |
| 1905 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1906 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1907 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1908 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1909 | disk_super->fsid); |
| 1910 | brelse(bh); |
| 1911 | if (!*device) |
| 1912 | ret = -ENOENT; |
| 1913 | blkdev_put(bdev, FMODE_READ); |
| 1914 | return ret; |
| 1915 | } |
| 1916 | |
| 1917 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1918 | char *device_path, |
| 1919 | struct btrfs_device **device) |
| 1920 | { |
| 1921 | *device = NULL; |
| 1922 | if (strcmp(device_path, "missing") == 0) { |
| 1923 | struct list_head *devices; |
| 1924 | struct btrfs_device *tmp; |
| 1925 | |
| 1926 | devices = &root->fs_info->fs_devices->devices; |
| 1927 | /* |
| 1928 | * It is safe to read the devices since the volume_mutex |
| 1929 | * is held by the caller. |
| 1930 | */ |
| 1931 | list_for_each_entry(tmp, devices, dev_list) { |
| 1932 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1933 | *device = tmp; |
| 1934 | break; |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1939 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1940 | return -ENOENT; |
| 1941 | } |
| 1942 | |
| 1943 | return 0; |
| 1944 | } else { |
| 1945 | return btrfs_find_device_by_path(root, device_path, device); |
| 1946 | } |
| 1947 | } |
| 1948 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1949 | /* |
| 1950 | * does all the dirty work required for changing file system's UUID. |
| 1951 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1952 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1953 | { |
| 1954 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1955 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1956 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1957 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1958 | struct btrfs_device *device; |
| 1959 | u64 super_flags; |
| 1960 | |
| 1961 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1962 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1963 | return -EINVAL; |
| 1964 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1965 | seed_devices = __alloc_fs_devices(); |
| 1966 | if (IS_ERR(seed_devices)) |
| 1967 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1968 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1969 | old_devices = clone_fs_devices(fs_devices); |
| 1970 | if (IS_ERR(old_devices)) { |
| 1971 | kfree(seed_devices); |
| 1972 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1973 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1974 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1975 | list_add(&old_devices->list, &fs_uuids); |
| 1976 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1977 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1978 | seed_devices->opened = 1; |
| 1979 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1980 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1981 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1982 | |
| 1983 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1984 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1985 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1986 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1987 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1988 | |
| 1989 | lock_chunks(root); |
| 1990 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1991 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1992 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1993 | fs_devices->seeding = 0; |
| 1994 | fs_devices->num_devices = 0; |
| 1995 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 1996 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 1997 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1998 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1999 | |
| 2000 | generate_random_uuid(fs_devices->fsid); |
| 2001 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2002 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2003 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2004 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2005 | super_flags = btrfs_super_flags(disk_super) & |
| 2006 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2007 | btrfs_set_super_flags(disk_super, super_flags); |
| 2008 | |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * strore the expected generation for seed devices in device items. |
| 2014 | */ |
| 2015 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2016 | struct btrfs_root *root) |
| 2017 | { |
| 2018 | struct btrfs_path *path; |
| 2019 | struct extent_buffer *leaf; |
| 2020 | struct btrfs_dev_item *dev_item; |
| 2021 | struct btrfs_device *device; |
| 2022 | struct btrfs_key key; |
| 2023 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2024 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2025 | u64 devid; |
| 2026 | int ret; |
| 2027 | |
| 2028 | path = btrfs_alloc_path(); |
| 2029 | if (!path) |
| 2030 | return -ENOMEM; |
| 2031 | |
| 2032 | root = root->fs_info->chunk_root; |
| 2033 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2034 | key.offset = 0; |
| 2035 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2036 | |
| 2037 | while (1) { |
| 2038 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2039 | if (ret < 0) |
| 2040 | goto error; |
| 2041 | |
| 2042 | leaf = path->nodes[0]; |
| 2043 | next_slot: |
| 2044 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2045 | ret = btrfs_next_leaf(root, path); |
| 2046 | if (ret > 0) |
| 2047 | break; |
| 2048 | if (ret < 0) |
| 2049 | goto error; |
| 2050 | leaf = path->nodes[0]; |
| 2051 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2052 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2053 | continue; |
| 2054 | } |
| 2055 | |
| 2056 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2057 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2058 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2059 | break; |
| 2060 | |
| 2061 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2062 | struct btrfs_dev_item); |
| 2063 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2064 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2065 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2066 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2067 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2068 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2069 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2070 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2071 | |
| 2072 | if (device->fs_devices->seeding) { |
| 2073 | btrfs_set_device_generation(leaf, dev_item, |
| 2074 | device->generation); |
| 2075 | btrfs_mark_buffer_dirty(leaf); |
| 2076 | } |
| 2077 | |
| 2078 | path->slots[0]++; |
| 2079 | goto next_slot; |
| 2080 | } |
| 2081 | ret = 0; |
| 2082 | error: |
| 2083 | btrfs_free_path(path); |
| 2084 | return ret; |
| 2085 | } |
| 2086 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2087 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2088 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2089 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2090 | struct btrfs_trans_handle *trans; |
| 2091 | struct btrfs_device *device; |
| 2092 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2093 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2094 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2095 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2096 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2097 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2098 | int ret = 0; |
| 2099 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2100 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2101 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2102 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2103 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2104 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2105 | if (IS_ERR(bdev)) |
| 2106 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2107 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2108 | if (root->fs_info->fs_devices->seeding) { |
| 2109 | seeding_dev = 1; |
| 2110 | down_write(&sb->s_umount); |
| 2111 | mutex_lock(&uuid_mutex); |
| 2112 | } |
| 2113 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2114 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2115 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2116 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2117 | |
| 2118 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2119 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2120 | if (device->bdev == bdev) { |
| 2121 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2122 | mutex_unlock( |
| 2123 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2124 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2125 | } |
| 2126 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2127 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2128 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2129 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2130 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2131 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2132 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2133 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2134 | } |
| 2135 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2136 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2137 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2138 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2139 | ret = -ENOMEM; |
| 2140 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2141 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2142 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2143 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2144 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2145 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2146 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2147 | kfree(device); |
| 2148 | ret = PTR_ERR(trans); |
| 2149 | goto error; |
| 2150 | } |
| 2151 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2152 | q = bdev_get_queue(bdev); |
| 2153 | if (blk_queue_discard(q)) |
| 2154 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2155 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2156 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2157 | device->io_width = root->sectorsize; |
| 2158 | device->io_align = root->sectorsize; |
| 2159 | device->sector_size = root->sectorsize; |
| 2160 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2161 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2162 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2163 | device->dev_root = root->fs_info->dev_root; |
| 2164 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2165 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2166 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2167 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2168 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2169 | set_blocksize(device->bdev, 4096); |
| 2170 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2171 | if (seeding_dev) { |
| 2172 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2173 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2174 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2178 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2179 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2180 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2181 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2182 | list_add(&device->dev_alloc_list, |
| 2183 | &root->fs_info->fs_devices->alloc_list); |
| 2184 | root->fs_info->fs_devices->num_devices++; |
| 2185 | root->fs_info->fs_devices->open_devices++; |
| 2186 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2187 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2188 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2189 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2190 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2191 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2192 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2193 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2194 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2195 | root->fs_info->fs_devices->rotating = 1; |
| 2196 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2197 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2198 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2199 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2200 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2201 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2202 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2203 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2204 | |
| 2205 | /* add sysfs device entry */ |
| 2206 | btrfs_kobj_add_device(root->fs_info, device); |
| 2207 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2208 | /* |
| 2209 | * we've got more storage, clear any full flags on the space |
| 2210 | * infos |
| 2211 | */ |
| 2212 | btrfs_clear_space_info_full(root->fs_info); |
| 2213 | |
| 2214 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2215 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2216 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2217 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2218 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2219 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2220 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2221 | if (ret) { |
| 2222 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2223 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2224 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | ret = btrfs_add_device(trans, root, device); |
| 2228 | if (ret) { |
| 2229 | btrfs_abort_transaction(trans, root, ret); |
| 2230 | goto error_trans; |
| 2231 | } |
| 2232 | |
| 2233 | if (seeding_dev) { |
| 2234 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2235 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2236 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2237 | if (ret) { |
| 2238 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2239 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2240 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2241 | |
| 2242 | /* Sprouting would change fsid of the mounted root, |
| 2243 | * so rename the fsid on the sysfs |
| 2244 | */ |
| 2245 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2246 | root->fs_info->fsid); |
| 2247 | if (kobject_rename(&root->fs_info->super_kobj, fsid_buf)) |
| 2248 | goto error_trans; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2249 | } |
| 2250 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2251 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2252 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2253 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2254 | |
| 2255 | if (seeding_dev) { |
| 2256 | mutex_unlock(&uuid_mutex); |
| 2257 | up_write(&sb->s_umount); |
| 2258 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2259 | if (ret) /* transaction commit */ |
| 2260 | return ret; |
| 2261 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2262 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2263 | if (ret < 0) |
| 2264 | btrfs_error(root->fs_info, ret, |
| 2265 | "Failed to relocate sys chunks after " |
| 2266 | "device initialization. This can be fixed " |
| 2267 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2268 | trans = btrfs_attach_transaction(root); |
| 2269 | if (IS_ERR(trans)) { |
| 2270 | if (PTR_ERR(trans) == -ENOENT) |
| 2271 | return 0; |
| 2272 | return PTR_ERR(trans); |
| 2273 | } |
| 2274 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2275 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2276 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2277 | /* Update ctime/mtime for libblkid */ |
| 2278 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2279 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2280 | |
| 2281 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2282 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2283 | rcu_string_free(device->name); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2284 | btrfs_kobj_rm_device(root->fs_info, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2285 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2286 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2287 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2288 | if (seeding_dev) { |
| 2289 | mutex_unlock(&uuid_mutex); |
| 2290 | up_write(&sb->s_umount); |
| 2291 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2292 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2293 | } |
| 2294 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2295 | 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] | 2296 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2297 | struct btrfs_device **device_out) |
| 2298 | { |
| 2299 | struct request_queue *q; |
| 2300 | struct btrfs_device *device; |
| 2301 | struct block_device *bdev; |
| 2302 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2303 | struct list_head *devices; |
| 2304 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2305 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2306 | int ret = 0; |
| 2307 | |
| 2308 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2309 | if (fs_info->fs_devices->seeding) { |
| 2310 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2311 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2312 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2313 | |
| 2314 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2315 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2316 | if (IS_ERR(bdev)) { |
| 2317 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2318 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2319 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2320 | |
| 2321 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2322 | |
| 2323 | devices = &fs_info->fs_devices->devices; |
| 2324 | list_for_each_entry(device, devices, dev_list) { |
| 2325 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2326 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2327 | ret = -EEXIST; |
| 2328 | goto error; |
| 2329 | } |
| 2330 | } |
| 2331 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2332 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2333 | if (i_size_read(bdev->bd_inode) < |
| 2334 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2335 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2336 | ret = -EINVAL; |
| 2337 | goto error; |
| 2338 | } |
| 2339 | |
| 2340 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2341 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2342 | if (IS_ERR(device)) { |
| 2343 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2344 | goto error; |
| 2345 | } |
| 2346 | |
| 2347 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2348 | if (!name) { |
| 2349 | kfree(device); |
| 2350 | ret = -ENOMEM; |
| 2351 | goto error; |
| 2352 | } |
| 2353 | rcu_assign_pointer(device->name, name); |
| 2354 | |
| 2355 | q = bdev_get_queue(bdev); |
| 2356 | if (blk_queue_discard(q)) |
| 2357 | device->can_discard = 1; |
| 2358 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2359 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2360 | device->generation = 0; |
| 2361 | device->io_width = root->sectorsize; |
| 2362 | device->io_align = root->sectorsize; |
| 2363 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2364 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2365 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2366 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2367 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2368 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2369 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2370 | device->dev_root = fs_info->dev_root; |
| 2371 | device->bdev = bdev; |
| 2372 | device->in_fs_metadata = 1; |
| 2373 | device->is_tgtdev_for_dev_replace = 1; |
| 2374 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2375 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2376 | set_blocksize(device->bdev, 4096); |
| 2377 | device->fs_devices = fs_info->fs_devices; |
| 2378 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2379 | fs_info->fs_devices->num_devices++; |
| 2380 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2381 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2382 | |
| 2383 | *device_out = device; |
| 2384 | return ret; |
| 2385 | |
| 2386 | error: |
| 2387 | blkdev_put(bdev, FMODE_EXCL); |
| 2388 | return ret; |
| 2389 | } |
| 2390 | |
| 2391 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2392 | struct btrfs_device *tgtdev) |
| 2393 | { |
| 2394 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2395 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2396 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2397 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2398 | tgtdev->dev_root = fs_info->dev_root; |
| 2399 | tgtdev->in_fs_metadata = 1; |
| 2400 | } |
| 2401 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2402 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2403 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2404 | { |
| 2405 | int ret; |
| 2406 | struct btrfs_path *path; |
| 2407 | struct btrfs_root *root; |
| 2408 | struct btrfs_dev_item *dev_item; |
| 2409 | struct extent_buffer *leaf; |
| 2410 | struct btrfs_key key; |
| 2411 | |
| 2412 | root = device->dev_root->fs_info->chunk_root; |
| 2413 | |
| 2414 | path = btrfs_alloc_path(); |
| 2415 | if (!path) |
| 2416 | return -ENOMEM; |
| 2417 | |
| 2418 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2419 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2420 | key.offset = device->devid; |
| 2421 | |
| 2422 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2423 | if (ret < 0) |
| 2424 | goto out; |
| 2425 | |
| 2426 | if (ret > 0) { |
| 2427 | ret = -ENOENT; |
| 2428 | goto out; |
| 2429 | } |
| 2430 | |
| 2431 | leaf = path->nodes[0]; |
| 2432 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2433 | |
| 2434 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2435 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2436 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2437 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2438 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2439 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2440 | btrfs_device_get_disk_total_bytes(device)); |
| 2441 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2442 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2443 | btrfs_mark_buffer_dirty(leaf); |
| 2444 | |
| 2445 | out: |
| 2446 | btrfs_free_path(path); |
| 2447 | return ret; |
| 2448 | } |
| 2449 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2450 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2451 | struct btrfs_device *device, u64 new_size) |
| 2452 | { |
| 2453 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2454 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2455 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2456 | u64 old_total; |
| 2457 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2458 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2459 | if (!device->writeable) |
| 2460 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2461 | |
| 2462 | lock_chunks(device->dev_root); |
| 2463 | old_total = btrfs_super_total_bytes(super_copy); |
| 2464 | diff = new_size - device->total_bytes; |
| 2465 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2466 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2467 | device->is_tgtdev_for_dev_replace) { |
| 2468 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2469 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2470 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2471 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2472 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2473 | |
| 2474 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2475 | device->fs_devices->total_rw_bytes += diff; |
| 2476 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2477 | btrfs_device_set_total_bytes(device, new_size); |
| 2478 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2479 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2480 | if (list_empty(&device->resized_list)) |
| 2481 | list_add_tail(&device->resized_list, |
| 2482 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2483 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2484 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2485 | return btrfs_update_device(trans, device); |
| 2486 | } |
| 2487 | |
| 2488 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2489 | struct btrfs_root *root, |
| 2490 | u64 chunk_tree, u64 chunk_objectid, |
| 2491 | u64 chunk_offset) |
| 2492 | { |
| 2493 | int ret; |
| 2494 | struct btrfs_path *path; |
| 2495 | struct btrfs_key key; |
| 2496 | |
| 2497 | root = root->fs_info->chunk_root; |
| 2498 | path = btrfs_alloc_path(); |
| 2499 | if (!path) |
| 2500 | return -ENOMEM; |
| 2501 | |
| 2502 | key.objectid = chunk_objectid; |
| 2503 | key.offset = chunk_offset; |
| 2504 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2505 | |
| 2506 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2507 | if (ret < 0) |
| 2508 | goto out; |
| 2509 | else if (ret > 0) { /* Logic error or corruption */ |
| 2510 | btrfs_error(root->fs_info, -ENOENT, |
| 2511 | "Failed lookup while freeing chunk."); |
| 2512 | ret = -ENOENT; |
| 2513 | goto out; |
| 2514 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2515 | |
| 2516 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2517 | if (ret < 0) |
| 2518 | btrfs_error(root->fs_info, ret, |
| 2519 | "Failed to delete chunk item."); |
| 2520 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2521 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2522 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2523 | } |
| 2524 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2525 | 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] | 2526 | chunk_offset) |
| 2527 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2528 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2529 | struct btrfs_disk_key *disk_key; |
| 2530 | struct btrfs_chunk *chunk; |
| 2531 | u8 *ptr; |
| 2532 | int ret = 0; |
| 2533 | u32 num_stripes; |
| 2534 | u32 array_size; |
| 2535 | u32 len = 0; |
| 2536 | u32 cur; |
| 2537 | struct btrfs_key key; |
| 2538 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2539 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2540 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2541 | |
| 2542 | ptr = super_copy->sys_chunk_array; |
| 2543 | cur = 0; |
| 2544 | |
| 2545 | while (cur < array_size) { |
| 2546 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2547 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2548 | |
| 2549 | len = sizeof(*disk_key); |
| 2550 | |
| 2551 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2552 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2553 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2554 | len += btrfs_chunk_item_size(num_stripes); |
| 2555 | } else { |
| 2556 | ret = -EIO; |
| 2557 | break; |
| 2558 | } |
| 2559 | if (key.objectid == chunk_objectid && |
| 2560 | key.offset == chunk_offset) { |
| 2561 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2562 | array_size -= len; |
| 2563 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2564 | } else { |
| 2565 | ptr += len; |
| 2566 | cur += len; |
| 2567 | } |
| 2568 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2569 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2570 | return ret; |
| 2571 | } |
| 2572 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2573 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2574 | struct btrfs_root *root, u64 chunk_offset) |
| 2575 | { |
| 2576 | struct extent_map_tree *em_tree; |
| 2577 | struct extent_map *em; |
| 2578 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2579 | struct map_lookup *map; |
| 2580 | u64 dev_extent_len = 0; |
| 2581 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2582 | u64 chunk_tree = root->fs_info->chunk_root->objectid; |
| 2583 | int i, ret = 0; |
| 2584 | |
| 2585 | /* Just in case */ |
| 2586 | root = root->fs_info->chunk_root; |
| 2587 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2588 | |
| 2589 | read_lock(&em_tree->lock); |
| 2590 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2591 | read_unlock(&em_tree->lock); |
| 2592 | |
| 2593 | if (!em || em->start > chunk_offset || |
| 2594 | em->start + em->len < chunk_offset) { |
| 2595 | /* |
| 2596 | * This is a logic error, but we don't want to just rely on the |
| 2597 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2598 | * do anything we still error out. |
| 2599 | */ |
| 2600 | ASSERT(0); |
| 2601 | if (em) |
| 2602 | free_extent_map(em); |
| 2603 | return -EINVAL; |
| 2604 | } |
| 2605 | map = (struct map_lookup *)em->bdev; |
| 2606 | |
| 2607 | for (i = 0; i < map->num_stripes; i++) { |
| 2608 | struct btrfs_device *device = map->stripes[i].dev; |
| 2609 | ret = btrfs_free_dev_extent(trans, device, |
| 2610 | map->stripes[i].physical, |
| 2611 | &dev_extent_len); |
| 2612 | if (ret) { |
| 2613 | btrfs_abort_transaction(trans, root, ret); |
| 2614 | goto out; |
| 2615 | } |
| 2616 | |
| 2617 | if (device->bytes_used > 0) { |
| 2618 | lock_chunks(root); |
| 2619 | btrfs_device_set_bytes_used(device, |
| 2620 | device->bytes_used - dev_extent_len); |
| 2621 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2622 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2623 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2624 | btrfs_clear_space_info_full(root->fs_info); |
| 2625 | unlock_chunks(root); |
| 2626 | } |
| 2627 | |
| 2628 | if (map->stripes[i].dev) { |
| 2629 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2630 | if (ret) { |
| 2631 | btrfs_abort_transaction(trans, root, ret); |
| 2632 | goto out; |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2637 | chunk_offset); |
| 2638 | if (ret) { |
| 2639 | btrfs_abort_transaction(trans, root, ret); |
| 2640 | goto out; |
| 2641 | } |
| 2642 | |
| 2643 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2644 | |
| 2645 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2646 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2647 | if (ret) { |
| 2648 | btrfs_abort_transaction(trans, root, ret); |
| 2649 | goto out; |
| 2650 | } |
| 2651 | } |
| 2652 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2653 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2654 | if (ret) { |
| 2655 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2656 | goto out; |
| 2657 | } |
| 2658 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2659 | out: |
| 2660 | /* once for us */ |
| 2661 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2662 | return ret; |
| 2663 | } |
| 2664 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2665 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2666 | u64 chunk_tree, u64 chunk_objectid, |
| 2667 | u64 chunk_offset) |
| 2668 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2669 | struct btrfs_root *extent_root; |
| 2670 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2671 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2672 | |
| 2673 | root = root->fs_info->chunk_root; |
| 2674 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2675 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2676 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2677 | if (ret) |
| 2678 | return -ENOSPC; |
| 2679 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2680 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2681 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2682 | if (ret) |
| 2683 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2684 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2685 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2686 | if (IS_ERR(trans)) { |
| 2687 | ret = PTR_ERR(trans); |
| 2688 | btrfs_std_error(root->fs_info, ret); |
| 2689 | return ret; |
| 2690 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2691 | |
| 2692 | /* |
| 2693 | * step two, delete the device extents and the |
| 2694 | * chunk tree entries |
| 2695 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2696 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2697 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2698 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2699 | } |
| 2700 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2701 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2702 | { |
| 2703 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2704 | struct btrfs_path *path; |
| 2705 | struct extent_buffer *leaf; |
| 2706 | struct btrfs_chunk *chunk; |
| 2707 | struct btrfs_key key; |
| 2708 | struct btrfs_key found_key; |
| 2709 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2710 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2711 | bool retried = false; |
| 2712 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2713 | int ret; |
| 2714 | |
| 2715 | path = btrfs_alloc_path(); |
| 2716 | if (!path) |
| 2717 | return -ENOMEM; |
| 2718 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2719 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2720 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2721 | key.offset = (u64)-1; |
| 2722 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2723 | |
| 2724 | while (1) { |
| 2725 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2726 | if (ret < 0) |
| 2727 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2728 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2729 | |
| 2730 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2731 | key.type); |
| 2732 | if (ret < 0) |
| 2733 | goto error; |
| 2734 | if (ret > 0) |
| 2735 | break; |
| 2736 | |
| 2737 | leaf = path->nodes[0]; |
| 2738 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2739 | |
| 2740 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2741 | struct btrfs_chunk); |
| 2742 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2743 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2744 | |
| 2745 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2746 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2747 | found_key.objectid, |
| 2748 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2749 | if (ret == -ENOSPC) |
| 2750 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2751 | else |
| 2752 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | if (found_key.offset == 0) |
| 2756 | break; |
| 2757 | key.offset = found_key.offset - 1; |
| 2758 | } |
| 2759 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2760 | if (failed && !retried) { |
| 2761 | failed = 0; |
| 2762 | retried = true; |
| 2763 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2764 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2765 | ret = -ENOSPC; |
| 2766 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2767 | error: |
| 2768 | btrfs_free_path(path); |
| 2769 | return ret; |
| 2770 | } |
| 2771 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2772 | static int insert_balance_item(struct btrfs_root *root, |
| 2773 | struct btrfs_balance_control *bctl) |
| 2774 | { |
| 2775 | struct btrfs_trans_handle *trans; |
| 2776 | struct btrfs_balance_item *item; |
| 2777 | struct btrfs_disk_balance_args disk_bargs; |
| 2778 | struct btrfs_path *path; |
| 2779 | struct extent_buffer *leaf; |
| 2780 | struct btrfs_key key; |
| 2781 | int ret, err; |
| 2782 | |
| 2783 | path = btrfs_alloc_path(); |
| 2784 | if (!path) |
| 2785 | return -ENOMEM; |
| 2786 | |
| 2787 | trans = btrfs_start_transaction(root, 0); |
| 2788 | if (IS_ERR(trans)) { |
| 2789 | btrfs_free_path(path); |
| 2790 | return PTR_ERR(trans); |
| 2791 | } |
| 2792 | |
| 2793 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2794 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2795 | key.offset = 0; |
| 2796 | |
| 2797 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2798 | sizeof(*item)); |
| 2799 | if (ret) |
| 2800 | goto out; |
| 2801 | |
| 2802 | leaf = path->nodes[0]; |
| 2803 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2804 | |
| 2805 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2806 | |
| 2807 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2808 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2809 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2810 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2811 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2812 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2813 | |
| 2814 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2815 | |
| 2816 | btrfs_mark_buffer_dirty(leaf); |
| 2817 | out: |
| 2818 | btrfs_free_path(path); |
| 2819 | err = btrfs_commit_transaction(trans, root); |
| 2820 | if (err && !ret) |
| 2821 | ret = err; |
| 2822 | return ret; |
| 2823 | } |
| 2824 | |
| 2825 | static int del_balance_item(struct btrfs_root *root) |
| 2826 | { |
| 2827 | struct btrfs_trans_handle *trans; |
| 2828 | struct btrfs_path *path; |
| 2829 | struct btrfs_key key; |
| 2830 | int ret, err; |
| 2831 | |
| 2832 | path = btrfs_alloc_path(); |
| 2833 | if (!path) |
| 2834 | return -ENOMEM; |
| 2835 | |
| 2836 | trans = btrfs_start_transaction(root, 0); |
| 2837 | if (IS_ERR(trans)) { |
| 2838 | btrfs_free_path(path); |
| 2839 | return PTR_ERR(trans); |
| 2840 | } |
| 2841 | |
| 2842 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2843 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2844 | key.offset = 0; |
| 2845 | |
| 2846 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2847 | if (ret < 0) |
| 2848 | goto out; |
| 2849 | if (ret > 0) { |
| 2850 | ret = -ENOENT; |
| 2851 | goto out; |
| 2852 | } |
| 2853 | |
| 2854 | ret = btrfs_del_item(trans, root, path); |
| 2855 | out: |
| 2856 | btrfs_free_path(path); |
| 2857 | err = btrfs_commit_transaction(trans, root); |
| 2858 | if (err && !ret) |
| 2859 | ret = err; |
| 2860 | return ret; |
| 2861 | } |
| 2862 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2863 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2864 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2865 | * resume after balance was interrupted. |
| 2866 | */ |
| 2867 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2868 | { |
| 2869 | /* |
| 2870 | * Turn on soft mode for chunk types that were being converted. |
| 2871 | */ |
| 2872 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2873 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2874 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2875 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2876 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2877 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2878 | |
| 2879 | /* |
| 2880 | * Turn on usage filter if is not already used. The idea is |
| 2881 | * that chunks that we have already balanced should be |
| 2882 | * reasonably full. Don't do it for chunks that are being |
| 2883 | * converted - that will keep us from relocating unconverted |
| 2884 | * (albeit full) chunks. |
| 2885 | */ |
| 2886 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2887 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2888 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2889 | bctl->data.usage = 90; |
| 2890 | } |
| 2891 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2892 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2893 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2894 | bctl->sys.usage = 90; |
| 2895 | } |
| 2896 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2897 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2898 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2899 | bctl->meta.usage = 90; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2904 | * Should be called with both balance and volume mutexes held to |
| 2905 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2906 | * restriper. Same goes for unset_balance_control. |
| 2907 | */ |
| 2908 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2909 | { |
| 2910 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2911 | |
| 2912 | BUG_ON(fs_info->balance_ctl); |
| 2913 | |
| 2914 | spin_lock(&fs_info->balance_lock); |
| 2915 | fs_info->balance_ctl = bctl; |
| 2916 | spin_unlock(&fs_info->balance_lock); |
| 2917 | } |
| 2918 | |
| 2919 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2920 | { |
| 2921 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2922 | |
| 2923 | BUG_ON(!fs_info->balance_ctl); |
| 2924 | |
| 2925 | spin_lock(&fs_info->balance_lock); |
| 2926 | fs_info->balance_ctl = NULL; |
| 2927 | spin_unlock(&fs_info->balance_lock); |
| 2928 | |
| 2929 | kfree(bctl); |
| 2930 | } |
| 2931 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2932 | /* |
| 2933 | * Balance filters. Return 1 if chunk should be filtered out |
| 2934 | * (should not be balanced). |
| 2935 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2936 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2937 | struct btrfs_balance_args *bargs) |
| 2938 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2939 | chunk_type = chunk_to_extended(chunk_type) & |
| 2940 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2941 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2942 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2943 | return 0; |
| 2944 | |
| 2945 | return 1; |
| 2946 | } |
| 2947 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2948 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2949 | struct btrfs_balance_args *bargs) |
| 2950 | { |
| 2951 | struct btrfs_block_group_cache *cache; |
| 2952 | u64 chunk_used, user_thresh; |
| 2953 | int ret = 1; |
| 2954 | |
| 2955 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2956 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2957 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2958 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 2959 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2960 | else if (bargs->usage > 100) |
| 2961 | user_thresh = cache->key.offset; |
| 2962 | else |
| 2963 | user_thresh = div_factor_fine(cache->key.offset, |
| 2964 | bargs->usage); |
| 2965 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2966 | if (chunk_used < user_thresh) |
| 2967 | ret = 0; |
| 2968 | |
| 2969 | btrfs_put_block_group(cache); |
| 2970 | return ret; |
| 2971 | } |
| 2972 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2973 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2974 | struct btrfs_chunk *chunk, |
| 2975 | struct btrfs_balance_args *bargs) |
| 2976 | { |
| 2977 | struct btrfs_stripe *stripe; |
| 2978 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2979 | int i; |
| 2980 | |
| 2981 | for (i = 0; i < num_stripes; i++) { |
| 2982 | stripe = btrfs_stripe_nr(chunk, i); |
| 2983 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
| 2987 | return 1; |
| 2988 | } |
| 2989 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2990 | /* [pstart, pend) */ |
| 2991 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2992 | struct btrfs_chunk *chunk, |
| 2993 | u64 chunk_offset, |
| 2994 | struct btrfs_balance_args *bargs) |
| 2995 | { |
| 2996 | struct btrfs_stripe *stripe; |
| 2997 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2998 | u64 stripe_offset; |
| 2999 | u64 stripe_length; |
| 3000 | int factor; |
| 3001 | int i; |
| 3002 | |
| 3003 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3004 | return 0; |
| 3005 | |
| 3006 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3007 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3008 | factor = num_stripes / 2; |
| 3009 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3010 | factor = num_stripes - 1; |
| 3011 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3012 | factor = num_stripes - 2; |
| 3013 | } else { |
| 3014 | factor = num_stripes; |
| 3015 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3016 | |
| 3017 | for (i = 0; i < num_stripes; i++) { |
| 3018 | stripe = btrfs_stripe_nr(chunk, i); |
| 3019 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3020 | continue; |
| 3021 | |
| 3022 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3023 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 3024 | do_div(stripe_length, factor); |
| 3025 | |
| 3026 | if (stripe_offset < bargs->pend && |
| 3027 | stripe_offset + stripe_length > bargs->pstart) |
| 3028 | return 0; |
| 3029 | } |
| 3030 | |
| 3031 | return 1; |
| 3032 | } |
| 3033 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3034 | /* [vstart, vend) */ |
| 3035 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3036 | struct btrfs_chunk *chunk, |
| 3037 | u64 chunk_offset, |
| 3038 | struct btrfs_balance_args *bargs) |
| 3039 | { |
| 3040 | if (chunk_offset < bargs->vend && |
| 3041 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3042 | /* at least part of the chunk is inside this vrange */ |
| 3043 | return 0; |
| 3044 | |
| 3045 | return 1; |
| 3046 | } |
| 3047 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3048 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3049 | struct btrfs_balance_args *bargs) |
| 3050 | { |
| 3051 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3052 | return 0; |
| 3053 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3054 | chunk_type = chunk_to_extended(chunk_type) & |
| 3055 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3056 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3057 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3058 | return 1; |
| 3059 | |
| 3060 | return 0; |
| 3061 | } |
| 3062 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3063 | static int should_balance_chunk(struct btrfs_root *root, |
| 3064 | struct extent_buffer *leaf, |
| 3065 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3066 | { |
| 3067 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3068 | struct btrfs_balance_args *bargs = NULL; |
| 3069 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3070 | |
| 3071 | /* type filter */ |
| 3072 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3073 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3074 | return 0; |
| 3075 | } |
| 3076 | |
| 3077 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3078 | bargs = &bctl->data; |
| 3079 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3080 | bargs = &bctl->sys; |
| 3081 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3082 | bargs = &bctl->meta; |
| 3083 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3084 | /* profiles filter */ |
| 3085 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3086 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3087 | return 0; |
| 3088 | } |
| 3089 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3090 | /* usage filter */ |
| 3091 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3092 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3093 | return 0; |
| 3094 | } |
| 3095 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3096 | /* devid filter */ |
| 3097 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3098 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3099 | return 0; |
| 3100 | } |
| 3101 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3102 | /* drange filter, makes sense only with devid filter */ |
| 3103 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3104 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3105 | return 0; |
| 3106 | } |
| 3107 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3108 | /* vrange filter */ |
| 3109 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3110 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3111 | return 0; |
| 3112 | } |
| 3113 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3114 | /* soft profile changing mode */ |
| 3115 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3116 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3117 | return 0; |
| 3118 | } |
| 3119 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3120 | /* |
| 3121 | * limited by count, must be the last filter |
| 3122 | */ |
| 3123 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3124 | if (bargs->limit == 0) |
| 3125 | return 0; |
| 3126 | else |
| 3127 | bargs->limit--; |
| 3128 | } |
| 3129 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3130 | return 1; |
| 3131 | } |
| 3132 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3133 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3134 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3135 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3136 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3137 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3138 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3139 | struct btrfs_device *device; |
| 3140 | u64 old_size; |
| 3141 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3142 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3143 | struct btrfs_path *path; |
| 3144 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3145 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3146 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3147 | struct extent_buffer *leaf; |
| 3148 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3149 | int ret; |
| 3150 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3151 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3152 | u64 limit_data = bctl->data.limit; |
| 3153 | u64 limit_meta = bctl->meta.limit; |
| 3154 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3155 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3156 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3157 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3158 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3159 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3160 | size_to_free = div_factor(old_size, 1); |
| 3161 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3162 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3163 | btrfs_device_get_total_bytes(device) - |
| 3164 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3165 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3166 | continue; |
| 3167 | |
| 3168 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3169 | if (ret == -ENOSPC) |
| 3170 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3171 | BUG_ON(ret); |
| 3172 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3173 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3174 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3175 | |
| 3176 | ret = btrfs_grow_device(trans, device, old_size); |
| 3177 | BUG_ON(ret); |
| 3178 | |
| 3179 | btrfs_end_transaction(trans, dev_root); |
| 3180 | } |
| 3181 | |
| 3182 | /* step two, relocate all the chunks */ |
| 3183 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3184 | if (!path) { |
| 3185 | ret = -ENOMEM; |
| 3186 | goto error; |
| 3187 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3188 | |
| 3189 | /* zero out stat counters */ |
| 3190 | spin_lock(&fs_info->balance_lock); |
| 3191 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3192 | spin_unlock(&fs_info->balance_lock); |
| 3193 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3194 | if (!counting) { |
| 3195 | bctl->data.limit = limit_data; |
| 3196 | bctl->meta.limit = limit_meta; |
| 3197 | bctl->sys.limit = limit_sys; |
| 3198 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3199 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3200 | key.offset = (u64)-1; |
| 3201 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3202 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3203 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3204 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3205 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3206 | ret = -ECANCELED; |
| 3207 | goto error; |
| 3208 | } |
| 3209 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3210 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 3211 | if (ret < 0) |
| 3212 | goto error; |
| 3213 | |
| 3214 | /* |
| 3215 | * this shouldn't happen, it means the last relocate |
| 3216 | * failed |
| 3217 | */ |
| 3218 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3219 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3220 | |
| 3221 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3222 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3223 | if (ret) { |
| 3224 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3225 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3226 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3227 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3228 | leaf = path->nodes[0]; |
| 3229 | slot = path->slots[0]; |
| 3230 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3231 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3232 | if (found_key.objectid != key.objectid) |
| 3233 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3234 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3235 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3236 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3237 | if (!counting) { |
| 3238 | spin_lock(&fs_info->balance_lock); |
| 3239 | bctl->stat.considered++; |
| 3240 | spin_unlock(&fs_info->balance_lock); |
| 3241 | } |
| 3242 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3243 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3244 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3245 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3246 | if (!ret) |
| 3247 | goto loop; |
| 3248 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3249 | if (counting) { |
| 3250 | spin_lock(&fs_info->balance_lock); |
| 3251 | bctl->stat.expected++; |
| 3252 | spin_unlock(&fs_info->balance_lock); |
| 3253 | goto loop; |
| 3254 | } |
| 3255 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3256 | ret = btrfs_relocate_chunk(chunk_root, |
| 3257 | chunk_root->root_key.objectid, |
| 3258 | found_key.objectid, |
| 3259 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3260 | if (ret && ret != -ENOSPC) |
| 3261 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3262 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3263 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3264 | } else { |
| 3265 | spin_lock(&fs_info->balance_lock); |
| 3266 | bctl->stat.completed++; |
| 3267 | spin_unlock(&fs_info->balance_lock); |
| 3268 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3269 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3270 | if (found_key.offset == 0) |
| 3271 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3272 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3273 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3274 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3275 | if (counting) { |
| 3276 | btrfs_release_path(path); |
| 3277 | counting = false; |
| 3278 | goto again; |
| 3279 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3280 | error: |
| 3281 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3282 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3283 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3284 | enospc_errors); |
| 3285 | if (!ret) |
| 3286 | ret = -ENOSPC; |
| 3287 | } |
| 3288 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3289 | return ret; |
| 3290 | } |
| 3291 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3292 | /** |
| 3293 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3294 | * @flags: profile to validate |
| 3295 | * @extended: if true @flags is treated as an extended profile |
| 3296 | */ |
| 3297 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3298 | { |
| 3299 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3300 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3301 | |
| 3302 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3303 | |
| 3304 | /* 1) check that all other bits are zeroed */ |
| 3305 | if (flags & ~mask) |
| 3306 | return 0; |
| 3307 | |
| 3308 | /* 2) see if profile is reduced */ |
| 3309 | if (flags == 0) |
| 3310 | return !extended; /* "0" is valid for usual profiles */ |
| 3311 | |
| 3312 | /* true if exactly one bit set */ |
| 3313 | return (flags & (flags - 1)) == 0; |
| 3314 | } |
| 3315 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3316 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3317 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3318 | /* cancel requested || normal exit path */ |
| 3319 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3320 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3321 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3324 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3325 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3326 | int ret; |
| 3327 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3328 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3329 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3330 | if (ret) |
| 3331 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3332 | |
| 3333 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3334 | } |
| 3335 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3336 | /* |
| 3337 | * Should be called with both balance and volume mutexes held |
| 3338 | */ |
| 3339 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3340 | struct btrfs_ioctl_balance_args *bargs) |
| 3341 | { |
| 3342 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3343 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3344 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3345 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3346 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3347 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3348 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3349 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3350 | atomic_read(&fs_info->balance_pause_req) || |
| 3351 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3352 | ret = -EINVAL; |
| 3353 | goto out; |
| 3354 | } |
| 3355 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3356 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3357 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3358 | mixed = 1; |
| 3359 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3360 | /* |
| 3361 | * In case of mixed groups both data and meta should be picked, |
| 3362 | * and identical options should be given for both of them. |
| 3363 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3364 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3365 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3366 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3367 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3368 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3369 | btrfs_err(fs_info, "with mixed groups data and " |
| 3370 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3371 | ret = -EINVAL; |
| 3372 | goto out; |
| 3373 | } |
| 3374 | } |
| 3375 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3376 | num_devices = fs_info->fs_devices->num_devices; |
| 3377 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3378 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3379 | BUG_ON(num_devices < 1); |
| 3380 | num_devices--; |
| 3381 | } |
| 3382 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3383 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3384 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3385 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3386 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3387 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3388 | if (num_devices > 2) |
| 3389 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3390 | if (num_devices > 3) |
| 3391 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3392 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3393 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3394 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3395 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3396 | btrfs_err(fs_info, "unable to start balance with target " |
| 3397 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3398 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3399 | ret = -EINVAL; |
| 3400 | goto out; |
| 3401 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3402 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3403 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3404 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3405 | btrfs_err(fs_info, |
| 3406 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3407 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3408 | ret = -EINVAL; |
| 3409 | goto out; |
| 3410 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3411 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3412 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3413 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3414 | btrfs_err(fs_info, |
| 3415 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3416 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3417 | ret = -EINVAL; |
| 3418 | goto out; |
| 3419 | } |
| 3420 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3421 | /* allow dup'ed data chunks only in mixed mode */ |
| 3422 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3423 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3424 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3425 | ret = -EINVAL; |
| 3426 | goto out; |
| 3427 | } |
| 3428 | |
| 3429 | /* allow to reduce meta or sys integrity only if force set */ |
| 3430 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3431 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3432 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3433 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3434 | do { |
| 3435 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3436 | |
| 3437 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3438 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3439 | !(bctl->sys.target & allowed)) || |
| 3440 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3441 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3442 | !(bctl->meta.target & allowed))) { |
| 3443 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3444 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3445 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3446 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3447 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3448 | ret = -EINVAL; |
| 3449 | goto out; |
| 3450 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3451 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3452 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3453 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3454 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3455 | int num_tolerated_disk_barrier_failures; |
| 3456 | u64 target = bctl->sys.target; |
| 3457 | |
| 3458 | num_tolerated_disk_barrier_failures = |
| 3459 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3460 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3461 | (target & |
| 3462 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3463 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3464 | num_tolerated_disk_barrier_failures = 0; |
| 3465 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3466 | (target & |
| 3467 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3468 | num_tolerated_disk_barrier_failures = 1; |
| 3469 | |
| 3470 | fs_info->num_tolerated_disk_barrier_failures = |
| 3471 | num_tolerated_disk_barrier_failures; |
| 3472 | } |
| 3473 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3474 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3475 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3476 | goto out; |
| 3477 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3478 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3479 | BUG_ON(ret == -EEXIST); |
| 3480 | set_balance_control(bctl); |
| 3481 | } else { |
| 3482 | BUG_ON(ret != -EEXIST); |
| 3483 | spin_lock(&fs_info->balance_lock); |
| 3484 | update_balance_args(bctl); |
| 3485 | spin_unlock(&fs_info->balance_lock); |
| 3486 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3487 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3488 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3489 | mutex_unlock(&fs_info->balance_mutex); |
| 3490 | |
| 3491 | ret = __btrfs_balance(fs_info); |
| 3492 | |
| 3493 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3494 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3495 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3496 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3497 | fs_info->num_tolerated_disk_barrier_failures = |
| 3498 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3499 | } |
| 3500 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3501 | if (bargs) { |
| 3502 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3503 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3504 | } |
| 3505 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3506 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3507 | balance_need_close(fs_info)) { |
| 3508 | __cancel_balance(fs_info); |
| 3509 | } |
| 3510 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3511 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3512 | |
| 3513 | return ret; |
| 3514 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3515 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3516 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3517 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3518 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3519 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3520 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3521 | return ret; |
| 3522 | } |
| 3523 | |
| 3524 | static int balance_kthread(void *data) |
| 3525 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3526 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3527 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3528 | |
| 3529 | mutex_lock(&fs_info->volume_mutex); |
| 3530 | mutex_lock(&fs_info->balance_mutex); |
| 3531 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3532 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3533 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3534 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3535 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3536 | |
| 3537 | mutex_unlock(&fs_info->balance_mutex); |
| 3538 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3539 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3540 | return ret; |
| 3541 | } |
| 3542 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3543 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3544 | { |
| 3545 | struct task_struct *tsk; |
| 3546 | |
| 3547 | spin_lock(&fs_info->balance_lock); |
| 3548 | if (!fs_info->balance_ctl) { |
| 3549 | spin_unlock(&fs_info->balance_lock); |
| 3550 | return 0; |
| 3551 | } |
| 3552 | spin_unlock(&fs_info->balance_lock); |
| 3553 | |
| 3554 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3555 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3556 | return 0; |
| 3557 | } |
| 3558 | |
| 3559 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3560 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3561 | } |
| 3562 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3563 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3564 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3565 | struct btrfs_balance_control *bctl; |
| 3566 | struct btrfs_balance_item *item; |
| 3567 | struct btrfs_disk_balance_args disk_bargs; |
| 3568 | struct btrfs_path *path; |
| 3569 | struct extent_buffer *leaf; |
| 3570 | struct btrfs_key key; |
| 3571 | int ret; |
| 3572 | |
| 3573 | path = btrfs_alloc_path(); |
| 3574 | if (!path) |
| 3575 | return -ENOMEM; |
| 3576 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3577 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3578 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3579 | key.offset = 0; |
| 3580 | |
| 3581 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3582 | if (ret < 0) |
| 3583 | goto out; |
| 3584 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3585 | ret = 0; |
| 3586 | goto out; |
| 3587 | } |
| 3588 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3589 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3590 | if (!bctl) { |
| 3591 | ret = -ENOMEM; |
| 3592 | goto out; |
| 3593 | } |
| 3594 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3595 | leaf = path->nodes[0]; |
| 3596 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3597 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3598 | bctl->fs_info = fs_info; |
| 3599 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3600 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3601 | |
| 3602 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3603 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3604 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3605 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3606 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3607 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3608 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3609 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3610 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3611 | mutex_lock(&fs_info->volume_mutex); |
| 3612 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3613 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3614 | set_balance_control(bctl); |
| 3615 | |
| 3616 | mutex_unlock(&fs_info->balance_mutex); |
| 3617 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3618 | out: |
| 3619 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3620 | return ret; |
| 3621 | } |
| 3622 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3623 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3624 | { |
| 3625 | int ret = 0; |
| 3626 | |
| 3627 | mutex_lock(&fs_info->balance_mutex); |
| 3628 | if (!fs_info->balance_ctl) { |
| 3629 | mutex_unlock(&fs_info->balance_mutex); |
| 3630 | return -ENOTCONN; |
| 3631 | } |
| 3632 | |
| 3633 | if (atomic_read(&fs_info->balance_running)) { |
| 3634 | atomic_inc(&fs_info->balance_pause_req); |
| 3635 | mutex_unlock(&fs_info->balance_mutex); |
| 3636 | |
| 3637 | wait_event(fs_info->balance_wait_q, |
| 3638 | atomic_read(&fs_info->balance_running) == 0); |
| 3639 | |
| 3640 | mutex_lock(&fs_info->balance_mutex); |
| 3641 | /* we are good with balance_ctl ripped off from under us */ |
| 3642 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3643 | atomic_dec(&fs_info->balance_pause_req); |
| 3644 | } else { |
| 3645 | ret = -ENOTCONN; |
| 3646 | } |
| 3647 | |
| 3648 | mutex_unlock(&fs_info->balance_mutex); |
| 3649 | return ret; |
| 3650 | } |
| 3651 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3652 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3653 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3654 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3655 | return -EROFS; |
| 3656 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3657 | mutex_lock(&fs_info->balance_mutex); |
| 3658 | if (!fs_info->balance_ctl) { |
| 3659 | mutex_unlock(&fs_info->balance_mutex); |
| 3660 | return -ENOTCONN; |
| 3661 | } |
| 3662 | |
| 3663 | atomic_inc(&fs_info->balance_cancel_req); |
| 3664 | /* |
| 3665 | * if we are running just wait and return, balance item is |
| 3666 | * deleted in btrfs_balance in this case |
| 3667 | */ |
| 3668 | if (atomic_read(&fs_info->balance_running)) { |
| 3669 | mutex_unlock(&fs_info->balance_mutex); |
| 3670 | wait_event(fs_info->balance_wait_q, |
| 3671 | atomic_read(&fs_info->balance_running) == 0); |
| 3672 | mutex_lock(&fs_info->balance_mutex); |
| 3673 | } else { |
| 3674 | /* __cancel_balance needs volume_mutex */ |
| 3675 | mutex_unlock(&fs_info->balance_mutex); |
| 3676 | mutex_lock(&fs_info->volume_mutex); |
| 3677 | mutex_lock(&fs_info->balance_mutex); |
| 3678 | |
| 3679 | if (fs_info->balance_ctl) |
| 3680 | __cancel_balance(fs_info); |
| 3681 | |
| 3682 | mutex_unlock(&fs_info->volume_mutex); |
| 3683 | } |
| 3684 | |
| 3685 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3686 | atomic_dec(&fs_info->balance_cancel_req); |
| 3687 | mutex_unlock(&fs_info->balance_mutex); |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3691 | static int btrfs_uuid_scan_kthread(void *data) |
| 3692 | { |
| 3693 | struct btrfs_fs_info *fs_info = data; |
| 3694 | struct btrfs_root *root = fs_info->tree_root; |
| 3695 | struct btrfs_key key; |
| 3696 | struct btrfs_key max_key; |
| 3697 | struct btrfs_path *path = NULL; |
| 3698 | int ret = 0; |
| 3699 | struct extent_buffer *eb; |
| 3700 | int slot; |
| 3701 | struct btrfs_root_item root_item; |
| 3702 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3703 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3704 | |
| 3705 | path = btrfs_alloc_path(); |
| 3706 | if (!path) { |
| 3707 | ret = -ENOMEM; |
| 3708 | goto out; |
| 3709 | } |
| 3710 | |
| 3711 | key.objectid = 0; |
| 3712 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3713 | key.offset = 0; |
| 3714 | |
| 3715 | max_key.objectid = (u64)-1; |
| 3716 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3717 | max_key.offset = (u64)-1; |
| 3718 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3719 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3720 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3721 | if (ret) { |
| 3722 | if (ret > 0) |
| 3723 | ret = 0; |
| 3724 | break; |
| 3725 | } |
| 3726 | |
| 3727 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3728 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3729 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3730 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3731 | goto skip; |
| 3732 | |
| 3733 | eb = path->nodes[0]; |
| 3734 | slot = path->slots[0]; |
| 3735 | item_size = btrfs_item_size_nr(eb, slot); |
| 3736 | if (item_size < sizeof(root_item)) |
| 3737 | goto skip; |
| 3738 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3739 | read_extent_buffer(eb, &root_item, |
| 3740 | btrfs_item_ptr_offset(eb, slot), |
| 3741 | (int)sizeof(root_item)); |
| 3742 | if (btrfs_root_refs(&root_item) == 0) |
| 3743 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3744 | |
| 3745 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3746 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3747 | if (trans) |
| 3748 | goto update_tree; |
| 3749 | |
| 3750 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3751 | /* |
| 3752 | * 1 - subvol uuid item |
| 3753 | * 1 - received_subvol uuid item |
| 3754 | */ |
| 3755 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3756 | if (IS_ERR(trans)) { |
| 3757 | ret = PTR_ERR(trans); |
| 3758 | break; |
| 3759 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3760 | continue; |
| 3761 | } else { |
| 3762 | goto skip; |
| 3763 | } |
| 3764 | update_tree: |
| 3765 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3766 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3767 | root_item.uuid, |
| 3768 | BTRFS_UUID_KEY_SUBVOL, |
| 3769 | key.objectid); |
| 3770 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3771 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3772 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3773 | break; |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3778 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3779 | root_item.received_uuid, |
| 3780 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3781 | key.objectid); |
| 3782 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3783 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3784 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3785 | break; |
| 3786 | } |
| 3787 | } |
| 3788 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3789 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3790 | if (trans) { |
| 3791 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3792 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3793 | if (ret) |
| 3794 | break; |
| 3795 | } |
| 3796 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3797 | btrfs_release_path(path); |
| 3798 | if (key.offset < (u64)-1) { |
| 3799 | key.offset++; |
| 3800 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3801 | key.offset = 0; |
| 3802 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3803 | } else if (key.objectid < (u64)-1) { |
| 3804 | key.offset = 0; |
| 3805 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3806 | key.objectid++; |
| 3807 | } else { |
| 3808 | break; |
| 3809 | } |
| 3810 | cond_resched(); |
| 3811 | } |
| 3812 | |
| 3813 | out: |
| 3814 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3815 | if (trans && !IS_ERR(trans)) |
| 3816 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3817 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3818 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3819 | else |
| 3820 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3821 | up(&fs_info->uuid_tree_rescan_sem); |
| 3822 | return 0; |
| 3823 | } |
| 3824 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3825 | /* |
| 3826 | * Callback for btrfs_uuid_tree_iterate(). |
| 3827 | * returns: |
| 3828 | * 0 check succeeded, the entry is not outdated. |
| 3829 | * < 0 if an error occured. |
| 3830 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3831 | */ |
| 3832 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3833 | u8 *uuid, u8 type, u64 subid) |
| 3834 | { |
| 3835 | struct btrfs_key key; |
| 3836 | int ret = 0; |
| 3837 | struct btrfs_root *subvol_root; |
| 3838 | |
| 3839 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3840 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3841 | goto out; |
| 3842 | |
| 3843 | key.objectid = subid; |
| 3844 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3845 | key.offset = (u64)-1; |
| 3846 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3847 | if (IS_ERR(subvol_root)) { |
| 3848 | ret = PTR_ERR(subvol_root); |
| 3849 | if (ret == -ENOENT) |
| 3850 | ret = 1; |
| 3851 | goto out; |
| 3852 | } |
| 3853 | |
| 3854 | switch (type) { |
| 3855 | case BTRFS_UUID_KEY_SUBVOL: |
| 3856 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3857 | ret = 1; |
| 3858 | break; |
| 3859 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3860 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3861 | BTRFS_UUID_SIZE)) |
| 3862 | ret = 1; |
| 3863 | break; |
| 3864 | } |
| 3865 | |
| 3866 | out: |
| 3867 | return ret; |
| 3868 | } |
| 3869 | |
| 3870 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3871 | { |
| 3872 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3873 | int ret; |
| 3874 | |
| 3875 | /* |
| 3876 | * 1st step is to iterate through the existing UUID tree and |
| 3877 | * to delete all entries that contain outdated data. |
| 3878 | * 2nd step is to add all missing entries to the UUID tree. |
| 3879 | */ |
| 3880 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 3881 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3882 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3883 | up(&fs_info->uuid_tree_rescan_sem); |
| 3884 | return ret; |
| 3885 | } |
| 3886 | return btrfs_uuid_scan_kthread(data); |
| 3887 | } |
| 3888 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3889 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3890 | { |
| 3891 | struct btrfs_trans_handle *trans; |
| 3892 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 3893 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3894 | struct task_struct *task; |
| 3895 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* |
| 3898 | * 1 - root node |
| 3899 | * 1 - root item |
| 3900 | */ |
| 3901 | trans = btrfs_start_transaction(tree_root, 2); |
| 3902 | if (IS_ERR(trans)) |
| 3903 | return PTR_ERR(trans); |
| 3904 | |
| 3905 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 3906 | BTRFS_UUID_TREE_OBJECTID); |
| 3907 | if (IS_ERR(uuid_root)) { |
| 3908 | btrfs_abort_transaction(trans, tree_root, |
| 3909 | PTR_ERR(uuid_root)); |
| 3910 | return PTR_ERR(uuid_root); |
| 3911 | } |
| 3912 | |
| 3913 | fs_info->uuid_root = uuid_root; |
| 3914 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3915 | ret = btrfs_commit_transaction(trans, tree_root); |
| 3916 | if (ret) |
| 3917 | return ret; |
| 3918 | |
| 3919 | down(&fs_info->uuid_tree_rescan_sem); |
| 3920 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 3921 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3922 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3923 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3924 | up(&fs_info->uuid_tree_rescan_sem); |
| 3925 | return PTR_ERR(task); |
| 3926 | } |
| 3927 | |
| 3928 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3929 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3930 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3931 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3932 | { |
| 3933 | struct task_struct *task; |
| 3934 | |
| 3935 | down(&fs_info->uuid_tree_rescan_sem); |
| 3936 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 3937 | if (IS_ERR(task)) { |
| 3938 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3939 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3940 | up(&fs_info->uuid_tree_rescan_sem); |
| 3941 | return PTR_ERR(task); |
| 3942 | } |
| 3943 | |
| 3944 | return 0; |
| 3945 | } |
| 3946 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3947 | /* |
| 3948 | * shrinking a device means finding all of the device extents past |
| 3949 | * the new size, and then following the back refs to the chunks. |
| 3950 | * The chunk relocation code actually frees the device extent |
| 3951 | */ |
| 3952 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3953 | { |
| 3954 | struct btrfs_trans_handle *trans; |
| 3955 | struct btrfs_root *root = device->dev_root; |
| 3956 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3957 | struct btrfs_path *path; |
| 3958 | u64 length; |
| 3959 | u64 chunk_tree; |
| 3960 | u64 chunk_objectid; |
| 3961 | u64 chunk_offset; |
| 3962 | int ret; |
| 3963 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3964 | int failed = 0; |
| 3965 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3966 | struct extent_buffer *l; |
| 3967 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3968 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3969 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3970 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 3971 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3972 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3973 | if (device->is_tgtdev_for_dev_replace) |
| 3974 | return -EINVAL; |
| 3975 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3976 | path = btrfs_alloc_path(); |
| 3977 | if (!path) |
| 3978 | return -ENOMEM; |
| 3979 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3980 | path->reada = 2; |
| 3981 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3982 | lock_chunks(root); |
| 3983 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3984 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3985 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3986 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3987 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3988 | root->fs_info->free_chunk_space -= diff; |
| 3989 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3990 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3991 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3992 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3993 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3994 | key.objectid = device->devid; |
| 3995 | key.offset = (u64)-1; |
| 3996 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3997 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3998 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3999 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4000 | if (ret < 0) |
| 4001 | goto done; |
| 4002 | |
| 4003 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 4004 | if (ret < 0) |
| 4005 | goto done; |
| 4006 | if (ret) { |
| 4007 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4008 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4009 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | l = path->nodes[0]; |
| 4013 | slot = path->slots[0]; |
| 4014 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4015 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4016 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4017 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4018 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4019 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4020 | |
| 4021 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4022 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4023 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4024 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4025 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4026 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4027 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4028 | |
| 4029 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 4030 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 4031 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4032 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4033 | |
| 4034 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 4035 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4036 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4037 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4038 | if (ret == -ENOSPC) |
| 4039 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4040 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4041 | |
| 4042 | if (failed && !retried) { |
| 4043 | failed = 0; |
| 4044 | retried = true; |
| 4045 | goto again; |
| 4046 | } else if (failed && retried) { |
| 4047 | ret = -ENOSPC; |
| 4048 | lock_chunks(root); |
| 4049 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4050 | btrfs_device_set_total_bytes(device, old_size); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4051 | if (device->writeable) |
| 4052 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4053 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4054 | root->fs_info->free_chunk_space += diff; |
| 4055 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4056 | unlock_chunks(root); |
| 4057 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4058 | } |
| 4059 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4060 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4061 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4062 | if (IS_ERR(trans)) { |
| 4063 | ret = PTR_ERR(trans); |
| 4064 | goto done; |
| 4065 | } |
| 4066 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4067 | lock_chunks(root); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4068 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4069 | if (list_empty(&device->resized_list)) |
| 4070 | list_add_tail(&device->resized_list, |
| 4071 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4072 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4073 | WARN_ON(diff > old_total); |
| 4074 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4075 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4076 | |
| 4077 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4078 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4079 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4080 | done: |
| 4081 | btrfs_free_path(path); |
| 4082 | return ret; |
| 4083 | } |
| 4084 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4085 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4086 | struct btrfs_key *key, |
| 4087 | struct btrfs_chunk *chunk, int item_size) |
| 4088 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4089 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4090 | struct btrfs_disk_key disk_key; |
| 4091 | u32 array_size; |
| 4092 | u8 *ptr; |
| 4093 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4094 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4095 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4096 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4097 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4098 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4099 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4100 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4101 | |
| 4102 | ptr = super_copy->sys_chunk_array + array_size; |
| 4103 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4104 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4105 | ptr += sizeof(disk_key); |
| 4106 | memcpy(ptr, chunk, item_size); |
| 4107 | item_size += sizeof(disk_key); |
| 4108 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4109 | unlock_chunks(root); |
| 4110 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4111 | return 0; |
| 4112 | } |
| 4113 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4114 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4115 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4116 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4117 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4118 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4119 | const struct btrfs_device_info *di_a = a; |
| 4120 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4121 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4122 | if (di_a->max_avail > di_b->max_avail) |
| 4123 | return -1; |
| 4124 | if (di_a->max_avail < di_b->max_avail) |
| 4125 | return 1; |
| 4126 | if (di_a->total_avail > di_b->total_avail) |
| 4127 | return -1; |
| 4128 | if (di_a->total_avail < di_b->total_avail) |
| 4129 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4130 | return 0; |
| 4131 | } |
| 4132 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4133 | static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 4134 | [BTRFS_RAID_RAID10] = { |
| 4135 | .sub_stripes = 2, |
| 4136 | .dev_stripes = 1, |
| 4137 | .devs_max = 0, /* 0 == as many as possible */ |
| 4138 | .devs_min = 4, |
| 4139 | .devs_increment = 2, |
| 4140 | .ncopies = 2, |
| 4141 | }, |
| 4142 | [BTRFS_RAID_RAID1] = { |
| 4143 | .sub_stripes = 1, |
| 4144 | .dev_stripes = 1, |
| 4145 | .devs_max = 2, |
| 4146 | .devs_min = 2, |
| 4147 | .devs_increment = 2, |
| 4148 | .ncopies = 2, |
| 4149 | }, |
| 4150 | [BTRFS_RAID_DUP] = { |
| 4151 | .sub_stripes = 1, |
| 4152 | .dev_stripes = 2, |
| 4153 | .devs_max = 1, |
| 4154 | .devs_min = 1, |
| 4155 | .devs_increment = 1, |
| 4156 | .ncopies = 2, |
| 4157 | }, |
| 4158 | [BTRFS_RAID_RAID0] = { |
| 4159 | .sub_stripes = 1, |
| 4160 | .dev_stripes = 1, |
| 4161 | .devs_max = 0, |
| 4162 | .devs_min = 2, |
| 4163 | .devs_increment = 1, |
| 4164 | .ncopies = 1, |
| 4165 | }, |
| 4166 | [BTRFS_RAID_SINGLE] = { |
| 4167 | .sub_stripes = 1, |
| 4168 | .dev_stripes = 1, |
| 4169 | .devs_max = 1, |
| 4170 | .devs_min = 1, |
| 4171 | .devs_increment = 1, |
| 4172 | .ncopies = 1, |
| 4173 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4174 | [BTRFS_RAID_RAID5] = { |
| 4175 | .sub_stripes = 1, |
| 4176 | .dev_stripes = 1, |
| 4177 | .devs_max = 0, |
| 4178 | .devs_min = 2, |
| 4179 | .devs_increment = 1, |
| 4180 | .ncopies = 2, |
| 4181 | }, |
| 4182 | [BTRFS_RAID_RAID6] = { |
| 4183 | .sub_stripes = 1, |
| 4184 | .dev_stripes = 1, |
| 4185 | .devs_max = 0, |
| 4186 | .devs_min = 3, |
| 4187 | .devs_increment = 1, |
| 4188 | .ncopies = 3, |
| 4189 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4190 | }; |
| 4191 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4192 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4193 | { |
| 4194 | /* TODO allow them to set a preferred stripe size */ |
| 4195 | return 64 * 1024; |
| 4196 | } |
| 4197 | |
| 4198 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4199 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4200 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4201 | return; |
| 4202 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4203 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4204 | } |
| 4205 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4206 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4207 | - sizeof(struct btrfs_item) \ |
| 4208 | - sizeof(struct btrfs_chunk)) \ |
| 4209 | / sizeof(struct btrfs_stripe) + 1) |
| 4210 | |
| 4211 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4212 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4213 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4214 | / sizeof(struct btrfs_stripe) + 1) |
| 4215 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4216 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4217 | struct btrfs_root *extent_root, u64 start, |
| 4218 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4219 | { |
| 4220 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4221 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4222 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4223 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4224 | struct extent_map_tree *em_tree; |
| 4225 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4226 | struct btrfs_device_info *devices_info = NULL; |
| 4227 | u64 total_avail; |
| 4228 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4229 | int data_stripes; /* number of stripes that count for |
| 4230 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4231 | int sub_stripes; /* sub_stripes info for map */ |
| 4232 | int dev_stripes; /* stripes per dev */ |
| 4233 | int devs_max; /* max devs to use */ |
| 4234 | int devs_min; /* min devs needed */ |
| 4235 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4236 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4237 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4238 | u64 max_stripe_size; |
| 4239 | u64 max_chunk_size; |
| 4240 | u64 stripe_size; |
| 4241 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4242 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4243 | int ndevs; |
| 4244 | int i; |
| 4245 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4246 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4247 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4248 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4249 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4250 | if (list_empty(&fs_devices->alloc_list)) |
| 4251 | return -ENOSPC; |
| 4252 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4253 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4254 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4255 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4256 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4257 | devs_max = btrfs_raid_array[index].devs_max; |
| 4258 | devs_min = btrfs_raid_array[index].devs_min; |
| 4259 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4260 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4261 | |
| 4262 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4263 | max_stripe_size = 1024 * 1024 * 1024; |
| 4264 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4265 | if (!devs_max) |
| 4266 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4267 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4268 | /* for larger filesystems, use larger metadata chunks */ |
| 4269 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4270 | max_stripe_size = 1024 * 1024 * 1024; |
| 4271 | else |
| 4272 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4273 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4274 | if (!devs_max) |
| 4275 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4276 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4277 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4278 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4279 | if (!devs_max) |
| 4280 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4281 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4282 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4283 | type); |
| 4284 | BUG_ON(1); |
| 4285 | } |
| 4286 | |
| 4287 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4288 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4289 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4290 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame^] | 4291 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4292 | GFP_NOFS); |
| 4293 | if (!devices_info) |
| 4294 | return -ENOMEM; |
| 4295 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4296 | cur = fs_devices->alloc_list.next; |
| 4297 | |
| 4298 | /* |
| 4299 | * in the first pass through the devices list, we gather information |
| 4300 | * about the available holes on each device. |
| 4301 | */ |
| 4302 | ndevs = 0; |
| 4303 | while (cur != &fs_devices->alloc_list) { |
| 4304 | struct btrfs_device *device; |
| 4305 | u64 max_avail; |
| 4306 | u64 dev_offset; |
| 4307 | |
| 4308 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4309 | |
| 4310 | cur = cur->next; |
| 4311 | |
| 4312 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4313 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4314 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4315 | continue; |
| 4316 | } |
| 4317 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4318 | if (!device->in_fs_metadata || |
| 4319 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4320 | continue; |
| 4321 | |
| 4322 | if (device->total_bytes > device->bytes_used) |
| 4323 | total_avail = device->total_bytes - device->bytes_used; |
| 4324 | else |
| 4325 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4326 | |
| 4327 | /* If there is no space on this device, skip it. */ |
| 4328 | if (total_avail == 0) |
| 4329 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4330 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4331 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4332 | max_stripe_size * dev_stripes, |
| 4333 | &dev_offset, &max_avail); |
| 4334 | if (ret && ret != -ENOSPC) |
| 4335 | goto error; |
| 4336 | |
| 4337 | if (ret == 0) |
| 4338 | max_avail = max_stripe_size * dev_stripes; |
| 4339 | |
| 4340 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4341 | continue; |
| 4342 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4343 | if (ndevs == fs_devices->rw_devices) { |
| 4344 | WARN(1, "%s: found more than %llu devices\n", |
| 4345 | __func__, fs_devices->rw_devices); |
| 4346 | break; |
| 4347 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4348 | devices_info[ndevs].dev_offset = dev_offset; |
| 4349 | devices_info[ndevs].max_avail = max_avail; |
| 4350 | devices_info[ndevs].total_avail = total_avail; |
| 4351 | devices_info[ndevs].dev = device; |
| 4352 | ++ndevs; |
| 4353 | } |
| 4354 | |
| 4355 | /* |
| 4356 | * now sort the devices by hole size / available space |
| 4357 | */ |
| 4358 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4359 | btrfs_cmp_device_info, NULL); |
| 4360 | |
| 4361 | /* round down to number of usable stripes */ |
| 4362 | ndevs -= ndevs % devs_increment; |
| 4363 | |
| 4364 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4365 | ret = -ENOSPC; |
| 4366 | goto error; |
| 4367 | } |
| 4368 | |
| 4369 | if (devs_max && ndevs > devs_max) |
| 4370 | ndevs = devs_max; |
| 4371 | /* |
| 4372 | * the primary goal is to maximize the number of stripes, so use as many |
| 4373 | * devices as possible, even if the stripes are not maximum sized. |
| 4374 | */ |
| 4375 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4376 | num_stripes = ndevs * dev_stripes; |
| 4377 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4378 | /* |
| 4379 | * this will have to be fixed for RAID1 and RAID10 over |
| 4380 | * more drives |
| 4381 | */ |
| 4382 | data_stripes = num_stripes / ncopies; |
| 4383 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4384 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4385 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4386 | btrfs_super_stripesize(info->super_copy)); |
| 4387 | data_stripes = num_stripes - 1; |
| 4388 | } |
| 4389 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4390 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4391 | btrfs_super_stripesize(info->super_copy)); |
| 4392 | data_stripes = num_stripes - 2; |
| 4393 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4394 | |
| 4395 | /* |
| 4396 | * Use the number of data stripes to figure out how big this chunk |
| 4397 | * is really going to be in terms of logical address space, |
| 4398 | * and compare that answer with the max chunk size |
| 4399 | */ |
| 4400 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4401 | u64 mask = (1ULL << 24) - 1; |
| 4402 | stripe_size = max_chunk_size; |
| 4403 | do_div(stripe_size, data_stripes); |
| 4404 | |
| 4405 | /* bump the answer up to a 16MB boundary */ |
| 4406 | stripe_size = (stripe_size + mask) & ~mask; |
| 4407 | |
| 4408 | /* but don't go higher than the limits we found |
| 4409 | * while searching for free extents |
| 4410 | */ |
| 4411 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4412 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4413 | } |
| 4414 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4415 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4416 | |
| 4417 | /* align to BTRFS_STRIPE_LEN */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4418 | do_div(stripe_size, raid_stripe_len); |
| 4419 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4420 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4421 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4422 | if (!map) { |
| 4423 | ret = -ENOMEM; |
| 4424 | goto error; |
| 4425 | } |
| 4426 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4427 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4428 | for (i = 0; i < ndevs; ++i) { |
| 4429 | for (j = 0; j < dev_stripes; ++j) { |
| 4430 | int s = i * dev_stripes + j; |
| 4431 | map->stripes[s].dev = devices_info[i].dev; |
| 4432 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4433 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4434 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4435 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4436 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4437 | map->stripe_len = raid_stripe_len; |
| 4438 | map->io_align = raid_stripe_len; |
| 4439 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4440 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4441 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4442 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4443 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4444 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4445 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4446 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4447 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4448 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4449 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4450 | ret = -ENOMEM; |
| 4451 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4452 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4453 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4454 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4455 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4456 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4457 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4458 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4459 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4460 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4461 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4462 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4463 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4464 | if (!ret) { |
| 4465 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4466 | atomic_inc(&em->refs); |
| 4467 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4468 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4469 | if (ret) { |
| 4470 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4471 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4472 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4473 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4474 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4475 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4476 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4477 | if (ret) |
| 4478 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4479 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4480 | for (i = 0; i < map->num_stripes; i++) { |
| 4481 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4482 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4483 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4484 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4485 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4486 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4487 | map->num_stripes); |
| 4488 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4489 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4490 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4491 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4492 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4493 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4494 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4495 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4496 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4497 | write_lock(&em_tree->lock); |
| 4498 | remove_extent_mapping(em_tree, em); |
| 4499 | write_unlock(&em_tree->lock); |
| 4500 | |
| 4501 | /* One for our allocation */ |
| 4502 | free_extent_map(em); |
| 4503 | /* One for the tree reference */ |
| 4504 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4505 | /* One for the pending_chunks list reference */ |
| 4506 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4507 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4508 | kfree(devices_info); |
| 4509 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4510 | } |
| 4511 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4512 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4513 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4514 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4515 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4516 | struct btrfs_key key; |
| 4517 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4518 | struct btrfs_device *device; |
| 4519 | struct btrfs_chunk *chunk; |
| 4520 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4521 | struct extent_map_tree *em_tree; |
| 4522 | struct extent_map *em; |
| 4523 | struct map_lookup *map; |
| 4524 | size_t item_size; |
| 4525 | u64 dev_offset; |
| 4526 | u64 stripe_size; |
| 4527 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4528 | int ret; |
| 4529 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4530 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4531 | read_lock(&em_tree->lock); |
| 4532 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4533 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4534 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4535 | if (!em) { |
| 4536 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4537 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4538 | return -EINVAL; |
| 4539 | } |
| 4540 | |
| 4541 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4542 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4543 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4544 | chunk_size, em->start, em->len); |
| 4545 | free_extent_map(em); |
| 4546 | return -EINVAL; |
| 4547 | } |
| 4548 | |
| 4549 | map = (struct map_lookup *)em->bdev; |
| 4550 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4551 | stripe_size = em->orig_block_len; |
| 4552 | |
| 4553 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4554 | if (!chunk) { |
| 4555 | ret = -ENOMEM; |
| 4556 | goto out; |
| 4557 | } |
| 4558 | |
| 4559 | for (i = 0; i < map->num_stripes; i++) { |
| 4560 | device = map->stripes[i].dev; |
| 4561 | dev_offset = map->stripes[i].physical; |
| 4562 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4563 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4564 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4565 | goto out; |
| 4566 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4567 | chunk_root->root_key.objectid, |
| 4568 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4569 | chunk_offset, dev_offset, |
| 4570 | stripe_size); |
| 4571 | if (ret) |
| 4572 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4573 | } |
| 4574 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4575 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4576 | for (i = 0; i < map->num_stripes; i++) { |
| 4577 | device = map->stripes[i].dev; |
| 4578 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4579 | |
| 4580 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4581 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4582 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4583 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4587 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4588 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4589 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4590 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4591 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4592 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4593 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4594 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4595 | |
| 4596 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4597 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4598 | key.offset = chunk_offset; |
| 4599 | |
| 4600 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4601 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4602 | /* |
| 4603 | * TODO: Cleanup of inserted chunk root in case of |
| 4604 | * failure. |
| 4605 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4606 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4607 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4608 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4609 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4610 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4611 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4612 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4613 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | /* |
| 4617 | * Chunk allocation falls into two parts. The first part does works |
| 4618 | * that make the new allocated chunk useable, but not do any operation |
| 4619 | * that modifies the chunk tree. The second part does the works that |
| 4620 | * require modifying the chunk tree. This division is important for the |
| 4621 | * bootstrap process of adding storage to a seed btrfs. |
| 4622 | */ |
| 4623 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4624 | struct btrfs_root *extent_root, u64 type) |
| 4625 | { |
| 4626 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4627 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4628 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4629 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4630 | } |
| 4631 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4632 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4633 | struct btrfs_root *root, |
| 4634 | struct btrfs_device *device) |
| 4635 | { |
| 4636 | u64 chunk_offset; |
| 4637 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4638 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4639 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4640 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4641 | int ret; |
| 4642 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4643 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4644 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4645 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4646 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4647 | if (ret) |
| 4648 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4649 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4650 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4651 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4652 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4653 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4654 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4655 | } |
| 4656 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4657 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4658 | { |
| 4659 | int max_errors; |
| 4660 | |
| 4661 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4662 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4663 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4664 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4665 | max_errors = 1; |
| 4666 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4667 | max_errors = 2; |
| 4668 | } else { |
| 4669 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4670 | } |
| 4671 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4672 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4673 | } |
| 4674 | |
| 4675 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4676 | { |
| 4677 | struct extent_map *em; |
| 4678 | struct map_lookup *map; |
| 4679 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4680 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4681 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4682 | int i; |
| 4683 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4684 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4685 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4686 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4687 | if (!em) |
| 4688 | return 1; |
| 4689 | |
| 4690 | map = (struct map_lookup *)em->bdev; |
| 4691 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4692 | if (map->stripes[i].dev->missing) { |
| 4693 | miss_ndevs++; |
| 4694 | continue; |
| 4695 | } |
| 4696 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4697 | if (!map->stripes[i].dev->writeable) { |
| 4698 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4699 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4700 | } |
| 4701 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4702 | |
| 4703 | /* |
| 4704 | * If the number of missing devices is larger than max errors, |
| 4705 | * we can not write the data into that chunk successfully, so |
| 4706 | * set it readonly. |
| 4707 | */ |
| 4708 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4709 | readonly = 1; |
| 4710 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4711 | free_extent_map(em); |
| 4712 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4716 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4717 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4721 | { |
| 4722 | struct extent_map *em; |
| 4723 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4724 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4725 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4726 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4727 | if (em) |
| 4728 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4729 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4730 | if (!em) |
| 4731 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4732 | /* once for us */ |
| 4733 | free_extent_map(em); |
| 4734 | /* once for the tree */ |
| 4735 | free_extent_map(em); |
| 4736 | } |
| 4737 | } |
| 4738 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4739 | 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] | 4740 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4741 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4742 | struct extent_map *em; |
| 4743 | struct map_lookup *map; |
| 4744 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4745 | int ret; |
| 4746 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4747 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4748 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4749 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4750 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4751 | /* |
| 4752 | * We could return errors for these cases, but that could get ugly and |
| 4753 | * we'd probably do the same thing which is just not do anything else |
| 4754 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4755 | */ |
| 4756 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4757 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4758 | logical+len); |
| 4759 | return 1; |
| 4760 | } |
| 4761 | |
| 4762 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4763 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4764 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4765 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4766 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4767 | return 1; |
| 4768 | } |
| 4769 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4770 | map = (struct map_lookup *)em->bdev; |
| 4771 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4772 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4773 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4774 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4775 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4776 | ret = 2; |
| 4777 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4778 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4779 | else |
| 4780 | ret = 1; |
| 4781 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4782 | |
| 4783 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4784 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4785 | ret++; |
| 4786 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4787 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4788 | return ret; |
| 4789 | } |
| 4790 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4791 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4792 | struct btrfs_mapping_tree *map_tree, |
| 4793 | u64 logical) |
| 4794 | { |
| 4795 | struct extent_map *em; |
| 4796 | struct map_lookup *map; |
| 4797 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4798 | unsigned long len = root->sectorsize; |
| 4799 | |
| 4800 | read_lock(&em_tree->lock); |
| 4801 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4802 | read_unlock(&em_tree->lock); |
| 4803 | BUG_ON(!em); |
| 4804 | |
| 4805 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4806 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4807 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4808 | len = map->stripe_len * nr_data_stripes(map); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4809 | free_extent_map(em); |
| 4810 | return len; |
| 4811 | } |
| 4812 | |
| 4813 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4814 | u64 logical, u64 len, int mirror_num) |
| 4815 | { |
| 4816 | struct extent_map *em; |
| 4817 | struct map_lookup *map; |
| 4818 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4819 | int ret = 0; |
| 4820 | |
| 4821 | read_lock(&em_tree->lock); |
| 4822 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4823 | read_unlock(&em_tree->lock); |
| 4824 | BUG_ON(!em); |
| 4825 | |
| 4826 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4827 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4828 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4829 | ret = 1; |
| 4830 | free_extent_map(em); |
| 4831 | return ret; |
| 4832 | } |
| 4833 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4834 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4835 | struct map_lookup *map, int first, int num, |
| 4836 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4837 | { |
| 4838 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4839 | int tolerance; |
| 4840 | struct btrfs_device *srcdev; |
| 4841 | |
| 4842 | if (dev_replace_is_ongoing && |
| 4843 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4844 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4845 | srcdev = fs_info->dev_replace.srcdev; |
| 4846 | else |
| 4847 | srcdev = NULL; |
| 4848 | |
| 4849 | /* |
| 4850 | * try to avoid the drive that is the source drive for a |
| 4851 | * dev-replace procedure, only choose it if no other non-missing |
| 4852 | * mirror is available |
| 4853 | */ |
| 4854 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4855 | if (map->stripes[optimal].dev->bdev && |
| 4856 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4857 | return optimal; |
| 4858 | for (i = first; i < first + num; i++) { |
| 4859 | if (map->stripes[i].dev->bdev && |
| 4860 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4861 | return i; |
| 4862 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4863 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4864 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4865 | /* we couldn't find one that doesn't fail. Just return something |
| 4866 | * and the io error handling code will clean up eventually |
| 4867 | */ |
| 4868 | return optimal; |
| 4869 | } |
| 4870 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4871 | static inline int parity_smaller(u64 a, u64 b) |
| 4872 | { |
| 4873 | return a > b; |
| 4874 | } |
| 4875 | |
| 4876 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4877 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4878 | { |
| 4879 | struct btrfs_bio_stripe s; |
| 4880 | int i; |
| 4881 | u64 l; |
| 4882 | int again = 1; |
| 4883 | |
| 4884 | while (again) { |
| 4885 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 4886 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4887 | if (parity_smaller(bbio->raid_map[i], |
| 4888 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4889 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4890 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4891 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4892 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4893 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4894 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4895 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4896 | again = 1; |
| 4897 | } |
| 4898 | } |
| 4899 | } |
| 4900 | } |
| 4901 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4902 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 4903 | { |
| 4904 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4905 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4906 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4907 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4908 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4909 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4910 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4911 | /* |
| 4912 | * plus the raid_map, which includes both the tgt dev |
| 4913 | * and the stripes |
| 4914 | */ |
| 4915 | sizeof(u64) * (total_stripes), |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4916 | GFP_NOFS); |
| 4917 | if (!bbio) |
| 4918 | return NULL; |
| 4919 | |
| 4920 | atomic_set(&bbio->error, 0); |
| 4921 | atomic_set(&bbio->refs, 1); |
| 4922 | |
| 4923 | return bbio; |
| 4924 | } |
| 4925 | |
| 4926 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 4927 | { |
| 4928 | WARN_ON(!atomic_read(&bbio->refs)); |
| 4929 | atomic_inc(&bbio->refs); |
| 4930 | } |
| 4931 | |
| 4932 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 4933 | { |
| 4934 | if (!bbio) |
| 4935 | return; |
| 4936 | if (atomic_dec_and_test(&bbio->refs)) |
| 4937 | kfree(bbio); |
| 4938 | } |
| 4939 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4940 | 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] | 4941 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4942 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4943 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4944 | { |
| 4945 | struct extent_map *em; |
| 4946 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4947 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4948 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4949 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4950 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4951 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4952 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4953 | u64 stripe_nr_orig; |
| 4954 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4955 | u64 stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4956 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4957 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4958 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4959 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4960 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4961 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4962 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4963 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4964 | int dev_replace_is_ongoing = 0; |
| 4965 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4966 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4967 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4968 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4969 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4970 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4971 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4972 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4973 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4974 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4975 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4976 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4977 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4978 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4979 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4980 | if (em->start > logical || em->start + em->len < logical) { |
| 4981 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4982 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4983 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4984 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4985 | return -EINVAL; |
| 4986 | } |
| 4987 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4988 | map = (struct map_lookup *)em->bdev; |
| 4989 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4990 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4991 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4992 | stripe_nr = offset; |
| 4993 | /* |
| 4994 | * stripe_nr counts the total number of stripes we have to stride |
| 4995 | * to get to this block |
| 4996 | */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4997 | do_div(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4998 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4999 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5000 | BUG_ON(offset < stripe_offset); |
| 5001 | |
| 5002 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5003 | stripe_offset = offset - stripe_offset; |
| 5004 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5005 | /* 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] | 5006 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5007 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5008 | raid56_full_stripe_start = offset; |
| 5009 | |
| 5010 | /* allow a write of a full stripe, but make sure we don't |
| 5011 | * allow straddling of stripes |
| 5012 | */ |
| 5013 | do_div(raid56_full_stripe_start, full_stripe_len); |
| 5014 | raid56_full_stripe_start *= full_stripe_len; |
| 5015 | } |
| 5016 | |
| 5017 | if (rw & REQ_DISCARD) { |
| 5018 | /* we don't discard raid56 yet */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5019 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5020 | ret = -EOPNOTSUPP; |
| 5021 | goto out; |
| 5022 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5023 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5024 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5025 | u64 max_len; |
| 5026 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5027 | For other RAID types and for RAID[56] reads, just allow a single |
| 5028 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5029 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5030 | (rw & REQ_WRITE)) { |
| 5031 | max_len = stripe_len * nr_data_stripes(map) - |
| 5032 | (offset - raid56_full_stripe_start); |
| 5033 | } else { |
| 5034 | /* we limit the length of each bio to what fits in a stripe */ |
| 5035 | max_len = stripe_len - stripe_offset; |
| 5036 | } |
| 5037 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5038 | } else { |
| 5039 | *length = em->len - offset; |
| 5040 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5041 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5042 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5043 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5044 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5045 | goto out; |
| 5046 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5047 | btrfs_dev_replace_lock(dev_replace); |
| 5048 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5049 | if (!dev_replace_is_ongoing) |
| 5050 | btrfs_dev_replace_unlock(dev_replace); |
| 5051 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5052 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5053 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5054 | dev_replace->tgtdev != NULL) { |
| 5055 | /* |
| 5056 | * in dev-replace case, for repair case (that's the only |
| 5057 | * case where the mirror is selected explicitly when |
| 5058 | * calling btrfs_map_block), blocks left of the left cursor |
| 5059 | * can also be read from the target drive. |
| 5060 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5061 | * the last one to the array of stripes. For READ, it also |
| 5062 | * needs to be supported using the same mirror number. |
| 5063 | * If the requested block is not left of the left cursor, |
| 5064 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5065 | * returns one more in the dev-replace case. |
| 5066 | */ |
| 5067 | u64 tmp_length = *length; |
| 5068 | struct btrfs_bio *tmp_bbio = NULL; |
| 5069 | int tmp_num_stripes; |
| 5070 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5071 | int index_srcdev = 0; |
| 5072 | int found = 0; |
| 5073 | u64 physical_of_found = 0; |
| 5074 | |
| 5075 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5076 | logical, &tmp_length, &tmp_bbio, 0, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5077 | if (ret) { |
| 5078 | WARN_ON(tmp_bbio != NULL); |
| 5079 | goto out; |
| 5080 | } |
| 5081 | |
| 5082 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5083 | if (mirror_num > tmp_num_stripes) { |
| 5084 | /* |
| 5085 | * REQ_GET_READ_MIRRORS does not contain this |
| 5086 | * mirror, that means that the requested area |
| 5087 | * is not left of the left cursor |
| 5088 | */ |
| 5089 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5090 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5091 | goto out; |
| 5092 | } |
| 5093 | |
| 5094 | /* |
| 5095 | * process the rest of the function using the mirror_num |
| 5096 | * of the source drive. Therefore look it up first. |
| 5097 | * At the end, patch the device pointer to the one of the |
| 5098 | * target drive. |
| 5099 | */ |
| 5100 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5101 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5102 | /* |
| 5103 | * In case of DUP, in order to keep it |
| 5104 | * simple, only add the mirror with the |
| 5105 | * lowest physical address |
| 5106 | */ |
| 5107 | if (found && |
| 5108 | physical_of_found <= |
| 5109 | tmp_bbio->stripes[i].physical) |
| 5110 | continue; |
| 5111 | index_srcdev = i; |
| 5112 | found = 1; |
| 5113 | physical_of_found = |
| 5114 | tmp_bbio->stripes[i].physical; |
| 5115 | } |
| 5116 | } |
| 5117 | |
| 5118 | if (found) { |
| 5119 | mirror_num = index_srcdev + 1; |
| 5120 | patch_the_first_stripe_for_dev_replace = 1; |
| 5121 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5122 | } else { |
| 5123 | WARN_ON(1); |
| 5124 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5125 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5126 | goto out; |
| 5127 | } |
| 5128 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5129 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5130 | } else if (mirror_num > map->num_stripes) { |
| 5131 | mirror_num = 0; |
| 5132 | } |
| 5133 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5134 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5135 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5136 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5137 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5138 | do_div(stripe_nr_end, map->stripe_len); |
| 5139 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5140 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5141 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5142 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5143 | if (rw & REQ_DISCARD) |
| 5144 | num_stripes = min_t(u64, map->num_stripes, |
| 5145 | stripe_nr_end - stripe_nr_orig); |
| 5146 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5147 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5148 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5149 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5150 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5151 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5152 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5153 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5154 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5155 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5156 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5157 | current->pid % map->num_stripes, |
| 5158 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5159 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5160 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5161 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5162 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5163 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5164 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5165 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5166 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5167 | } else { |
| 5168 | mirror_num = 1; |
| 5169 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5170 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5171 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5172 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5173 | |
| 5174 | stripe_index = do_div(stripe_nr, factor); |
| 5175 | stripe_index *= map->sub_stripes; |
| 5176 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5177 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5178 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5179 | else if (rw & REQ_DISCARD) |
| 5180 | num_stripes = min_t(u64, map->sub_stripes * |
| 5181 | (stripe_nr_end - stripe_nr_orig), |
| 5182 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5183 | else if (mirror_num) |
| 5184 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5185 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5186 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5187 | stripe_index = find_live_mirror(fs_info, map, |
| 5188 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5189 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5190 | current->pid % map->sub_stripes, |
| 5191 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5192 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5193 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5194 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5195 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5196 | if (need_raid_map && |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5197 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5198 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5199 | /* push stripe_nr back to the start of the full stripe */ |
| 5200 | stripe_nr = raid56_full_stripe_start; |
Zhao Lei | 6de6565 | 2014-11-13 11:45:40 +0800 | [diff] [blame] | 5201 | do_div(stripe_nr, stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5202 | |
| 5203 | /* RAID[56] write or recovery. Return all stripes */ |
| 5204 | num_stripes = map->num_stripes; |
| 5205 | max_errors = nr_parity_stripes(map); |
| 5206 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5207 | *length = map->stripe_len; |
| 5208 | stripe_index = 0; |
| 5209 | stripe_offset = 0; |
| 5210 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5211 | u64 tmp; |
| 5212 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5213 | /* |
| 5214 | * Mirror #0 or #1 means the original data block. |
| 5215 | * Mirror #2 is RAID5 parity block. |
| 5216 | * Mirror #3 is RAID6 Q block. |
| 5217 | */ |
| 5218 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5219 | if (mirror_num > 1) |
| 5220 | stripe_index = nr_data_stripes(map) + |
| 5221 | mirror_num - 2; |
| 5222 | |
| 5223 | /* We distribute the parity blocks across stripes */ |
| 5224 | tmp = stripe_nr + stripe_index; |
| 5225 | stripe_index = do_div(tmp, map->num_stripes); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5226 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5227 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5228 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5229 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5230 | } else { |
| 5231 | /* |
| 5232 | * after this do_div call, stripe_nr is the number of stripes |
| 5233 | * on this device we have to walk to find the data, and |
| 5234 | * stripe_index is the number of our device in the stripe array |
| 5235 | */ |
| 5236 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5237 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5238 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5239 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5240 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5241 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5242 | if (dev_replace_is_ongoing) { |
| 5243 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5244 | num_alloc_stripes <<= 1; |
| 5245 | if (rw & REQ_GET_READ_MIRRORS) |
| 5246 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5247 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5248 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5249 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5250 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5251 | if (!bbio) { |
| 5252 | ret = -ENOMEM; |
| 5253 | goto out; |
| 5254 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5255 | if (dev_replace_is_ongoing) |
| 5256 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5257 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5258 | /* build raid_map */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5259 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5260 | need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5261 | mirror_num > 1)) { |
| 5262 | u64 tmp; |
| 5263 | int i, rot; |
| 5264 | |
| 5265 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5266 | sizeof(struct btrfs_bio_stripe) * |
| 5267 | num_alloc_stripes + |
| 5268 | sizeof(int) * tgtdev_indexes); |
| 5269 | |
| 5270 | /* Work out the disk rotation on this stripe-set */ |
| 5271 | tmp = stripe_nr; |
| 5272 | rot = do_div(tmp, num_stripes); |
| 5273 | |
| 5274 | /* Fill in the logical address of each stripe */ |
| 5275 | tmp = stripe_nr * nr_data_stripes(map); |
| 5276 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5277 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5278 | em->start + (tmp + i) * map->stripe_len; |
| 5279 | |
| 5280 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5281 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5282 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5283 | RAID6_Q_STRIPE; |
| 5284 | } |
| 5285 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5286 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5287 | int factor = 0; |
| 5288 | int sub_stripes = 0; |
| 5289 | u64 stripes_per_dev = 0; |
| 5290 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5291 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5292 | |
| 5293 | if (map->type & |
| 5294 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5295 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5296 | sub_stripes = 1; |
| 5297 | else |
| 5298 | sub_stripes = map->sub_stripes; |
| 5299 | |
| 5300 | factor = map->num_stripes / sub_stripes; |
| 5301 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5302 | stripe_nr_orig, |
| 5303 | factor, |
| 5304 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5305 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5306 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5307 | } |
| 5308 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5309 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5310 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5311 | map->stripes[stripe_index].physical + |
| 5312 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5313 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5314 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5315 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5316 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5317 | bbio->stripes[i].length = stripes_per_dev * |
| 5318 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5319 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5320 | if (i / sub_stripes < remaining_stripes) |
| 5321 | bbio->stripes[i].length += |
| 5322 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5323 | |
| 5324 | /* |
| 5325 | * Special for the first stripe and |
| 5326 | * the last stripe: |
| 5327 | * |
| 5328 | * |-------|...|-------| |
| 5329 | * |----------| |
| 5330 | * off end_off |
| 5331 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5332 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5333 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5334 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5335 | |
| 5336 | if (stripe_index >= last_stripe && |
| 5337 | stripe_index <= (last_stripe + |
| 5338 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5339 | bbio->stripes[i].length -= |
| 5340 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5341 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5342 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5343 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5344 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5345 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5346 | |
| 5347 | stripe_index++; |
| 5348 | if (stripe_index == map->num_stripes) { |
| 5349 | /* This could only happen for RAID0/10 */ |
| 5350 | stripe_index = 0; |
| 5351 | stripe_nr++; |
| 5352 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5353 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5354 | } else { |
| 5355 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5356 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5357 | map->stripes[stripe_index].physical + |
| 5358 | stripe_offset + |
| 5359 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5360 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5361 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5362 | stripe_index++; |
| 5363 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5364 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5365 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5366 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5367 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5368 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5369 | if (bbio->raid_map) |
| 5370 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5371 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5372 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5373 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5374 | dev_replace->tgtdev != NULL) { |
| 5375 | int index_where_to_add; |
| 5376 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5377 | |
| 5378 | /* |
| 5379 | * duplicate the write operations while the dev replace |
| 5380 | * procedure is running. Since the copying of the old disk |
| 5381 | * to the new disk takes place at run time while the |
| 5382 | * filesystem is mounted writable, the regular write |
| 5383 | * operations to the old disk have to be duplicated to go |
| 5384 | * to the new disk as well. |
| 5385 | * Note that device->missing is handled by the caller, and |
| 5386 | * that the write to the old disk is already set up in the |
| 5387 | * stripes array. |
| 5388 | */ |
| 5389 | index_where_to_add = num_stripes; |
| 5390 | for (i = 0; i < num_stripes; i++) { |
| 5391 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5392 | /* write to new disk, too */ |
| 5393 | struct btrfs_bio_stripe *new = |
| 5394 | bbio->stripes + index_where_to_add; |
| 5395 | struct btrfs_bio_stripe *old = |
| 5396 | bbio->stripes + i; |
| 5397 | |
| 5398 | new->physical = old->physical; |
| 5399 | new->length = old->length; |
| 5400 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5401 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5402 | index_where_to_add++; |
| 5403 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5404 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5405 | } |
| 5406 | } |
| 5407 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5408 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5409 | dev_replace->tgtdev != NULL) { |
| 5410 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5411 | int index_srcdev = 0; |
| 5412 | int found = 0; |
| 5413 | u64 physical_of_found = 0; |
| 5414 | |
| 5415 | /* |
| 5416 | * During the dev-replace procedure, the target drive can |
| 5417 | * also be used to read data in case it is needed to repair |
| 5418 | * a corrupt block elsewhere. This is possible if the |
| 5419 | * requested area is left of the left cursor. In this area, |
| 5420 | * the target drive is a full copy of the source drive. |
| 5421 | */ |
| 5422 | for (i = 0; i < num_stripes; i++) { |
| 5423 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5424 | /* |
| 5425 | * In case of DUP, in order to keep it |
| 5426 | * simple, only add the mirror with the |
| 5427 | * lowest physical address |
| 5428 | */ |
| 5429 | if (found && |
| 5430 | physical_of_found <= |
| 5431 | bbio->stripes[i].physical) |
| 5432 | continue; |
| 5433 | index_srcdev = i; |
| 5434 | found = 1; |
| 5435 | physical_of_found = bbio->stripes[i].physical; |
| 5436 | } |
| 5437 | } |
| 5438 | if (found) { |
| 5439 | u64 length = map->stripe_len; |
| 5440 | |
| 5441 | if (physical_of_found + length <= |
| 5442 | dev_replace->cursor_left) { |
| 5443 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5444 | bbio->stripes + num_stripes; |
| 5445 | |
| 5446 | tgtdev_stripe->physical = physical_of_found; |
| 5447 | tgtdev_stripe->length = |
| 5448 | bbio->stripes[index_srcdev].length; |
| 5449 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5450 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5451 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5452 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5453 | num_stripes++; |
| 5454 | } |
| 5455 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5456 | } |
| 5457 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5458 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5459 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5460 | bbio->num_stripes = num_stripes; |
| 5461 | bbio->max_errors = max_errors; |
| 5462 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5463 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5464 | |
| 5465 | /* |
| 5466 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5467 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5468 | * available as a mirror |
| 5469 | */ |
| 5470 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5471 | WARN_ON(num_stripes > 1); |
| 5472 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5473 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5474 | bbio->mirror_num = map->num_stripes + 1; |
| 5475 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5476 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5477 | if (dev_replace_is_ongoing) |
| 5478 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5479 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5480 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5481 | } |
| 5482 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5483 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5484 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5485 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5486 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5487 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5488 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5489 | } |
| 5490 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5491 | /* For Scrub/replace */ |
| 5492 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5493 | u64 logical, u64 *length, |
| 5494 | struct btrfs_bio **bbio_ret, int mirror_num, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5495 | int need_raid_map) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5496 | { |
| 5497 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5498 | mirror_num, need_raid_map); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5499 | } |
| 5500 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5501 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5502 | u64 chunk_start, u64 physical, u64 devid, |
| 5503 | u64 **logical, int *naddrs, int *stripe_len) |
| 5504 | { |
| 5505 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5506 | struct extent_map *em; |
| 5507 | struct map_lookup *map; |
| 5508 | u64 *buf; |
| 5509 | u64 bytenr; |
| 5510 | u64 length; |
| 5511 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5512 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5513 | int i, j, nr = 0; |
| 5514 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5515 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5516 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5517 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5518 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5519 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5520 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5521 | chunk_start); |
| 5522 | return -EIO; |
| 5523 | } |
| 5524 | |
| 5525 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5526 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5527 | em->start, chunk_start); |
| 5528 | free_extent_map(em); |
| 5529 | return -EIO; |
| 5530 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5531 | map = (struct map_lookup *)em->bdev; |
| 5532 | |
| 5533 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5534 | rmap_len = map->stripe_len; |
| 5535 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5536 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5537 | do_div(length, map->num_stripes / map->sub_stripes); |
| 5538 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5539 | do_div(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5540 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5541 | do_div(length, nr_data_stripes(map)); |
| 5542 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5543 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5544 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame^] | 5545 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5546 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5547 | |
| 5548 | for (i = 0; i < map->num_stripes; i++) { |
| 5549 | if (devid && map->stripes[i].dev->devid != devid) |
| 5550 | continue; |
| 5551 | if (map->stripes[i].physical > physical || |
| 5552 | map->stripes[i].physical + length <= physical) |
| 5553 | continue; |
| 5554 | |
| 5555 | stripe_nr = physical - map->stripes[i].physical; |
| 5556 | do_div(stripe_nr, map->stripe_len); |
| 5557 | |
| 5558 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5559 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 5560 | do_div(stripe_nr, map->sub_stripes); |
| 5561 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5562 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5563 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5564 | * Alternatively, just use rmap_len below instead of |
| 5565 | * map->stripe_len */ |
| 5566 | |
| 5567 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5568 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5569 | for (j = 0; j < nr; j++) { |
| 5570 | if (buf[j] == bytenr) |
| 5571 | break; |
| 5572 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5573 | if (j == nr) { |
| 5574 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5575 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5576 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5577 | } |
| 5578 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5579 | *logical = buf; |
| 5580 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5581 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5582 | |
| 5583 | free_extent_map(em); |
| 5584 | return 0; |
| 5585 | } |
| 5586 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5587 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5588 | { |
| 5589 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5590 | bio_endio_nodec(bio, err); |
| 5591 | else |
| 5592 | bio_endio(bio, err); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5593 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5594 | } |
| 5595 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5596 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5597 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5598 | struct btrfs_bio *bbio = bio->bi_private; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5599 | struct btrfs_device *dev = bbio->stripes[0].dev; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5600 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5601 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5602 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5603 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5604 | if (err == -EIO || err == -EREMOTEIO) { |
| 5605 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5606 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5607 | |
| 5608 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5609 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5610 | if (dev->bdev) { |
| 5611 | if (bio->bi_rw & WRITE) |
| 5612 | btrfs_dev_stat_inc(dev, |
| 5613 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5614 | else |
| 5615 | btrfs_dev_stat_inc(dev, |
| 5616 | BTRFS_DEV_STAT_READ_ERRS); |
| 5617 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5618 | btrfs_dev_stat_inc(dev, |
| 5619 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5620 | btrfs_dev_stat_print_on_error(dev); |
| 5621 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5622 | } |
| 5623 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5624 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5625 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5626 | is_orig_bio = 1; |
| 5627 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5628 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5629 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5630 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5631 | if (!is_orig_bio) { |
| 5632 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5633 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5634 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5635 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5636 | bio->bi_private = bbio->private; |
| 5637 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5638 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5639 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5640 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5641 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5642 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5643 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5644 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5645 | /* |
| 5646 | * this bio is actually up to date, we didn't |
| 5647 | * go over the max number of errors |
| 5648 | */ |
| 5649 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5650 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5651 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5652 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5653 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5654 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5655 | bio_put(bio); |
| 5656 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5657 | } |
| 5658 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5659 | /* |
| 5660 | * see run_scheduled_bios for a description of why bios are collected for |
| 5661 | * async submit. |
| 5662 | * |
| 5663 | * This will add one bio to the pending list for a device and make sure |
| 5664 | * the work struct is scheduled. |
| 5665 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5666 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5667 | struct btrfs_device *device, |
| 5668 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5669 | { |
| 5670 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5671 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5672 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5673 | if (device->missing || !device->bdev) { |
| 5674 | bio_endio(bio, -EIO); |
| 5675 | return; |
| 5676 | } |
| 5677 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5678 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5679 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5680 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5681 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5682 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5683 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5684 | } |
| 5685 | |
| 5686 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5687 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5688 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5689 | * made progress against dirty pages when we've really just put it |
| 5690 | * on a queue for later |
| 5691 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5692 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5693 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5694 | bio->bi_next = NULL; |
| 5695 | bio->bi_rw |= rw; |
| 5696 | |
| 5697 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5698 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5699 | pending_bios = &device->pending_sync_bios; |
| 5700 | else |
| 5701 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5702 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5703 | if (pending_bios->tail) |
| 5704 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5705 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5706 | pending_bios->tail = bio; |
| 5707 | if (!pending_bios->head) |
| 5708 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5709 | if (device->running_pending) |
| 5710 | should_queue = 0; |
| 5711 | |
| 5712 | spin_unlock(&device->io_lock); |
| 5713 | |
| 5714 | if (should_queue) |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5715 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5716 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5717 | } |
| 5718 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5719 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5720 | sector_t sector) |
| 5721 | { |
| 5722 | struct bio_vec *prev; |
| 5723 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5724 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5725 | struct bvec_merge_data bvm = { |
| 5726 | .bi_bdev = bdev, |
| 5727 | .bi_sector = sector, |
| 5728 | .bi_rw = bio->bi_rw, |
| 5729 | }; |
| 5730 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5731 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5732 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5733 | |
| 5734 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5735 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5736 | return 0; |
| 5737 | |
| 5738 | if (!q->merge_bvec_fn) |
| 5739 | return 1; |
| 5740 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5741 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5742 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5743 | return 0; |
| 5744 | return 1; |
| 5745 | } |
| 5746 | |
| 5747 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5748 | struct bio *bio, u64 physical, int dev_nr, |
| 5749 | int rw, int async) |
| 5750 | { |
| 5751 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5752 | |
| 5753 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5754 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5755 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5756 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5757 | #ifdef DEBUG |
| 5758 | { |
| 5759 | struct rcu_string *name; |
| 5760 | |
| 5761 | rcu_read_lock(); |
| 5762 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5763 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5764 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5765 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5766 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5767 | rcu_read_unlock(); |
| 5768 | } |
| 5769 | #endif |
| 5770 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5771 | |
| 5772 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5773 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5774 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5775 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5776 | else |
| 5777 | btrfsic_submit_bio(rw, bio); |
| 5778 | } |
| 5779 | |
| 5780 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5781 | struct bio *first_bio, struct btrfs_device *dev, |
| 5782 | int dev_nr, int rw, int async) |
| 5783 | { |
| 5784 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5785 | struct bio *bio; |
| 5786 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5787 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5788 | |
| 5789 | again: |
| 5790 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5791 | if (!bio) |
| 5792 | return -ENOMEM; |
| 5793 | |
| 5794 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5795 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5796 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5797 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5798 | |
| 5799 | atomic_inc(&bbio->stripes_pending); |
| 5800 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5801 | rw, async); |
| 5802 | physical += len; |
| 5803 | goto again; |
| 5804 | } |
| 5805 | bvec++; |
| 5806 | } |
| 5807 | |
| 5808 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5809 | return 0; |
| 5810 | } |
| 5811 | |
| 5812 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5813 | { |
| 5814 | atomic_inc(&bbio->error); |
| 5815 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5816 | /* Shoud be the original bio. */ |
| 5817 | WARN_ON(bio != bbio->orig_bio); |
| 5818 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5819 | bio->bi_private = bbio->private; |
| 5820 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5821 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5822 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5823 | |
| 5824 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5825 | } |
| 5826 | } |
| 5827 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5828 | 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] | 5829 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5830 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5831 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5832 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5833 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5834 | u64 length = 0; |
| 5835 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5836 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5837 | int dev_nr = 0; |
| 5838 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5839 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5840 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5841 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5842 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5843 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5844 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5845 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5846 | mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5847 | if (ret) { |
| 5848 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5849 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5850 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5851 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5852 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5853 | bbio->orig_bio = first_bio; |
| 5854 | bbio->private = first_bio->bi_private; |
| 5855 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5856 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5857 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5858 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5859 | if (bbio->raid_map) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5860 | /* In this case, map_length has been set to the length of |
| 5861 | a single stripe; not the whole write */ |
| 5862 | if (rw & WRITE) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5863 | ret = raid56_parity_write(root, bio, bbio, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5864 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5865 | ret = raid56_parity_recover(root, bio, bbio, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5866 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5867 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5868 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5869 | btrfs_bio_counter_dec(root->fs_info); |
| 5870 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5871 | } |
| 5872 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5873 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5874 | 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] | 5875 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5876 | BUG(); |
| 5877 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5878 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5879 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5880 | dev = bbio->stripes[dev_nr].dev; |
| 5881 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5882 | bbio_error(bbio, first_bio, logical); |
| 5883 | dev_nr++; |
| 5884 | continue; |
| 5885 | } |
| 5886 | |
| 5887 | /* |
| 5888 | * Check and see if we're ok with this bio based on it's size |
| 5889 | * and offset with the given device. |
| 5890 | */ |
| 5891 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5892 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5893 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5894 | dev_nr, rw, async_submit); |
| 5895 | BUG_ON(ret); |
| 5896 | dev_nr++; |
| 5897 | continue; |
| 5898 | } |
| 5899 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5900 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5901 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5902 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5903 | } else { |
| 5904 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5905 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5906 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5907 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5908 | submit_stripe_bio(root, bbio, bio, |
| 5909 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5910 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5911 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5912 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5913 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5914 | return 0; |
| 5915 | } |
| 5916 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5917 | 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] | 5918 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5919 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5920 | struct btrfs_device *device; |
| 5921 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5922 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5923 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5924 | while (cur_devices) { |
| 5925 | if (!fsid || |
| 5926 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5927 | device = __find_device(&cur_devices->devices, |
| 5928 | devid, uuid); |
| 5929 | if (device) |
| 5930 | return device; |
| 5931 | } |
| 5932 | cur_devices = cur_devices->seed; |
| 5933 | } |
| 5934 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5935 | } |
| 5936 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5937 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 5938 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5939 | u64 devid, u8 *dev_uuid) |
| 5940 | { |
| 5941 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5942 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5943 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 5944 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5945 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5946 | |
| 5947 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5948 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5949 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5950 | |
| 5951 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5952 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5953 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5954 | return device; |
| 5955 | } |
| 5956 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5957 | /** |
| 5958 | * btrfs_alloc_device - allocate struct btrfs_device |
| 5959 | * @fs_info: used only for generating a new devid, can be NULL if |
| 5960 | * devid is provided (i.e. @devid != NULL). |
| 5961 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 5962 | * is generated. |
| 5963 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 5964 | * is generated. |
| 5965 | * |
| 5966 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 5967 | * on error. Returned struct is not linked onto any lists and can be |
| 5968 | * destroyed with kfree() right away. |
| 5969 | */ |
| 5970 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 5971 | const u64 *devid, |
| 5972 | const u8 *uuid) |
| 5973 | { |
| 5974 | struct btrfs_device *dev; |
| 5975 | u64 tmp; |
| 5976 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5977 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5978 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5979 | |
| 5980 | dev = __alloc_device(); |
| 5981 | if (IS_ERR(dev)) |
| 5982 | return dev; |
| 5983 | |
| 5984 | if (devid) |
| 5985 | tmp = *devid; |
| 5986 | else { |
| 5987 | int ret; |
| 5988 | |
| 5989 | ret = find_next_devid(fs_info, &tmp); |
| 5990 | if (ret) { |
| 5991 | kfree(dev); |
| 5992 | return ERR_PTR(ret); |
| 5993 | } |
| 5994 | } |
| 5995 | dev->devid = tmp; |
| 5996 | |
| 5997 | if (uuid) |
| 5998 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 5999 | else |
| 6000 | generate_random_uuid(dev->uuid); |
| 6001 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6002 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6003 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6004 | |
| 6005 | return dev; |
| 6006 | } |
| 6007 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6008 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 6009 | struct extent_buffer *leaf, |
| 6010 | struct btrfs_chunk *chunk) |
| 6011 | { |
| 6012 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6013 | struct map_lookup *map; |
| 6014 | struct extent_map *em; |
| 6015 | u64 logical; |
| 6016 | u64 length; |
| 6017 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6018 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6019 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6020 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6021 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6022 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6023 | logical = key->offset; |
| 6024 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6025 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6026 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6027 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6028 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6029 | |
| 6030 | /* already mapped? */ |
| 6031 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6032 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6033 | return 0; |
| 6034 | } else if (em) { |
| 6035 | free_extent_map(em); |
| 6036 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6037 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6038 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6039 | if (!em) |
| 6040 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6041 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6042 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6043 | if (!map) { |
| 6044 | free_extent_map(em); |
| 6045 | return -ENOMEM; |
| 6046 | } |
| 6047 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6048 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6049 | em->bdev = (struct block_device *)map; |
| 6050 | em->start = logical; |
| 6051 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6052 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6053 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6054 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6055 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6056 | map->num_stripes = num_stripes; |
| 6057 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6058 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6059 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6060 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6061 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6062 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6063 | for (i = 0; i < num_stripes; i++) { |
| 6064 | map->stripes[i].physical = |
| 6065 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6066 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6067 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6068 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6069 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6070 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6071 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6072 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6073 | free_extent_map(em); |
| 6074 | return -EIO; |
| 6075 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6076 | if (!map->stripes[i].dev) { |
| 6077 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6078 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6079 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6080 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6081 | free_extent_map(em); |
| 6082 | return -EIO; |
| 6083 | } |
| 6084 | } |
| 6085 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6086 | } |
| 6087 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6088 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6089 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6090 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6091 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6092 | free_extent_map(em); |
| 6093 | |
| 6094 | return 0; |
| 6095 | } |
| 6096 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6097 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6098 | struct btrfs_dev_item *dev_item, |
| 6099 | struct btrfs_device *device) |
| 6100 | { |
| 6101 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6102 | |
| 6103 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6104 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6105 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6106 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6107 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6108 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6109 | device->type = btrfs_device_type(leaf, dev_item); |
| 6110 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6111 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6112 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6113 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6114 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6115 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6116 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6117 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6118 | } |
| 6119 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6120 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6121 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6122 | { |
| 6123 | struct btrfs_fs_devices *fs_devices; |
| 6124 | int ret; |
| 6125 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6126 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6127 | |
| 6128 | fs_devices = root->fs_info->fs_devices->seed; |
| 6129 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6130 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6131 | return fs_devices; |
| 6132 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6133 | fs_devices = fs_devices->seed; |
| 6134 | } |
| 6135 | |
| 6136 | fs_devices = find_fsid(fsid); |
| 6137 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6138 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6139 | return ERR_PTR(-ENOENT); |
| 6140 | |
| 6141 | fs_devices = alloc_fs_devices(fsid); |
| 6142 | if (IS_ERR(fs_devices)) |
| 6143 | return fs_devices; |
| 6144 | |
| 6145 | fs_devices->seeding = 1; |
| 6146 | fs_devices->opened = 1; |
| 6147 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6148 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6149 | |
| 6150 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6151 | if (IS_ERR(fs_devices)) |
| 6152 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6153 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6154 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6155 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6156 | if (ret) { |
| 6157 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6158 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6159 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6160 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6161 | |
| 6162 | if (!fs_devices->seeding) { |
| 6163 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6164 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6165 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6166 | goto out; |
| 6167 | } |
| 6168 | |
| 6169 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6170 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6171 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6172 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6173 | } |
| 6174 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6175 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6176 | struct extent_buffer *leaf, |
| 6177 | struct btrfs_dev_item *dev_item) |
| 6178 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6179 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6180 | struct btrfs_device *device; |
| 6181 | u64 devid; |
| 6182 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6183 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6184 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6185 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6186 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6187 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6188 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6189 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6190 | BTRFS_UUID_SIZE); |
| 6191 | |
| 6192 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6193 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6194 | if (IS_ERR(fs_devices)) |
| 6195 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6196 | } |
| 6197 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6198 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6199 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6200 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6201 | return -EIO; |
| 6202 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6203 | btrfs_warn(root->fs_info, "devid %llu missing", devid); |
| 6204 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6205 | if (!device) |
| 6206 | return -ENOMEM; |
| 6207 | } else { |
| 6208 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6209 | return -EIO; |
| 6210 | |
| 6211 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6212 | /* |
| 6213 | * this happens when a device that was properly setup |
| 6214 | * in the device info lists suddenly goes bad. |
| 6215 | * device->bdev is NULL, and so we have to set |
| 6216 | * device->missing to one here |
| 6217 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6218 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6219 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6220 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6221 | |
| 6222 | /* Move the device to its own fs_devices */ |
| 6223 | if (device->fs_devices != fs_devices) { |
| 6224 | ASSERT(device->missing); |
| 6225 | |
| 6226 | list_move(&device->dev_list, &fs_devices->devices); |
| 6227 | device->fs_devices->num_devices--; |
| 6228 | fs_devices->num_devices++; |
| 6229 | |
| 6230 | device->fs_devices->missing_devices--; |
| 6231 | fs_devices->missing_devices++; |
| 6232 | |
| 6233 | device->fs_devices = fs_devices; |
| 6234 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6238 | BUG_ON(device->writeable); |
| 6239 | if (device->generation != |
| 6240 | btrfs_device_generation(leaf, dev_item)) |
| 6241 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6242 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6243 | |
| 6244 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6245 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6246 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6247 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6248 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6249 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6250 | device->bytes_used; |
| 6251 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6252 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6253 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6254 | return ret; |
| 6255 | } |
| 6256 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6257 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6258 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6259 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6260 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6261 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6262 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6263 | u8 *array_ptr; |
| 6264 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6265 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6266 | u32 num_stripes; |
| 6267 | u32 array_size; |
| 6268 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6269 | u32 cur_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6270 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6271 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6272 | ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize); |
| 6273 | /* |
| 6274 | * This will create extent buffer of nodesize, superblock size is |
| 6275 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6276 | * overallocate but we can keep it as-is, only the first page is used. |
| 6277 | */ |
| 6278 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6279 | if (!sb) |
| 6280 | return -ENOMEM; |
| 6281 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6282 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6283 | /* |
| 6284 | * The sb extent buffer is artifical and just used to read the system array. |
| 6285 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6286 | * pages up-to-date when the page is larger: extent does not cover the |
| 6287 | * whole page and consequently check_page_uptodate does not find all |
| 6288 | * the page's extents up-to-date (the hole beyond sb), |
| 6289 | * write_extent_buffer then triggers a WARN_ON. |
| 6290 | * |
| 6291 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6292 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6293 | * to silence the warning eg. on PowerPC 64. |
| 6294 | */ |
| 6295 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6296 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6297 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6298 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6299 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6300 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6301 | array_ptr = super_copy->sys_chunk_array; |
| 6302 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6303 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6304 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6305 | while (cur_offset < array_size) { |
| 6306 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6307 | len = sizeof(*disk_key); |
| 6308 | if (cur_offset + len > array_size) |
| 6309 | goto out_short_read; |
| 6310 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6311 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6312 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6313 | array_ptr += len; |
| 6314 | sb_array_offset += len; |
| 6315 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6316 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6317 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6318 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6319 | /* |
| 6320 | * At least one btrfs_chunk with one stripe must be |
| 6321 | * present, exact stripe count check comes afterwards |
| 6322 | */ |
| 6323 | len = btrfs_chunk_item_size(1); |
| 6324 | if (cur_offset + len > array_size) |
| 6325 | goto out_short_read; |
| 6326 | |
| 6327 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6328 | len = btrfs_chunk_item_size(num_stripes); |
| 6329 | if (cur_offset + len > array_size) |
| 6330 | goto out_short_read; |
| 6331 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6332 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6333 | if (ret) |
| 6334 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6335 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6336 | ret = -EIO; |
| 6337 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6338 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6339 | array_ptr += len; |
| 6340 | sb_array_offset += len; |
| 6341 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6342 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6343 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6344 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6345 | |
| 6346 | out_short_read: |
| 6347 | printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n", |
| 6348 | len, cur_offset); |
| 6349 | free_extent_buffer(sb); |
| 6350 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6351 | } |
| 6352 | |
| 6353 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6354 | { |
| 6355 | struct btrfs_path *path; |
| 6356 | struct extent_buffer *leaf; |
| 6357 | struct btrfs_key key; |
| 6358 | struct btrfs_key found_key; |
| 6359 | int ret; |
| 6360 | int slot; |
| 6361 | |
| 6362 | root = root->fs_info->chunk_root; |
| 6363 | |
| 6364 | path = btrfs_alloc_path(); |
| 6365 | if (!path) |
| 6366 | return -ENOMEM; |
| 6367 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6368 | mutex_lock(&uuid_mutex); |
| 6369 | lock_chunks(root); |
| 6370 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6371 | /* |
| 6372 | * Read all device items, and then all the chunk items. All |
| 6373 | * device items are found before any chunk item (their object id |
| 6374 | * is smaller than the lowest possible object id for a chunk |
| 6375 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6376 | */ |
| 6377 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6378 | key.offset = 0; |
| 6379 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6380 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6381 | if (ret < 0) |
| 6382 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6383 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6384 | leaf = path->nodes[0]; |
| 6385 | slot = path->slots[0]; |
| 6386 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6387 | ret = btrfs_next_leaf(root, path); |
| 6388 | if (ret == 0) |
| 6389 | continue; |
| 6390 | if (ret < 0) |
| 6391 | goto error; |
| 6392 | break; |
| 6393 | } |
| 6394 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6395 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6396 | struct btrfs_dev_item *dev_item; |
| 6397 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6398 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6399 | ret = read_one_dev(root, leaf, dev_item); |
| 6400 | if (ret) |
| 6401 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6402 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6403 | struct btrfs_chunk *chunk; |
| 6404 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6405 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6406 | if (ret) |
| 6407 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6408 | } |
| 6409 | path->slots[0]++; |
| 6410 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6411 | ret = 0; |
| 6412 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6413 | unlock_chunks(root); |
| 6414 | mutex_unlock(&uuid_mutex); |
| 6415 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6416 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6417 | return ret; |
| 6418 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6419 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6420 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6421 | { |
| 6422 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6423 | struct btrfs_device *device; |
| 6424 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6425 | while (fs_devices) { |
| 6426 | mutex_lock(&fs_devices->device_list_mutex); |
| 6427 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6428 | device->dev_root = fs_info->dev_root; |
| 6429 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6430 | |
| 6431 | fs_devices = fs_devices->seed; |
| 6432 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6435 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6436 | { |
| 6437 | int i; |
| 6438 | |
| 6439 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6440 | btrfs_dev_stat_reset(dev, i); |
| 6441 | } |
| 6442 | |
| 6443 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6444 | { |
| 6445 | struct btrfs_key key; |
| 6446 | struct btrfs_key found_key; |
| 6447 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6448 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6449 | struct extent_buffer *eb; |
| 6450 | int slot; |
| 6451 | int ret = 0; |
| 6452 | struct btrfs_device *device; |
| 6453 | struct btrfs_path *path = NULL; |
| 6454 | int i; |
| 6455 | |
| 6456 | path = btrfs_alloc_path(); |
| 6457 | if (!path) { |
| 6458 | ret = -ENOMEM; |
| 6459 | goto out; |
| 6460 | } |
| 6461 | |
| 6462 | mutex_lock(&fs_devices->device_list_mutex); |
| 6463 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6464 | int item_size; |
| 6465 | struct btrfs_dev_stats_item *ptr; |
| 6466 | |
| 6467 | key.objectid = 0; |
| 6468 | key.type = BTRFS_DEV_STATS_KEY; |
| 6469 | key.offset = device->devid; |
| 6470 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6471 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6472 | __btrfs_reset_dev_stats(device); |
| 6473 | device->dev_stats_valid = 1; |
| 6474 | btrfs_release_path(path); |
| 6475 | continue; |
| 6476 | } |
| 6477 | slot = path->slots[0]; |
| 6478 | eb = path->nodes[0]; |
| 6479 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6480 | item_size = btrfs_item_size_nr(eb, slot); |
| 6481 | |
| 6482 | ptr = btrfs_item_ptr(eb, slot, |
| 6483 | struct btrfs_dev_stats_item); |
| 6484 | |
| 6485 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6486 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6487 | btrfs_dev_stat_set(device, i, |
| 6488 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6489 | else |
| 6490 | btrfs_dev_stat_reset(device, i); |
| 6491 | } |
| 6492 | |
| 6493 | device->dev_stats_valid = 1; |
| 6494 | btrfs_dev_stat_print_on_load(device); |
| 6495 | btrfs_release_path(path); |
| 6496 | } |
| 6497 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6498 | |
| 6499 | out: |
| 6500 | btrfs_free_path(path); |
| 6501 | return ret < 0 ? ret : 0; |
| 6502 | } |
| 6503 | |
| 6504 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6505 | struct btrfs_root *dev_root, |
| 6506 | struct btrfs_device *device) |
| 6507 | { |
| 6508 | struct btrfs_path *path; |
| 6509 | struct btrfs_key key; |
| 6510 | struct extent_buffer *eb; |
| 6511 | struct btrfs_dev_stats_item *ptr; |
| 6512 | int ret; |
| 6513 | int i; |
| 6514 | |
| 6515 | key.objectid = 0; |
| 6516 | key.type = BTRFS_DEV_STATS_KEY; |
| 6517 | key.offset = device->devid; |
| 6518 | |
| 6519 | path = btrfs_alloc_path(); |
| 6520 | BUG_ON(!path); |
| 6521 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6522 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6523 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6524 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6525 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6526 | goto out; |
| 6527 | } |
| 6528 | |
| 6529 | if (ret == 0 && |
| 6530 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6531 | /* need to delete old one and insert a new one */ |
| 6532 | ret = btrfs_del_item(trans, dev_root, path); |
| 6533 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6534 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6535 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6536 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6537 | goto out; |
| 6538 | } |
| 6539 | ret = 1; |
| 6540 | } |
| 6541 | |
| 6542 | if (ret == 1) { |
| 6543 | /* need to insert a new item */ |
| 6544 | btrfs_release_path(path); |
| 6545 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6546 | &key, sizeof(*ptr)); |
| 6547 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6548 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6549 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6550 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6551 | goto out; |
| 6552 | } |
| 6553 | } |
| 6554 | |
| 6555 | eb = path->nodes[0]; |
| 6556 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6557 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6558 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6559 | btrfs_dev_stat_read(device, i)); |
| 6560 | btrfs_mark_buffer_dirty(eb); |
| 6561 | |
| 6562 | out: |
| 6563 | btrfs_free_path(path); |
| 6564 | return ret; |
| 6565 | } |
| 6566 | |
| 6567 | /* |
| 6568 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6569 | */ |
| 6570 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6571 | struct btrfs_fs_info *fs_info) |
| 6572 | { |
| 6573 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6574 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6575 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6576 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6577 | int ret = 0; |
| 6578 | |
| 6579 | mutex_lock(&fs_devices->device_list_mutex); |
| 6580 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6581 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6582 | continue; |
| 6583 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6584 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6585 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6586 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6587 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6588 | } |
| 6589 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6590 | |
| 6591 | return ret; |
| 6592 | } |
| 6593 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6594 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6595 | { |
| 6596 | btrfs_dev_stat_inc(dev, index); |
| 6597 | btrfs_dev_stat_print_on_error(dev); |
| 6598 | } |
| 6599 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6600 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6601 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6602 | if (!dev->dev_stats_valid) |
| 6603 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6604 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6605 | "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] | 6606 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6607 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6608 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6609 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6610 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6611 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6612 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6613 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6614 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6615 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6616 | int i; |
| 6617 | |
| 6618 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6619 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6620 | break; |
| 6621 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6622 | return; /* all values == 0, suppress message */ |
| 6623 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6624 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6625 | "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] | 6626 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6627 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6628 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6629 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6630 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6631 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6632 | } |
| 6633 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6634 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6635 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6636 | { |
| 6637 | struct btrfs_device *dev; |
| 6638 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6639 | int i; |
| 6640 | |
| 6641 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6642 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6643 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6644 | |
| 6645 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6646 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6647 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6648 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6649 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6650 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6651 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6652 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6653 | if (stats->nr_items > i) |
| 6654 | stats->values[i] = |
| 6655 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6656 | else |
| 6657 | btrfs_dev_stat_reset(dev, i); |
| 6658 | } |
| 6659 | } else { |
| 6660 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6661 | if (stats->nr_items > i) |
| 6662 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6663 | } |
| 6664 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6665 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6666 | return 0; |
| 6667 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6668 | |
| 6669 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6670 | { |
| 6671 | struct buffer_head *bh; |
| 6672 | struct btrfs_super_block *disk_super; |
| 6673 | |
| 6674 | bh = btrfs_read_dev_super(device->bdev); |
| 6675 | if (!bh) |
| 6676 | return -EINVAL; |
| 6677 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6678 | |
| 6679 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6680 | set_buffer_dirty(bh); |
| 6681 | sync_dirty_buffer(bh); |
| 6682 | brelse(bh); |
| 6683 | |
| 6684 | return 0; |
| 6685 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6686 | |
| 6687 | /* |
| 6688 | * Update the size of all devices, which is used for writing out the |
| 6689 | * super blocks. |
| 6690 | */ |
| 6691 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6692 | { |
| 6693 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6694 | struct btrfs_device *curr, *next; |
| 6695 | |
| 6696 | if (list_empty(&fs_devices->resized_devices)) |
| 6697 | return; |
| 6698 | |
| 6699 | mutex_lock(&fs_devices->device_list_mutex); |
| 6700 | lock_chunks(fs_info->dev_root); |
| 6701 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6702 | resized_list) { |
| 6703 | list_del_init(&curr->resized_list); |
| 6704 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6705 | } |
| 6706 | unlock_chunks(fs_info->dev_root); |
| 6707 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6708 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6709 | |
| 6710 | /* Must be invoked during the transaction commit */ |
| 6711 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6712 | struct btrfs_transaction *transaction) |
| 6713 | { |
| 6714 | struct extent_map *em; |
| 6715 | struct map_lookup *map; |
| 6716 | struct btrfs_device *dev; |
| 6717 | int i; |
| 6718 | |
| 6719 | if (list_empty(&transaction->pending_chunks)) |
| 6720 | return; |
| 6721 | |
| 6722 | /* In order to kick the device replace finish process */ |
| 6723 | lock_chunks(root); |
| 6724 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6725 | map = (struct map_lookup *)em->bdev; |
| 6726 | |
| 6727 | for (i = 0; i < map->num_stripes; i++) { |
| 6728 | dev = map->stripes[i].dev; |
| 6729 | dev->commit_bytes_used = dev->bytes_used; |
| 6730 | } |
| 6731 | } |
| 6732 | unlock_chunks(root); |
| 6733 | } |