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 | |
Eric Sandeen | 9eaed21 | 2014-08-01 18:12:35 -0500 | [diff] [blame] | 611 | void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 612 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 613 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 614 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 615 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 616 | mutex_lock(&uuid_mutex); |
| 617 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 618 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 619 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 620 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 621 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 622 | (!latest_dev || |
| 623 | device->generation > latest_dev->generation)) { |
| 624 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 625 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 626 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 627 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 628 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 629 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 630 | /* |
| 631 | * In the first step, keep the device which has |
| 632 | * the correct fsid and the devid that is used |
| 633 | * for the dev_replace procedure. |
| 634 | * In the second step, the dev_replace state is |
| 635 | * read from the device tree and it is known |
| 636 | * whether the procedure is really active or |
| 637 | * not, which means whether this device is |
| 638 | * used or whether it should be removed. |
| 639 | */ |
| 640 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 641 | continue; |
| 642 | } |
| 643 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 644 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 645 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 646 | device->bdev = NULL; |
| 647 | fs_devices->open_devices--; |
| 648 | } |
| 649 | if (device->writeable) { |
| 650 | list_del_init(&device->dev_alloc_list); |
| 651 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 652 | if (!device->is_tgtdev_for_dev_replace) |
| 653 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 654 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 655 | list_del_init(&device->dev_list); |
| 656 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 657 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 658 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 659 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 660 | |
| 661 | if (fs_devices->seed) { |
| 662 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 663 | goto again; |
| 664 | } |
| 665 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 666 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 667 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 668 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 669 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 670 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 671 | static void __free_device(struct work_struct *work) |
| 672 | { |
| 673 | struct btrfs_device *device; |
| 674 | |
| 675 | device = container_of(work, struct btrfs_device, rcu_work); |
| 676 | |
| 677 | if (device->bdev) |
| 678 | blkdev_put(device->bdev, device->mode); |
| 679 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 680 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 681 | kfree(device); |
| 682 | } |
| 683 | |
| 684 | static void free_device(struct rcu_head *head) |
| 685 | { |
| 686 | struct btrfs_device *device; |
| 687 | |
| 688 | device = container_of(head, struct btrfs_device, rcu); |
| 689 | |
| 690 | INIT_WORK(&device->rcu_work, __free_device); |
| 691 | schedule_work(&device->rcu_work); |
| 692 | } |
| 693 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 694 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 695 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 696 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 697 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 698 | if (--fs_devices->opened > 0) |
| 699 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 700 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 701 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 702 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 703 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 704 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 705 | |
| 706 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 707 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 708 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 709 | if (device->writeable && |
| 710 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 711 | list_del_init(&device->dev_alloc_list); |
| 712 | fs_devices->rw_devices--; |
| 713 | } |
| 714 | |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 715 | if (device->missing) |
| 716 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 717 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 718 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 719 | device->uuid); |
| 720 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 721 | |
| 722 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 723 | if (device->name) { |
| 724 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 725 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 726 | rcu_assign_pointer(new_device->name, name); |
| 727 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 728 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 729 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 730 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 731 | |
| 732 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 733 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 734 | mutex_unlock(&fs_devices->device_list_mutex); |
| 735 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 736 | WARN_ON(fs_devices->open_devices); |
| 737 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 738 | fs_devices->opened = 0; |
| 739 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 740 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 744 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 745 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 746 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 747 | int ret; |
| 748 | |
| 749 | mutex_lock(&uuid_mutex); |
| 750 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 751 | if (!fs_devices->opened) { |
| 752 | seed_devices = fs_devices->seed; |
| 753 | fs_devices->seed = NULL; |
| 754 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 755 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 756 | |
| 757 | while (seed_devices) { |
| 758 | fs_devices = seed_devices; |
| 759 | seed_devices = fs_devices->seed; |
| 760 | __btrfs_close_devices(fs_devices); |
| 761 | free_fs_devices(fs_devices); |
| 762 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 763 | /* |
| 764 | * Wait for rcu kworkers under __btrfs_close_devices |
| 765 | * to finish all blkdev_puts so device is really |
| 766 | * free when umount is done. |
| 767 | */ |
| 768 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 769 | return ret; |
| 770 | } |
| 771 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 772 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 773 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 774 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 775 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 776 | struct block_device *bdev; |
| 777 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 778 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 779 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 780 | struct buffer_head *bh; |
| 781 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 782 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 783 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 784 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 785 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 786 | flags |= FMODE_EXCL; |
| 787 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 788 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 789 | if (device->bdev) |
| 790 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 791 | if (!device->name) |
| 792 | continue; |
| 793 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 794 | /* Just open everything we can; ignore failures here */ |
| 795 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 796 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 797 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 798 | |
| 799 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 800 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 801 | if (devid != device->devid) |
| 802 | goto error_brelse; |
| 803 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 804 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 805 | BTRFS_UUID_SIZE)) |
| 806 | goto error_brelse; |
| 807 | |
| 808 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 809 | if (!latest_dev || |
| 810 | device->generation > latest_dev->generation) |
| 811 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 812 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 813 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 814 | device->writeable = 0; |
| 815 | } else { |
| 816 | device->writeable = !bdev_read_only(bdev); |
| 817 | seeding = 0; |
| 818 | } |
| 819 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 820 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 821 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 822 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 823 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 824 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 825 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 826 | device->mode = flags; |
| 827 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 828 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 829 | fs_devices->rotating = 1; |
| 830 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 831 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 832 | if (device->writeable && |
| 833 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 834 | fs_devices->rw_devices++; |
| 835 | list_add(&device->dev_alloc_list, |
| 836 | &fs_devices->alloc_list); |
| 837 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 838 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 839 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 840 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 841 | error_brelse: |
| 842 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 843 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 844 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 845 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 846 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 847 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 848 | goto out; |
| 849 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 850 | fs_devices->seeding = seeding; |
| 851 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 852 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 853 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 854 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 855 | return ret; |
| 856 | } |
| 857 | |
| 858 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 859 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 860 | { |
| 861 | int ret; |
| 862 | |
| 863 | mutex_lock(&uuid_mutex); |
| 864 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 865 | fs_devices->opened++; |
| 866 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 867 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 868 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 869 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 870 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 871 | return ret; |
| 872 | } |
| 873 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 874 | /* |
| 875 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 876 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 877 | * is read via pagecache |
| 878 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 879 | 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] | 880 | struct btrfs_fs_devices **fs_devices_ret) |
| 881 | { |
| 882 | struct btrfs_super_block *disk_super; |
| 883 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 884 | struct page *page; |
| 885 | void *p; |
| 886 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 887 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 888 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 889 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 890 | u64 bytenr; |
| 891 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 892 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 893 | /* |
| 894 | * we would like to check all the supers, but that would make |
| 895 | * a btrfs mount succeed after a mkfs from a different FS. |
| 896 | * So, we need to add a special mount option to scan for |
| 897 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 898 | */ |
| 899 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 900 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 901 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 902 | |
| 903 | bdev = blkdev_get_by_path(path, flags, holder); |
| 904 | |
| 905 | if (IS_ERR(bdev)) { |
| 906 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 907 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* make sure our super fits in the device */ |
| 911 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 912 | goto error_bdev_put; |
| 913 | |
| 914 | /* make sure our super fits in the page */ |
| 915 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 916 | goto error_bdev_put; |
| 917 | |
| 918 | /* make sure our super doesn't straddle pages on disk */ |
| 919 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 920 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 921 | goto error_bdev_put; |
| 922 | |
| 923 | /* pull in the page with our super */ |
| 924 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 925 | index, GFP_NOFS); |
| 926 | |
| 927 | if (IS_ERR_OR_NULL(page)) |
| 928 | goto error_bdev_put; |
| 929 | |
| 930 | p = kmap(page); |
| 931 | |
| 932 | /* align our pointer to the offset of the super block */ |
| 933 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 934 | |
| 935 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 936 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 937 | goto error_unmap; |
| 938 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 939 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 940 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 941 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 942 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 943 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 944 | if (ret > 0) { |
| 945 | if (disk_super->label[0]) { |
| 946 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 947 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 948 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 949 | } else { |
| 950 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 951 | } |
| 952 | |
| 953 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 954 | ret = 0; |
| 955 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 956 | if (!ret && fs_devices_ret) |
| 957 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 958 | |
| 959 | error_unmap: |
| 960 | kunmap(page); |
| 961 | page_cache_release(page); |
| 962 | |
| 963 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 964 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 965 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 966 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 967 | return ret; |
| 968 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 969 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 970 | /* helper to account the used device space in the range */ |
| 971 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 972 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 973 | { |
| 974 | struct btrfs_key key; |
| 975 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 976 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 977 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 978 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 979 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 980 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 981 | struct extent_buffer *l; |
| 982 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 983 | *length = 0; |
| 984 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 985 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 986 | return 0; |
| 987 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 988 | path = btrfs_alloc_path(); |
| 989 | if (!path) |
| 990 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 991 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 992 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 993 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 994 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 995 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 996 | |
| 997 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 998 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 999 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1000 | if (ret > 0) { |
| 1001 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1002 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1003 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1004 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1005 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1006 | while (1) { |
| 1007 | l = path->nodes[0]; |
| 1008 | slot = path->slots[0]; |
| 1009 | if (slot >= btrfs_header_nritems(l)) { |
| 1010 | ret = btrfs_next_leaf(root, path); |
| 1011 | if (ret == 0) |
| 1012 | continue; |
| 1013 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1014 | goto out; |
| 1015 | |
| 1016 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1017 | } |
| 1018 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1019 | |
| 1020 | if (key.objectid < device->devid) |
| 1021 | goto next; |
| 1022 | |
| 1023 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1024 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1025 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1026 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1027 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1028 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1029 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1030 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1031 | dev_extent); |
| 1032 | if (key.offset <= start && extent_end > end) { |
| 1033 | *length = end - start + 1; |
| 1034 | break; |
| 1035 | } else if (key.offset <= start && extent_end > start) |
| 1036 | *length += extent_end - start; |
| 1037 | else if (key.offset > start && extent_end <= end) |
| 1038 | *length += extent_end - key.offset; |
| 1039 | else if (key.offset > start && key.offset <= end) { |
| 1040 | *length += end - key.offset + 1; |
| 1041 | break; |
| 1042 | } else if (key.offset > end) |
| 1043 | break; |
| 1044 | |
| 1045 | next: |
| 1046 | path->slots[0]++; |
| 1047 | } |
| 1048 | ret = 0; |
| 1049 | out: |
| 1050 | btrfs_free_path(path); |
| 1051 | return ret; |
| 1052 | } |
| 1053 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1054 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1055 | struct btrfs_device *device, |
| 1056 | u64 *start, u64 len) |
| 1057 | { |
| 1058 | struct extent_map *em; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1059 | struct list_head *search_list = &trans->transaction->pending_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1060 | int ret = 0; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1061 | u64 physical_start = *start; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 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; |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1072 | if (map->stripes[i].physical >= physical_start + len || |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1073 | map->stripes[i].physical + em->orig_block_len <= |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1074 | physical_start) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 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; |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1138 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1139 | max_hole_start = search_start; |
| 1140 | max_hole_size = 0; |
| 1141 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1142 | again: |
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, |
Forrest Liu | 1b98450 | 2015-02-09 17:30:47 +0800 | [diff] [blame] | 1197 | hole_size)) { |
| 1198 | if (key.offset >= search_start) { |
| 1199 | hole_size = key.offset - search_start; |
| 1200 | } else { |
| 1201 | WARN_ON_ONCE(1); |
| 1202 | hole_size = 0; |
| 1203 | } |
| 1204 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1205 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1206 | if (hole_size > max_hole_size) { |
| 1207 | max_hole_start = search_start; |
| 1208 | max_hole_size = hole_size; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * If this free space is greater than which we need, |
| 1213 | * it must be the max free space that we have found |
| 1214 | * until now, so max_hole_start must point to the start |
| 1215 | * of this free space and the length of this free space |
| 1216 | * is stored in max_hole_size. Thus, we return |
| 1217 | * max_hole_start and max_hole_size and go back to the |
| 1218 | * caller. |
| 1219 | */ |
| 1220 | if (hole_size >= num_bytes) { |
| 1221 | ret = 0; |
| 1222 | goto out; |
| 1223 | } |
| 1224 | } |
| 1225 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1226 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1227 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1228 | dev_extent); |
| 1229 | if (extent_end > search_start) |
| 1230 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1231 | next: |
| 1232 | path->slots[0]++; |
| 1233 | cond_resched(); |
| 1234 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1235 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1236 | /* |
| 1237 | * At this point, search_start should be the end of |
| 1238 | * allocated dev extents, and when shrinking the device, |
| 1239 | * search_end may be smaller than search_start. |
| 1240 | */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1241 | if (search_end > search_start) { |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1242 | hole_size = search_end - search_start; |
| 1243 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1244 | if (contains_pending_extent(trans, device, &search_start, |
| 1245 | hole_size)) { |
| 1246 | btrfs_release_path(path); |
| 1247 | goto again; |
| 1248 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1249 | |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1250 | if (hole_size > max_hole_size) { |
| 1251 | max_hole_start = search_start; |
| 1252 | max_hole_size = hole_size; |
| 1253 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1254 | } |
| 1255 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1256 | /* See above. */ |
Zhao Lei | f2ab761 | 2015-02-16 18:52:17 +0800 | [diff] [blame] | 1257 | if (max_hole_size < num_bytes) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1258 | ret = -ENOSPC; |
| 1259 | else |
| 1260 | ret = 0; |
| 1261 | |
| 1262 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1263 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1264 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1265 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1266 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1267 | return ret; |
| 1268 | } |
| 1269 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1270 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1271 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1272 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1273 | { |
| 1274 | int ret; |
| 1275 | struct btrfs_path *path; |
| 1276 | struct btrfs_root *root = device->dev_root; |
| 1277 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1278 | struct btrfs_key found_key; |
| 1279 | struct extent_buffer *leaf = NULL; |
| 1280 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1281 | |
| 1282 | path = btrfs_alloc_path(); |
| 1283 | if (!path) |
| 1284 | return -ENOMEM; |
| 1285 | |
| 1286 | key.objectid = device->devid; |
| 1287 | key.offset = start; |
| 1288 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1289 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1290 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1291 | if (ret > 0) { |
| 1292 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1293 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1294 | if (ret) |
| 1295 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1296 | leaf = path->nodes[0]; |
| 1297 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1298 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1299 | struct btrfs_dev_extent); |
| 1300 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1301 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1302 | key = found_key; |
| 1303 | btrfs_release_path(path); |
| 1304 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1305 | } else if (ret == 0) { |
| 1306 | leaf = path->nodes[0]; |
| 1307 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1308 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1309 | } else { |
| 1310 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1311 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1312 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1313 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1314 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1315 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1316 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1317 | if (ret) { |
| 1318 | btrfs_error(root->fs_info, ret, |
| 1319 | "Failed to remove dev extent item"); |
Zhao Lei | 13212b5 | 2015-02-12 14:18:17 +0800 | [diff] [blame] | 1320 | } else { |
| 1321 | trans->transaction->have_free_bgs = 1; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1322 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1323 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1324 | btrfs_free_path(path); |
| 1325 | return ret; |
| 1326 | } |
| 1327 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1328 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1329 | struct btrfs_device *device, |
| 1330 | u64 chunk_tree, u64 chunk_objectid, |
| 1331 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1332 | { |
| 1333 | int ret; |
| 1334 | struct btrfs_path *path; |
| 1335 | struct btrfs_root *root = device->dev_root; |
| 1336 | struct btrfs_dev_extent *extent; |
| 1337 | struct extent_buffer *leaf; |
| 1338 | struct btrfs_key key; |
| 1339 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1340 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1341 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1342 | path = btrfs_alloc_path(); |
| 1343 | if (!path) |
| 1344 | return -ENOMEM; |
| 1345 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1346 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1347 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1348 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1349 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1350 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1351 | if (ret) |
| 1352 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1353 | |
| 1354 | leaf = path->nodes[0]; |
| 1355 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1356 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1357 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1358 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1359 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1360 | |
| 1361 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1362 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1363 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1364 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1365 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1366 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1367 | btrfs_free_path(path); |
| 1368 | return ret; |
| 1369 | } |
| 1370 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1371 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1372 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1373 | struct extent_map_tree *em_tree; |
| 1374 | struct extent_map *em; |
| 1375 | struct rb_node *n; |
| 1376 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1377 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1378 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1379 | read_lock(&em_tree->lock); |
| 1380 | n = rb_last(&em_tree->map); |
| 1381 | if (n) { |
| 1382 | em = rb_entry(n, struct extent_map, rb_node); |
| 1383 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1384 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1385 | read_unlock(&em_tree->lock); |
| 1386 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1387 | return ret; |
| 1388 | } |
| 1389 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1390 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1391 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1392 | { |
| 1393 | int ret; |
| 1394 | struct btrfs_key key; |
| 1395 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1396 | struct btrfs_path *path; |
| 1397 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1398 | path = btrfs_alloc_path(); |
| 1399 | if (!path) |
| 1400 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1401 | |
| 1402 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1403 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1404 | key.offset = (u64)-1; |
| 1405 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1406 | 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] | 1407 | if (ret < 0) |
| 1408 | goto error; |
| 1409 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1410 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1411 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1412 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1413 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1414 | BTRFS_DEV_ITEM_KEY); |
| 1415 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1416 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1417 | } else { |
| 1418 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1419 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1420 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1421 | } |
| 1422 | ret = 0; |
| 1423 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1424 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | /* |
| 1429 | * the device information is stored in the chunk root |
| 1430 | * the btrfs_device struct should be fully filled in |
| 1431 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1432 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1433 | struct btrfs_root *root, |
| 1434 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1435 | { |
| 1436 | int ret; |
| 1437 | struct btrfs_path *path; |
| 1438 | struct btrfs_dev_item *dev_item; |
| 1439 | struct extent_buffer *leaf; |
| 1440 | struct btrfs_key key; |
| 1441 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1442 | |
| 1443 | root = root->fs_info->chunk_root; |
| 1444 | |
| 1445 | path = btrfs_alloc_path(); |
| 1446 | if (!path) |
| 1447 | return -ENOMEM; |
| 1448 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1449 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1450 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1451 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1452 | |
| 1453 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1454 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1455 | if (ret) |
| 1456 | goto out; |
| 1457 | |
| 1458 | leaf = path->nodes[0]; |
| 1459 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1460 | |
| 1461 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1462 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1463 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1464 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1465 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1466 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1467 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1468 | btrfs_device_get_disk_total_bytes(device)); |
| 1469 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1470 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1471 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1472 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1473 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1474 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1475 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1476 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1477 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1478 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1479 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1480 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1481 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1482 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1483 | out: |
| 1484 | btrfs_free_path(path); |
| 1485 | return ret; |
| 1486 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1487 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1488 | /* |
| 1489 | * Function to update ctime/mtime for a given device path. |
| 1490 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1491 | */ |
| 1492 | static void update_dev_time(char *path_name) |
| 1493 | { |
| 1494 | struct file *filp; |
| 1495 | |
| 1496 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1497 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1498 | return; |
| 1499 | file_update_time(filp); |
| 1500 | filp_close(filp, NULL); |
| 1501 | return; |
| 1502 | } |
| 1503 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1504 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1505 | struct btrfs_device *device) |
| 1506 | { |
| 1507 | int ret; |
| 1508 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1509 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1510 | struct btrfs_trans_handle *trans; |
| 1511 | |
| 1512 | root = root->fs_info->chunk_root; |
| 1513 | |
| 1514 | path = btrfs_alloc_path(); |
| 1515 | if (!path) |
| 1516 | return -ENOMEM; |
| 1517 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1518 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1519 | if (IS_ERR(trans)) { |
| 1520 | btrfs_free_path(path); |
| 1521 | return PTR_ERR(trans); |
| 1522 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1523 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1524 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1525 | key.offset = device->devid; |
| 1526 | |
| 1527 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1528 | if (ret < 0) |
| 1529 | goto out; |
| 1530 | |
| 1531 | if (ret > 0) { |
| 1532 | ret = -ENOENT; |
| 1533 | goto out; |
| 1534 | } |
| 1535 | |
| 1536 | ret = btrfs_del_item(trans, root, path); |
| 1537 | if (ret) |
| 1538 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1539 | out: |
| 1540 | btrfs_free_path(path); |
| 1541 | btrfs_commit_transaction(trans, root); |
| 1542 | return ret; |
| 1543 | } |
| 1544 | |
| 1545 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1546 | { |
| 1547 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1548 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1549 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1550 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1551 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1552 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1553 | u64 all_avail; |
| 1554 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1555 | u64 num_devices; |
| 1556 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1557 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1558 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1559 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1560 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1561 | mutex_lock(&uuid_mutex); |
| 1562 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1563 | do { |
| 1564 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1565 | |
| 1566 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1567 | root->fs_info->avail_system_alloc_bits | |
| 1568 | root->fs_info->avail_metadata_alloc_bits; |
| 1569 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1570 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1571 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1572 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1573 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1574 | WARN_ON(num_devices < 1); |
| 1575 | num_devices--; |
| 1576 | } |
| 1577 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1578 | |
| 1579 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1580 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1581 | goto out; |
| 1582 | } |
| 1583 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1584 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1585 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1586 | goto out; |
| 1587 | } |
| 1588 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1589 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1590 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1591 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1592 | goto out; |
| 1593 | } |
| 1594 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1595 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1596 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1597 | goto out; |
| 1598 | } |
| 1599 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1600 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1601 | struct list_head *devices; |
| 1602 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1603 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1604 | device = NULL; |
| 1605 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1606 | /* |
| 1607 | * It is safe to read the devices since the volume_mutex |
| 1608 | * is held. |
| 1609 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1610 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1611 | if (tmp->in_fs_metadata && |
| 1612 | !tmp->is_tgtdev_for_dev_replace && |
| 1613 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1614 | device = tmp; |
| 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | bdev = NULL; |
| 1619 | bh = NULL; |
| 1620 | disk_super = NULL; |
| 1621 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1622 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1623 | goto out; |
| 1624 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1625 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1626 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1627 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1628 | root->fs_info->bdev_holder, 0, |
| 1629 | &bdev, &bh); |
| 1630 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1631 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1632 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1633 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1634 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1635 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1636 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1637 | if (!device) { |
| 1638 | ret = -ENOENT; |
| 1639 | goto error_brelse; |
| 1640 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1641 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1642 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1643 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1644 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1645 | goto error_brelse; |
| 1646 | } |
| 1647 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1648 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1649 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1650 | goto error_brelse; |
| 1651 | } |
| 1652 | |
| 1653 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1654 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1655 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1656 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1657 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1658 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1659 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1660 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1661 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1662 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1663 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1664 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1665 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1666 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1667 | /* |
| 1668 | * TODO: the superblock still includes this device in its num_devices |
| 1669 | * counter although write_all_supers() is not locked out. This |
| 1670 | * could give a filesystem state which requires a degraded mount. |
| 1671 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1672 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1673 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1674 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1675 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1676 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1677 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1678 | |
| 1679 | /* |
| 1680 | * the device list mutex makes sure that we don't change |
| 1681 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1682 | * the device supers. Whoever is writing all supers, should |
| 1683 | * lock the device list mutex before getting the number of |
| 1684 | * devices in the super block (super_copy). Conversely, |
| 1685 | * whoever updates the number of devices in the super block |
| 1686 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1687 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1688 | |
| 1689 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1690 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1691 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1692 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1693 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1694 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1695 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1696 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1697 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1698 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1699 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1700 | struct btrfs_device, dev_list); |
| 1701 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1702 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1703 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1704 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1705 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1706 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1707 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1708 | /* remove sysfs entry */ |
| 1709 | btrfs_kobj_rm_device(root->fs_info, device); |
| 1710 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1711 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1712 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1713 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1714 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1715 | 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] | 1716 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1717 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1718 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1719 | struct btrfs_fs_devices *fs_devices; |
| 1720 | fs_devices = root->fs_info->fs_devices; |
| 1721 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1722 | if (fs_devices->seed == cur_devices) { |
| 1723 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1724 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1725 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1726 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1727 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1728 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1729 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1730 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1731 | } |
| 1732 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1733 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1734 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1735 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1736 | /* |
| 1737 | * at this point, the device is zero sized. We want to |
| 1738 | * remove it from the devices list and zero out the old super |
| 1739 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1740 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1741 | u64 bytenr; |
| 1742 | int i; |
| 1743 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1744 | /* make sure this device isn't detected as part of |
| 1745 | * the FS anymore |
| 1746 | */ |
| 1747 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1748 | set_buffer_dirty(bh); |
| 1749 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1750 | |
| 1751 | /* clear the mirror copies of super block on the disk |
| 1752 | * being removed, 0th copy is been taken care above and |
| 1753 | * the below would take of the rest |
| 1754 | */ |
| 1755 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1756 | bytenr = btrfs_sb_offset(i); |
| 1757 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1758 | i_size_read(bdev->bd_inode)) |
| 1759 | break; |
| 1760 | |
| 1761 | brelse(bh); |
| 1762 | bh = __bread(bdev, bytenr / 4096, |
| 1763 | BTRFS_SUPER_INFO_SIZE); |
| 1764 | if (!bh) |
| 1765 | continue; |
| 1766 | |
| 1767 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1768 | |
| 1769 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1770 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1771 | continue; |
| 1772 | } |
| 1773 | memset(&disk_super->magic, 0, |
| 1774 | sizeof(disk_super->magic)); |
| 1775 | set_buffer_dirty(bh); |
| 1776 | sync_dirty_buffer(bh); |
| 1777 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1778 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1779 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1780 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1781 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1782 | if (bdev) { |
| 1783 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1784 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1785 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1786 | /* Update ctime/mtime for device path for libblkid */ |
| 1787 | update_dev_time(device_path); |
| 1788 | } |
| 1789 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1790 | error_brelse: |
| 1791 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1792 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1793 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1794 | out: |
| 1795 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1796 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1797 | error_undo: |
| 1798 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1799 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1800 | list_add(&device->dev_alloc_list, |
| 1801 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1802 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1803 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1804 | } |
| 1805 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1806 | } |
| 1807 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1808 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1809 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1810 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1811 | struct btrfs_fs_devices *fs_devices; |
| 1812 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1813 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1814 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1815 | /* |
| 1816 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1817 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1818 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1819 | * srcdev will have its correct fs_devices in both the cases. |
| 1820 | */ |
| 1821 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1822 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1823 | list_del_rcu(&srcdev->dev_list); |
| 1824 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1825 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1826 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1827 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1828 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1829 | if (srcdev->writeable) { |
| 1830 | fs_devices->rw_devices--; |
| 1831 | /* zero out the old super if it is writable */ |
| 1832 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1833 | } |
| 1834 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1835 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1836 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 1840 | struct btrfs_device *srcdev) |
| 1841 | { |
| 1842 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1843 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1844 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1845 | |
| 1846 | /* |
| 1847 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 1848 | * zero |
| 1849 | */ |
| 1850 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 1851 | |
| 1852 | /* if this is no devs we rather delete the fs_devices */ |
| 1853 | if (!fs_devices->num_devices) { |
| 1854 | struct btrfs_fs_devices *tmp_fs_devices; |
| 1855 | |
| 1856 | tmp_fs_devices = fs_info->fs_devices; |
| 1857 | while (tmp_fs_devices) { |
| 1858 | if (tmp_fs_devices->seed == fs_devices) { |
| 1859 | tmp_fs_devices->seed = fs_devices->seed; |
| 1860 | break; |
| 1861 | } |
| 1862 | tmp_fs_devices = tmp_fs_devices->seed; |
| 1863 | } |
| 1864 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 1865 | __btrfs_close_devices(fs_devices); |
| 1866 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1867 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1871 | struct btrfs_device *tgtdev) |
| 1872 | { |
| 1873 | struct btrfs_device *next_device; |
| 1874 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1875 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1876 | WARN_ON(!tgtdev); |
| 1877 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1878 | if (tgtdev->bdev) { |
| 1879 | btrfs_scratch_superblock(tgtdev); |
| 1880 | fs_info->fs_devices->open_devices--; |
| 1881 | } |
| 1882 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1883 | |
| 1884 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1885 | struct btrfs_device, dev_list); |
| 1886 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1887 | fs_info->sb->s_bdev = next_device->bdev; |
| 1888 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1889 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1890 | list_del_rcu(&tgtdev->dev_list); |
| 1891 | |
| 1892 | call_rcu(&tgtdev->rcu, free_device); |
| 1893 | |
| 1894 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1895 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1896 | } |
| 1897 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1898 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1899 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1900 | { |
| 1901 | int ret = 0; |
| 1902 | struct btrfs_super_block *disk_super; |
| 1903 | u64 devid; |
| 1904 | u8 *dev_uuid; |
| 1905 | struct block_device *bdev; |
| 1906 | struct buffer_head *bh; |
| 1907 | |
| 1908 | *device = NULL; |
| 1909 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1910 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1911 | if (ret) |
| 1912 | return ret; |
| 1913 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1914 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1915 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1916 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1917 | disk_super->fsid); |
| 1918 | brelse(bh); |
| 1919 | if (!*device) |
| 1920 | ret = -ENOENT; |
| 1921 | blkdev_put(bdev, FMODE_READ); |
| 1922 | return ret; |
| 1923 | } |
| 1924 | |
| 1925 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1926 | char *device_path, |
| 1927 | struct btrfs_device **device) |
| 1928 | { |
| 1929 | *device = NULL; |
| 1930 | if (strcmp(device_path, "missing") == 0) { |
| 1931 | struct list_head *devices; |
| 1932 | struct btrfs_device *tmp; |
| 1933 | |
| 1934 | devices = &root->fs_info->fs_devices->devices; |
| 1935 | /* |
| 1936 | * It is safe to read the devices since the volume_mutex |
| 1937 | * is held by the caller. |
| 1938 | */ |
| 1939 | list_for_each_entry(tmp, devices, dev_list) { |
| 1940 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1941 | *device = tmp; |
| 1942 | break; |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1947 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1948 | return -ENOENT; |
| 1949 | } |
| 1950 | |
| 1951 | return 0; |
| 1952 | } else { |
| 1953 | return btrfs_find_device_by_path(root, device_path, device); |
| 1954 | } |
| 1955 | } |
| 1956 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1957 | /* |
| 1958 | * does all the dirty work required for changing file system's UUID. |
| 1959 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1960 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1961 | { |
| 1962 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1963 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1964 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1965 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1966 | struct btrfs_device *device; |
| 1967 | u64 super_flags; |
| 1968 | |
| 1969 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1970 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1971 | return -EINVAL; |
| 1972 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1973 | seed_devices = __alloc_fs_devices(); |
| 1974 | if (IS_ERR(seed_devices)) |
| 1975 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1976 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1977 | old_devices = clone_fs_devices(fs_devices); |
| 1978 | if (IS_ERR(old_devices)) { |
| 1979 | kfree(seed_devices); |
| 1980 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1981 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1982 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1983 | list_add(&old_devices->list, &fs_uuids); |
| 1984 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1985 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1986 | seed_devices->opened = 1; |
| 1987 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1988 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1989 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1990 | |
| 1991 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1992 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1993 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1994 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1995 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1996 | |
| 1997 | lock_chunks(root); |
| 1998 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1999 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2000 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2001 | fs_devices->seeding = 0; |
| 2002 | fs_devices->num_devices = 0; |
| 2003 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2004 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 2005 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 2006 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2007 | |
| 2008 | generate_random_uuid(fs_devices->fsid); |
| 2009 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2010 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2011 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2012 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2013 | super_flags = btrfs_super_flags(disk_super) & |
| 2014 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2015 | btrfs_set_super_flags(disk_super, super_flags); |
| 2016 | |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * strore the expected generation for seed devices in device items. |
| 2022 | */ |
| 2023 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2024 | struct btrfs_root *root) |
| 2025 | { |
| 2026 | struct btrfs_path *path; |
| 2027 | struct extent_buffer *leaf; |
| 2028 | struct btrfs_dev_item *dev_item; |
| 2029 | struct btrfs_device *device; |
| 2030 | struct btrfs_key key; |
| 2031 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2032 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2033 | u64 devid; |
| 2034 | int ret; |
| 2035 | |
| 2036 | path = btrfs_alloc_path(); |
| 2037 | if (!path) |
| 2038 | return -ENOMEM; |
| 2039 | |
| 2040 | root = root->fs_info->chunk_root; |
| 2041 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2042 | key.offset = 0; |
| 2043 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2044 | |
| 2045 | while (1) { |
| 2046 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2047 | if (ret < 0) |
| 2048 | goto error; |
| 2049 | |
| 2050 | leaf = path->nodes[0]; |
| 2051 | next_slot: |
| 2052 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2053 | ret = btrfs_next_leaf(root, path); |
| 2054 | if (ret > 0) |
| 2055 | break; |
| 2056 | if (ret < 0) |
| 2057 | goto error; |
| 2058 | leaf = path->nodes[0]; |
| 2059 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2060 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2061 | continue; |
| 2062 | } |
| 2063 | |
| 2064 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2065 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2066 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2067 | break; |
| 2068 | |
| 2069 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2070 | struct btrfs_dev_item); |
| 2071 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2072 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2073 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2074 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2075 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2076 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2077 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2078 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2079 | |
| 2080 | if (device->fs_devices->seeding) { |
| 2081 | btrfs_set_device_generation(leaf, dev_item, |
| 2082 | device->generation); |
| 2083 | btrfs_mark_buffer_dirty(leaf); |
| 2084 | } |
| 2085 | |
| 2086 | path->slots[0]++; |
| 2087 | goto next_slot; |
| 2088 | } |
| 2089 | ret = 0; |
| 2090 | error: |
| 2091 | btrfs_free_path(path); |
| 2092 | return ret; |
| 2093 | } |
| 2094 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2095 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2096 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2097 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2098 | struct btrfs_trans_handle *trans; |
| 2099 | struct btrfs_device *device; |
| 2100 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2101 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2102 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2103 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2104 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2105 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2106 | int ret = 0; |
| 2107 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2108 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2109 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2110 | |
Li Zefan | a5d16333 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2111 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2112 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2113 | if (IS_ERR(bdev)) |
| 2114 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2115 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2116 | if (root->fs_info->fs_devices->seeding) { |
| 2117 | seeding_dev = 1; |
| 2118 | down_write(&sb->s_umount); |
| 2119 | mutex_lock(&uuid_mutex); |
| 2120 | } |
| 2121 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2122 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2123 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2124 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2125 | |
| 2126 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2127 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2128 | if (device->bdev == bdev) { |
| 2129 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2130 | mutex_unlock( |
| 2131 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2132 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2133 | } |
| 2134 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2135 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2136 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2137 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2138 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2139 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2140 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2141 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2142 | } |
| 2143 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2144 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2145 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2146 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2147 | ret = -ENOMEM; |
| 2148 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2149 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2150 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2151 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2152 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2153 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2154 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2155 | kfree(device); |
| 2156 | ret = PTR_ERR(trans); |
| 2157 | goto error; |
| 2158 | } |
| 2159 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2160 | q = bdev_get_queue(bdev); |
| 2161 | if (blk_queue_discard(q)) |
| 2162 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2163 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2164 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2165 | device->io_width = root->sectorsize; |
| 2166 | device->io_align = root->sectorsize; |
| 2167 | device->sector_size = root->sectorsize; |
| 2168 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2169 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2170 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2171 | device->dev_root = root->fs_info->dev_root; |
| 2172 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2173 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2174 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2175 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2176 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2177 | set_blocksize(device->bdev, 4096); |
| 2178 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2179 | if (seeding_dev) { |
| 2180 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2181 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2182 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2186 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2187 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2188 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2189 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2190 | list_add(&device->dev_alloc_list, |
| 2191 | &root->fs_info->fs_devices->alloc_list); |
| 2192 | root->fs_info->fs_devices->num_devices++; |
| 2193 | root->fs_info->fs_devices->open_devices++; |
| 2194 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2195 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2196 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2197 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2198 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2199 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2200 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2201 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2202 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2203 | root->fs_info->fs_devices->rotating = 1; |
| 2204 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2205 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2206 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2207 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2208 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2209 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2210 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2211 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2212 | |
| 2213 | /* add sysfs device entry */ |
| 2214 | btrfs_kobj_add_device(root->fs_info, device); |
| 2215 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2216 | /* |
| 2217 | * we've got more storage, clear any full flags on the space |
| 2218 | * infos |
| 2219 | */ |
| 2220 | btrfs_clear_space_info_full(root->fs_info); |
| 2221 | |
| 2222 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2223 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2224 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2225 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2226 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2227 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2228 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2229 | if (ret) { |
| 2230 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2231 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2232 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | ret = btrfs_add_device(trans, root, device); |
| 2236 | if (ret) { |
| 2237 | btrfs_abort_transaction(trans, root, ret); |
| 2238 | goto error_trans; |
| 2239 | } |
| 2240 | |
| 2241 | if (seeding_dev) { |
| 2242 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2243 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2244 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2245 | if (ret) { |
| 2246 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2247 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2248 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2249 | |
| 2250 | /* Sprouting would change fsid of the mounted root, |
| 2251 | * so rename the fsid on the sysfs |
| 2252 | */ |
| 2253 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2254 | root->fs_info->fsid); |
| 2255 | if (kobject_rename(&root->fs_info->super_kobj, fsid_buf)) |
| 2256 | goto error_trans; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2257 | } |
| 2258 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2259 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2260 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2261 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2262 | |
| 2263 | if (seeding_dev) { |
| 2264 | mutex_unlock(&uuid_mutex); |
| 2265 | up_write(&sb->s_umount); |
| 2266 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2267 | if (ret) /* transaction commit */ |
| 2268 | return ret; |
| 2269 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2270 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2271 | if (ret < 0) |
| 2272 | btrfs_error(root->fs_info, ret, |
| 2273 | "Failed to relocate sys chunks after " |
| 2274 | "device initialization. This can be fixed " |
| 2275 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2276 | trans = btrfs_attach_transaction(root); |
| 2277 | if (IS_ERR(trans)) { |
| 2278 | if (PTR_ERR(trans) == -ENOENT) |
| 2279 | return 0; |
| 2280 | return PTR_ERR(trans); |
| 2281 | } |
| 2282 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2283 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2284 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2285 | /* Update ctime/mtime for libblkid */ |
| 2286 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2287 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2288 | |
| 2289 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2290 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2291 | rcu_string_free(device->name); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2292 | btrfs_kobj_rm_device(root->fs_info, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2293 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2294 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2295 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2296 | if (seeding_dev) { |
| 2297 | mutex_unlock(&uuid_mutex); |
| 2298 | up_write(&sb->s_umount); |
| 2299 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2300 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2301 | } |
| 2302 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2303 | 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] | 2304 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2305 | struct btrfs_device **device_out) |
| 2306 | { |
| 2307 | struct request_queue *q; |
| 2308 | struct btrfs_device *device; |
| 2309 | struct block_device *bdev; |
| 2310 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2311 | struct list_head *devices; |
| 2312 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2313 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2314 | int ret = 0; |
| 2315 | |
| 2316 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2317 | if (fs_info->fs_devices->seeding) { |
| 2318 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2319 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2320 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2321 | |
| 2322 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2323 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2324 | if (IS_ERR(bdev)) { |
| 2325 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2326 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2327 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2328 | |
| 2329 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2330 | |
| 2331 | devices = &fs_info->fs_devices->devices; |
| 2332 | list_for_each_entry(device, devices, dev_list) { |
| 2333 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2334 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2335 | ret = -EEXIST; |
| 2336 | goto error; |
| 2337 | } |
| 2338 | } |
| 2339 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2340 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2341 | if (i_size_read(bdev->bd_inode) < |
| 2342 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2343 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2344 | ret = -EINVAL; |
| 2345 | goto error; |
| 2346 | } |
| 2347 | |
| 2348 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2349 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2350 | if (IS_ERR(device)) { |
| 2351 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2352 | goto error; |
| 2353 | } |
| 2354 | |
| 2355 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2356 | if (!name) { |
| 2357 | kfree(device); |
| 2358 | ret = -ENOMEM; |
| 2359 | goto error; |
| 2360 | } |
| 2361 | rcu_assign_pointer(device->name, name); |
| 2362 | |
| 2363 | q = bdev_get_queue(bdev); |
| 2364 | if (blk_queue_discard(q)) |
| 2365 | device->can_discard = 1; |
| 2366 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2367 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2368 | device->generation = 0; |
| 2369 | device->io_width = root->sectorsize; |
| 2370 | device->io_align = root->sectorsize; |
| 2371 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2372 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2373 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2374 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2375 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2376 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2377 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2378 | device->dev_root = fs_info->dev_root; |
| 2379 | device->bdev = bdev; |
| 2380 | device->in_fs_metadata = 1; |
| 2381 | device->is_tgtdev_for_dev_replace = 1; |
| 2382 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2383 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2384 | set_blocksize(device->bdev, 4096); |
| 2385 | device->fs_devices = fs_info->fs_devices; |
| 2386 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2387 | fs_info->fs_devices->num_devices++; |
| 2388 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2389 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2390 | |
| 2391 | *device_out = device; |
| 2392 | return ret; |
| 2393 | |
| 2394 | error: |
| 2395 | blkdev_put(bdev, FMODE_EXCL); |
| 2396 | return ret; |
| 2397 | } |
| 2398 | |
| 2399 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2400 | struct btrfs_device *tgtdev) |
| 2401 | { |
| 2402 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2403 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2404 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2405 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2406 | tgtdev->dev_root = fs_info->dev_root; |
| 2407 | tgtdev->in_fs_metadata = 1; |
| 2408 | } |
| 2409 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2410 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2411 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2412 | { |
| 2413 | int ret; |
| 2414 | struct btrfs_path *path; |
| 2415 | struct btrfs_root *root; |
| 2416 | struct btrfs_dev_item *dev_item; |
| 2417 | struct extent_buffer *leaf; |
| 2418 | struct btrfs_key key; |
| 2419 | |
| 2420 | root = device->dev_root->fs_info->chunk_root; |
| 2421 | |
| 2422 | path = btrfs_alloc_path(); |
| 2423 | if (!path) |
| 2424 | return -ENOMEM; |
| 2425 | |
| 2426 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2427 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2428 | key.offset = device->devid; |
| 2429 | |
| 2430 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2431 | if (ret < 0) |
| 2432 | goto out; |
| 2433 | |
| 2434 | if (ret > 0) { |
| 2435 | ret = -ENOENT; |
| 2436 | goto out; |
| 2437 | } |
| 2438 | |
| 2439 | leaf = path->nodes[0]; |
| 2440 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2441 | |
| 2442 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2443 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2444 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2445 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2446 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2447 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2448 | btrfs_device_get_disk_total_bytes(device)); |
| 2449 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2450 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2451 | btrfs_mark_buffer_dirty(leaf); |
| 2452 | |
| 2453 | out: |
| 2454 | btrfs_free_path(path); |
| 2455 | return ret; |
| 2456 | } |
| 2457 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2458 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2459 | struct btrfs_device *device, u64 new_size) |
| 2460 | { |
| 2461 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2462 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2463 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2464 | u64 old_total; |
| 2465 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2466 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2467 | if (!device->writeable) |
| 2468 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2469 | |
| 2470 | lock_chunks(device->dev_root); |
| 2471 | old_total = btrfs_super_total_bytes(super_copy); |
| 2472 | diff = new_size - device->total_bytes; |
| 2473 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2474 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2475 | device->is_tgtdev_for_dev_replace) { |
| 2476 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2477 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2478 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2479 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2480 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2481 | |
| 2482 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2483 | device->fs_devices->total_rw_bytes += diff; |
| 2484 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2485 | btrfs_device_set_total_bytes(device, new_size); |
| 2486 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2487 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2488 | if (list_empty(&device->resized_list)) |
| 2489 | list_add_tail(&device->resized_list, |
| 2490 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2491 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2492 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2493 | return btrfs_update_device(trans, device); |
| 2494 | } |
| 2495 | |
| 2496 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2497 | struct btrfs_root *root, u64 chunk_objectid, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2498 | u64 chunk_offset) |
| 2499 | { |
| 2500 | int ret; |
| 2501 | struct btrfs_path *path; |
| 2502 | struct btrfs_key key; |
| 2503 | |
| 2504 | root = root->fs_info->chunk_root; |
| 2505 | path = btrfs_alloc_path(); |
| 2506 | if (!path) |
| 2507 | return -ENOMEM; |
| 2508 | |
| 2509 | key.objectid = chunk_objectid; |
| 2510 | key.offset = chunk_offset; |
| 2511 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2512 | |
| 2513 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2514 | if (ret < 0) |
| 2515 | goto out; |
| 2516 | else if (ret > 0) { /* Logic error or corruption */ |
| 2517 | btrfs_error(root->fs_info, -ENOENT, |
| 2518 | "Failed lookup while freeing chunk."); |
| 2519 | ret = -ENOENT; |
| 2520 | goto out; |
| 2521 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2522 | |
| 2523 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2524 | if (ret < 0) |
| 2525 | btrfs_error(root->fs_info, ret, |
| 2526 | "Failed to delete chunk item."); |
| 2527 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2528 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2529 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2530 | } |
| 2531 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2532 | 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] | 2533 | chunk_offset) |
| 2534 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2535 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2536 | struct btrfs_disk_key *disk_key; |
| 2537 | struct btrfs_chunk *chunk; |
| 2538 | u8 *ptr; |
| 2539 | int ret = 0; |
| 2540 | u32 num_stripes; |
| 2541 | u32 array_size; |
| 2542 | u32 len = 0; |
| 2543 | u32 cur; |
| 2544 | struct btrfs_key key; |
| 2545 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2546 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2547 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2548 | |
| 2549 | ptr = super_copy->sys_chunk_array; |
| 2550 | cur = 0; |
| 2551 | |
| 2552 | while (cur < array_size) { |
| 2553 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2554 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2555 | |
| 2556 | len = sizeof(*disk_key); |
| 2557 | |
| 2558 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2559 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2560 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2561 | len += btrfs_chunk_item_size(num_stripes); |
| 2562 | } else { |
| 2563 | ret = -EIO; |
| 2564 | break; |
| 2565 | } |
| 2566 | if (key.objectid == chunk_objectid && |
| 2567 | key.offset == chunk_offset) { |
| 2568 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2569 | array_size -= len; |
| 2570 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2571 | } else { |
| 2572 | ptr += len; |
| 2573 | cur += len; |
| 2574 | } |
| 2575 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2576 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2577 | return ret; |
| 2578 | } |
| 2579 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2580 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2581 | struct btrfs_root *root, u64 chunk_offset) |
| 2582 | { |
| 2583 | struct extent_map_tree *em_tree; |
| 2584 | struct extent_map *em; |
| 2585 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2586 | struct map_lookup *map; |
| 2587 | u64 dev_extent_len = 0; |
| 2588 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2589 | int i, ret = 0; |
| 2590 | |
| 2591 | /* Just in case */ |
| 2592 | root = root->fs_info->chunk_root; |
| 2593 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2594 | |
| 2595 | read_lock(&em_tree->lock); |
| 2596 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2597 | read_unlock(&em_tree->lock); |
| 2598 | |
| 2599 | if (!em || em->start > chunk_offset || |
| 2600 | em->start + em->len < chunk_offset) { |
| 2601 | /* |
| 2602 | * This is a logic error, but we don't want to just rely on the |
| 2603 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2604 | * do anything we still error out. |
| 2605 | */ |
| 2606 | ASSERT(0); |
| 2607 | if (em) |
| 2608 | free_extent_map(em); |
| 2609 | return -EINVAL; |
| 2610 | } |
| 2611 | map = (struct map_lookup *)em->bdev; |
| 2612 | |
| 2613 | for (i = 0; i < map->num_stripes; i++) { |
| 2614 | struct btrfs_device *device = map->stripes[i].dev; |
| 2615 | ret = btrfs_free_dev_extent(trans, device, |
| 2616 | map->stripes[i].physical, |
| 2617 | &dev_extent_len); |
| 2618 | if (ret) { |
| 2619 | btrfs_abort_transaction(trans, root, ret); |
| 2620 | goto out; |
| 2621 | } |
| 2622 | |
| 2623 | if (device->bytes_used > 0) { |
| 2624 | lock_chunks(root); |
| 2625 | btrfs_device_set_bytes_used(device, |
| 2626 | device->bytes_used - dev_extent_len); |
| 2627 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2628 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2629 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2630 | btrfs_clear_space_info_full(root->fs_info); |
| 2631 | unlock_chunks(root); |
| 2632 | } |
| 2633 | |
| 2634 | if (map->stripes[i].dev) { |
| 2635 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2636 | if (ret) { |
| 2637 | btrfs_abort_transaction(trans, root, ret); |
| 2638 | goto out; |
| 2639 | } |
| 2640 | } |
| 2641 | } |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2642 | ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2643 | if (ret) { |
| 2644 | btrfs_abort_transaction(trans, root, ret); |
| 2645 | goto out; |
| 2646 | } |
| 2647 | |
| 2648 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2649 | |
| 2650 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2651 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2652 | if (ret) { |
| 2653 | btrfs_abort_transaction(trans, root, ret); |
| 2654 | goto out; |
| 2655 | } |
| 2656 | } |
| 2657 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2658 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2659 | if (ret) { |
| 2660 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2661 | goto out; |
| 2662 | } |
| 2663 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2664 | out: |
| 2665 | /* once for us */ |
| 2666 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2667 | return ret; |
| 2668 | } |
| 2669 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2670 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2671 | u64 chunk_objectid, |
| 2672 | u64 chunk_offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2673 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2674 | struct btrfs_root *extent_root; |
| 2675 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2676 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2677 | |
| 2678 | root = root->fs_info->chunk_root; |
| 2679 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2680 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2681 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2682 | if (ret) |
| 2683 | return -ENOSPC; |
| 2684 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2685 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2686 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2687 | if (ret) |
| 2688 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2689 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2690 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2691 | if (IS_ERR(trans)) { |
| 2692 | ret = PTR_ERR(trans); |
| 2693 | btrfs_std_error(root->fs_info, ret); |
| 2694 | return ret; |
| 2695 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2696 | |
| 2697 | /* |
| 2698 | * step two, delete the device extents and the |
| 2699 | * chunk tree entries |
| 2700 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2701 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2702 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2703 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2704 | } |
| 2705 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2706 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2707 | { |
| 2708 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2709 | struct btrfs_path *path; |
| 2710 | struct extent_buffer *leaf; |
| 2711 | struct btrfs_chunk *chunk; |
| 2712 | struct btrfs_key key; |
| 2713 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2714 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2715 | bool retried = false; |
| 2716 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2717 | int ret; |
| 2718 | |
| 2719 | path = btrfs_alloc_path(); |
| 2720 | if (!path) |
| 2721 | return -ENOMEM; |
| 2722 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2723 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2724 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2725 | key.offset = (u64)-1; |
| 2726 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2727 | |
| 2728 | while (1) { |
| 2729 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2730 | if (ret < 0) |
| 2731 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2732 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2733 | |
| 2734 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2735 | key.type); |
| 2736 | if (ret < 0) |
| 2737 | goto error; |
| 2738 | if (ret > 0) |
| 2739 | break; |
| 2740 | |
| 2741 | leaf = path->nodes[0]; |
| 2742 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2743 | |
| 2744 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2745 | struct btrfs_chunk); |
| 2746 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2747 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2748 | |
| 2749 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 2750 | ret = btrfs_relocate_chunk(chunk_root, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2751 | found_key.objectid, |
| 2752 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2753 | if (ret == -ENOSPC) |
| 2754 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2755 | else |
| 2756 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | if (found_key.offset == 0) |
| 2760 | break; |
| 2761 | key.offset = found_key.offset - 1; |
| 2762 | } |
| 2763 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2764 | if (failed && !retried) { |
| 2765 | failed = 0; |
| 2766 | retried = true; |
| 2767 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2768 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2769 | ret = -ENOSPC; |
| 2770 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2771 | error: |
| 2772 | btrfs_free_path(path); |
| 2773 | return ret; |
| 2774 | } |
| 2775 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2776 | static int insert_balance_item(struct btrfs_root *root, |
| 2777 | struct btrfs_balance_control *bctl) |
| 2778 | { |
| 2779 | struct btrfs_trans_handle *trans; |
| 2780 | struct btrfs_balance_item *item; |
| 2781 | struct btrfs_disk_balance_args disk_bargs; |
| 2782 | struct btrfs_path *path; |
| 2783 | struct extent_buffer *leaf; |
| 2784 | struct btrfs_key key; |
| 2785 | int ret, err; |
| 2786 | |
| 2787 | path = btrfs_alloc_path(); |
| 2788 | if (!path) |
| 2789 | return -ENOMEM; |
| 2790 | |
| 2791 | trans = btrfs_start_transaction(root, 0); |
| 2792 | if (IS_ERR(trans)) { |
| 2793 | btrfs_free_path(path); |
| 2794 | return PTR_ERR(trans); |
| 2795 | } |
| 2796 | |
| 2797 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2798 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2799 | key.offset = 0; |
| 2800 | |
| 2801 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2802 | sizeof(*item)); |
| 2803 | if (ret) |
| 2804 | goto out; |
| 2805 | |
| 2806 | leaf = path->nodes[0]; |
| 2807 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2808 | |
| 2809 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2810 | |
| 2811 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2812 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2813 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2814 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2815 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2816 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2817 | |
| 2818 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2819 | |
| 2820 | btrfs_mark_buffer_dirty(leaf); |
| 2821 | out: |
| 2822 | btrfs_free_path(path); |
| 2823 | err = btrfs_commit_transaction(trans, root); |
| 2824 | if (err && !ret) |
| 2825 | ret = err; |
| 2826 | return ret; |
| 2827 | } |
| 2828 | |
| 2829 | static int del_balance_item(struct btrfs_root *root) |
| 2830 | { |
| 2831 | struct btrfs_trans_handle *trans; |
| 2832 | struct btrfs_path *path; |
| 2833 | struct btrfs_key key; |
| 2834 | int ret, err; |
| 2835 | |
| 2836 | path = btrfs_alloc_path(); |
| 2837 | if (!path) |
| 2838 | return -ENOMEM; |
| 2839 | |
| 2840 | trans = btrfs_start_transaction(root, 0); |
| 2841 | if (IS_ERR(trans)) { |
| 2842 | btrfs_free_path(path); |
| 2843 | return PTR_ERR(trans); |
| 2844 | } |
| 2845 | |
| 2846 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2847 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2848 | key.offset = 0; |
| 2849 | |
| 2850 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2851 | if (ret < 0) |
| 2852 | goto out; |
| 2853 | if (ret > 0) { |
| 2854 | ret = -ENOENT; |
| 2855 | goto out; |
| 2856 | } |
| 2857 | |
| 2858 | ret = btrfs_del_item(trans, root, path); |
| 2859 | out: |
| 2860 | btrfs_free_path(path); |
| 2861 | err = btrfs_commit_transaction(trans, root); |
| 2862 | if (err && !ret) |
| 2863 | ret = err; |
| 2864 | return ret; |
| 2865 | } |
| 2866 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2867 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2868 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2869 | * resume after balance was interrupted. |
| 2870 | */ |
| 2871 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2872 | { |
| 2873 | /* |
| 2874 | * Turn on soft mode for chunk types that were being converted. |
| 2875 | */ |
| 2876 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2877 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2878 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2879 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2880 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2881 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2882 | |
| 2883 | /* |
| 2884 | * Turn on usage filter if is not already used. The idea is |
| 2885 | * that chunks that we have already balanced should be |
| 2886 | * reasonably full. Don't do it for chunks that are being |
| 2887 | * converted - that will keep us from relocating unconverted |
| 2888 | * (albeit full) chunks. |
| 2889 | */ |
| 2890 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2891 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2892 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2893 | bctl->data.usage = 90; |
| 2894 | } |
| 2895 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2896 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2897 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2898 | bctl->sys.usage = 90; |
| 2899 | } |
| 2900 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2901 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2902 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2903 | bctl->meta.usage = 90; |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2908 | * Should be called with both balance and volume mutexes held to |
| 2909 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2910 | * restriper. Same goes for unset_balance_control. |
| 2911 | */ |
| 2912 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2913 | { |
| 2914 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2915 | |
| 2916 | BUG_ON(fs_info->balance_ctl); |
| 2917 | |
| 2918 | spin_lock(&fs_info->balance_lock); |
| 2919 | fs_info->balance_ctl = bctl; |
| 2920 | spin_unlock(&fs_info->balance_lock); |
| 2921 | } |
| 2922 | |
| 2923 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2924 | { |
| 2925 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2926 | |
| 2927 | BUG_ON(!fs_info->balance_ctl); |
| 2928 | |
| 2929 | spin_lock(&fs_info->balance_lock); |
| 2930 | fs_info->balance_ctl = NULL; |
| 2931 | spin_unlock(&fs_info->balance_lock); |
| 2932 | |
| 2933 | kfree(bctl); |
| 2934 | } |
| 2935 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2936 | /* |
| 2937 | * Balance filters. Return 1 if chunk should be filtered out |
| 2938 | * (should not be balanced). |
| 2939 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2940 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2941 | struct btrfs_balance_args *bargs) |
| 2942 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2943 | chunk_type = chunk_to_extended(chunk_type) & |
| 2944 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2945 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2946 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2947 | return 0; |
| 2948 | |
| 2949 | return 1; |
| 2950 | } |
| 2951 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2952 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2953 | struct btrfs_balance_args *bargs) |
| 2954 | { |
| 2955 | struct btrfs_block_group_cache *cache; |
| 2956 | u64 chunk_used, user_thresh; |
| 2957 | int ret = 1; |
| 2958 | |
| 2959 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2960 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2961 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2962 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 2963 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2964 | else if (bargs->usage > 100) |
| 2965 | user_thresh = cache->key.offset; |
| 2966 | else |
| 2967 | user_thresh = div_factor_fine(cache->key.offset, |
| 2968 | bargs->usage); |
| 2969 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2970 | if (chunk_used < user_thresh) |
| 2971 | ret = 0; |
| 2972 | |
| 2973 | btrfs_put_block_group(cache); |
| 2974 | return ret; |
| 2975 | } |
| 2976 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2977 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2978 | struct btrfs_chunk *chunk, |
| 2979 | struct btrfs_balance_args *bargs) |
| 2980 | { |
| 2981 | struct btrfs_stripe *stripe; |
| 2982 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2983 | int i; |
| 2984 | |
| 2985 | for (i = 0; i < num_stripes; i++) { |
| 2986 | stripe = btrfs_stripe_nr(chunk, i); |
| 2987 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2988 | return 0; |
| 2989 | } |
| 2990 | |
| 2991 | return 1; |
| 2992 | } |
| 2993 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2994 | /* [pstart, pend) */ |
| 2995 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2996 | struct btrfs_chunk *chunk, |
| 2997 | u64 chunk_offset, |
| 2998 | struct btrfs_balance_args *bargs) |
| 2999 | { |
| 3000 | struct btrfs_stripe *stripe; |
| 3001 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 3002 | u64 stripe_offset; |
| 3003 | u64 stripe_length; |
| 3004 | int factor; |
| 3005 | int i; |
| 3006 | |
| 3007 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3008 | return 0; |
| 3009 | |
| 3010 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3011 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3012 | factor = num_stripes / 2; |
| 3013 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3014 | factor = num_stripes - 1; |
| 3015 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3016 | factor = num_stripes - 2; |
| 3017 | } else { |
| 3018 | factor = num_stripes; |
| 3019 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3020 | |
| 3021 | for (i = 0; i < num_stripes; i++) { |
| 3022 | stripe = btrfs_stripe_nr(chunk, i); |
| 3023 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3024 | continue; |
| 3025 | |
| 3026 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3027 | stripe_length = btrfs_chunk_length(leaf, chunk); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 3028 | stripe_length = div_u64(stripe_length, factor); |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3029 | |
| 3030 | if (stripe_offset < bargs->pend && |
| 3031 | stripe_offset + stripe_length > bargs->pstart) |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | return 1; |
| 3036 | } |
| 3037 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3038 | /* [vstart, vend) */ |
| 3039 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3040 | struct btrfs_chunk *chunk, |
| 3041 | u64 chunk_offset, |
| 3042 | struct btrfs_balance_args *bargs) |
| 3043 | { |
| 3044 | if (chunk_offset < bargs->vend && |
| 3045 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3046 | /* at least part of the chunk is inside this vrange */ |
| 3047 | return 0; |
| 3048 | |
| 3049 | return 1; |
| 3050 | } |
| 3051 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3052 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3053 | struct btrfs_balance_args *bargs) |
| 3054 | { |
| 3055 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3056 | return 0; |
| 3057 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3058 | chunk_type = chunk_to_extended(chunk_type) & |
| 3059 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3060 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3061 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3062 | return 1; |
| 3063 | |
| 3064 | return 0; |
| 3065 | } |
| 3066 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3067 | static int should_balance_chunk(struct btrfs_root *root, |
| 3068 | struct extent_buffer *leaf, |
| 3069 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3070 | { |
| 3071 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3072 | struct btrfs_balance_args *bargs = NULL; |
| 3073 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3074 | |
| 3075 | /* type filter */ |
| 3076 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3077 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3078 | return 0; |
| 3079 | } |
| 3080 | |
| 3081 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3082 | bargs = &bctl->data; |
| 3083 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3084 | bargs = &bctl->sys; |
| 3085 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3086 | bargs = &bctl->meta; |
| 3087 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3088 | /* profiles filter */ |
| 3089 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3090 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3091 | return 0; |
| 3092 | } |
| 3093 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3094 | /* usage filter */ |
| 3095 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3096 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3097 | return 0; |
| 3098 | } |
| 3099 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3100 | /* devid filter */ |
| 3101 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3102 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3103 | return 0; |
| 3104 | } |
| 3105 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3106 | /* drange filter, makes sense only with devid filter */ |
| 3107 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3108 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3109 | return 0; |
| 3110 | } |
| 3111 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3112 | /* vrange filter */ |
| 3113 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3114 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3115 | return 0; |
| 3116 | } |
| 3117 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3118 | /* soft profile changing mode */ |
| 3119 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3120 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3121 | return 0; |
| 3122 | } |
| 3123 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3124 | /* |
| 3125 | * limited by count, must be the last filter |
| 3126 | */ |
| 3127 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3128 | if (bargs->limit == 0) |
| 3129 | return 0; |
| 3130 | else |
| 3131 | bargs->limit--; |
| 3132 | } |
| 3133 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3134 | return 1; |
| 3135 | } |
| 3136 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3137 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3138 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3139 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3140 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3141 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3142 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3143 | struct btrfs_device *device; |
| 3144 | u64 old_size; |
| 3145 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3146 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3147 | struct btrfs_path *path; |
| 3148 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3149 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3150 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3151 | struct extent_buffer *leaf; |
| 3152 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3153 | int ret; |
| 3154 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3155 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3156 | u64 limit_data = bctl->data.limit; |
| 3157 | u64 limit_meta = bctl->meta.limit; |
| 3158 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3159 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3160 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3161 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3162 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3163 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3164 | size_to_free = div_factor(old_size, 1); |
| 3165 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3166 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3167 | btrfs_device_get_total_bytes(device) - |
| 3168 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3169 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3170 | continue; |
| 3171 | |
| 3172 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3173 | if (ret == -ENOSPC) |
| 3174 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3175 | BUG_ON(ret); |
| 3176 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3177 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3178 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3179 | |
| 3180 | ret = btrfs_grow_device(trans, device, old_size); |
| 3181 | BUG_ON(ret); |
| 3182 | |
| 3183 | btrfs_end_transaction(trans, dev_root); |
| 3184 | } |
| 3185 | |
| 3186 | /* step two, relocate all the chunks */ |
| 3187 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3188 | if (!path) { |
| 3189 | ret = -ENOMEM; |
| 3190 | goto error; |
| 3191 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3192 | |
| 3193 | /* zero out stat counters */ |
| 3194 | spin_lock(&fs_info->balance_lock); |
| 3195 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3196 | spin_unlock(&fs_info->balance_lock); |
| 3197 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3198 | if (!counting) { |
| 3199 | bctl->data.limit = limit_data; |
| 3200 | bctl->meta.limit = limit_meta; |
| 3201 | bctl->sys.limit = limit_sys; |
| 3202 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3203 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3204 | key.offset = (u64)-1; |
| 3205 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3206 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3207 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3208 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3209 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3210 | ret = -ECANCELED; |
| 3211 | goto error; |
| 3212 | } |
| 3213 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3214 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 3215 | if (ret < 0) |
| 3216 | goto error; |
| 3217 | |
| 3218 | /* |
| 3219 | * this shouldn't happen, it means the last relocate |
| 3220 | * failed |
| 3221 | */ |
| 3222 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3223 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3224 | |
| 3225 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3226 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3227 | if (ret) { |
| 3228 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3229 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3230 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3231 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3232 | leaf = path->nodes[0]; |
| 3233 | slot = path->slots[0]; |
| 3234 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3235 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3236 | if (found_key.objectid != key.objectid) |
| 3237 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3238 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3239 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3240 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3241 | if (!counting) { |
| 3242 | spin_lock(&fs_info->balance_lock); |
| 3243 | bctl->stat.considered++; |
| 3244 | spin_unlock(&fs_info->balance_lock); |
| 3245 | } |
| 3246 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3247 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3248 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3249 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3250 | if (!ret) |
| 3251 | goto loop; |
| 3252 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3253 | if (counting) { |
| 3254 | spin_lock(&fs_info->balance_lock); |
| 3255 | bctl->stat.expected++; |
| 3256 | spin_unlock(&fs_info->balance_lock); |
| 3257 | goto loop; |
| 3258 | } |
| 3259 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3260 | ret = btrfs_relocate_chunk(chunk_root, |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3261 | found_key.objectid, |
| 3262 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3263 | if (ret && ret != -ENOSPC) |
| 3264 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3265 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3266 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3267 | } else { |
| 3268 | spin_lock(&fs_info->balance_lock); |
| 3269 | bctl->stat.completed++; |
| 3270 | spin_unlock(&fs_info->balance_lock); |
| 3271 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3272 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3273 | if (found_key.offset == 0) |
| 3274 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3275 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3276 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3277 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3278 | if (counting) { |
| 3279 | btrfs_release_path(path); |
| 3280 | counting = false; |
| 3281 | goto again; |
| 3282 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3283 | error: |
| 3284 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3285 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3286 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3287 | enospc_errors); |
| 3288 | if (!ret) |
| 3289 | ret = -ENOSPC; |
| 3290 | } |
| 3291 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3292 | return ret; |
| 3293 | } |
| 3294 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3295 | /** |
| 3296 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3297 | * @flags: profile to validate |
| 3298 | * @extended: if true @flags is treated as an extended profile |
| 3299 | */ |
| 3300 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3301 | { |
| 3302 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3303 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3304 | |
| 3305 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3306 | |
| 3307 | /* 1) check that all other bits are zeroed */ |
| 3308 | if (flags & ~mask) |
| 3309 | return 0; |
| 3310 | |
| 3311 | /* 2) see if profile is reduced */ |
| 3312 | if (flags == 0) |
| 3313 | return !extended; /* "0" is valid for usual profiles */ |
| 3314 | |
| 3315 | /* true if exactly one bit set */ |
| 3316 | return (flags & (flags - 1)) == 0; |
| 3317 | } |
| 3318 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3319 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3320 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3321 | /* cancel requested || normal exit path */ |
| 3322 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3323 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3324 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3325 | } |
| 3326 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3327 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3328 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3329 | int ret; |
| 3330 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3331 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3332 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3333 | if (ret) |
| 3334 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3335 | |
| 3336 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3337 | } |
| 3338 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3339 | /* |
| 3340 | * Should be called with both balance and volume mutexes held |
| 3341 | */ |
| 3342 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3343 | struct btrfs_ioctl_balance_args *bargs) |
| 3344 | { |
| 3345 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3346 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3347 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3348 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3349 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3350 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3351 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3352 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3353 | atomic_read(&fs_info->balance_pause_req) || |
| 3354 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3355 | ret = -EINVAL; |
| 3356 | goto out; |
| 3357 | } |
| 3358 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3359 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3360 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3361 | mixed = 1; |
| 3362 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3363 | /* |
| 3364 | * In case of mixed groups both data and meta should be picked, |
| 3365 | * and identical options should be given for both of them. |
| 3366 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3367 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3368 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3369 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3370 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3371 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3372 | btrfs_err(fs_info, "with mixed groups data and " |
| 3373 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3374 | ret = -EINVAL; |
| 3375 | goto out; |
| 3376 | } |
| 3377 | } |
| 3378 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3379 | num_devices = fs_info->fs_devices->num_devices; |
| 3380 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3381 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3382 | BUG_ON(num_devices < 1); |
| 3383 | num_devices--; |
| 3384 | } |
| 3385 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3386 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3387 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3388 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3389 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3390 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3391 | if (num_devices > 2) |
| 3392 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3393 | if (num_devices > 3) |
| 3394 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3395 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3396 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3397 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3398 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3399 | btrfs_err(fs_info, "unable to start balance with target " |
| 3400 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3401 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3402 | ret = -EINVAL; |
| 3403 | goto out; |
| 3404 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3405 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3406 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3407 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3408 | btrfs_err(fs_info, |
| 3409 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3410 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3411 | ret = -EINVAL; |
| 3412 | goto out; |
| 3413 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3414 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3415 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3416 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3417 | btrfs_err(fs_info, |
| 3418 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3419 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3420 | ret = -EINVAL; |
| 3421 | goto out; |
| 3422 | } |
| 3423 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3424 | /* allow dup'ed data chunks only in mixed mode */ |
| 3425 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3426 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3427 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3428 | ret = -EINVAL; |
| 3429 | goto out; |
| 3430 | } |
| 3431 | |
| 3432 | /* allow to reduce meta or sys integrity only if force set */ |
| 3433 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3434 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3435 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3436 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3437 | do { |
| 3438 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3439 | |
| 3440 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3441 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3442 | !(bctl->sys.target & allowed)) || |
| 3443 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3444 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3445 | !(bctl->meta.target & allowed))) { |
| 3446 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3447 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3448 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3449 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3450 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3451 | ret = -EINVAL; |
| 3452 | goto out; |
| 3453 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3454 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3455 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3456 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3457 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3458 | int num_tolerated_disk_barrier_failures; |
| 3459 | u64 target = bctl->sys.target; |
| 3460 | |
| 3461 | num_tolerated_disk_barrier_failures = |
| 3462 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3463 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3464 | (target & |
| 3465 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3466 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3467 | num_tolerated_disk_barrier_failures = 0; |
| 3468 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3469 | (target & |
| 3470 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3471 | num_tolerated_disk_barrier_failures = 1; |
| 3472 | |
| 3473 | fs_info->num_tolerated_disk_barrier_failures = |
| 3474 | num_tolerated_disk_barrier_failures; |
| 3475 | } |
| 3476 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3477 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3478 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3479 | goto out; |
| 3480 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3481 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3482 | BUG_ON(ret == -EEXIST); |
| 3483 | set_balance_control(bctl); |
| 3484 | } else { |
| 3485 | BUG_ON(ret != -EEXIST); |
| 3486 | spin_lock(&fs_info->balance_lock); |
| 3487 | update_balance_args(bctl); |
| 3488 | spin_unlock(&fs_info->balance_lock); |
| 3489 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3490 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3491 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3492 | mutex_unlock(&fs_info->balance_mutex); |
| 3493 | |
| 3494 | ret = __btrfs_balance(fs_info); |
| 3495 | |
| 3496 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3497 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3498 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3499 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3500 | fs_info->num_tolerated_disk_barrier_failures = |
| 3501 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3502 | } |
| 3503 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3504 | if (bargs) { |
| 3505 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3506 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3507 | } |
| 3508 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3509 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3510 | balance_need_close(fs_info)) { |
| 3511 | __cancel_balance(fs_info); |
| 3512 | } |
| 3513 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3514 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3515 | |
| 3516 | return ret; |
| 3517 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3518 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3519 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3520 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3521 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3522 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3523 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3524 | return ret; |
| 3525 | } |
| 3526 | |
| 3527 | static int balance_kthread(void *data) |
| 3528 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3529 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3530 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3531 | |
| 3532 | mutex_lock(&fs_info->volume_mutex); |
| 3533 | mutex_lock(&fs_info->balance_mutex); |
| 3534 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3535 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3536 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3537 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3538 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3539 | |
| 3540 | mutex_unlock(&fs_info->balance_mutex); |
| 3541 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3542 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3543 | return ret; |
| 3544 | } |
| 3545 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3546 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3547 | { |
| 3548 | struct task_struct *tsk; |
| 3549 | |
| 3550 | spin_lock(&fs_info->balance_lock); |
| 3551 | if (!fs_info->balance_ctl) { |
| 3552 | spin_unlock(&fs_info->balance_lock); |
| 3553 | return 0; |
| 3554 | } |
| 3555 | spin_unlock(&fs_info->balance_lock); |
| 3556 | |
| 3557 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3558 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3559 | return 0; |
| 3560 | } |
| 3561 | |
| 3562 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3563 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3564 | } |
| 3565 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3566 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3567 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3568 | struct btrfs_balance_control *bctl; |
| 3569 | struct btrfs_balance_item *item; |
| 3570 | struct btrfs_disk_balance_args disk_bargs; |
| 3571 | struct btrfs_path *path; |
| 3572 | struct extent_buffer *leaf; |
| 3573 | struct btrfs_key key; |
| 3574 | int ret; |
| 3575 | |
| 3576 | path = btrfs_alloc_path(); |
| 3577 | if (!path) |
| 3578 | return -ENOMEM; |
| 3579 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3580 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3581 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3582 | key.offset = 0; |
| 3583 | |
| 3584 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3585 | if (ret < 0) |
| 3586 | goto out; |
| 3587 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3588 | ret = 0; |
| 3589 | goto out; |
| 3590 | } |
| 3591 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3592 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3593 | if (!bctl) { |
| 3594 | ret = -ENOMEM; |
| 3595 | goto out; |
| 3596 | } |
| 3597 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3598 | leaf = path->nodes[0]; |
| 3599 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3600 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3601 | bctl->fs_info = fs_info; |
| 3602 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3603 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3604 | |
| 3605 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3606 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3607 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3608 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3609 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3610 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3611 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3612 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3613 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3614 | mutex_lock(&fs_info->volume_mutex); |
| 3615 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3616 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3617 | set_balance_control(bctl); |
| 3618 | |
| 3619 | mutex_unlock(&fs_info->balance_mutex); |
| 3620 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3621 | out: |
| 3622 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3623 | return ret; |
| 3624 | } |
| 3625 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3626 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3627 | { |
| 3628 | int ret = 0; |
| 3629 | |
| 3630 | mutex_lock(&fs_info->balance_mutex); |
| 3631 | if (!fs_info->balance_ctl) { |
| 3632 | mutex_unlock(&fs_info->balance_mutex); |
| 3633 | return -ENOTCONN; |
| 3634 | } |
| 3635 | |
| 3636 | if (atomic_read(&fs_info->balance_running)) { |
| 3637 | atomic_inc(&fs_info->balance_pause_req); |
| 3638 | mutex_unlock(&fs_info->balance_mutex); |
| 3639 | |
| 3640 | wait_event(fs_info->balance_wait_q, |
| 3641 | atomic_read(&fs_info->balance_running) == 0); |
| 3642 | |
| 3643 | mutex_lock(&fs_info->balance_mutex); |
| 3644 | /* we are good with balance_ctl ripped off from under us */ |
| 3645 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3646 | atomic_dec(&fs_info->balance_pause_req); |
| 3647 | } else { |
| 3648 | ret = -ENOTCONN; |
| 3649 | } |
| 3650 | |
| 3651 | mutex_unlock(&fs_info->balance_mutex); |
| 3652 | return ret; |
| 3653 | } |
| 3654 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3655 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3656 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3657 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3658 | return -EROFS; |
| 3659 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3660 | mutex_lock(&fs_info->balance_mutex); |
| 3661 | if (!fs_info->balance_ctl) { |
| 3662 | mutex_unlock(&fs_info->balance_mutex); |
| 3663 | return -ENOTCONN; |
| 3664 | } |
| 3665 | |
| 3666 | atomic_inc(&fs_info->balance_cancel_req); |
| 3667 | /* |
| 3668 | * if we are running just wait and return, balance item is |
| 3669 | * deleted in btrfs_balance in this case |
| 3670 | */ |
| 3671 | if (atomic_read(&fs_info->balance_running)) { |
| 3672 | mutex_unlock(&fs_info->balance_mutex); |
| 3673 | wait_event(fs_info->balance_wait_q, |
| 3674 | atomic_read(&fs_info->balance_running) == 0); |
| 3675 | mutex_lock(&fs_info->balance_mutex); |
| 3676 | } else { |
| 3677 | /* __cancel_balance needs volume_mutex */ |
| 3678 | mutex_unlock(&fs_info->balance_mutex); |
| 3679 | mutex_lock(&fs_info->volume_mutex); |
| 3680 | mutex_lock(&fs_info->balance_mutex); |
| 3681 | |
| 3682 | if (fs_info->balance_ctl) |
| 3683 | __cancel_balance(fs_info); |
| 3684 | |
| 3685 | mutex_unlock(&fs_info->volume_mutex); |
| 3686 | } |
| 3687 | |
| 3688 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3689 | atomic_dec(&fs_info->balance_cancel_req); |
| 3690 | mutex_unlock(&fs_info->balance_mutex); |
| 3691 | return 0; |
| 3692 | } |
| 3693 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3694 | static int btrfs_uuid_scan_kthread(void *data) |
| 3695 | { |
| 3696 | struct btrfs_fs_info *fs_info = data; |
| 3697 | struct btrfs_root *root = fs_info->tree_root; |
| 3698 | struct btrfs_key key; |
| 3699 | struct btrfs_key max_key; |
| 3700 | struct btrfs_path *path = NULL; |
| 3701 | int ret = 0; |
| 3702 | struct extent_buffer *eb; |
| 3703 | int slot; |
| 3704 | struct btrfs_root_item root_item; |
| 3705 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3706 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3707 | |
| 3708 | path = btrfs_alloc_path(); |
| 3709 | if (!path) { |
| 3710 | ret = -ENOMEM; |
| 3711 | goto out; |
| 3712 | } |
| 3713 | |
| 3714 | key.objectid = 0; |
| 3715 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3716 | key.offset = 0; |
| 3717 | |
| 3718 | max_key.objectid = (u64)-1; |
| 3719 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3720 | max_key.offset = (u64)-1; |
| 3721 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3722 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3723 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3724 | if (ret) { |
| 3725 | if (ret > 0) |
| 3726 | ret = 0; |
| 3727 | break; |
| 3728 | } |
| 3729 | |
| 3730 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3731 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3732 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3733 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3734 | goto skip; |
| 3735 | |
| 3736 | eb = path->nodes[0]; |
| 3737 | slot = path->slots[0]; |
| 3738 | item_size = btrfs_item_size_nr(eb, slot); |
| 3739 | if (item_size < sizeof(root_item)) |
| 3740 | goto skip; |
| 3741 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3742 | read_extent_buffer(eb, &root_item, |
| 3743 | btrfs_item_ptr_offset(eb, slot), |
| 3744 | (int)sizeof(root_item)); |
| 3745 | if (btrfs_root_refs(&root_item) == 0) |
| 3746 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3747 | |
| 3748 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3749 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3750 | if (trans) |
| 3751 | goto update_tree; |
| 3752 | |
| 3753 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3754 | /* |
| 3755 | * 1 - subvol uuid item |
| 3756 | * 1 - received_subvol uuid item |
| 3757 | */ |
| 3758 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3759 | if (IS_ERR(trans)) { |
| 3760 | ret = PTR_ERR(trans); |
| 3761 | break; |
| 3762 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3763 | continue; |
| 3764 | } else { |
| 3765 | goto skip; |
| 3766 | } |
| 3767 | update_tree: |
| 3768 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3769 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3770 | root_item.uuid, |
| 3771 | BTRFS_UUID_KEY_SUBVOL, |
| 3772 | key.objectid); |
| 3773 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3774 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3775 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3776 | break; |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3781 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3782 | root_item.received_uuid, |
| 3783 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3784 | key.objectid); |
| 3785 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3786 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3787 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3788 | break; |
| 3789 | } |
| 3790 | } |
| 3791 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3792 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3793 | if (trans) { |
| 3794 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3795 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3796 | if (ret) |
| 3797 | break; |
| 3798 | } |
| 3799 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3800 | btrfs_release_path(path); |
| 3801 | if (key.offset < (u64)-1) { |
| 3802 | key.offset++; |
| 3803 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3804 | key.offset = 0; |
| 3805 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3806 | } else if (key.objectid < (u64)-1) { |
| 3807 | key.offset = 0; |
| 3808 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3809 | key.objectid++; |
| 3810 | } else { |
| 3811 | break; |
| 3812 | } |
| 3813 | cond_resched(); |
| 3814 | } |
| 3815 | |
| 3816 | out: |
| 3817 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3818 | if (trans && !IS_ERR(trans)) |
| 3819 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3820 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3821 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3822 | else |
| 3823 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3824 | up(&fs_info->uuid_tree_rescan_sem); |
| 3825 | return 0; |
| 3826 | } |
| 3827 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3828 | /* |
| 3829 | * Callback for btrfs_uuid_tree_iterate(). |
| 3830 | * returns: |
| 3831 | * 0 check succeeded, the entry is not outdated. |
| 3832 | * < 0 if an error occured. |
| 3833 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3834 | */ |
| 3835 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3836 | u8 *uuid, u8 type, u64 subid) |
| 3837 | { |
| 3838 | struct btrfs_key key; |
| 3839 | int ret = 0; |
| 3840 | struct btrfs_root *subvol_root; |
| 3841 | |
| 3842 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3843 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3844 | goto out; |
| 3845 | |
| 3846 | key.objectid = subid; |
| 3847 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3848 | key.offset = (u64)-1; |
| 3849 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3850 | if (IS_ERR(subvol_root)) { |
| 3851 | ret = PTR_ERR(subvol_root); |
| 3852 | if (ret == -ENOENT) |
| 3853 | ret = 1; |
| 3854 | goto out; |
| 3855 | } |
| 3856 | |
| 3857 | switch (type) { |
| 3858 | case BTRFS_UUID_KEY_SUBVOL: |
| 3859 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3860 | ret = 1; |
| 3861 | break; |
| 3862 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3863 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3864 | BTRFS_UUID_SIZE)) |
| 3865 | ret = 1; |
| 3866 | break; |
| 3867 | } |
| 3868 | |
| 3869 | out: |
| 3870 | return ret; |
| 3871 | } |
| 3872 | |
| 3873 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3874 | { |
| 3875 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3876 | int ret; |
| 3877 | |
| 3878 | /* |
| 3879 | * 1st step is to iterate through the existing UUID tree and |
| 3880 | * to delete all entries that contain outdated data. |
| 3881 | * 2nd step is to add all missing entries to the UUID tree. |
| 3882 | */ |
| 3883 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 3884 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3885 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3886 | up(&fs_info->uuid_tree_rescan_sem); |
| 3887 | return ret; |
| 3888 | } |
| 3889 | return btrfs_uuid_scan_kthread(data); |
| 3890 | } |
| 3891 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3892 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3893 | { |
| 3894 | struct btrfs_trans_handle *trans; |
| 3895 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 3896 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3897 | struct task_struct *task; |
| 3898 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3899 | |
| 3900 | /* |
| 3901 | * 1 - root node |
| 3902 | * 1 - root item |
| 3903 | */ |
| 3904 | trans = btrfs_start_transaction(tree_root, 2); |
| 3905 | if (IS_ERR(trans)) |
| 3906 | return PTR_ERR(trans); |
| 3907 | |
| 3908 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 3909 | BTRFS_UUID_TREE_OBJECTID); |
| 3910 | if (IS_ERR(uuid_root)) { |
David Sterba | 6d13f54 | 2015-04-24 19:12:01 +0200 | [diff] [blame^] | 3911 | ret = PTR_ERR(uuid_root); |
| 3912 | btrfs_abort_transaction(trans, tree_root, ret); |
| 3913 | return ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | fs_info->uuid_root = uuid_root; |
| 3917 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3918 | ret = btrfs_commit_transaction(trans, tree_root); |
| 3919 | if (ret) |
| 3920 | return ret; |
| 3921 | |
| 3922 | down(&fs_info->uuid_tree_rescan_sem); |
| 3923 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 3924 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3925 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3926 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3927 | up(&fs_info->uuid_tree_rescan_sem); |
| 3928 | return PTR_ERR(task); |
| 3929 | } |
| 3930 | |
| 3931 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3932 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3933 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3934 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3935 | { |
| 3936 | struct task_struct *task; |
| 3937 | |
| 3938 | down(&fs_info->uuid_tree_rescan_sem); |
| 3939 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 3940 | if (IS_ERR(task)) { |
| 3941 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3942 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3943 | up(&fs_info->uuid_tree_rescan_sem); |
| 3944 | return PTR_ERR(task); |
| 3945 | } |
| 3946 | |
| 3947 | return 0; |
| 3948 | } |
| 3949 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3950 | /* |
| 3951 | * shrinking a device means finding all of the device extents past |
| 3952 | * the new size, and then following the back refs to the chunks. |
| 3953 | * The chunk relocation code actually frees the device extent |
| 3954 | */ |
| 3955 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3956 | { |
| 3957 | struct btrfs_trans_handle *trans; |
| 3958 | struct btrfs_root *root = device->dev_root; |
| 3959 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3960 | struct btrfs_path *path; |
| 3961 | u64 length; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3962 | u64 chunk_objectid; |
| 3963 | u64 chunk_offset; |
| 3964 | int ret; |
| 3965 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3966 | int failed = 0; |
| 3967 | bool retried = false; |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 3968 | bool checked_pending_chunks = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3969 | struct extent_buffer *l; |
| 3970 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3971 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3972 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3973 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 3974 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3975 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3976 | if (device->is_tgtdev_for_dev_replace) |
| 3977 | return -EINVAL; |
| 3978 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3979 | path = btrfs_alloc_path(); |
| 3980 | if (!path) |
| 3981 | return -ENOMEM; |
| 3982 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3983 | path->reada = 2; |
| 3984 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3985 | lock_chunks(root); |
| 3986 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3987 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3988 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3989 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3990 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3991 | root->fs_info->free_chunk_space -= diff; |
| 3992 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3993 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3994 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3995 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3996 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3997 | key.objectid = device->devid; |
| 3998 | key.offset = (u64)-1; |
| 3999 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4000 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4001 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4002 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4003 | if (ret < 0) |
| 4004 | goto done; |
| 4005 | |
| 4006 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 4007 | if (ret < 0) |
| 4008 | goto done; |
| 4009 | if (ret) { |
| 4010 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4011 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4012 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | l = path->nodes[0]; |
| 4016 | slot = path->slots[0]; |
| 4017 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4018 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4019 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4020 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4021 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4022 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4023 | |
| 4024 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4025 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4026 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4027 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4028 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4029 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4030 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4031 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4032 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 4033 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4034 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4035 | |
Zhao Lei | a688a04 | 2015-02-09 20:31:44 +0800 | [diff] [blame] | 4036 | ret = btrfs_relocate_chunk(root, chunk_objectid, chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4037 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4038 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4039 | if (ret == -ENOSPC) |
| 4040 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4041 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4042 | |
| 4043 | if (failed && !retried) { |
| 4044 | failed = 0; |
| 4045 | retried = true; |
| 4046 | goto again; |
| 4047 | } else if (failed && retried) { |
| 4048 | ret = -ENOSPC; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4049 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4050 | } |
| 4051 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4052 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4053 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4054 | if (IS_ERR(trans)) { |
| 4055 | ret = PTR_ERR(trans); |
| 4056 | goto done; |
| 4057 | } |
| 4058 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4059 | lock_chunks(root); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4060 | |
| 4061 | /* |
| 4062 | * We checked in the above loop all device extents that were already in |
| 4063 | * the device tree. However before we have updated the device's |
| 4064 | * total_bytes to the new size, we might have had chunk allocations that |
| 4065 | * have not complete yet (new block groups attached to transaction |
| 4066 | * handles), and therefore their device extents were not yet in the |
| 4067 | * device tree and we missed them in the loop above. So if we have any |
| 4068 | * pending chunk using a device extent that overlaps the device range |
| 4069 | * that we can not use anymore, commit the current transaction and |
| 4070 | * repeat the search on the device tree - this way we guarantee we will |
| 4071 | * not have chunks using device extents that end beyond 'new_size'. |
| 4072 | */ |
| 4073 | if (!checked_pending_chunks) { |
| 4074 | u64 start = new_size; |
| 4075 | u64 len = old_size - new_size; |
| 4076 | |
| 4077 | if (contains_pending_extent(trans, device, &start, len)) { |
| 4078 | unlock_chunks(root); |
| 4079 | checked_pending_chunks = true; |
| 4080 | failed = 0; |
| 4081 | retried = false; |
| 4082 | ret = btrfs_commit_transaction(trans, root); |
| 4083 | if (ret) |
| 4084 | goto done; |
| 4085 | goto again; |
| 4086 | } |
| 4087 | } |
| 4088 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4089 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4090 | if (list_empty(&device->resized_list)) |
| 4091 | list_add_tail(&device->resized_list, |
| 4092 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4093 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4094 | WARN_ON(diff > old_total); |
| 4095 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4096 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4097 | |
| 4098 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4099 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4100 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4101 | done: |
| 4102 | btrfs_free_path(path); |
Filipe Manana | 53e489b | 2015-06-02 14:43:21 +0100 | [diff] [blame] | 4103 | if (ret) { |
| 4104 | lock_chunks(root); |
| 4105 | btrfs_device_set_total_bytes(device, old_size); |
| 4106 | if (device->writeable) |
| 4107 | device->fs_devices->total_rw_bytes += diff; |
| 4108 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4109 | root->fs_info->free_chunk_space += diff; |
| 4110 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4111 | unlock_chunks(root); |
| 4112 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4113 | return ret; |
| 4114 | } |
| 4115 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4116 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4117 | struct btrfs_key *key, |
| 4118 | struct btrfs_chunk *chunk, int item_size) |
| 4119 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4120 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4121 | struct btrfs_disk_key disk_key; |
| 4122 | u32 array_size; |
| 4123 | u8 *ptr; |
| 4124 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4125 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4126 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4127 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4128 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4129 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4130 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4131 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4132 | |
| 4133 | ptr = super_copy->sys_chunk_array + array_size; |
| 4134 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4135 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4136 | ptr += sizeof(disk_key); |
| 4137 | memcpy(ptr, chunk, item_size); |
| 4138 | item_size += sizeof(disk_key); |
| 4139 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4140 | unlock_chunks(root); |
| 4141 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4145 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4146 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4147 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4148 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4149 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4150 | const struct btrfs_device_info *di_a = a; |
| 4151 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4152 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4153 | if (di_a->max_avail > di_b->max_avail) |
| 4154 | return -1; |
| 4155 | if (di_a->max_avail < di_b->max_avail) |
| 4156 | return 1; |
| 4157 | if (di_a->total_avail > di_b->total_avail) |
| 4158 | return -1; |
| 4159 | if (di_a->total_avail < di_b->total_avail) |
| 4160 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4161 | return 0; |
| 4162 | } |
| 4163 | |
David Sterba | e8c9f18 | 2015-01-02 18:23:10 +0100 | [diff] [blame] | 4164 | static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 4165 | [BTRFS_RAID_RAID10] = { |
| 4166 | .sub_stripes = 2, |
| 4167 | .dev_stripes = 1, |
| 4168 | .devs_max = 0, /* 0 == as many as possible */ |
| 4169 | .devs_min = 4, |
| 4170 | .devs_increment = 2, |
| 4171 | .ncopies = 2, |
| 4172 | }, |
| 4173 | [BTRFS_RAID_RAID1] = { |
| 4174 | .sub_stripes = 1, |
| 4175 | .dev_stripes = 1, |
| 4176 | .devs_max = 2, |
| 4177 | .devs_min = 2, |
| 4178 | .devs_increment = 2, |
| 4179 | .ncopies = 2, |
| 4180 | }, |
| 4181 | [BTRFS_RAID_DUP] = { |
| 4182 | .sub_stripes = 1, |
| 4183 | .dev_stripes = 2, |
| 4184 | .devs_max = 1, |
| 4185 | .devs_min = 1, |
| 4186 | .devs_increment = 1, |
| 4187 | .ncopies = 2, |
| 4188 | }, |
| 4189 | [BTRFS_RAID_RAID0] = { |
| 4190 | .sub_stripes = 1, |
| 4191 | .dev_stripes = 1, |
| 4192 | .devs_max = 0, |
| 4193 | .devs_min = 2, |
| 4194 | .devs_increment = 1, |
| 4195 | .ncopies = 1, |
| 4196 | }, |
| 4197 | [BTRFS_RAID_SINGLE] = { |
| 4198 | .sub_stripes = 1, |
| 4199 | .dev_stripes = 1, |
| 4200 | .devs_max = 1, |
| 4201 | .devs_min = 1, |
| 4202 | .devs_increment = 1, |
| 4203 | .ncopies = 1, |
| 4204 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4205 | [BTRFS_RAID_RAID5] = { |
| 4206 | .sub_stripes = 1, |
| 4207 | .dev_stripes = 1, |
| 4208 | .devs_max = 0, |
| 4209 | .devs_min = 2, |
| 4210 | .devs_increment = 1, |
| 4211 | .ncopies = 2, |
| 4212 | }, |
| 4213 | [BTRFS_RAID_RAID6] = { |
| 4214 | .sub_stripes = 1, |
| 4215 | .dev_stripes = 1, |
| 4216 | .devs_max = 0, |
| 4217 | .devs_min = 3, |
| 4218 | .devs_increment = 1, |
| 4219 | .ncopies = 3, |
| 4220 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4221 | }; |
| 4222 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4223 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4224 | { |
| 4225 | /* TODO allow them to set a preferred stripe size */ |
| 4226 | return 64 * 1024; |
| 4227 | } |
| 4228 | |
| 4229 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4230 | { |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4231 | if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK)) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4232 | return; |
| 4233 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4234 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4235 | } |
| 4236 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4237 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4238 | - sizeof(struct btrfs_item) \ |
| 4239 | - sizeof(struct btrfs_chunk)) \ |
| 4240 | / sizeof(struct btrfs_stripe) + 1) |
| 4241 | |
| 4242 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4243 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4244 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4245 | / sizeof(struct btrfs_stripe) + 1) |
| 4246 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4247 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4248 | struct btrfs_root *extent_root, u64 start, |
| 4249 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4250 | { |
| 4251 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4252 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4253 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4254 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4255 | struct extent_map_tree *em_tree; |
| 4256 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4257 | struct btrfs_device_info *devices_info = NULL; |
| 4258 | u64 total_avail; |
| 4259 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4260 | int data_stripes; /* number of stripes that count for |
| 4261 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4262 | int sub_stripes; /* sub_stripes info for map */ |
| 4263 | int dev_stripes; /* stripes per dev */ |
| 4264 | int devs_max; /* max devs to use */ |
| 4265 | int devs_min; /* min devs needed */ |
| 4266 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4267 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4268 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4269 | u64 max_stripe_size; |
| 4270 | u64 max_chunk_size; |
| 4271 | u64 stripe_size; |
| 4272 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4273 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4274 | int ndevs; |
| 4275 | int i; |
| 4276 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4277 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4278 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4279 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4280 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4281 | if (list_empty(&fs_devices->alloc_list)) |
| 4282 | return -ENOSPC; |
| 4283 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4284 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4285 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4286 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4287 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4288 | devs_max = btrfs_raid_array[index].devs_max; |
| 4289 | devs_min = btrfs_raid_array[index].devs_min; |
| 4290 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4291 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4292 | |
| 4293 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4294 | max_stripe_size = 1024 * 1024 * 1024; |
| 4295 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4296 | if (!devs_max) |
| 4297 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4298 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4299 | /* for larger filesystems, use larger metadata chunks */ |
| 4300 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4301 | max_stripe_size = 1024 * 1024 * 1024; |
| 4302 | else |
| 4303 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4304 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4305 | if (!devs_max) |
| 4306 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4307 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4308 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4309 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4310 | if (!devs_max) |
| 4311 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4312 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4313 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4314 | type); |
| 4315 | BUG_ON(1); |
| 4316 | } |
| 4317 | |
| 4318 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4319 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4320 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4321 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 4322 | devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info), |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4323 | GFP_NOFS); |
| 4324 | if (!devices_info) |
| 4325 | return -ENOMEM; |
| 4326 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4327 | cur = fs_devices->alloc_list.next; |
| 4328 | |
| 4329 | /* |
| 4330 | * in the first pass through the devices list, we gather information |
| 4331 | * about the available holes on each device. |
| 4332 | */ |
| 4333 | ndevs = 0; |
| 4334 | while (cur != &fs_devices->alloc_list) { |
| 4335 | struct btrfs_device *device; |
| 4336 | u64 max_avail; |
| 4337 | u64 dev_offset; |
| 4338 | |
| 4339 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4340 | |
| 4341 | cur = cur->next; |
| 4342 | |
| 4343 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4344 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4345 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4346 | continue; |
| 4347 | } |
| 4348 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4349 | if (!device->in_fs_metadata || |
| 4350 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4351 | continue; |
| 4352 | |
| 4353 | if (device->total_bytes > device->bytes_used) |
| 4354 | total_avail = device->total_bytes - device->bytes_used; |
| 4355 | else |
| 4356 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4357 | |
| 4358 | /* If there is no space on this device, skip it. */ |
| 4359 | if (total_avail == 0) |
| 4360 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4361 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4362 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4363 | max_stripe_size * dev_stripes, |
| 4364 | &dev_offset, &max_avail); |
| 4365 | if (ret && ret != -ENOSPC) |
| 4366 | goto error; |
| 4367 | |
| 4368 | if (ret == 0) |
| 4369 | max_avail = max_stripe_size * dev_stripes; |
| 4370 | |
| 4371 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4372 | continue; |
| 4373 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4374 | if (ndevs == fs_devices->rw_devices) { |
| 4375 | WARN(1, "%s: found more than %llu devices\n", |
| 4376 | __func__, fs_devices->rw_devices); |
| 4377 | break; |
| 4378 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4379 | devices_info[ndevs].dev_offset = dev_offset; |
| 4380 | devices_info[ndevs].max_avail = max_avail; |
| 4381 | devices_info[ndevs].total_avail = total_avail; |
| 4382 | devices_info[ndevs].dev = device; |
| 4383 | ++ndevs; |
| 4384 | } |
| 4385 | |
| 4386 | /* |
| 4387 | * now sort the devices by hole size / available space |
| 4388 | */ |
| 4389 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4390 | btrfs_cmp_device_info, NULL); |
| 4391 | |
| 4392 | /* round down to number of usable stripes */ |
| 4393 | ndevs -= ndevs % devs_increment; |
| 4394 | |
| 4395 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4396 | ret = -ENOSPC; |
| 4397 | goto error; |
| 4398 | } |
| 4399 | |
| 4400 | if (devs_max && ndevs > devs_max) |
| 4401 | ndevs = devs_max; |
| 4402 | /* |
| 4403 | * the primary goal is to maximize the number of stripes, so use as many |
| 4404 | * devices as possible, even if the stripes are not maximum sized. |
| 4405 | */ |
| 4406 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4407 | num_stripes = ndevs * dev_stripes; |
| 4408 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4409 | /* |
| 4410 | * this will have to be fixed for RAID1 and RAID10 over |
| 4411 | * more drives |
| 4412 | */ |
| 4413 | data_stripes = num_stripes / ncopies; |
| 4414 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4415 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4416 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4417 | btrfs_super_stripesize(info->super_copy)); |
| 4418 | data_stripes = num_stripes - 1; |
| 4419 | } |
| 4420 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4421 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4422 | btrfs_super_stripesize(info->super_copy)); |
| 4423 | data_stripes = num_stripes - 2; |
| 4424 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4425 | |
| 4426 | /* |
| 4427 | * Use the number of data stripes to figure out how big this chunk |
| 4428 | * is really going to be in terms of logical address space, |
| 4429 | * and compare that answer with the max chunk size |
| 4430 | */ |
| 4431 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4432 | u64 mask = (1ULL << 24) - 1; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4433 | |
| 4434 | stripe_size = div_u64(max_chunk_size, data_stripes); |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4435 | |
| 4436 | /* bump the answer up to a 16MB boundary */ |
| 4437 | stripe_size = (stripe_size + mask) & ~mask; |
| 4438 | |
| 4439 | /* but don't go higher than the limits we found |
| 4440 | * while searching for free extents |
| 4441 | */ |
| 4442 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4443 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4444 | } |
| 4445 | |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4446 | stripe_size = div_u64(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4447 | |
| 4448 | /* align to BTRFS_STRIPE_LEN */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 4449 | stripe_size = div_u64(stripe_size, raid_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4450 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4451 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4452 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4453 | if (!map) { |
| 4454 | ret = -ENOMEM; |
| 4455 | goto error; |
| 4456 | } |
| 4457 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4458 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4459 | for (i = 0; i < ndevs; ++i) { |
| 4460 | for (j = 0; j < dev_stripes; ++j) { |
| 4461 | int s = i * dev_stripes + j; |
| 4462 | map->stripes[s].dev = devices_info[i].dev; |
| 4463 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4464 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4465 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4466 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4467 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4468 | map->stripe_len = raid_stripe_len; |
| 4469 | map->io_align = raid_stripe_len; |
| 4470 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4471 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4472 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4473 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4474 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4475 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4476 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4477 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4478 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4479 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4480 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4481 | ret = -ENOMEM; |
| 4482 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4483 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4484 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4485 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4486 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4487 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4488 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4489 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4490 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4491 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4492 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4493 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4494 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4495 | if (!ret) { |
| 4496 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4497 | atomic_inc(&em->refs); |
| 4498 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4499 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4500 | if (ret) { |
| 4501 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4502 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4503 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4504 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4505 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4506 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4507 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4508 | if (ret) |
| 4509 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4510 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4511 | for (i = 0; i < map->num_stripes; i++) { |
| 4512 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4513 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4514 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4515 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4516 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4517 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4518 | map->num_stripes); |
| 4519 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4520 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4521 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4522 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4523 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4524 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4525 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4526 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4527 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4528 | write_lock(&em_tree->lock); |
| 4529 | remove_extent_mapping(em_tree, em); |
| 4530 | write_unlock(&em_tree->lock); |
| 4531 | |
| 4532 | /* One for our allocation */ |
| 4533 | free_extent_map(em); |
| 4534 | /* One for the tree reference */ |
| 4535 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4536 | /* One for the pending_chunks list reference */ |
| 4537 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4538 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4539 | kfree(devices_info); |
| 4540 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4541 | } |
| 4542 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4543 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4544 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4545 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4546 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4547 | struct btrfs_key key; |
| 4548 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4549 | struct btrfs_device *device; |
| 4550 | struct btrfs_chunk *chunk; |
| 4551 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4552 | struct extent_map_tree *em_tree; |
| 4553 | struct extent_map *em; |
| 4554 | struct map_lookup *map; |
| 4555 | size_t item_size; |
| 4556 | u64 dev_offset; |
| 4557 | u64 stripe_size; |
| 4558 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4559 | int ret; |
| 4560 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4561 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4562 | read_lock(&em_tree->lock); |
| 4563 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4564 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4565 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4566 | if (!em) { |
| 4567 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4568 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4569 | return -EINVAL; |
| 4570 | } |
| 4571 | |
| 4572 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4573 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4574 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4575 | chunk_size, em->start, em->len); |
| 4576 | free_extent_map(em); |
| 4577 | return -EINVAL; |
| 4578 | } |
| 4579 | |
| 4580 | map = (struct map_lookup *)em->bdev; |
| 4581 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4582 | stripe_size = em->orig_block_len; |
| 4583 | |
| 4584 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4585 | if (!chunk) { |
| 4586 | ret = -ENOMEM; |
| 4587 | goto out; |
| 4588 | } |
| 4589 | |
| 4590 | for (i = 0; i < map->num_stripes; i++) { |
| 4591 | device = map->stripes[i].dev; |
| 4592 | dev_offset = map->stripes[i].physical; |
| 4593 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4594 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4595 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4596 | goto out; |
| 4597 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4598 | chunk_root->root_key.objectid, |
| 4599 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4600 | chunk_offset, dev_offset, |
| 4601 | stripe_size); |
| 4602 | if (ret) |
| 4603 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4604 | } |
| 4605 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4606 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4607 | for (i = 0; i < map->num_stripes; i++) { |
| 4608 | device = map->stripes[i].dev; |
| 4609 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4610 | |
| 4611 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4612 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4613 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4614 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4618 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4619 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4620 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4621 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4622 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4623 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4624 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4625 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4626 | |
| 4627 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4628 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4629 | key.offset = chunk_offset; |
| 4630 | |
| 4631 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4632 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4633 | /* |
| 4634 | * TODO: Cleanup of inserted chunk root in case of |
| 4635 | * failure. |
| 4636 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4637 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4638 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4639 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4640 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4641 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4642 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4643 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4644 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | /* |
| 4648 | * Chunk allocation falls into two parts. The first part does works |
| 4649 | * that make the new allocated chunk useable, but not do any operation |
| 4650 | * that modifies the chunk tree. The second part does the works that |
| 4651 | * require modifying the chunk tree. This division is important for the |
| 4652 | * bootstrap process of adding storage to a seed btrfs. |
| 4653 | */ |
| 4654 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4655 | struct btrfs_root *extent_root, u64 type) |
| 4656 | { |
| 4657 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4658 | |
Filipe Manana | a962959 | 2015-05-18 19:11:40 +0100 | [diff] [blame] | 4659 | ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex)); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4660 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4661 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4662 | } |
| 4663 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4664 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4665 | struct btrfs_root *root, |
| 4666 | struct btrfs_device *device) |
| 4667 | { |
| 4668 | u64 chunk_offset; |
| 4669 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4670 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4671 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4672 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4673 | int ret; |
| 4674 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4675 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4676 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4677 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4678 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4679 | if (ret) |
| 4680 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4681 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4682 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4683 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4684 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4685 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4686 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4687 | } |
| 4688 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4689 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4690 | { |
| 4691 | int max_errors; |
| 4692 | |
| 4693 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4694 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4695 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4696 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4697 | max_errors = 1; |
| 4698 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4699 | max_errors = 2; |
| 4700 | } else { |
| 4701 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4702 | } |
| 4703 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4704 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4708 | { |
| 4709 | struct extent_map *em; |
| 4710 | struct map_lookup *map; |
| 4711 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4712 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4713 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4714 | int i; |
| 4715 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4716 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4717 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4718 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4719 | if (!em) |
| 4720 | return 1; |
| 4721 | |
| 4722 | map = (struct map_lookup *)em->bdev; |
| 4723 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4724 | if (map->stripes[i].dev->missing) { |
| 4725 | miss_ndevs++; |
| 4726 | continue; |
| 4727 | } |
| 4728 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4729 | if (!map->stripes[i].dev->writeable) { |
| 4730 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4731 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4732 | } |
| 4733 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4734 | |
| 4735 | /* |
| 4736 | * If the number of missing devices is larger than max errors, |
| 4737 | * we can not write the data into that chunk successfully, so |
| 4738 | * set it readonly. |
| 4739 | */ |
| 4740 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4741 | readonly = 1; |
| 4742 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4743 | free_extent_map(em); |
| 4744 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4748 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4749 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4753 | { |
| 4754 | struct extent_map *em; |
| 4755 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4756 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4757 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4758 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4759 | if (em) |
| 4760 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4761 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4762 | if (!em) |
| 4763 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4764 | /* once for us */ |
| 4765 | free_extent_map(em); |
| 4766 | /* once for the tree */ |
| 4767 | free_extent_map(em); |
| 4768 | } |
| 4769 | } |
| 4770 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4771 | 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] | 4772 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4773 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4774 | struct extent_map *em; |
| 4775 | struct map_lookup *map; |
| 4776 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4777 | int ret; |
| 4778 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4779 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4780 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4781 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4782 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4783 | /* |
| 4784 | * We could return errors for these cases, but that could get ugly and |
| 4785 | * we'd probably do the same thing which is just not do anything else |
| 4786 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4787 | */ |
| 4788 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4789 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4790 | logical+len); |
| 4791 | return 1; |
| 4792 | } |
| 4793 | |
| 4794 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4795 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4796 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4797 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4798 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4799 | return 1; |
| 4800 | } |
| 4801 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4802 | map = (struct map_lookup *)em->bdev; |
| 4803 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4804 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4805 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4806 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4807 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4808 | ret = 2; |
| 4809 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4810 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4811 | else |
| 4812 | ret = 1; |
| 4813 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4814 | |
| 4815 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4816 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4817 | ret++; |
| 4818 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4819 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4820 | return ret; |
| 4821 | } |
| 4822 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4823 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4824 | struct btrfs_mapping_tree *map_tree, |
| 4825 | u64 logical) |
| 4826 | { |
| 4827 | struct extent_map *em; |
| 4828 | struct map_lookup *map; |
| 4829 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4830 | unsigned long len = root->sectorsize; |
| 4831 | |
| 4832 | read_lock(&em_tree->lock); |
| 4833 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4834 | read_unlock(&em_tree->lock); |
| 4835 | BUG_ON(!em); |
| 4836 | |
| 4837 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4838 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4839 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4840 | len = map->stripe_len * nr_data_stripes(map); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4841 | free_extent_map(em); |
| 4842 | return len; |
| 4843 | } |
| 4844 | |
| 4845 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4846 | u64 logical, u64 len, int mirror_num) |
| 4847 | { |
| 4848 | struct extent_map *em; |
| 4849 | struct map_lookup *map; |
| 4850 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4851 | int ret = 0; |
| 4852 | |
| 4853 | read_lock(&em_tree->lock); |
| 4854 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4855 | read_unlock(&em_tree->lock); |
| 4856 | BUG_ON(!em); |
| 4857 | |
| 4858 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4859 | map = (struct map_lookup *)em->bdev; |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 4860 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4861 | ret = 1; |
| 4862 | free_extent_map(em); |
| 4863 | return ret; |
| 4864 | } |
| 4865 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4866 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4867 | struct map_lookup *map, int first, int num, |
| 4868 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4869 | { |
| 4870 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4871 | int tolerance; |
| 4872 | struct btrfs_device *srcdev; |
| 4873 | |
| 4874 | if (dev_replace_is_ongoing && |
| 4875 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4876 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4877 | srcdev = fs_info->dev_replace.srcdev; |
| 4878 | else |
| 4879 | srcdev = NULL; |
| 4880 | |
| 4881 | /* |
| 4882 | * try to avoid the drive that is the source drive for a |
| 4883 | * dev-replace procedure, only choose it if no other non-missing |
| 4884 | * mirror is available |
| 4885 | */ |
| 4886 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4887 | if (map->stripes[optimal].dev->bdev && |
| 4888 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4889 | return optimal; |
| 4890 | for (i = first; i < first + num; i++) { |
| 4891 | if (map->stripes[i].dev->bdev && |
| 4892 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4893 | return i; |
| 4894 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4895 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4896 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4897 | /* we couldn't find one that doesn't fail. Just return something |
| 4898 | * and the io error handling code will clean up eventually |
| 4899 | */ |
| 4900 | return optimal; |
| 4901 | } |
| 4902 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4903 | static inline int parity_smaller(u64 a, u64 b) |
| 4904 | { |
| 4905 | return a > b; |
| 4906 | } |
| 4907 | |
| 4908 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4909 | static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4910 | { |
| 4911 | struct btrfs_bio_stripe s; |
| 4912 | int i; |
| 4913 | u64 l; |
| 4914 | int again = 1; |
| 4915 | |
| 4916 | while (again) { |
| 4917 | again = 0; |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 4918 | for (i = 0; i < num_stripes - 1; i++) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4919 | if (parity_smaller(bbio->raid_map[i], |
| 4920 | bbio->raid_map[i+1])) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4921 | s = bbio->stripes[i]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4922 | l = bbio->raid_map[i]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4923 | bbio->stripes[i] = bbio->stripes[i+1]; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4924 | bbio->raid_map[i] = bbio->raid_map[i+1]; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4925 | bbio->stripes[i+1] = s; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4926 | bbio->raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4927 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4928 | again = 1; |
| 4929 | } |
| 4930 | } |
| 4931 | } |
| 4932 | } |
| 4933 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4934 | static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes) |
| 4935 | { |
| 4936 | struct btrfs_bio *bbio = kzalloc( |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4937 | /* the size of the btrfs_bio */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4938 | sizeof(struct btrfs_bio) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4939 | /* plus the variable array for the stripes */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4940 | sizeof(struct btrfs_bio_stripe) * (total_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4941 | /* plus the variable array for the tgt dev */ |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4942 | sizeof(int) * (real_stripes) + |
Chris Mason | e57cf21 | 2015-02-19 17:51:39 -0800 | [diff] [blame] | 4943 | /* |
| 4944 | * plus the raid_map, which includes both the tgt dev |
| 4945 | * and the stripes |
| 4946 | */ |
| 4947 | sizeof(u64) * (total_stripes), |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 4948 | GFP_NOFS); |
| 4949 | if (!bbio) |
| 4950 | return NULL; |
| 4951 | |
| 4952 | atomic_set(&bbio->error, 0); |
| 4953 | atomic_set(&bbio->refs, 1); |
| 4954 | |
| 4955 | return bbio; |
| 4956 | } |
| 4957 | |
| 4958 | void btrfs_get_bbio(struct btrfs_bio *bbio) |
| 4959 | { |
| 4960 | WARN_ON(!atomic_read(&bbio->refs)); |
| 4961 | atomic_inc(&bbio->refs); |
| 4962 | } |
| 4963 | |
| 4964 | void btrfs_put_bbio(struct btrfs_bio *bbio) |
| 4965 | { |
| 4966 | if (!bbio) |
| 4967 | return; |
| 4968 | if (atomic_dec_and_test(&bbio->refs)) |
| 4969 | kfree(bbio); |
| 4970 | } |
| 4971 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4972 | 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] | 4973 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4974 | struct btrfs_bio **bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 4975 | int mirror_num, int need_raid_map) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4976 | { |
| 4977 | struct extent_map *em; |
| 4978 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4979 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4980 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4981 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4982 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4983 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4984 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4985 | u64 stripe_nr_orig; |
| 4986 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4987 | u64 stripe_len; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 4988 | u32 stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4989 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4990 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4991 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4992 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4993 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4994 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4995 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4996 | int dev_replace_is_ongoing = 0; |
| 4997 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4998 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4999 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5000 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5001 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5002 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5003 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5004 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5005 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5006 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5007 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5008 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5009 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5010 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5011 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5012 | if (em->start > logical || em->start + em->len < logical) { |
| 5013 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 5014 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5015 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 5016 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 5017 | return -EINVAL; |
| 5018 | } |
| 5019 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5020 | map = (struct map_lookup *)em->bdev; |
| 5021 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5022 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5023 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5024 | stripe_nr = offset; |
| 5025 | /* |
| 5026 | * stripe_nr counts the total number of stripes we have to stride |
| 5027 | * to get to this block |
| 5028 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5029 | stripe_nr = div64_u64(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5030 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5031 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5032 | BUG_ON(offset < stripe_offset); |
| 5033 | |
| 5034 | /* stripe_offset is the offset of this block in its stripe*/ |
| 5035 | stripe_offset = offset - stripe_offset; |
| 5036 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5037 | /* 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] | 5038 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5039 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 5040 | raid56_full_stripe_start = offset; |
| 5041 | |
| 5042 | /* allow a write of a full stripe, but make sure we don't |
| 5043 | * allow straddling of stripes |
| 5044 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5045 | raid56_full_stripe_start = div64_u64(raid56_full_stripe_start, |
| 5046 | full_stripe_len); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5047 | raid56_full_stripe_start *= full_stripe_len; |
| 5048 | } |
| 5049 | |
| 5050 | if (rw & REQ_DISCARD) { |
| 5051 | /* we don't discard raid56 yet */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5052 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5053 | ret = -EOPNOTSUPP; |
| 5054 | goto out; |
| 5055 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5056 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5057 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 5058 | u64 max_len; |
| 5059 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5060 | For other RAID types and for RAID[56] reads, just allow a single |
| 5061 | stripe (on a single disk). */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5062 | if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) && |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5063 | (rw & REQ_WRITE)) { |
| 5064 | max_len = stripe_len * nr_data_stripes(map) - |
| 5065 | (offset - raid56_full_stripe_start); |
| 5066 | } else { |
| 5067 | /* we limit the length of each bio to what fits in a stripe */ |
| 5068 | max_len = stripe_len - stripe_offset; |
| 5069 | } |
| 5070 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5071 | } else { |
| 5072 | *length = em->len - offset; |
| 5073 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5074 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5075 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5076 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5077 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5078 | goto out; |
| 5079 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5080 | btrfs_dev_replace_lock(dev_replace); |
| 5081 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5082 | if (!dev_replace_is_ongoing) |
| 5083 | btrfs_dev_replace_unlock(dev_replace); |
| 5084 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5085 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5086 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5087 | dev_replace->tgtdev != NULL) { |
| 5088 | /* |
| 5089 | * in dev-replace case, for repair case (that's the only |
| 5090 | * case where the mirror is selected explicitly when |
| 5091 | * calling btrfs_map_block), blocks left of the left cursor |
| 5092 | * can also be read from the target drive. |
| 5093 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5094 | * the last one to the array of stripes. For READ, it also |
| 5095 | * needs to be supported using the same mirror number. |
| 5096 | * If the requested block is not left of the left cursor, |
| 5097 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5098 | * returns one more in the dev-replace case. |
| 5099 | */ |
| 5100 | u64 tmp_length = *length; |
| 5101 | struct btrfs_bio *tmp_bbio = NULL; |
| 5102 | int tmp_num_stripes; |
| 5103 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5104 | int index_srcdev = 0; |
| 5105 | int found = 0; |
| 5106 | u64 physical_of_found = 0; |
| 5107 | |
| 5108 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5109 | logical, &tmp_length, &tmp_bbio, 0, 0); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5110 | if (ret) { |
| 5111 | WARN_ON(tmp_bbio != NULL); |
| 5112 | goto out; |
| 5113 | } |
| 5114 | |
| 5115 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5116 | if (mirror_num > tmp_num_stripes) { |
| 5117 | /* |
| 5118 | * REQ_GET_READ_MIRRORS does not contain this |
| 5119 | * mirror, that means that the requested area |
| 5120 | * is not left of the left cursor |
| 5121 | */ |
| 5122 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5123 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5124 | goto out; |
| 5125 | } |
| 5126 | |
| 5127 | /* |
| 5128 | * process the rest of the function using the mirror_num |
| 5129 | * of the source drive. Therefore look it up first. |
| 5130 | * At the end, patch the device pointer to the one of the |
| 5131 | * target drive. |
| 5132 | */ |
| 5133 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5134 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5135 | /* |
| 5136 | * In case of DUP, in order to keep it |
| 5137 | * simple, only add the mirror with the |
| 5138 | * lowest physical address |
| 5139 | */ |
| 5140 | if (found && |
| 5141 | physical_of_found <= |
| 5142 | tmp_bbio->stripes[i].physical) |
| 5143 | continue; |
| 5144 | index_srcdev = i; |
| 5145 | found = 1; |
| 5146 | physical_of_found = |
| 5147 | tmp_bbio->stripes[i].physical; |
| 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | if (found) { |
| 5152 | mirror_num = index_srcdev + 1; |
| 5153 | patch_the_first_stripe_for_dev_replace = 1; |
| 5154 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5155 | } else { |
| 5156 | WARN_ON(1); |
| 5157 | ret = -EIO; |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5158 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5159 | goto out; |
| 5160 | } |
| 5161 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5162 | btrfs_put_bbio(tmp_bbio); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5163 | } else if (mirror_num > map->num_stripes) { |
| 5164 | mirror_num = 0; |
| 5165 | } |
| 5166 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5167 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5168 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5169 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5170 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5171 | stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5172 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5173 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5174 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5175 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5176 | if (rw & REQ_DISCARD) |
| 5177 | num_stripes = min_t(u64, map->num_stripes, |
| 5178 | stripe_nr_end - stripe_nr_orig); |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5179 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5180 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5181 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5182 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5183 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5184 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5185 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5186 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5187 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5188 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5189 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5190 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5191 | current->pid % map->num_stripes, |
| 5192 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5193 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5194 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5195 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5196 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5197 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5198 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5199 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5200 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5201 | } else { |
| 5202 | mirror_num = 1; |
| 5203 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5204 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5205 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5206 | u32 factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5207 | |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5208 | stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5209 | stripe_index *= map->sub_stripes; |
| 5210 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5211 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5212 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5213 | else if (rw & REQ_DISCARD) |
| 5214 | num_stripes = min_t(u64, map->sub_stripes * |
| 5215 | (stripe_nr_end - stripe_nr_orig), |
| 5216 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5217 | else if (mirror_num) |
| 5218 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5219 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5220 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5221 | stripe_index = find_live_mirror(fs_info, map, |
| 5222 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5223 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5224 | current->pid % map->sub_stripes, |
| 5225 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5226 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5227 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5228 | |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5229 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5230 | if (need_raid_map && |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5231 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5232 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5233 | /* push stripe_nr back to the start of the full stripe */ |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5234 | stripe_nr = div_u64(raid56_full_stripe_start, |
| 5235 | stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5236 | |
| 5237 | /* RAID[56] write or recovery. Return all stripes */ |
| 5238 | num_stripes = map->num_stripes; |
| 5239 | max_errors = nr_parity_stripes(map); |
| 5240 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5241 | *length = map->stripe_len; |
| 5242 | stripe_index = 0; |
| 5243 | stripe_offset = 0; |
| 5244 | } else { |
| 5245 | /* |
| 5246 | * Mirror #0 or #1 means the original data block. |
| 5247 | * Mirror #2 is RAID5 parity block. |
| 5248 | * Mirror #3 is RAID6 Q block. |
| 5249 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5250 | stripe_nr = div_u64_rem(stripe_nr, |
| 5251 | nr_data_stripes(map), &stripe_index); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5252 | if (mirror_num > 1) |
| 5253 | stripe_index = nr_data_stripes(map) + |
| 5254 | mirror_num - 2; |
| 5255 | |
| 5256 | /* We distribute the parity blocks across stripes */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5257 | div_u64_rem(stripe_nr + stripe_index, map->num_stripes, |
| 5258 | &stripe_index); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5259 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5260 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5261 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5262 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5263 | } else { |
| 5264 | /* |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5265 | * after this, stripe_nr is the number of stripes on this |
| 5266 | * device we have to walk to find the data, and stripe_index is |
| 5267 | * the number of our device in the stripe array |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5268 | */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5269 | stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, |
| 5270 | &stripe_index); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5271 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5272 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5273 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5274 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5275 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5276 | if (dev_replace_is_ongoing) { |
| 5277 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5278 | num_alloc_stripes <<= 1; |
| 5279 | if (rw & REQ_GET_READ_MIRRORS) |
| 5280 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5281 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5282 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5283 | |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5284 | bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5285 | if (!bbio) { |
| 5286 | ret = -ENOMEM; |
| 5287 | goto out; |
| 5288 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5289 | if (dev_replace_is_ongoing) |
| 5290 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5291 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5292 | /* build raid_map */ |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5293 | if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5294 | need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5295 | mirror_num > 1)) { |
| 5296 | u64 tmp; |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5297 | unsigned rot; |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5298 | |
| 5299 | bbio->raid_map = (u64 *)((void *)bbio->stripes + |
| 5300 | sizeof(struct btrfs_bio_stripe) * |
| 5301 | num_alloc_stripes + |
| 5302 | sizeof(int) * tgtdev_indexes); |
| 5303 | |
| 5304 | /* Work out the disk rotation on this stripe-set */ |
David Sterba | 47c5713 | 2015-02-20 18:43:47 +0100 | [diff] [blame] | 5305 | div_u64_rem(stripe_nr, num_stripes, &rot); |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5306 | |
| 5307 | /* Fill in the logical address of each stripe */ |
| 5308 | tmp = stripe_nr * nr_data_stripes(map); |
| 5309 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5310 | bbio->raid_map[(i+rot) % num_stripes] = |
| 5311 | em->start + (tmp + i) * map->stripe_len; |
| 5312 | |
| 5313 | bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5314 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5315 | bbio->raid_map[(i+rot+1) % num_stripes] = |
| 5316 | RAID6_Q_STRIPE; |
| 5317 | } |
| 5318 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5319 | if (rw & REQ_DISCARD) { |
David Sterba | 9d644a6 | 2015-02-20 18:42:11 +0100 | [diff] [blame] | 5320 | u32 factor = 0; |
| 5321 | u32 sub_stripes = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5322 | u64 stripes_per_dev = 0; |
| 5323 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5324 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5325 | |
| 5326 | if (map->type & |
| 5327 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5328 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5329 | sub_stripes = 1; |
| 5330 | else |
| 5331 | sub_stripes = map->sub_stripes; |
| 5332 | |
| 5333 | factor = map->num_stripes / sub_stripes; |
| 5334 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5335 | stripe_nr_orig, |
| 5336 | factor, |
| 5337 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5338 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5339 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5340 | } |
| 5341 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5342 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5343 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5344 | map->stripes[stripe_index].physical + |
| 5345 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5346 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5347 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5348 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5349 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5350 | bbio->stripes[i].length = stripes_per_dev * |
| 5351 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5352 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5353 | if (i / sub_stripes < remaining_stripes) |
| 5354 | bbio->stripes[i].length += |
| 5355 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5356 | |
| 5357 | /* |
| 5358 | * Special for the first stripe and |
| 5359 | * the last stripe: |
| 5360 | * |
| 5361 | * |-------|...|-------| |
| 5362 | * |----------| |
| 5363 | * off end_off |
| 5364 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5365 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5366 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5367 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5368 | |
| 5369 | if (stripe_index >= last_stripe && |
| 5370 | stripe_index <= (last_stripe + |
| 5371 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5372 | bbio->stripes[i].length -= |
| 5373 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5374 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5375 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5376 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5377 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5378 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5379 | |
| 5380 | stripe_index++; |
| 5381 | if (stripe_index == map->num_stripes) { |
| 5382 | /* This could only happen for RAID0/10 */ |
| 5383 | stripe_index = 0; |
| 5384 | stripe_nr++; |
| 5385 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5386 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5387 | } else { |
| 5388 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5389 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5390 | map->stripes[stripe_index].physical + |
| 5391 | stripe_offset + |
| 5392 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5393 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5394 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5395 | stripe_index++; |
| 5396 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5397 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5398 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5399 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5400 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5401 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5402 | if (bbio->raid_map) |
| 5403 | sort_parity_stripes(bbio, num_stripes); |
Zhao Lei | cc7539e | 2015-01-20 15:11:32 +0800 | [diff] [blame] | 5404 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5405 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5406 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5407 | dev_replace->tgtdev != NULL) { |
| 5408 | int index_where_to_add; |
| 5409 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5410 | |
| 5411 | /* |
| 5412 | * duplicate the write operations while the dev replace |
| 5413 | * procedure is running. Since the copying of the old disk |
| 5414 | * to the new disk takes place at run time while the |
| 5415 | * filesystem is mounted writable, the regular write |
| 5416 | * operations to the old disk have to be duplicated to go |
| 5417 | * to the new disk as well. |
| 5418 | * Note that device->missing is handled by the caller, and |
| 5419 | * that the write to the old disk is already set up in the |
| 5420 | * stripes array. |
| 5421 | */ |
| 5422 | index_where_to_add = num_stripes; |
| 5423 | for (i = 0; i < num_stripes; i++) { |
| 5424 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5425 | /* write to new disk, too */ |
| 5426 | struct btrfs_bio_stripe *new = |
| 5427 | bbio->stripes + index_where_to_add; |
| 5428 | struct btrfs_bio_stripe *old = |
| 5429 | bbio->stripes + i; |
| 5430 | |
| 5431 | new->physical = old->physical; |
| 5432 | new->length = old->length; |
| 5433 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5434 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5435 | index_where_to_add++; |
| 5436 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5437 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5438 | } |
| 5439 | } |
| 5440 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5441 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5442 | dev_replace->tgtdev != NULL) { |
| 5443 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5444 | int index_srcdev = 0; |
| 5445 | int found = 0; |
| 5446 | u64 physical_of_found = 0; |
| 5447 | |
| 5448 | /* |
| 5449 | * During the dev-replace procedure, the target drive can |
| 5450 | * also be used to read data in case it is needed to repair |
| 5451 | * a corrupt block elsewhere. This is possible if the |
| 5452 | * requested area is left of the left cursor. In this area, |
| 5453 | * the target drive is a full copy of the source drive. |
| 5454 | */ |
| 5455 | for (i = 0; i < num_stripes; i++) { |
| 5456 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5457 | /* |
| 5458 | * In case of DUP, in order to keep it |
| 5459 | * simple, only add the mirror with the |
| 5460 | * lowest physical address |
| 5461 | */ |
| 5462 | if (found && |
| 5463 | physical_of_found <= |
| 5464 | bbio->stripes[i].physical) |
| 5465 | continue; |
| 5466 | index_srcdev = i; |
| 5467 | found = 1; |
| 5468 | physical_of_found = bbio->stripes[i].physical; |
| 5469 | } |
| 5470 | } |
| 5471 | if (found) { |
David Sterba | 258ece0 | 2015-02-24 19:45:15 +0100 | [diff] [blame] | 5472 | if (physical_of_found + map->stripe_len <= |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5473 | dev_replace->cursor_left) { |
| 5474 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5475 | bbio->stripes + num_stripes; |
| 5476 | |
| 5477 | tgtdev_stripe->physical = physical_of_found; |
| 5478 | tgtdev_stripe->length = |
| 5479 | bbio->stripes[index_srcdev].length; |
| 5480 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5481 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5482 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5483 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5484 | num_stripes++; |
| 5485 | } |
| 5486 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5487 | } |
| 5488 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5489 | *bbio_ret = bbio; |
Zhao Lei | 10f1190 | 2015-01-20 15:11:43 +0800 | [diff] [blame] | 5490 | bbio->map_type = map->type; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5491 | bbio->num_stripes = num_stripes; |
| 5492 | bbio->max_errors = max_errors; |
| 5493 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5494 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5495 | |
| 5496 | /* |
| 5497 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5498 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5499 | * available as a mirror |
| 5500 | */ |
| 5501 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5502 | WARN_ON(num_stripes > 1); |
| 5503 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5504 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5505 | bbio->mirror_num = map->num_stripes + 1; |
| 5506 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5507 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5508 | if (dev_replace_is_ongoing) |
| 5509 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5510 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5511 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5512 | } |
| 5513 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5514 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5515 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5516 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5517 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5518 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5519 | mirror_num, 0); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5520 | } |
| 5521 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5522 | /* For Scrub/replace */ |
| 5523 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5524 | u64 logical, u64 *length, |
| 5525 | struct btrfs_bio **bbio_ret, int mirror_num, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5526 | int need_raid_map) |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5527 | { |
| 5528 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5529 | mirror_num, need_raid_map); |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5530 | } |
| 5531 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5532 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5533 | u64 chunk_start, u64 physical, u64 devid, |
| 5534 | u64 **logical, int *naddrs, int *stripe_len) |
| 5535 | { |
| 5536 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5537 | struct extent_map *em; |
| 5538 | struct map_lookup *map; |
| 5539 | u64 *buf; |
| 5540 | u64 bytenr; |
| 5541 | u64 length; |
| 5542 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5543 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5544 | int i, j, nr = 0; |
| 5545 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5546 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5547 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5548 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5549 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5550 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5551 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5552 | chunk_start); |
| 5553 | return -EIO; |
| 5554 | } |
| 5555 | |
| 5556 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5557 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5558 | em->start, chunk_start); |
| 5559 | free_extent_map(em); |
| 5560 | return -EIO; |
| 5561 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5562 | map = (struct map_lookup *)em->bdev; |
| 5563 | |
| 5564 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5565 | rmap_len = map->stripe_len; |
| 5566 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5567 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5568 | length = div_u64(length, map->num_stripes / map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5569 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5570 | length = div_u64(length, map->num_stripes); |
Zhao Lei | ffe2d20 | 2015-01-20 15:11:44 +0800 | [diff] [blame] | 5571 | else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) { |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5572 | length = div_u64(length, nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5573 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5574 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5575 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 5576 | buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5577 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5578 | |
| 5579 | for (i = 0; i < map->num_stripes; i++) { |
| 5580 | if (devid && map->stripes[i].dev->devid != devid) |
| 5581 | continue; |
| 5582 | if (map->stripes[i].physical > physical || |
| 5583 | map->stripes[i].physical + length <= physical) |
| 5584 | continue; |
| 5585 | |
| 5586 | stripe_nr = physical - map->stripes[i].physical; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5587 | stripe_nr = div_u64(stripe_nr, map->stripe_len); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5588 | |
| 5589 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5590 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Sterba | b8b93ad | 2015-01-16 17:26:13 +0100 | [diff] [blame] | 5591 | stripe_nr = div_u64(stripe_nr, map->sub_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5592 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5593 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5594 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5595 | * Alternatively, just use rmap_len below instead of |
| 5596 | * map->stripe_len */ |
| 5597 | |
| 5598 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5599 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5600 | for (j = 0; j < nr; j++) { |
| 5601 | if (buf[j] == bytenr) |
| 5602 | break; |
| 5603 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5604 | if (j == nr) { |
| 5605 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5606 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5607 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5608 | } |
| 5609 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5610 | *logical = buf; |
| 5611 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5612 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5613 | |
| 5614 | free_extent_map(em); |
| 5615 | return 0; |
| 5616 | } |
| 5617 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5618 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5619 | { |
| 5620 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5621 | bio_endio_nodec(bio, err); |
| 5622 | else |
| 5623 | bio_endio(bio, err); |
Zhao Lei | 6e9606d | 2015-01-20 15:11:34 +0800 | [diff] [blame] | 5624 | btrfs_put_bbio(bbio); |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5625 | } |
| 5626 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5627 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5628 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5629 | struct btrfs_bio *bbio = bio->bi_private; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5630 | struct btrfs_device *dev = bbio->stripes[0].dev; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5631 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5632 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5633 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5634 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5635 | if (err == -EIO || err == -EREMOTEIO) { |
| 5636 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5637 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5638 | |
| 5639 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5640 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5641 | if (dev->bdev) { |
| 5642 | if (bio->bi_rw & WRITE) |
| 5643 | btrfs_dev_stat_inc(dev, |
| 5644 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5645 | else |
| 5646 | btrfs_dev_stat_inc(dev, |
| 5647 | BTRFS_DEV_STAT_READ_ERRS); |
| 5648 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5649 | btrfs_dev_stat_inc(dev, |
| 5650 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5651 | btrfs_dev_stat_print_on_error(dev); |
| 5652 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5653 | } |
| 5654 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5655 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5656 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5657 | is_orig_bio = 1; |
| 5658 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5659 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5660 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5661 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5662 | if (!is_orig_bio) { |
| 5663 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5664 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5665 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5666 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5667 | bio->bi_private = bbio->private; |
| 5668 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5669 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5670 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5671 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5672 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5673 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5674 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5675 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5676 | /* |
| 5677 | * this bio is actually up to date, we didn't |
| 5678 | * go over the max number of errors |
| 5679 | */ |
| 5680 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5681 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5682 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5683 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5684 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5685 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5686 | bio_put(bio); |
| 5687 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5688 | } |
| 5689 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5690 | /* |
| 5691 | * see run_scheduled_bios for a description of why bios are collected for |
| 5692 | * async submit. |
| 5693 | * |
| 5694 | * This will add one bio to the pending list for a device and make sure |
| 5695 | * the work struct is scheduled. |
| 5696 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5697 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5698 | struct btrfs_device *device, |
| 5699 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5700 | { |
| 5701 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5702 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5703 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5704 | if (device->missing || !device->bdev) { |
| 5705 | bio_endio(bio, -EIO); |
| 5706 | return; |
| 5707 | } |
| 5708 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5709 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5710 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5711 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5712 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5713 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5714 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5718 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5719 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5720 | * made progress against dirty pages when we've really just put it |
| 5721 | * on a queue for later |
| 5722 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5723 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5724 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5725 | bio->bi_next = NULL; |
| 5726 | bio->bi_rw |= rw; |
| 5727 | |
| 5728 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5729 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5730 | pending_bios = &device->pending_sync_bios; |
| 5731 | else |
| 5732 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5733 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5734 | if (pending_bios->tail) |
| 5735 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5736 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5737 | pending_bios->tail = bio; |
| 5738 | if (!pending_bios->head) |
| 5739 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5740 | if (device->running_pending) |
| 5741 | should_queue = 0; |
| 5742 | |
| 5743 | spin_unlock(&device->io_lock); |
| 5744 | |
| 5745 | if (should_queue) |
Qu Wenruo | a8c93d4e | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5746 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5747 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5748 | } |
| 5749 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5750 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5751 | sector_t sector) |
| 5752 | { |
| 5753 | struct bio_vec *prev; |
| 5754 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5755 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5756 | struct bvec_merge_data bvm = { |
| 5757 | .bi_bdev = bdev, |
| 5758 | .bi_sector = sector, |
| 5759 | .bi_rw = bio->bi_rw, |
| 5760 | }; |
| 5761 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5762 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5763 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5764 | |
| 5765 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5766 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5767 | return 0; |
| 5768 | |
| 5769 | if (!q->merge_bvec_fn) |
| 5770 | return 1; |
| 5771 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5772 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5773 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5774 | return 0; |
| 5775 | return 1; |
| 5776 | } |
| 5777 | |
| 5778 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5779 | struct bio *bio, u64 physical, int dev_nr, |
| 5780 | int rw, int async) |
| 5781 | { |
| 5782 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5783 | |
| 5784 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5785 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5786 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5787 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5788 | #ifdef DEBUG |
| 5789 | { |
| 5790 | struct rcu_string *name; |
| 5791 | |
| 5792 | rcu_read_lock(); |
| 5793 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5794 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5795 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5796 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5797 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5798 | rcu_read_unlock(); |
| 5799 | } |
| 5800 | #endif |
| 5801 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5802 | |
| 5803 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5804 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5805 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5806 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5807 | else |
| 5808 | btrfsic_submit_bio(rw, bio); |
| 5809 | } |
| 5810 | |
| 5811 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5812 | struct bio *first_bio, struct btrfs_device *dev, |
| 5813 | int dev_nr, int rw, int async) |
| 5814 | { |
| 5815 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5816 | struct bio *bio; |
| 5817 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5818 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5819 | |
| 5820 | again: |
| 5821 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5822 | if (!bio) |
| 5823 | return -ENOMEM; |
| 5824 | |
| 5825 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5826 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5827 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5828 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5829 | |
| 5830 | atomic_inc(&bbio->stripes_pending); |
| 5831 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5832 | rw, async); |
| 5833 | physical += len; |
| 5834 | goto again; |
| 5835 | } |
| 5836 | bvec++; |
| 5837 | } |
| 5838 | |
| 5839 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5840 | return 0; |
| 5841 | } |
| 5842 | |
| 5843 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5844 | { |
| 5845 | atomic_inc(&bbio->error); |
| 5846 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5847 | /* Shoud be the original bio. */ |
| 5848 | WARN_ON(bio != bbio->orig_bio); |
| 5849 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5850 | bio->bi_private = bbio->private; |
| 5851 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5852 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5853 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5854 | |
| 5855 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5856 | } |
| 5857 | } |
| 5858 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5859 | 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] | 5860 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5861 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5862 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5863 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5864 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5865 | u64 length = 0; |
| 5866 | u64 map_length; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5867 | int ret; |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5868 | int dev_nr; |
| 5869 | int total_devs; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5870 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5871 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5872 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5873 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5874 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5875 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5876 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5877 | mirror_num, 1); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5878 | if (ret) { |
| 5879 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5880 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5881 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5882 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5883 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5884 | bbio->orig_bio = first_bio; |
| 5885 | bbio->private = first_bio->bi_private; |
| 5886 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5887 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5888 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5889 | |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5890 | if (bbio->raid_map) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5891 | /* In this case, map_length has been set to the length of |
| 5892 | a single stripe; not the whole write */ |
| 5893 | if (rw & WRITE) { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5894 | ret = raid56_parity_write(root, bio, bbio, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5895 | } else { |
Zhao Lei | 8e5cfb5 | 2015-01-20 15:11:33 +0800 | [diff] [blame] | 5896 | ret = raid56_parity_recover(root, bio, bbio, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5897 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5898 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5899 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5900 | btrfs_bio_counter_dec(root->fs_info); |
| 5901 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5902 | } |
| 5903 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5904 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5905 | 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] | 5906 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5907 | BUG(); |
| 5908 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5909 | |
Zhao Lei | 08da757 | 2015-02-12 15:42:16 +0800 | [diff] [blame] | 5910 | for (dev_nr = 0; dev_nr < total_devs; dev_nr++) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5911 | dev = bbio->stripes[dev_nr].dev; |
| 5912 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5913 | bbio_error(bbio, first_bio, logical); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5914 | continue; |
| 5915 | } |
| 5916 | |
| 5917 | /* |
| 5918 | * Check and see if we're ok with this bio based on it's size |
| 5919 | * and offset with the given device. |
| 5920 | */ |
| 5921 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5922 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5923 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5924 | dev_nr, rw, async_submit); |
| 5925 | BUG_ON(ret); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5926 | continue; |
| 5927 | } |
| 5928 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5929 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5930 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5931 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5932 | } else { |
| 5933 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5934 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5935 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5936 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5937 | submit_stripe_bio(root, bbio, bio, |
| 5938 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5939 | async_submit); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5940 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5941 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5942 | return 0; |
| 5943 | } |
| 5944 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5945 | 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] | 5946 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5947 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5948 | struct btrfs_device *device; |
| 5949 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5950 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5951 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5952 | while (cur_devices) { |
| 5953 | if (!fsid || |
| 5954 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5955 | device = __find_device(&cur_devices->devices, |
| 5956 | devid, uuid); |
| 5957 | if (device) |
| 5958 | return device; |
| 5959 | } |
| 5960 | cur_devices = cur_devices->seed; |
| 5961 | } |
| 5962 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5963 | } |
| 5964 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5965 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 5966 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5967 | u64 devid, u8 *dev_uuid) |
| 5968 | { |
| 5969 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5970 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5971 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 5972 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5973 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5974 | |
| 5975 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5976 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5977 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5978 | |
| 5979 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5980 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5981 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5982 | return device; |
| 5983 | } |
| 5984 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5985 | /** |
| 5986 | * btrfs_alloc_device - allocate struct btrfs_device |
| 5987 | * @fs_info: used only for generating a new devid, can be NULL if |
| 5988 | * devid is provided (i.e. @devid != NULL). |
| 5989 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 5990 | * is generated. |
| 5991 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 5992 | * is generated. |
| 5993 | * |
| 5994 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 5995 | * on error. Returned struct is not linked onto any lists and can be |
| 5996 | * destroyed with kfree() right away. |
| 5997 | */ |
| 5998 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 5999 | const u64 *devid, |
| 6000 | const u8 *uuid) |
| 6001 | { |
| 6002 | struct btrfs_device *dev; |
| 6003 | u64 tmp; |
| 6004 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 6005 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6006 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6007 | |
| 6008 | dev = __alloc_device(); |
| 6009 | if (IS_ERR(dev)) |
| 6010 | return dev; |
| 6011 | |
| 6012 | if (devid) |
| 6013 | tmp = *devid; |
| 6014 | else { |
| 6015 | int ret; |
| 6016 | |
| 6017 | ret = find_next_devid(fs_info, &tmp); |
| 6018 | if (ret) { |
| 6019 | kfree(dev); |
| 6020 | return ERR_PTR(ret); |
| 6021 | } |
| 6022 | } |
| 6023 | dev->devid = tmp; |
| 6024 | |
| 6025 | if (uuid) |
| 6026 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 6027 | else |
| 6028 | generate_random_uuid(dev->uuid); |
| 6029 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 6030 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 6031 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 6032 | |
| 6033 | return dev; |
| 6034 | } |
| 6035 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6036 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 6037 | struct extent_buffer *leaf, |
| 6038 | struct btrfs_chunk *chunk) |
| 6039 | { |
| 6040 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 6041 | struct map_lookup *map; |
| 6042 | struct extent_map *em; |
| 6043 | u64 logical; |
| 6044 | u64 length; |
| 6045 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6046 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6047 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6048 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6049 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6050 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6051 | logical = key->offset; |
| 6052 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6053 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6054 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6055 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6056 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6057 | |
| 6058 | /* already mapped? */ |
| 6059 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6060 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6061 | return 0; |
| 6062 | } else if (em) { |
| 6063 | free_extent_map(em); |
| 6064 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6065 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6066 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6067 | if (!em) |
| 6068 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6069 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6070 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6071 | if (!map) { |
| 6072 | free_extent_map(em); |
| 6073 | return -ENOMEM; |
| 6074 | } |
| 6075 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6076 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6077 | em->bdev = (struct block_device *)map; |
| 6078 | em->start = logical; |
| 6079 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6080 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6081 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6082 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6083 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6084 | map->num_stripes = num_stripes; |
| 6085 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6086 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6087 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6088 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6089 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6090 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6091 | for (i = 0; i < num_stripes; i++) { |
| 6092 | map->stripes[i].physical = |
| 6093 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6094 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6095 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6096 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6097 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6098 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6099 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6100 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6101 | free_extent_map(em); |
| 6102 | return -EIO; |
| 6103 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6104 | if (!map->stripes[i].dev) { |
| 6105 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6106 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6107 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6108 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6109 | free_extent_map(em); |
| 6110 | return -EIO; |
| 6111 | } |
| 6112 | } |
| 6113 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6114 | } |
| 6115 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6116 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6117 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6118 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6119 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6120 | free_extent_map(em); |
| 6121 | |
| 6122 | return 0; |
| 6123 | } |
| 6124 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6125 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6126 | struct btrfs_dev_item *dev_item, |
| 6127 | struct btrfs_device *device) |
| 6128 | { |
| 6129 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6130 | |
| 6131 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6132 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6133 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6134 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6135 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6136 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6137 | device->type = btrfs_device_type(leaf, dev_item); |
| 6138 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6139 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6140 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6141 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6142 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6143 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6144 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6145 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6146 | } |
| 6147 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6148 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6149 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6150 | { |
| 6151 | struct btrfs_fs_devices *fs_devices; |
| 6152 | int ret; |
| 6153 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6154 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6155 | |
| 6156 | fs_devices = root->fs_info->fs_devices->seed; |
| 6157 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6158 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6159 | return fs_devices; |
| 6160 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6161 | fs_devices = fs_devices->seed; |
| 6162 | } |
| 6163 | |
| 6164 | fs_devices = find_fsid(fsid); |
| 6165 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6166 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6167 | return ERR_PTR(-ENOENT); |
| 6168 | |
| 6169 | fs_devices = alloc_fs_devices(fsid); |
| 6170 | if (IS_ERR(fs_devices)) |
| 6171 | return fs_devices; |
| 6172 | |
| 6173 | fs_devices->seeding = 1; |
| 6174 | fs_devices->opened = 1; |
| 6175 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6176 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6177 | |
| 6178 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6179 | if (IS_ERR(fs_devices)) |
| 6180 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6181 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6182 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6183 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6184 | if (ret) { |
| 6185 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6186 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6187 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6188 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6189 | |
| 6190 | if (!fs_devices->seeding) { |
| 6191 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6192 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6193 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6194 | goto out; |
| 6195 | } |
| 6196 | |
| 6197 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6198 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6199 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6200 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6201 | } |
| 6202 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6203 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6204 | struct extent_buffer *leaf, |
| 6205 | struct btrfs_dev_item *dev_item) |
| 6206 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6207 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6208 | struct btrfs_device *device; |
| 6209 | u64 devid; |
| 6210 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6211 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6212 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6213 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6214 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6215 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6216 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6217 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6218 | BTRFS_UUID_SIZE); |
| 6219 | |
| 6220 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6221 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6222 | if (IS_ERR(fs_devices)) |
| 6223 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6224 | } |
| 6225 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6226 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6227 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6228 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6229 | return -EIO; |
| 6230 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6231 | btrfs_warn(root->fs_info, "devid %llu missing", devid); |
| 6232 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6233 | if (!device) |
| 6234 | return -ENOMEM; |
| 6235 | } else { |
| 6236 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6237 | return -EIO; |
| 6238 | |
| 6239 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6240 | /* |
| 6241 | * this happens when a device that was properly setup |
| 6242 | * in the device info lists suddenly goes bad. |
| 6243 | * device->bdev is NULL, and so we have to set |
| 6244 | * device->missing to one here |
| 6245 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6246 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6247 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6248 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6249 | |
| 6250 | /* Move the device to its own fs_devices */ |
| 6251 | if (device->fs_devices != fs_devices) { |
| 6252 | ASSERT(device->missing); |
| 6253 | |
| 6254 | list_move(&device->dev_list, &fs_devices->devices); |
| 6255 | device->fs_devices->num_devices--; |
| 6256 | fs_devices->num_devices++; |
| 6257 | |
| 6258 | device->fs_devices->missing_devices--; |
| 6259 | fs_devices->missing_devices++; |
| 6260 | |
| 6261 | device->fs_devices = fs_devices; |
| 6262 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6263 | } |
| 6264 | |
| 6265 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6266 | BUG_ON(device->writeable); |
| 6267 | if (device->generation != |
| 6268 | btrfs_device_generation(leaf, dev_item)) |
| 6269 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6270 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6271 | |
| 6272 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6273 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6274 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6275 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6276 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6277 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6278 | device->bytes_used; |
| 6279 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6280 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6281 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6282 | return ret; |
| 6283 | } |
| 6284 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6285 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6286 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6287 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6288 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6289 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6290 | struct btrfs_chunk *chunk; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6291 | u8 *array_ptr; |
| 6292 | unsigned long sb_array_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6293 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6294 | u32 num_stripes; |
| 6295 | u32 array_size; |
| 6296 | u32 len = 0; |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6297 | u32 cur_offset; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6298 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6299 | |
David Sterba | a83fffb | 2014-06-15 02:39:54 +0200 | [diff] [blame] | 6300 | ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize); |
| 6301 | /* |
| 6302 | * This will create extent buffer of nodesize, superblock size is |
| 6303 | * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will |
| 6304 | * overallocate but we can keep it as-is, only the first page is used. |
| 6305 | */ |
| 6306 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6307 | if (!sb) |
| 6308 | return -ENOMEM; |
| 6309 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6310 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6311 | /* |
| 6312 | * The sb extent buffer is artifical and just used to read the system array. |
| 6313 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6314 | * pages up-to-date when the page is larger: extent does not cover the |
| 6315 | * whole page and consequently check_page_uptodate does not find all |
| 6316 | * the page's extents up-to-date (the hole beyond sb), |
| 6317 | * write_extent_buffer then triggers a WARN_ON. |
| 6318 | * |
| 6319 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6320 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6321 | * to silence the warning eg. on PowerPC 64. |
| 6322 | */ |
| 6323 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6324 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6325 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6326 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6327 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6328 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6329 | array_ptr = super_copy->sys_chunk_array; |
| 6330 | sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6331 | cur_offset = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6332 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6333 | while (cur_offset < array_size) { |
| 6334 | disk_key = (struct btrfs_disk_key *)array_ptr; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6335 | len = sizeof(*disk_key); |
| 6336 | if (cur_offset + len > array_size) |
| 6337 | goto out_short_read; |
| 6338 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6339 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6340 | |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6341 | array_ptr += len; |
| 6342 | sb_array_offset += len; |
| 6343 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6344 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6345 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6346 | chunk = (struct btrfs_chunk *)sb_array_offset; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6347 | /* |
| 6348 | * At least one btrfs_chunk with one stripe must be |
| 6349 | * present, exact stripe count check comes afterwards |
| 6350 | */ |
| 6351 | len = btrfs_chunk_item_size(1); |
| 6352 | if (cur_offset + len > array_size) |
| 6353 | goto out_short_read; |
| 6354 | |
| 6355 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6356 | len = btrfs_chunk_item_size(num_stripes); |
| 6357 | if (cur_offset + len > array_size) |
| 6358 | goto out_short_read; |
| 6359 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6360 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6361 | if (ret) |
| 6362 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6363 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6364 | ret = -EIO; |
| 6365 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6366 | } |
David Sterba | 1ffb22c | 2014-10-31 19:02:42 +0100 | [diff] [blame] | 6367 | array_ptr += len; |
| 6368 | sb_array_offset += len; |
| 6369 | cur_offset += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6370 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6371 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6372 | return ret; |
David Sterba | e3540ea | 2014-11-05 15:24:51 +0100 | [diff] [blame] | 6373 | |
| 6374 | out_short_read: |
| 6375 | printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n", |
| 6376 | len, cur_offset); |
| 6377 | free_extent_buffer(sb); |
| 6378 | return -EIO; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6379 | } |
| 6380 | |
| 6381 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6382 | { |
| 6383 | struct btrfs_path *path; |
| 6384 | struct extent_buffer *leaf; |
| 6385 | struct btrfs_key key; |
| 6386 | struct btrfs_key found_key; |
| 6387 | int ret; |
| 6388 | int slot; |
| 6389 | |
| 6390 | root = root->fs_info->chunk_root; |
| 6391 | |
| 6392 | path = btrfs_alloc_path(); |
| 6393 | if (!path) |
| 6394 | return -ENOMEM; |
| 6395 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6396 | mutex_lock(&uuid_mutex); |
| 6397 | lock_chunks(root); |
| 6398 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6399 | /* |
| 6400 | * Read all device items, and then all the chunk items. All |
| 6401 | * device items are found before any chunk item (their object id |
| 6402 | * is smaller than the lowest possible object id for a chunk |
| 6403 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6404 | */ |
| 6405 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6406 | key.offset = 0; |
| 6407 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6408 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6409 | if (ret < 0) |
| 6410 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6411 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6412 | leaf = path->nodes[0]; |
| 6413 | slot = path->slots[0]; |
| 6414 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6415 | ret = btrfs_next_leaf(root, path); |
| 6416 | if (ret == 0) |
| 6417 | continue; |
| 6418 | if (ret < 0) |
| 6419 | goto error; |
| 6420 | break; |
| 6421 | } |
| 6422 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6423 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6424 | struct btrfs_dev_item *dev_item; |
| 6425 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6426 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6427 | ret = read_one_dev(root, leaf, dev_item); |
| 6428 | if (ret) |
| 6429 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6430 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6431 | struct btrfs_chunk *chunk; |
| 6432 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6433 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6434 | if (ret) |
| 6435 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6436 | } |
| 6437 | path->slots[0]++; |
| 6438 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6439 | ret = 0; |
| 6440 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6441 | unlock_chunks(root); |
| 6442 | mutex_unlock(&uuid_mutex); |
| 6443 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6444 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6445 | return ret; |
| 6446 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6447 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6448 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6449 | { |
| 6450 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6451 | struct btrfs_device *device; |
| 6452 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6453 | while (fs_devices) { |
| 6454 | mutex_lock(&fs_devices->device_list_mutex); |
| 6455 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6456 | device->dev_root = fs_info->dev_root; |
| 6457 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6458 | |
| 6459 | fs_devices = fs_devices->seed; |
| 6460 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6461 | } |
| 6462 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6463 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6464 | { |
| 6465 | int i; |
| 6466 | |
| 6467 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6468 | btrfs_dev_stat_reset(dev, i); |
| 6469 | } |
| 6470 | |
| 6471 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6472 | { |
| 6473 | struct btrfs_key key; |
| 6474 | struct btrfs_key found_key; |
| 6475 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6476 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6477 | struct extent_buffer *eb; |
| 6478 | int slot; |
| 6479 | int ret = 0; |
| 6480 | struct btrfs_device *device; |
| 6481 | struct btrfs_path *path = NULL; |
| 6482 | int i; |
| 6483 | |
| 6484 | path = btrfs_alloc_path(); |
| 6485 | if (!path) { |
| 6486 | ret = -ENOMEM; |
| 6487 | goto out; |
| 6488 | } |
| 6489 | |
| 6490 | mutex_lock(&fs_devices->device_list_mutex); |
| 6491 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6492 | int item_size; |
| 6493 | struct btrfs_dev_stats_item *ptr; |
| 6494 | |
| 6495 | key.objectid = 0; |
| 6496 | key.type = BTRFS_DEV_STATS_KEY; |
| 6497 | key.offset = device->devid; |
| 6498 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6499 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6500 | __btrfs_reset_dev_stats(device); |
| 6501 | device->dev_stats_valid = 1; |
| 6502 | btrfs_release_path(path); |
| 6503 | continue; |
| 6504 | } |
| 6505 | slot = path->slots[0]; |
| 6506 | eb = path->nodes[0]; |
| 6507 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6508 | item_size = btrfs_item_size_nr(eb, slot); |
| 6509 | |
| 6510 | ptr = btrfs_item_ptr(eb, slot, |
| 6511 | struct btrfs_dev_stats_item); |
| 6512 | |
| 6513 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6514 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6515 | btrfs_dev_stat_set(device, i, |
| 6516 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6517 | else |
| 6518 | btrfs_dev_stat_reset(device, i); |
| 6519 | } |
| 6520 | |
| 6521 | device->dev_stats_valid = 1; |
| 6522 | btrfs_dev_stat_print_on_load(device); |
| 6523 | btrfs_release_path(path); |
| 6524 | } |
| 6525 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6526 | |
| 6527 | out: |
| 6528 | btrfs_free_path(path); |
| 6529 | return ret < 0 ? ret : 0; |
| 6530 | } |
| 6531 | |
| 6532 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6533 | struct btrfs_root *dev_root, |
| 6534 | struct btrfs_device *device) |
| 6535 | { |
| 6536 | struct btrfs_path *path; |
| 6537 | struct btrfs_key key; |
| 6538 | struct extent_buffer *eb; |
| 6539 | struct btrfs_dev_stats_item *ptr; |
| 6540 | int ret; |
| 6541 | int i; |
| 6542 | |
| 6543 | key.objectid = 0; |
| 6544 | key.type = BTRFS_DEV_STATS_KEY; |
| 6545 | key.offset = device->devid; |
| 6546 | |
| 6547 | path = btrfs_alloc_path(); |
| 6548 | BUG_ON(!path); |
| 6549 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6550 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6551 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6552 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6553 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6554 | goto out; |
| 6555 | } |
| 6556 | |
| 6557 | if (ret == 0 && |
| 6558 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6559 | /* need to delete old one and insert a new one */ |
| 6560 | ret = btrfs_del_item(trans, dev_root, path); |
| 6561 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6562 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6563 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6564 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6565 | goto out; |
| 6566 | } |
| 6567 | ret = 1; |
| 6568 | } |
| 6569 | |
| 6570 | if (ret == 1) { |
| 6571 | /* need to insert a new item */ |
| 6572 | btrfs_release_path(path); |
| 6573 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6574 | &key, sizeof(*ptr)); |
| 6575 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6576 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6577 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6578 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6579 | goto out; |
| 6580 | } |
| 6581 | } |
| 6582 | |
| 6583 | eb = path->nodes[0]; |
| 6584 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6585 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6586 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6587 | btrfs_dev_stat_read(device, i)); |
| 6588 | btrfs_mark_buffer_dirty(eb); |
| 6589 | |
| 6590 | out: |
| 6591 | btrfs_free_path(path); |
| 6592 | return ret; |
| 6593 | } |
| 6594 | |
| 6595 | /* |
| 6596 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6597 | */ |
| 6598 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6599 | struct btrfs_fs_info *fs_info) |
| 6600 | { |
| 6601 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6602 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6603 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6604 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6605 | int ret = 0; |
| 6606 | |
| 6607 | mutex_lock(&fs_devices->device_list_mutex); |
| 6608 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6609 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6610 | continue; |
| 6611 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6612 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6613 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6614 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6615 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6616 | } |
| 6617 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6618 | |
| 6619 | return ret; |
| 6620 | } |
| 6621 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6622 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6623 | { |
| 6624 | btrfs_dev_stat_inc(dev, index); |
| 6625 | btrfs_dev_stat_print_on_error(dev); |
| 6626 | } |
| 6627 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6628 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6629 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6630 | if (!dev->dev_stats_valid) |
| 6631 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6632 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6633 | "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] | 6634 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6635 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6636 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6637 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6638 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6639 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6640 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6641 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6642 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6643 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6644 | int i; |
| 6645 | |
| 6646 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6647 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6648 | break; |
| 6649 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6650 | return; /* all values == 0, suppress message */ |
| 6651 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6652 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6653 | "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] | 6654 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6655 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6656 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6657 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6658 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6659 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6660 | } |
| 6661 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6662 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6663 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6664 | { |
| 6665 | struct btrfs_device *dev; |
| 6666 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6667 | int i; |
| 6668 | |
| 6669 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6670 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6671 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6672 | |
| 6673 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6674 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6675 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6676 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6677 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6678 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6679 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6680 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6681 | if (stats->nr_items > i) |
| 6682 | stats->values[i] = |
| 6683 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6684 | else |
| 6685 | btrfs_dev_stat_reset(dev, i); |
| 6686 | } |
| 6687 | } else { |
| 6688 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6689 | if (stats->nr_items > i) |
| 6690 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6691 | } |
| 6692 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6693 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6694 | return 0; |
| 6695 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6696 | |
| 6697 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6698 | { |
| 6699 | struct buffer_head *bh; |
| 6700 | struct btrfs_super_block *disk_super; |
| 6701 | |
| 6702 | bh = btrfs_read_dev_super(device->bdev); |
| 6703 | if (!bh) |
| 6704 | return -EINVAL; |
| 6705 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6706 | |
| 6707 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6708 | set_buffer_dirty(bh); |
| 6709 | sync_dirty_buffer(bh); |
| 6710 | brelse(bh); |
| 6711 | |
| 6712 | return 0; |
| 6713 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6714 | |
| 6715 | /* |
| 6716 | * Update the size of all devices, which is used for writing out the |
| 6717 | * super blocks. |
| 6718 | */ |
| 6719 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6720 | { |
| 6721 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6722 | struct btrfs_device *curr, *next; |
| 6723 | |
| 6724 | if (list_empty(&fs_devices->resized_devices)) |
| 6725 | return; |
| 6726 | |
| 6727 | mutex_lock(&fs_devices->device_list_mutex); |
| 6728 | lock_chunks(fs_info->dev_root); |
| 6729 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6730 | resized_list) { |
| 6731 | list_del_init(&curr->resized_list); |
| 6732 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6733 | } |
| 6734 | unlock_chunks(fs_info->dev_root); |
| 6735 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6736 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6737 | |
| 6738 | /* Must be invoked during the transaction commit */ |
| 6739 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6740 | struct btrfs_transaction *transaction) |
| 6741 | { |
| 6742 | struct extent_map *em; |
| 6743 | struct map_lookup *map; |
| 6744 | struct btrfs_device *dev; |
| 6745 | int i; |
| 6746 | |
| 6747 | if (list_empty(&transaction->pending_chunks)) |
| 6748 | return; |
| 6749 | |
| 6750 | /* In order to kick the device replace finish process */ |
| 6751 | lock_chunks(root); |
| 6752 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6753 | map = (struct map_lookup *)em->bdev; |
| 6754 | |
| 6755 | for (i = 0; i < map->num_stripes; i++) { |
| 6756 | dev = map->stripes[i].dev; |
| 6757 | dev->commit_bytes_used = dev->bytes_used; |
| 6758 | } |
| 6759 | } |
| 6760 | unlock_chunks(root); |
| 6761 | } |