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 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 56 | static void lock_chunks(struct btrfs_root *root) |
| 57 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 58 | mutex_lock(&root->fs_info->chunk_mutex); |
| 59 | } |
| 60 | |
| 61 | static void unlock_chunks(struct btrfs_root *root) |
| 62 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 63 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 64 | } |
| 65 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 66 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 67 | { |
| 68 | struct btrfs_fs_devices *fs_devs; |
| 69 | |
| 70 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 71 | if (!fs_devs) |
| 72 | return ERR_PTR(-ENOMEM); |
| 73 | |
| 74 | mutex_init(&fs_devs->device_list_mutex); |
| 75 | |
| 76 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 77 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 78 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 79 | INIT_LIST_HEAD(&fs_devs->list); |
| 80 | |
| 81 | return fs_devs; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 86 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 87 | * generated. |
| 88 | * |
| 89 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 90 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 91 | * can be destroyed with kfree() right away. |
| 92 | */ |
| 93 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 94 | { |
| 95 | struct btrfs_fs_devices *fs_devs; |
| 96 | |
| 97 | fs_devs = __alloc_fs_devices(); |
| 98 | if (IS_ERR(fs_devs)) |
| 99 | return fs_devs; |
| 100 | |
| 101 | if (fsid) |
| 102 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 103 | else |
| 104 | generate_random_uuid(fs_devs->fsid); |
| 105 | |
| 106 | return fs_devs; |
| 107 | } |
| 108 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 109 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 110 | { |
| 111 | struct btrfs_device *device; |
| 112 | WARN_ON(fs_devices->opened); |
| 113 | while (!list_empty(&fs_devices->devices)) { |
| 114 | device = list_entry(fs_devices->devices.next, |
| 115 | struct btrfs_device, dev_list); |
| 116 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 117 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 118 | kfree(device); |
| 119 | } |
| 120 | kfree(fs_devices); |
| 121 | } |
| 122 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 123 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 124 | enum kobject_action action) |
| 125 | { |
| 126 | int ret; |
| 127 | |
| 128 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 129 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 130 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 131 | action, |
| 132 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 133 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 134 | } |
| 135 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 136 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 137 | { |
| 138 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 139 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 140 | while (!list_empty(&fs_uuids)) { |
| 141 | fs_devices = list_entry(fs_uuids.next, |
| 142 | struct btrfs_fs_devices, list); |
| 143 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 144 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 145 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 148 | static struct btrfs_device *__alloc_device(void) |
| 149 | { |
| 150 | struct btrfs_device *dev; |
| 151 | |
| 152 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 153 | if (!dev) |
| 154 | return ERR_PTR(-ENOMEM); |
| 155 | |
| 156 | INIT_LIST_HEAD(&dev->dev_list); |
| 157 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 158 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 159 | |
| 160 | spin_lock_init(&dev->io_lock); |
| 161 | |
| 162 | spin_lock_init(&dev->reada_lock); |
| 163 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 164 | atomic_set(&dev->dev_stats_ccnt, 0); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 165 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 166 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 167 | |
| 168 | return dev; |
| 169 | } |
| 170 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 171 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 172 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 173 | { |
| 174 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 175 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 176 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 177 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 178 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 179 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 180 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 181 | } |
| 182 | return NULL; |
| 183 | } |
| 184 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 185 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 186 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 187 | struct btrfs_fs_devices *fs_devices; |
| 188 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 189 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 190 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 191 | return fs_devices; |
| 192 | } |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 196 | static int |
| 197 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 198 | int flush, struct block_device **bdev, |
| 199 | struct buffer_head **bh) |
| 200 | { |
| 201 | int ret; |
| 202 | |
| 203 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 204 | |
| 205 | if (IS_ERR(*bdev)) { |
| 206 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 207 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 208 | goto error; |
| 209 | } |
| 210 | |
| 211 | if (flush) |
| 212 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 213 | ret = set_blocksize(*bdev, 4096); |
| 214 | if (ret) { |
| 215 | blkdev_put(*bdev, flags); |
| 216 | goto error; |
| 217 | } |
| 218 | invalidate_bdev(*bdev); |
| 219 | *bh = btrfs_read_dev_super(*bdev); |
| 220 | if (!*bh) { |
| 221 | ret = -EINVAL; |
| 222 | blkdev_put(*bdev, flags); |
| 223 | goto error; |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | |
| 228 | error: |
| 229 | *bdev = NULL; |
| 230 | *bh = NULL; |
| 231 | return ret; |
| 232 | } |
| 233 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 234 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 235 | struct bio *head, struct bio *tail) |
| 236 | { |
| 237 | |
| 238 | struct bio *old_head; |
| 239 | |
| 240 | old_head = pending_bios->head; |
| 241 | pending_bios->head = head; |
| 242 | if (pending_bios->tail) |
| 243 | tail->bi_next = old_head; |
| 244 | else |
| 245 | pending_bios->tail = tail; |
| 246 | } |
| 247 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 248 | /* |
| 249 | * we try to collect pending bios for a device so we don't get a large |
| 250 | * number of procs sending bios down to the same device. This greatly |
| 251 | * improves the schedulers ability to collect and merge the bios. |
| 252 | * |
| 253 | * But, it also turns into a long list of bios to process and that is sure |
| 254 | * to eventually make the worker thread block. The solution here is to |
| 255 | * make some progress and then put this work struct back at the end of |
| 256 | * the list if the block device is congested. This way, multiple devices |
| 257 | * can make progress from a single worker thread. |
| 258 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 259 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 260 | { |
| 261 | struct bio *pending; |
| 262 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 263 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 264 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 265 | struct bio *tail; |
| 266 | struct bio *cur; |
| 267 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 268 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 269 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 270 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 271 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 272 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 273 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 274 | struct blk_plug plug; |
| 275 | |
| 276 | /* |
| 277 | * this function runs all the bios we've collected for |
| 278 | * a particular device. We don't want to wander off to |
| 279 | * another device without first sending all of these down. |
| 280 | * So, setup a plug here and finish it off before we return |
| 281 | */ |
| 282 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 283 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 284 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 285 | fs_info = device->dev_root->fs_info; |
| 286 | limit = btrfs_async_submit_limit(fs_info); |
| 287 | limit = limit * 2 / 3; |
| 288 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 289 | loop: |
| 290 | spin_lock(&device->io_lock); |
| 291 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 292 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 293 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 294 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 295 | /* take all the bios off the list at once and process them |
| 296 | * later on (without the lock held). But, remember the |
| 297 | * tail and other pointers so the bios can be properly reinserted |
| 298 | * into the list if we hit congestion |
| 299 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 300 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 301 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 302 | force_reg = 1; |
| 303 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 304 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 305 | force_reg = 0; |
| 306 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 307 | |
| 308 | pending = pending_bios->head; |
| 309 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 310 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * if pending was null this time around, no bios need processing |
| 314 | * at all and we can stop. Otherwise it'll loop back up again |
| 315 | * and do an additional check so no bios are missed. |
| 316 | * |
| 317 | * device->running_pending is used to synchronize with the |
| 318 | * schedule_bio code. |
| 319 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 320 | if (device->pending_sync_bios.head == NULL && |
| 321 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 322 | again = 0; |
| 323 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 324 | } else { |
| 325 | again = 1; |
| 326 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 327 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 328 | |
| 329 | pending_bios->head = NULL; |
| 330 | pending_bios->tail = NULL; |
| 331 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 332 | spin_unlock(&device->io_lock); |
| 333 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 334 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 335 | |
| 336 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 337 | /* we want to work on both lists, but do more bios on the |
| 338 | * sync list than the regular list |
| 339 | */ |
| 340 | if ((num_run > 32 && |
| 341 | pending_bios != &device->pending_sync_bios && |
| 342 | device->pending_sync_bios.head) || |
| 343 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 344 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 345 | spin_lock(&device->io_lock); |
| 346 | requeue_list(pending_bios, pending, tail); |
| 347 | goto loop_lock; |
| 348 | } |
| 349 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 350 | cur = pending; |
| 351 | pending = pending->bi_next; |
| 352 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 353 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 354 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 355 | waitqueue_active(&fs_info->async_submit_wait)) |
| 356 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 357 | |
| 358 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 359 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 360 | /* |
| 361 | * if we're doing the sync list, record that our |
| 362 | * plug has some sync requests on it |
| 363 | * |
| 364 | * If we're doing the regular list and there are |
| 365 | * sync requests sitting around, unplug before |
| 366 | * we add more |
| 367 | */ |
| 368 | if (pending_bios == &device->pending_sync_bios) { |
| 369 | sync_pending = 1; |
| 370 | } else if (sync_pending) { |
| 371 | blk_finish_plug(&plug); |
| 372 | blk_start_plug(&plug); |
| 373 | sync_pending = 0; |
| 374 | } |
| 375 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 376 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 377 | num_run++; |
| 378 | batch_run++; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 379 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 380 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * we made progress, there is more work to do and the bdi |
| 384 | * is now congested. Back off and let other work structs |
| 385 | * run instead |
| 386 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 387 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 388 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 389 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 390 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 391 | ioc = current->io_context; |
| 392 | |
| 393 | /* |
| 394 | * the main goal here is that we don't want to |
| 395 | * block if we're going to be able to submit |
| 396 | * more requests without blocking. |
| 397 | * |
| 398 | * This code does two great things, it pokes into |
| 399 | * the elevator code from a filesystem _and_ |
| 400 | * it makes assumptions about how batching works. |
| 401 | */ |
| 402 | if (ioc && ioc->nr_batch_requests > 0 && |
| 403 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 404 | (last_waited == 0 || |
| 405 | ioc->last_waited == last_waited)) { |
| 406 | /* |
| 407 | * we want to go through our batch of |
| 408 | * requests and stop. So, we copy out |
| 409 | * the ioc->last_waited time and test |
| 410 | * against it before looping |
| 411 | */ |
| 412 | last_waited = ioc->last_waited; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 413 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 414 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 415 | continue; |
| 416 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 417 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 418 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 419 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 420 | |
| 421 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 422 | btrfs_queue_work(fs_info->submit_workers, |
| 423 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 424 | goto done; |
| 425 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 426 | /* unplug every 64 requests just for good measure */ |
| 427 | if (batch_run % 64 == 0) { |
| 428 | blk_finish_plug(&plug); |
| 429 | blk_start_plug(&plug); |
| 430 | sync_pending = 0; |
| 431 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 432 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 433 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 434 | cond_resched(); |
| 435 | if (again) |
| 436 | goto loop; |
| 437 | |
| 438 | spin_lock(&device->io_lock); |
| 439 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 440 | goto loop_lock; |
| 441 | spin_unlock(&device->io_lock); |
| 442 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 443 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 444 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 447 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 448 | { |
| 449 | struct btrfs_device *device; |
| 450 | |
| 451 | device = container_of(work, struct btrfs_device, work); |
| 452 | run_scheduled_bios(device); |
| 453 | } |
| 454 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 455 | /* |
| 456 | * Add new device to list of registered devices |
| 457 | * |
| 458 | * Returns: |
| 459 | * 1 - first time device is seen |
| 460 | * 0 - device already known |
| 461 | * < 0 - error |
| 462 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 463 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 464 | struct btrfs_super_block *disk_super, |
| 465 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 466 | { |
| 467 | struct btrfs_device *device; |
| 468 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 469 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 470 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 471 | u64 found_transid = btrfs_super_generation(disk_super); |
| 472 | |
| 473 | fs_devices = find_fsid(disk_super->fsid); |
| 474 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 475 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 476 | if (IS_ERR(fs_devices)) |
| 477 | return PTR_ERR(fs_devices); |
| 478 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 479 | list_add(&fs_devices->list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 480 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 481 | device = NULL; |
| 482 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 483 | device = __find_device(&fs_devices->devices, devid, |
| 484 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 485 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 486 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 487 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 488 | if (fs_devices->opened) |
| 489 | return -EBUSY; |
| 490 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 491 | device = btrfs_alloc_device(NULL, &devid, |
| 492 | disk_super->dev_item.uuid); |
| 493 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 494 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 495 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 496 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 497 | |
| 498 | name = rcu_string_strdup(path, GFP_NOFS); |
| 499 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 500 | kfree(device); |
| 501 | return -ENOMEM; |
| 502 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 503 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 504 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 505 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 506 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 507 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 508 | mutex_unlock(&fs_devices->device_list_mutex); |
| 509 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 510 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 511 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 512 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 513 | /* |
| 514 | * When FS is already mounted. |
| 515 | * 1. If you are here and if the device->name is NULL that |
| 516 | * means this device was missing at time of FS mount. |
| 517 | * 2. If you are here and if the device->name is different |
| 518 | * from 'path' that means either |
| 519 | * a. The same device disappeared and reappeared with |
| 520 | * different name. or |
| 521 | * b. The missing-disk-which-was-replaced, has |
| 522 | * reappeared now. |
| 523 | * |
| 524 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 525 | * and unintentional. |
| 526 | * |
| 527 | * Further in case of 1 and 2a above, the disk at 'path' |
| 528 | * would have missed some transaction when it was away and |
| 529 | * in case of 2a the stale bdev has to be updated as well. |
| 530 | * 2b must not be allowed at all time. |
| 531 | */ |
| 532 | |
| 533 | /* |
| 534 | * As of now don't allow update to btrfs_fs_device through |
| 535 | * the btrfs dev scan cli, after FS has been mounted. |
| 536 | */ |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 537 | if (fs_devices->opened) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 538 | return -EBUSY; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 539 | } else { |
| 540 | /* |
| 541 | * That is if the FS is _not_ mounted and if you |
| 542 | * are here, that means there is more than one |
| 543 | * disk with same uuid and devid.We keep the one |
| 544 | * with larger generation number or the last-in if |
| 545 | * generation are equal. |
| 546 | */ |
| 547 | if (found_transid < device->generation) |
| 548 | return -EEXIST; |
| 549 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 550 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 551 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 552 | if (!name) |
| 553 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 554 | rcu_string_free(device->name); |
| 555 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 556 | if (device->missing) { |
| 557 | fs_devices->missing_devices--; |
| 558 | device->missing = 0; |
| 559 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 562 | /* |
| 563 | * Unmount does not free the btrfs_device struct but would zero |
| 564 | * generation along with most of the other members. So just update |
| 565 | * it back. We need it to pick the disk with largest generation |
| 566 | * (as above). |
| 567 | */ |
| 568 | if (!fs_devices->opened) |
| 569 | device->generation = found_transid; |
| 570 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 571 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 572 | |
| 573 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 574 | } |
| 575 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 576 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 577 | { |
| 578 | struct btrfs_fs_devices *fs_devices; |
| 579 | struct btrfs_device *device; |
| 580 | struct btrfs_device *orig_dev; |
| 581 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 582 | fs_devices = alloc_fs_devices(orig->fsid); |
| 583 | if (IS_ERR(fs_devices)) |
| 584 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 585 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 586 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 587 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 588 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 589 | /* 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] | 590 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 591 | struct rcu_string *name; |
| 592 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 593 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 594 | orig_dev->uuid); |
| 595 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 596 | goto error; |
| 597 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 598 | /* |
| 599 | * This is ok to do without rcu read locked because we hold the |
| 600 | * uuid mutex so nothing we touch in here is going to disappear. |
| 601 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 602 | if (orig_dev->name) { |
| 603 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 604 | if (!name) { |
| 605 | kfree(device); |
| 606 | goto error; |
| 607 | } |
| 608 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 609 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 610 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 611 | list_add(&device->dev_list, &fs_devices->devices); |
| 612 | device->fs_devices = fs_devices; |
| 613 | fs_devices->num_devices++; |
| 614 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 615 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 616 | return fs_devices; |
| 617 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 618 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 619 | free_fs_devices(fs_devices); |
| 620 | return ERR_PTR(-ENOMEM); |
| 621 | } |
| 622 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 623 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 624 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 625 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 626 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 627 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 628 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 629 | mutex_lock(&uuid_mutex); |
| 630 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 631 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 632 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 633 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 634 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 635 | (!latest_dev || |
| 636 | device->generation > latest_dev->generation)) { |
| 637 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 638 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 639 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 640 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 641 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 642 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 643 | /* |
| 644 | * In the first step, keep the device which has |
| 645 | * the correct fsid and the devid that is used |
| 646 | * for the dev_replace procedure. |
| 647 | * In the second step, the dev_replace state is |
| 648 | * read from the device tree and it is known |
| 649 | * whether the procedure is really active or |
| 650 | * not, which means whether this device is |
| 651 | * used or whether it should be removed. |
| 652 | */ |
| 653 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 654 | continue; |
| 655 | } |
| 656 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 657 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 658 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 659 | device->bdev = NULL; |
| 660 | fs_devices->open_devices--; |
| 661 | } |
| 662 | if (device->writeable) { |
| 663 | list_del_init(&device->dev_alloc_list); |
| 664 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 665 | if (!device->is_tgtdev_for_dev_replace) |
| 666 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 667 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 668 | list_del_init(&device->dev_list); |
| 669 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 670 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 671 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 672 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 673 | |
| 674 | if (fs_devices->seed) { |
| 675 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 676 | goto again; |
| 677 | } |
| 678 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 679 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 680 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 681 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 682 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 683 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 684 | static void __free_device(struct work_struct *work) |
| 685 | { |
| 686 | struct btrfs_device *device; |
| 687 | |
| 688 | device = container_of(work, struct btrfs_device, rcu_work); |
| 689 | |
| 690 | if (device->bdev) |
| 691 | blkdev_put(device->bdev, device->mode); |
| 692 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 693 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 694 | kfree(device); |
| 695 | } |
| 696 | |
| 697 | static void free_device(struct rcu_head *head) |
| 698 | { |
| 699 | struct btrfs_device *device; |
| 700 | |
| 701 | device = container_of(head, struct btrfs_device, rcu); |
| 702 | |
| 703 | INIT_WORK(&device->rcu_work, __free_device); |
| 704 | schedule_work(&device->rcu_work); |
| 705 | } |
| 706 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 707 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 708 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 709 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 710 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 711 | if (--fs_devices->opened > 0) |
| 712 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 713 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 714 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 715 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 716 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 717 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 718 | |
| 719 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 720 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 721 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 722 | if (device->writeable && |
| 723 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 724 | list_del_init(&device->dev_alloc_list); |
| 725 | fs_devices->rw_devices--; |
| 726 | } |
| 727 | |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 728 | if (device->missing) |
| 729 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 730 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 731 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 732 | device->uuid); |
| 733 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 734 | |
| 735 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 736 | if (device->name) { |
| 737 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 738 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 739 | rcu_assign_pointer(new_device->name, name); |
| 740 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 741 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 742 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 743 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 744 | |
| 745 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 746 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 747 | mutex_unlock(&fs_devices->device_list_mutex); |
| 748 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 749 | WARN_ON(fs_devices->open_devices); |
| 750 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 751 | fs_devices->opened = 0; |
| 752 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 753 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 754 | return 0; |
| 755 | } |
| 756 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 757 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 758 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 759 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 760 | int ret; |
| 761 | |
| 762 | mutex_lock(&uuid_mutex); |
| 763 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 764 | if (!fs_devices->opened) { |
| 765 | seed_devices = fs_devices->seed; |
| 766 | fs_devices->seed = NULL; |
| 767 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 768 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 769 | |
| 770 | while (seed_devices) { |
| 771 | fs_devices = seed_devices; |
| 772 | seed_devices = fs_devices->seed; |
| 773 | __btrfs_close_devices(fs_devices); |
| 774 | free_fs_devices(fs_devices); |
| 775 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 776 | /* |
| 777 | * Wait for rcu kworkers under __btrfs_close_devices |
| 778 | * to finish all blkdev_puts so device is really |
| 779 | * free when umount is done. |
| 780 | */ |
| 781 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 782 | return ret; |
| 783 | } |
| 784 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 785 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 786 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 787 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 788 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 789 | struct block_device *bdev; |
| 790 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 791 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 792 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 793 | struct buffer_head *bh; |
| 794 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 795 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 796 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 797 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 798 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 799 | flags |= FMODE_EXCL; |
| 800 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 801 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 802 | if (device->bdev) |
| 803 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 804 | if (!device->name) |
| 805 | continue; |
| 806 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 807 | /* Just open everything we can; ignore failures here */ |
| 808 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 809 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 810 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 811 | |
| 812 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 813 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 814 | if (devid != device->devid) |
| 815 | goto error_brelse; |
| 816 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 817 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 818 | BTRFS_UUID_SIZE)) |
| 819 | goto error_brelse; |
| 820 | |
| 821 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 822 | if (!latest_dev || |
| 823 | device->generation > latest_dev->generation) |
| 824 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 825 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 826 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 827 | device->writeable = 0; |
| 828 | } else { |
| 829 | device->writeable = !bdev_read_only(bdev); |
| 830 | seeding = 0; |
| 831 | } |
| 832 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 833 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 834 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 835 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 836 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 837 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 838 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 839 | device->mode = flags; |
| 840 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 841 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 842 | fs_devices->rotating = 1; |
| 843 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 844 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 845 | if (device->writeable && |
| 846 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 847 | fs_devices->rw_devices++; |
| 848 | list_add(&device->dev_alloc_list, |
| 849 | &fs_devices->alloc_list); |
| 850 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 851 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 852 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 853 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 854 | error_brelse: |
| 855 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 856 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 857 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 858 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 859 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 860 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 861 | goto out; |
| 862 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 863 | fs_devices->seeding = seeding; |
| 864 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 865 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 866 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 867 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 868 | return ret; |
| 869 | } |
| 870 | |
| 871 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 872 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 873 | { |
| 874 | int ret; |
| 875 | |
| 876 | mutex_lock(&uuid_mutex); |
| 877 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 878 | fs_devices->opened++; |
| 879 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 880 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 881 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 882 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 883 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 884 | return ret; |
| 885 | } |
| 886 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 887 | /* |
| 888 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 889 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 890 | * is read via pagecache |
| 891 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 892 | 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] | 893 | struct btrfs_fs_devices **fs_devices_ret) |
| 894 | { |
| 895 | struct btrfs_super_block *disk_super; |
| 896 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 897 | struct page *page; |
| 898 | void *p; |
| 899 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 900 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 901 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 902 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 903 | u64 bytenr; |
| 904 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 905 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 906 | /* |
| 907 | * we would like to check all the supers, but that would make |
| 908 | * a btrfs mount succeed after a mkfs from a different FS. |
| 909 | * So, we need to add a special mount option to scan for |
| 910 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 911 | */ |
| 912 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 913 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 914 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 915 | |
| 916 | bdev = blkdev_get_by_path(path, flags, holder); |
| 917 | |
| 918 | if (IS_ERR(bdev)) { |
| 919 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 920 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* make sure our super fits in the device */ |
| 924 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 925 | goto error_bdev_put; |
| 926 | |
| 927 | /* make sure our super fits in the page */ |
| 928 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 929 | goto error_bdev_put; |
| 930 | |
| 931 | /* make sure our super doesn't straddle pages on disk */ |
| 932 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 933 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 934 | goto error_bdev_put; |
| 935 | |
| 936 | /* pull in the page with our super */ |
| 937 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 938 | index, GFP_NOFS); |
| 939 | |
| 940 | if (IS_ERR_OR_NULL(page)) |
| 941 | goto error_bdev_put; |
| 942 | |
| 943 | p = kmap(page); |
| 944 | |
| 945 | /* align our pointer to the offset of the super block */ |
| 946 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 947 | |
| 948 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 949 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 950 | goto error_unmap; |
| 951 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 952 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 953 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 954 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 955 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 956 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 957 | if (ret > 0) { |
| 958 | if (disk_super->label[0]) { |
| 959 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 960 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 961 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 962 | } else { |
| 963 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 964 | } |
| 965 | |
| 966 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 967 | ret = 0; |
| 968 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 969 | if (!ret && fs_devices_ret) |
| 970 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 971 | |
| 972 | error_unmap: |
| 973 | kunmap(page); |
| 974 | page_cache_release(page); |
| 975 | |
| 976 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 977 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 978 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 979 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 980 | return ret; |
| 981 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 982 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 983 | /* helper to account the used device space in the range */ |
| 984 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 985 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 986 | { |
| 987 | struct btrfs_key key; |
| 988 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 989 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 990 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 991 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 992 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 993 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 994 | struct extent_buffer *l; |
| 995 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 996 | *length = 0; |
| 997 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 998 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 999 | return 0; |
| 1000 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1001 | path = btrfs_alloc_path(); |
| 1002 | if (!path) |
| 1003 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1004 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1005 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1006 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1007 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1008 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1009 | |
| 1010 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1011 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1012 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1013 | if (ret > 0) { |
| 1014 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1015 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1016 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1017 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1018 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1019 | while (1) { |
| 1020 | l = path->nodes[0]; |
| 1021 | slot = path->slots[0]; |
| 1022 | if (slot >= btrfs_header_nritems(l)) { |
| 1023 | ret = btrfs_next_leaf(root, path); |
| 1024 | if (ret == 0) |
| 1025 | continue; |
| 1026 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1027 | goto out; |
| 1028 | |
| 1029 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1030 | } |
| 1031 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1032 | |
| 1033 | if (key.objectid < device->devid) |
| 1034 | goto next; |
| 1035 | |
| 1036 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1037 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1038 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1039 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1040 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1041 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1042 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1043 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1044 | dev_extent); |
| 1045 | if (key.offset <= start && extent_end > end) { |
| 1046 | *length = end - start + 1; |
| 1047 | break; |
| 1048 | } else if (key.offset <= start && extent_end > start) |
| 1049 | *length += extent_end - start; |
| 1050 | else if (key.offset > start && extent_end <= end) |
| 1051 | *length += extent_end - key.offset; |
| 1052 | else if (key.offset > start && key.offset <= end) { |
| 1053 | *length += end - key.offset + 1; |
| 1054 | break; |
| 1055 | } else if (key.offset > end) |
| 1056 | break; |
| 1057 | |
| 1058 | next: |
| 1059 | path->slots[0]++; |
| 1060 | } |
| 1061 | ret = 0; |
| 1062 | out: |
| 1063 | btrfs_free_path(path); |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1067 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1068 | struct btrfs_device *device, |
| 1069 | u64 *start, u64 len) |
| 1070 | { |
| 1071 | struct extent_map *em; |
| 1072 | int ret = 0; |
| 1073 | |
| 1074 | list_for_each_entry(em, &trans->transaction->pending_chunks, list) { |
| 1075 | struct map_lookup *map; |
| 1076 | int i; |
| 1077 | |
| 1078 | map = (struct map_lookup *)em->bdev; |
| 1079 | for (i = 0; i < map->num_stripes; i++) { |
| 1080 | if (map->stripes[i].dev != device) |
| 1081 | continue; |
| 1082 | if (map->stripes[i].physical >= *start + len || |
| 1083 | map->stripes[i].physical + em->orig_block_len <= |
| 1084 | *start) |
| 1085 | continue; |
| 1086 | *start = map->stripes[i].physical + |
| 1087 | em->orig_block_len; |
| 1088 | ret = 1; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
| 1095 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1096 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1097 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1098 | * @device: the device which we search the free space in |
| 1099 | * @num_bytes: the size of the free space that we need |
| 1100 | * @start: store the start of the free space. |
| 1101 | * @len: the size of the free space. that we find, or the size of the max |
| 1102 | * free space if we don't find suitable free space |
| 1103 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1104 | * this uses a pretty simple search, the expectation is that it is |
| 1105 | * called very infrequently and that a given device has a small number |
| 1106 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1107 | * |
| 1108 | * @start is used to store the start of the free space if we find. But if we |
| 1109 | * don't find suitable free space, it will be used to store the start position |
| 1110 | * of the max free space. |
| 1111 | * |
| 1112 | * @len is used to store the size of the free space that we find. |
| 1113 | * But if we don't find suitable free space, it is used to store the size of |
| 1114 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1115 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1116 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1117 | struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1118 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1119 | { |
| 1120 | struct btrfs_key key; |
| 1121 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1122 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1123 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1124 | u64 hole_size; |
| 1125 | u64 max_hole_start; |
| 1126 | u64 max_hole_size; |
| 1127 | u64 extent_end; |
| 1128 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1129 | u64 search_end = device->total_bytes; |
| 1130 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1131 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1132 | struct extent_buffer *l; |
| 1133 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1134 | /* FIXME use last free of some kind */ |
| 1135 | |
| 1136 | /* we don't want to overwrite the superblock on the drive, |
| 1137 | * so we make sure to start at an offset of at least 1MB |
| 1138 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1139 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1140 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1141 | path = btrfs_alloc_path(); |
| 1142 | if (!path) |
| 1143 | return -ENOMEM; |
| 1144 | again: |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1145 | max_hole_start = search_start; |
| 1146 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1147 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1148 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1149 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1150 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1151 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1154 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1155 | path->search_commit_root = 1; |
| 1156 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1157 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1158 | key.objectid = device->devid; |
| 1159 | key.offset = search_start; |
| 1160 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1161 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1162 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1163 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1164 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1165 | if (ret > 0) { |
| 1166 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1167 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1168 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1169 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1170 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1171 | while (1) { |
| 1172 | l = path->nodes[0]; |
| 1173 | slot = path->slots[0]; |
| 1174 | if (slot >= btrfs_header_nritems(l)) { |
| 1175 | ret = btrfs_next_leaf(root, path); |
| 1176 | if (ret == 0) |
| 1177 | continue; |
| 1178 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1179 | goto out; |
| 1180 | |
| 1181 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1182 | } |
| 1183 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1184 | |
| 1185 | if (key.objectid < device->devid) |
| 1186 | goto next; |
| 1187 | |
| 1188 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1189 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1190 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1191 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1192 | goto next; |
| 1193 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1194 | if (key.offset > search_start) { |
| 1195 | hole_size = key.offset - search_start; |
| 1196 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1197 | /* |
| 1198 | * Have to check before we set max_hole_start, otherwise |
| 1199 | * we could end up sending back this offset anyway. |
| 1200 | */ |
| 1201 | if (contains_pending_extent(trans, device, |
| 1202 | &search_start, |
| 1203 | hole_size)) |
| 1204 | hole_size = 0; |
| 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 | */ |
| 1241 | if (search_end > search_start) |
| 1242 | hole_size = search_end - search_start; |
| 1243 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1244 | if (hole_size > max_hole_size) { |
| 1245 | max_hole_start = search_start; |
| 1246 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1247 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1248 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1249 | if (contains_pending_extent(trans, device, &search_start, hole_size)) { |
| 1250 | btrfs_release_path(path); |
| 1251 | goto again; |
| 1252 | } |
| 1253 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1254 | /* See above. */ |
| 1255 | if (hole_size < num_bytes) |
| 1256 | ret = -ENOSPC; |
| 1257 | else |
| 1258 | ret = 0; |
| 1259 | |
| 1260 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1261 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1262 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1263 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1264 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1265 | return ret; |
| 1266 | } |
| 1267 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1268 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1269 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1270 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1271 | { |
| 1272 | int ret; |
| 1273 | struct btrfs_path *path; |
| 1274 | struct btrfs_root *root = device->dev_root; |
| 1275 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1276 | struct btrfs_key found_key; |
| 1277 | struct extent_buffer *leaf = NULL; |
| 1278 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1279 | |
| 1280 | path = btrfs_alloc_path(); |
| 1281 | if (!path) |
| 1282 | return -ENOMEM; |
| 1283 | |
| 1284 | key.objectid = device->devid; |
| 1285 | key.offset = start; |
| 1286 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1287 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1288 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1289 | if (ret > 0) { |
| 1290 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1291 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1292 | if (ret) |
| 1293 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1294 | leaf = path->nodes[0]; |
| 1295 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1296 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1297 | struct btrfs_dev_extent); |
| 1298 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1299 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1300 | key = found_key; |
| 1301 | btrfs_release_path(path); |
| 1302 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1303 | } else if (ret == 0) { |
| 1304 | leaf = path->nodes[0]; |
| 1305 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1306 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1307 | } else { |
| 1308 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1309 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1310 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1311 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1312 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1313 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1314 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1315 | if (ret) { |
| 1316 | btrfs_error(root->fs_info, ret, |
| 1317 | "Failed to remove dev extent item"); |
| 1318 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1319 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1320 | btrfs_free_path(path); |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1324 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1325 | struct btrfs_device *device, |
| 1326 | u64 chunk_tree, u64 chunk_objectid, |
| 1327 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1328 | { |
| 1329 | int ret; |
| 1330 | struct btrfs_path *path; |
| 1331 | struct btrfs_root *root = device->dev_root; |
| 1332 | struct btrfs_dev_extent *extent; |
| 1333 | struct extent_buffer *leaf; |
| 1334 | struct btrfs_key key; |
| 1335 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1336 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1337 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1338 | path = btrfs_alloc_path(); |
| 1339 | if (!path) |
| 1340 | return -ENOMEM; |
| 1341 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1342 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1343 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1344 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1345 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1346 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1347 | if (ret) |
| 1348 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1349 | |
| 1350 | leaf = path->nodes[0]; |
| 1351 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1352 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1353 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1354 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1355 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1356 | |
| 1357 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1358 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1359 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1360 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1361 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1362 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1363 | btrfs_free_path(path); |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1367 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1368 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1369 | struct extent_map_tree *em_tree; |
| 1370 | struct extent_map *em; |
| 1371 | struct rb_node *n; |
| 1372 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1373 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1374 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1375 | read_lock(&em_tree->lock); |
| 1376 | n = rb_last(&em_tree->map); |
| 1377 | if (n) { |
| 1378 | em = rb_entry(n, struct extent_map, rb_node); |
| 1379 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1380 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1381 | read_unlock(&em_tree->lock); |
| 1382 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1383 | return ret; |
| 1384 | } |
| 1385 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1386 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1387 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1388 | { |
| 1389 | int ret; |
| 1390 | struct btrfs_key key; |
| 1391 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1392 | struct btrfs_path *path; |
| 1393 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1394 | path = btrfs_alloc_path(); |
| 1395 | if (!path) |
| 1396 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1397 | |
| 1398 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1399 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1400 | key.offset = (u64)-1; |
| 1401 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1402 | 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] | 1403 | if (ret < 0) |
| 1404 | goto error; |
| 1405 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1406 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1407 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1408 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1409 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1410 | BTRFS_DEV_ITEM_KEY); |
| 1411 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1412 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1413 | } else { |
| 1414 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1415 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1416 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1417 | } |
| 1418 | ret = 0; |
| 1419 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1420 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1421 | return ret; |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * the device information is stored in the chunk root |
| 1426 | * the btrfs_device struct should be fully filled in |
| 1427 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1428 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1429 | struct btrfs_root *root, |
| 1430 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1431 | { |
| 1432 | int ret; |
| 1433 | struct btrfs_path *path; |
| 1434 | struct btrfs_dev_item *dev_item; |
| 1435 | struct extent_buffer *leaf; |
| 1436 | struct btrfs_key key; |
| 1437 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1438 | |
| 1439 | root = root->fs_info->chunk_root; |
| 1440 | |
| 1441 | path = btrfs_alloc_path(); |
| 1442 | if (!path) |
| 1443 | return -ENOMEM; |
| 1444 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1445 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1446 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1447 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1448 | |
| 1449 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1450 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1451 | if (ret) |
| 1452 | goto out; |
| 1453 | |
| 1454 | leaf = path->nodes[0]; |
| 1455 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1456 | |
| 1457 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1458 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1459 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1460 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1461 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1462 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1463 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1464 | btrfs_device_get_disk_total_bytes(device)); |
| 1465 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1466 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1467 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1468 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1469 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1470 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1471 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1472 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1473 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1474 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1475 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1476 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1477 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1478 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1479 | out: |
| 1480 | btrfs_free_path(path); |
| 1481 | return ret; |
| 1482 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1483 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1484 | /* |
| 1485 | * Function to update ctime/mtime for a given device path. |
| 1486 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1487 | */ |
| 1488 | static void update_dev_time(char *path_name) |
| 1489 | { |
| 1490 | struct file *filp; |
| 1491 | |
| 1492 | filp = filp_open(path_name, O_RDWR, 0); |
| 1493 | if (!filp) |
| 1494 | return; |
| 1495 | file_update_time(filp); |
| 1496 | filp_close(filp, NULL); |
| 1497 | return; |
| 1498 | } |
| 1499 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1500 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1501 | struct btrfs_device *device) |
| 1502 | { |
| 1503 | int ret; |
| 1504 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1505 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1506 | struct btrfs_trans_handle *trans; |
| 1507 | |
| 1508 | root = root->fs_info->chunk_root; |
| 1509 | |
| 1510 | path = btrfs_alloc_path(); |
| 1511 | if (!path) |
| 1512 | return -ENOMEM; |
| 1513 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1514 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1515 | if (IS_ERR(trans)) { |
| 1516 | btrfs_free_path(path); |
| 1517 | return PTR_ERR(trans); |
| 1518 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1519 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1520 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1521 | key.offset = device->devid; |
| 1522 | |
| 1523 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1524 | if (ret < 0) |
| 1525 | goto out; |
| 1526 | |
| 1527 | if (ret > 0) { |
| 1528 | ret = -ENOENT; |
| 1529 | goto out; |
| 1530 | } |
| 1531 | |
| 1532 | ret = btrfs_del_item(trans, root, path); |
| 1533 | if (ret) |
| 1534 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1535 | out: |
| 1536 | btrfs_free_path(path); |
| 1537 | btrfs_commit_transaction(trans, root); |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1542 | { |
| 1543 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1544 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1545 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1546 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1547 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1548 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1549 | u64 all_avail; |
| 1550 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1551 | u64 num_devices; |
| 1552 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1553 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1554 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1555 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1556 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1557 | mutex_lock(&uuid_mutex); |
| 1558 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1559 | do { |
| 1560 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1561 | |
| 1562 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1563 | root->fs_info->avail_system_alloc_bits | |
| 1564 | root->fs_info->avail_metadata_alloc_bits; |
| 1565 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1566 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1567 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1568 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1569 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1570 | WARN_ON(num_devices < 1); |
| 1571 | num_devices--; |
| 1572 | } |
| 1573 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1574 | |
| 1575 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1576 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1577 | goto out; |
| 1578 | } |
| 1579 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1580 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1581 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1582 | goto out; |
| 1583 | } |
| 1584 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1585 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1586 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1587 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1588 | goto out; |
| 1589 | } |
| 1590 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1591 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1592 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1593 | goto out; |
| 1594 | } |
| 1595 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1596 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1597 | struct list_head *devices; |
| 1598 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1599 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1600 | device = NULL; |
| 1601 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1602 | /* |
| 1603 | * It is safe to read the devices since the volume_mutex |
| 1604 | * is held. |
| 1605 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1606 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1607 | if (tmp->in_fs_metadata && |
| 1608 | !tmp->is_tgtdev_for_dev_replace && |
| 1609 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1610 | device = tmp; |
| 1611 | break; |
| 1612 | } |
| 1613 | } |
| 1614 | bdev = NULL; |
| 1615 | bh = NULL; |
| 1616 | disk_super = NULL; |
| 1617 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1618 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1619 | goto out; |
| 1620 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1621 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1622 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1623 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1624 | root->fs_info->bdev_holder, 0, |
| 1625 | &bdev, &bh); |
| 1626 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1627 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1628 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1629 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1630 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1631 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1632 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1633 | if (!device) { |
| 1634 | ret = -ENOENT; |
| 1635 | goto error_brelse; |
| 1636 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1637 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1638 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1639 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1640 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1641 | goto error_brelse; |
| 1642 | } |
| 1643 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1644 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1645 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1646 | goto error_brelse; |
| 1647 | } |
| 1648 | |
| 1649 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1650 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1651 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1652 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1653 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1654 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1655 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1656 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1657 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1658 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1659 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1660 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1661 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1662 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1663 | /* |
| 1664 | * TODO: the superblock still includes this device in its num_devices |
| 1665 | * counter although write_all_supers() is not locked out. This |
| 1666 | * could give a filesystem state which requires a degraded mount. |
| 1667 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1668 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1669 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1670 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1671 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1672 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1673 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1674 | |
| 1675 | /* |
| 1676 | * the device list mutex makes sure that we don't change |
| 1677 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1678 | * the device supers. Whoever is writing all supers, should |
| 1679 | * lock the device list mutex before getting the number of |
| 1680 | * devices in the super block (super_copy). Conversely, |
| 1681 | * whoever updates the number of devices in the super block |
| 1682 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1683 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1684 | |
| 1685 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1686 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1687 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1688 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1689 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1690 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1691 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1692 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1693 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1694 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1695 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1696 | struct btrfs_device, dev_list); |
| 1697 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1698 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1699 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1700 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1701 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1702 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1703 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1704 | /* remove sysfs entry */ |
| 1705 | btrfs_kobj_rm_device(root->fs_info, device); |
| 1706 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1707 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1708 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1709 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1710 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1711 | 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] | 1712 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1713 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1714 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1715 | struct btrfs_fs_devices *fs_devices; |
| 1716 | fs_devices = root->fs_info->fs_devices; |
| 1717 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1718 | if (fs_devices->seed == cur_devices) { |
| 1719 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1720 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1721 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1722 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1723 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1724 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1725 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1726 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1727 | } |
| 1728 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1729 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1730 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1731 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1732 | /* |
| 1733 | * at this point, the device is zero sized. We want to |
| 1734 | * remove it from the devices list and zero out the old super |
| 1735 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1736 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1737 | u64 bytenr; |
| 1738 | int i; |
| 1739 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1740 | /* make sure this device isn't detected as part of |
| 1741 | * the FS anymore |
| 1742 | */ |
| 1743 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1744 | set_buffer_dirty(bh); |
| 1745 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1746 | |
| 1747 | /* clear the mirror copies of super block on the disk |
| 1748 | * being removed, 0th copy is been taken care above and |
| 1749 | * the below would take of the rest |
| 1750 | */ |
| 1751 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1752 | bytenr = btrfs_sb_offset(i); |
| 1753 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1754 | i_size_read(bdev->bd_inode)) |
| 1755 | break; |
| 1756 | |
| 1757 | brelse(bh); |
| 1758 | bh = __bread(bdev, bytenr / 4096, |
| 1759 | BTRFS_SUPER_INFO_SIZE); |
| 1760 | if (!bh) |
| 1761 | continue; |
| 1762 | |
| 1763 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1764 | |
| 1765 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1766 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1767 | continue; |
| 1768 | } |
| 1769 | memset(&disk_super->magic, 0, |
| 1770 | sizeof(disk_super->magic)); |
| 1771 | set_buffer_dirty(bh); |
| 1772 | sync_dirty_buffer(bh); |
| 1773 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1774 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1775 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1776 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1777 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1778 | if (bdev) { |
| 1779 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1780 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1781 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1782 | /* Update ctime/mtime for device path for libblkid */ |
| 1783 | update_dev_time(device_path); |
| 1784 | } |
| 1785 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1786 | error_brelse: |
| 1787 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1788 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1789 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1790 | out: |
| 1791 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1792 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1793 | error_undo: |
| 1794 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1795 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1796 | list_add(&device->dev_alloc_list, |
| 1797 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1798 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1799 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1800 | } |
| 1801 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1804 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, |
| 1805 | struct btrfs_device *srcdev) |
| 1806 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1807 | struct btrfs_fs_devices *fs_devices; |
| 1808 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1809 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1810 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1811 | /* |
| 1812 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1813 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1814 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1815 | * srcdev will have its correct fs_devices in both the cases. |
| 1816 | */ |
| 1817 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1818 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1819 | list_del_rcu(&srcdev->dev_list); |
| 1820 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1821 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1822 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1823 | fs_devices->missing_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1824 | |
| 1825 | if (srcdev->writeable) { |
| 1826 | fs_devices->rw_devices--; |
| 1827 | /* zero out the old super if it is writable */ |
| 1828 | btrfs_scratch_superblock(srcdev); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1829 | } |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 1830 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1831 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1832 | fs_devices->open_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1833 | |
| 1834 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1835 | |
| 1836 | /* |
| 1837 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 1838 | * zero |
| 1839 | */ |
| 1840 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 1841 | |
| 1842 | /* if this is no devs we rather delete the fs_devices */ |
| 1843 | if (!fs_devices->num_devices) { |
| 1844 | struct btrfs_fs_devices *tmp_fs_devices; |
| 1845 | |
| 1846 | tmp_fs_devices = fs_info->fs_devices; |
| 1847 | while (tmp_fs_devices) { |
| 1848 | if (tmp_fs_devices->seed == fs_devices) { |
| 1849 | tmp_fs_devices->seed = fs_devices->seed; |
| 1850 | break; |
| 1851 | } |
| 1852 | tmp_fs_devices = tmp_fs_devices->seed; |
| 1853 | } |
| 1854 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 1855 | __btrfs_close_devices(fs_devices); |
| 1856 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1857 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1861 | struct btrfs_device *tgtdev) |
| 1862 | { |
| 1863 | struct btrfs_device *next_device; |
| 1864 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1865 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1866 | WARN_ON(!tgtdev); |
| 1867 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1868 | if (tgtdev->bdev) { |
| 1869 | btrfs_scratch_superblock(tgtdev); |
| 1870 | fs_info->fs_devices->open_devices--; |
| 1871 | } |
| 1872 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1873 | |
| 1874 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1875 | struct btrfs_device, dev_list); |
| 1876 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1877 | fs_info->sb->s_bdev = next_device->bdev; |
| 1878 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1879 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1880 | list_del_rcu(&tgtdev->dev_list); |
| 1881 | |
| 1882 | call_rcu(&tgtdev->rcu, free_device); |
| 1883 | |
| 1884 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1885 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1888 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1889 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1890 | { |
| 1891 | int ret = 0; |
| 1892 | struct btrfs_super_block *disk_super; |
| 1893 | u64 devid; |
| 1894 | u8 *dev_uuid; |
| 1895 | struct block_device *bdev; |
| 1896 | struct buffer_head *bh; |
| 1897 | |
| 1898 | *device = NULL; |
| 1899 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1900 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1901 | if (ret) |
| 1902 | return ret; |
| 1903 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1904 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1905 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1906 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1907 | disk_super->fsid); |
| 1908 | brelse(bh); |
| 1909 | if (!*device) |
| 1910 | ret = -ENOENT; |
| 1911 | blkdev_put(bdev, FMODE_READ); |
| 1912 | return ret; |
| 1913 | } |
| 1914 | |
| 1915 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1916 | char *device_path, |
| 1917 | struct btrfs_device **device) |
| 1918 | { |
| 1919 | *device = NULL; |
| 1920 | if (strcmp(device_path, "missing") == 0) { |
| 1921 | struct list_head *devices; |
| 1922 | struct btrfs_device *tmp; |
| 1923 | |
| 1924 | devices = &root->fs_info->fs_devices->devices; |
| 1925 | /* |
| 1926 | * It is safe to read the devices since the volume_mutex |
| 1927 | * is held by the caller. |
| 1928 | */ |
| 1929 | list_for_each_entry(tmp, devices, dev_list) { |
| 1930 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1931 | *device = tmp; |
| 1932 | break; |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1937 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1938 | return -ENOENT; |
| 1939 | } |
| 1940 | |
| 1941 | return 0; |
| 1942 | } else { |
| 1943 | return btrfs_find_device_by_path(root, device_path, device); |
| 1944 | } |
| 1945 | } |
| 1946 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1947 | /* |
| 1948 | * does all the dirty work required for changing file system's UUID. |
| 1949 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1950 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1951 | { |
| 1952 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1953 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1954 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1955 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1956 | struct btrfs_device *device; |
| 1957 | u64 super_flags; |
| 1958 | |
| 1959 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1960 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1961 | return -EINVAL; |
| 1962 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1963 | seed_devices = __alloc_fs_devices(); |
| 1964 | if (IS_ERR(seed_devices)) |
| 1965 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1966 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1967 | old_devices = clone_fs_devices(fs_devices); |
| 1968 | if (IS_ERR(old_devices)) { |
| 1969 | kfree(seed_devices); |
| 1970 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1971 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1972 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1973 | list_add(&old_devices->list, &fs_uuids); |
| 1974 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1975 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1976 | seed_devices->opened = 1; |
| 1977 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1978 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1979 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1980 | |
| 1981 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1982 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1983 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1984 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1985 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1986 | |
| 1987 | lock_chunks(root); |
| 1988 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1989 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1990 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1991 | fs_devices->seeding = 0; |
| 1992 | fs_devices->num_devices = 0; |
| 1993 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 1994 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 1995 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1996 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1997 | |
| 1998 | generate_random_uuid(fs_devices->fsid); |
| 1999 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2000 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2001 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2002 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2003 | super_flags = btrfs_super_flags(disk_super) & |
| 2004 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2005 | btrfs_set_super_flags(disk_super, super_flags); |
| 2006 | |
| 2007 | return 0; |
| 2008 | } |
| 2009 | |
| 2010 | /* |
| 2011 | * strore the expected generation for seed devices in device items. |
| 2012 | */ |
| 2013 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2014 | struct btrfs_root *root) |
| 2015 | { |
| 2016 | struct btrfs_path *path; |
| 2017 | struct extent_buffer *leaf; |
| 2018 | struct btrfs_dev_item *dev_item; |
| 2019 | struct btrfs_device *device; |
| 2020 | struct btrfs_key key; |
| 2021 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2022 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2023 | u64 devid; |
| 2024 | int ret; |
| 2025 | |
| 2026 | path = btrfs_alloc_path(); |
| 2027 | if (!path) |
| 2028 | return -ENOMEM; |
| 2029 | |
| 2030 | root = root->fs_info->chunk_root; |
| 2031 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2032 | key.offset = 0; |
| 2033 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2034 | |
| 2035 | while (1) { |
| 2036 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2037 | if (ret < 0) |
| 2038 | goto error; |
| 2039 | |
| 2040 | leaf = path->nodes[0]; |
| 2041 | next_slot: |
| 2042 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2043 | ret = btrfs_next_leaf(root, path); |
| 2044 | if (ret > 0) |
| 2045 | break; |
| 2046 | if (ret < 0) |
| 2047 | goto error; |
| 2048 | leaf = path->nodes[0]; |
| 2049 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2050 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2051 | continue; |
| 2052 | } |
| 2053 | |
| 2054 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2055 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2056 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2057 | break; |
| 2058 | |
| 2059 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2060 | struct btrfs_dev_item); |
| 2061 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2062 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2063 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2064 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2065 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2066 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2067 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2068 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2069 | |
| 2070 | if (device->fs_devices->seeding) { |
| 2071 | btrfs_set_device_generation(leaf, dev_item, |
| 2072 | device->generation); |
| 2073 | btrfs_mark_buffer_dirty(leaf); |
| 2074 | } |
| 2075 | |
| 2076 | path->slots[0]++; |
| 2077 | goto next_slot; |
| 2078 | } |
| 2079 | ret = 0; |
| 2080 | error: |
| 2081 | btrfs_free_path(path); |
| 2082 | return ret; |
| 2083 | } |
| 2084 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2085 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2086 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2087 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2088 | struct btrfs_trans_handle *trans; |
| 2089 | struct btrfs_device *device; |
| 2090 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2091 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2092 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2093 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2094 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2095 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2096 | int ret = 0; |
| 2097 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2098 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2099 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2100 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2101 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2102 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2103 | if (IS_ERR(bdev)) |
| 2104 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2105 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2106 | if (root->fs_info->fs_devices->seeding) { |
| 2107 | seeding_dev = 1; |
| 2108 | down_write(&sb->s_umount); |
| 2109 | mutex_lock(&uuid_mutex); |
| 2110 | } |
| 2111 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2112 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2113 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2114 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2115 | |
| 2116 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2117 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2118 | if (device->bdev == bdev) { |
| 2119 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2120 | mutex_unlock( |
| 2121 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2122 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2123 | } |
| 2124 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2125 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2126 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2127 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2128 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2129 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2130 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2131 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2132 | } |
| 2133 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2134 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2135 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2136 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2137 | ret = -ENOMEM; |
| 2138 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2139 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2140 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2141 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2142 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2143 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2144 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2145 | kfree(device); |
| 2146 | ret = PTR_ERR(trans); |
| 2147 | goto error; |
| 2148 | } |
| 2149 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2150 | q = bdev_get_queue(bdev); |
| 2151 | if (blk_queue_discard(q)) |
| 2152 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2153 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2154 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2155 | device->io_width = root->sectorsize; |
| 2156 | device->io_align = root->sectorsize; |
| 2157 | device->sector_size = root->sectorsize; |
| 2158 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2159 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2160 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2161 | device->dev_root = root->fs_info->dev_root; |
| 2162 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2163 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2164 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2165 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2166 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2167 | set_blocksize(device->bdev, 4096); |
| 2168 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2169 | if (seeding_dev) { |
| 2170 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2171 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2172 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2176 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2177 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2178 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2179 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2180 | list_add(&device->dev_alloc_list, |
| 2181 | &root->fs_info->fs_devices->alloc_list); |
| 2182 | root->fs_info->fs_devices->num_devices++; |
| 2183 | root->fs_info->fs_devices->open_devices++; |
| 2184 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2185 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2186 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2187 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2188 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2189 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2190 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2191 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2192 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2193 | root->fs_info->fs_devices->rotating = 1; |
| 2194 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2195 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2196 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2197 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2198 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2199 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2200 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2201 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2202 | |
| 2203 | /* add sysfs device entry */ |
| 2204 | btrfs_kobj_add_device(root->fs_info, device); |
| 2205 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2206 | /* |
| 2207 | * we've got more storage, clear any full flags on the space |
| 2208 | * infos |
| 2209 | */ |
| 2210 | btrfs_clear_space_info_full(root->fs_info); |
| 2211 | |
| 2212 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2213 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2214 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2215 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2216 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2217 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2218 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2219 | if (ret) { |
| 2220 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2221 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2222 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | ret = btrfs_add_device(trans, root, device); |
| 2226 | if (ret) { |
| 2227 | btrfs_abort_transaction(trans, root, ret); |
| 2228 | goto error_trans; |
| 2229 | } |
| 2230 | |
| 2231 | if (seeding_dev) { |
| 2232 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2233 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2234 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2235 | if (ret) { |
| 2236 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2237 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2238 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2239 | |
| 2240 | /* Sprouting would change fsid of the mounted root, |
| 2241 | * so rename the fsid on the sysfs |
| 2242 | */ |
| 2243 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2244 | root->fs_info->fsid); |
| 2245 | if (kobject_rename(&root->fs_info->super_kobj, fsid_buf)) |
| 2246 | goto error_trans; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2247 | } |
| 2248 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2249 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2250 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2251 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2252 | |
| 2253 | if (seeding_dev) { |
| 2254 | mutex_unlock(&uuid_mutex); |
| 2255 | up_write(&sb->s_umount); |
| 2256 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2257 | if (ret) /* transaction commit */ |
| 2258 | return ret; |
| 2259 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2260 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2261 | if (ret < 0) |
| 2262 | btrfs_error(root->fs_info, ret, |
| 2263 | "Failed to relocate sys chunks after " |
| 2264 | "device initialization. This can be fixed " |
| 2265 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2266 | trans = btrfs_attach_transaction(root); |
| 2267 | if (IS_ERR(trans)) { |
| 2268 | if (PTR_ERR(trans) == -ENOENT) |
| 2269 | return 0; |
| 2270 | return PTR_ERR(trans); |
| 2271 | } |
| 2272 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2273 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2274 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2275 | /* Update ctime/mtime for libblkid */ |
| 2276 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2277 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2278 | |
| 2279 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2280 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2281 | rcu_string_free(device->name); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2282 | btrfs_kobj_rm_device(root->fs_info, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2283 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2284 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2285 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2286 | if (seeding_dev) { |
| 2287 | mutex_unlock(&uuid_mutex); |
| 2288 | up_write(&sb->s_umount); |
| 2289 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2290 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2291 | } |
| 2292 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2293 | 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] | 2294 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2295 | struct btrfs_device **device_out) |
| 2296 | { |
| 2297 | struct request_queue *q; |
| 2298 | struct btrfs_device *device; |
| 2299 | struct block_device *bdev; |
| 2300 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2301 | struct list_head *devices; |
| 2302 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2303 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2304 | int ret = 0; |
| 2305 | |
| 2306 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2307 | if (fs_info->fs_devices->seeding) { |
| 2308 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2309 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2310 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2311 | |
| 2312 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2313 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2314 | if (IS_ERR(bdev)) { |
| 2315 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2316 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2317 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2318 | |
| 2319 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2320 | |
| 2321 | devices = &fs_info->fs_devices->devices; |
| 2322 | list_for_each_entry(device, devices, dev_list) { |
| 2323 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2324 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2325 | ret = -EEXIST; |
| 2326 | goto error; |
| 2327 | } |
| 2328 | } |
| 2329 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2330 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2331 | if (i_size_read(bdev->bd_inode) < |
| 2332 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2333 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2334 | ret = -EINVAL; |
| 2335 | goto error; |
| 2336 | } |
| 2337 | |
| 2338 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2339 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2340 | if (IS_ERR(device)) { |
| 2341 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2342 | goto error; |
| 2343 | } |
| 2344 | |
| 2345 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2346 | if (!name) { |
| 2347 | kfree(device); |
| 2348 | ret = -ENOMEM; |
| 2349 | goto error; |
| 2350 | } |
| 2351 | rcu_assign_pointer(device->name, name); |
| 2352 | |
| 2353 | q = bdev_get_queue(bdev); |
| 2354 | if (blk_queue_discard(q)) |
| 2355 | device->can_discard = 1; |
| 2356 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2357 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2358 | device->generation = 0; |
| 2359 | device->io_width = root->sectorsize; |
| 2360 | device->io_align = root->sectorsize; |
| 2361 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2362 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2363 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2364 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2365 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2366 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2367 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2368 | device->dev_root = fs_info->dev_root; |
| 2369 | device->bdev = bdev; |
| 2370 | device->in_fs_metadata = 1; |
| 2371 | device->is_tgtdev_for_dev_replace = 1; |
| 2372 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2373 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2374 | set_blocksize(device->bdev, 4096); |
| 2375 | device->fs_devices = fs_info->fs_devices; |
| 2376 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2377 | fs_info->fs_devices->num_devices++; |
| 2378 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2379 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2380 | |
| 2381 | *device_out = device; |
| 2382 | return ret; |
| 2383 | |
| 2384 | error: |
| 2385 | blkdev_put(bdev, FMODE_EXCL); |
| 2386 | return ret; |
| 2387 | } |
| 2388 | |
| 2389 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2390 | struct btrfs_device *tgtdev) |
| 2391 | { |
| 2392 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2393 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2394 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2395 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2396 | tgtdev->dev_root = fs_info->dev_root; |
| 2397 | tgtdev->in_fs_metadata = 1; |
| 2398 | } |
| 2399 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2400 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2401 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2402 | { |
| 2403 | int ret; |
| 2404 | struct btrfs_path *path; |
| 2405 | struct btrfs_root *root; |
| 2406 | struct btrfs_dev_item *dev_item; |
| 2407 | struct extent_buffer *leaf; |
| 2408 | struct btrfs_key key; |
| 2409 | |
| 2410 | root = device->dev_root->fs_info->chunk_root; |
| 2411 | |
| 2412 | path = btrfs_alloc_path(); |
| 2413 | if (!path) |
| 2414 | return -ENOMEM; |
| 2415 | |
| 2416 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2417 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2418 | key.offset = device->devid; |
| 2419 | |
| 2420 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2421 | if (ret < 0) |
| 2422 | goto out; |
| 2423 | |
| 2424 | if (ret > 0) { |
| 2425 | ret = -ENOENT; |
| 2426 | goto out; |
| 2427 | } |
| 2428 | |
| 2429 | leaf = path->nodes[0]; |
| 2430 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2431 | |
| 2432 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2433 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2434 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2435 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2436 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2437 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2438 | btrfs_device_get_disk_total_bytes(device)); |
| 2439 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2440 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2441 | btrfs_mark_buffer_dirty(leaf); |
| 2442 | |
| 2443 | out: |
| 2444 | btrfs_free_path(path); |
| 2445 | return ret; |
| 2446 | } |
| 2447 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2448 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2449 | struct btrfs_device *device, u64 new_size) |
| 2450 | { |
| 2451 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2452 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2453 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2454 | u64 old_total; |
| 2455 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2456 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2457 | if (!device->writeable) |
| 2458 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2459 | |
| 2460 | lock_chunks(device->dev_root); |
| 2461 | old_total = btrfs_super_total_bytes(super_copy); |
| 2462 | diff = new_size - device->total_bytes; |
| 2463 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2464 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2465 | device->is_tgtdev_for_dev_replace) { |
| 2466 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2467 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2468 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2469 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2470 | fs_devices = device->dev_root->fs_info->fs_devices; |
| 2471 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2472 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2473 | device->fs_devices->total_rw_bytes += diff; |
| 2474 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2475 | btrfs_device_set_total_bytes(device, new_size); |
| 2476 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2477 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2478 | if (list_empty(&device->resized_list)) |
| 2479 | list_add_tail(&device->resized_list, |
| 2480 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2481 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2482 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2483 | return btrfs_update_device(trans, device); |
| 2484 | } |
| 2485 | |
| 2486 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2487 | struct btrfs_root *root, |
| 2488 | u64 chunk_tree, u64 chunk_objectid, |
| 2489 | u64 chunk_offset) |
| 2490 | { |
| 2491 | int ret; |
| 2492 | struct btrfs_path *path; |
| 2493 | struct btrfs_key key; |
| 2494 | |
| 2495 | root = root->fs_info->chunk_root; |
| 2496 | path = btrfs_alloc_path(); |
| 2497 | if (!path) |
| 2498 | return -ENOMEM; |
| 2499 | |
| 2500 | key.objectid = chunk_objectid; |
| 2501 | key.offset = chunk_offset; |
| 2502 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2503 | |
| 2504 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2505 | if (ret < 0) |
| 2506 | goto out; |
| 2507 | else if (ret > 0) { /* Logic error or corruption */ |
| 2508 | btrfs_error(root->fs_info, -ENOENT, |
| 2509 | "Failed lookup while freeing chunk."); |
| 2510 | ret = -ENOENT; |
| 2511 | goto out; |
| 2512 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2513 | |
| 2514 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2515 | if (ret < 0) |
| 2516 | btrfs_error(root->fs_info, ret, |
| 2517 | "Failed to delete chunk item."); |
| 2518 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2519 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2520 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2521 | } |
| 2522 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2523 | 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] | 2524 | chunk_offset) |
| 2525 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2526 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2527 | struct btrfs_disk_key *disk_key; |
| 2528 | struct btrfs_chunk *chunk; |
| 2529 | u8 *ptr; |
| 2530 | int ret = 0; |
| 2531 | u32 num_stripes; |
| 2532 | u32 array_size; |
| 2533 | u32 len = 0; |
| 2534 | u32 cur; |
| 2535 | struct btrfs_key key; |
| 2536 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2537 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2538 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2539 | |
| 2540 | ptr = super_copy->sys_chunk_array; |
| 2541 | cur = 0; |
| 2542 | |
| 2543 | while (cur < array_size) { |
| 2544 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2545 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2546 | |
| 2547 | len = sizeof(*disk_key); |
| 2548 | |
| 2549 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2550 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2551 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2552 | len += btrfs_chunk_item_size(num_stripes); |
| 2553 | } else { |
| 2554 | ret = -EIO; |
| 2555 | break; |
| 2556 | } |
| 2557 | if (key.objectid == chunk_objectid && |
| 2558 | key.offset == chunk_offset) { |
| 2559 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2560 | array_size -= len; |
| 2561 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2562 | } else { |
| 2563 | ptr += len; |
| 2564 | cur += len; |
| 2565 | } |
| 2566 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2567 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2568 | return ret; |
| 2569 | } |
| 2570 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame^] | 2571 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2572 | struct btrfs_root *root, u64 chunk_offset) |
| 2573 | { |
| 2574 | struct extent_map_tree *em_tree; |
| 2575 | struct extent_map *em; |
| 2576 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2577 | struct map_lookup *map; |
| 2578 | u64 dev_extent_len = 0; |
| 2579 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2580 | u64 chunk_tree = root->fs_info->chunk_root->objectid; |
| 2581 | int i, ret = 0; |
| 2582 | |
| 2583 | /* Just in case */ |
| 2584 | root = root->fs_info->chunk_root; |
| 2585 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2586 | |
| 2587 | read_lock(&em_tree->lock); |
| 2588 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2589 | read_unlock(&em_tree->lock); |
| 2590 | |
| 2591 | if (!em || em->start > chunk_offset || |
| 2592 | em->start + em->len < chunk_offset) { |
| 2593 | /* |
| 2594 | * This is a logic error, but we don't want to just rely on the |
| 2595 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2596 | * do anything we still error out. |
| 2597 | */ |
| 2598 | ASSERT(0); |
| 2599 | if (em) |
| 2600 | free_extent_map(em); |
| 2601 | return -EINVAL; |
| 2602 | } |
| 2603 | map = (struct map_lookup *)em->bdev; |
| 2604 | |
| 2605 | for (i = 0; i < map->num_stripes; i++) { |
| 2606 | struct btrfs_device *device = map->stripes[i].dev; |
| 2607 | ret = btrfs_free_dev_extent(trans, device, |
| 2608 | map->stripes[i].physical, |
| 2609 | &dev_extent_len); |
| 2610 | if (ret) { |
| 2611 | btrfs_abort_transaction(trans, root, ret); |
| 2612 | goto out; |
| 2613 | } |
| 2614 | |
| 2615 | if (device->bytes_used > 0) { |
| 2616 | lock_chunks(root); |
| 2617 | btrfs_device_set_bytes_used(device, |
| 2618 | device->bytes_used - dev_extent_len); |
| 2619 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2620 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2621 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2622 | btrfs_clear_space_info_full(root->fs_info); |
| 2623 | unlock_chunks(root); |
| 2624 | } |
| 2625 | |
| 2626 | if (map->stripes[i].dev) { |
| 2627 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2628 | if (ret) { |
| 2629 | btrfs_abort_transaction(trans, root, ret); |
| 2630 | goto out; |
| 2631 | } |
| 2632 | } |
| 2633 | } |
| 2634 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2635 | chunk_offset); |
| 2636 | if (ret) { |
| 2637 | btrfs_abort_transaction(trans, root, ret); |
| 2638 | goto out; |
| 2639 | } |
| 2640 | |
| 2641 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2642 | |
| 2643 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2644 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2645 | if (ret) { |
| 2646 | btrfs_abort_transaction(trans, root, ret); |
| 2647 | goto out; |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 2652 | if (ret) { |
| 2653 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2654 | goto out; |
| 2655 | } |
| 2656 | |
| 2657 | write_lock(&em_tree->lock); |
| 2658 | remove_extent_mapping(em_tree, em); |
| 2659 | write_unlock(&em_tree->lock); |
| 2660 | |
| 2661 | /* once for the tree */ |
| 2662 | free_extent_map(em); |
| 2663 | out: |
| 2664 | /* once for us */ |
| 2665 | free_extent_map(em); |
| 2666 | return ret; |
| 2667 | } |
| 2668 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2669 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2670 | u64 chunk_tree, u64 chunk_objectid, |
| 2671 | u64 chunk_offset) |
| 2672 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2673 | struct btrfs_root *extent_root; |
| 2674 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2675 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2676 | |
| 2677 | root = root->fs_info->chunk_root; |
| 2678 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2679 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2680 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2681 | if (ret) |
| 2682 | return -ENOSPC; |
| 2683 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2684 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2685 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2686 | if (ret) |
| 2687 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2688 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2689 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2690 | if (IS_ERR(trans)) { |
| 2691 | ret = PTR_ERR(trans); |
| 2692 | btrfs_std_error(root->fs_info, ret); |
| 2693 | return ret; |
| 2694 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2695 | |
| 2696 | /* |
| 2697 | * step two, delete the device extents and the |
| 2698 | * chunk tree entries |
| 2699 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame^] | 2700 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2701 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame^] | 2702 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2703 | } |
| 2704 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2705 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2706 | { |
| 2707 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2708 | struct btrfs_path *path; |
| 2709 | struct extent_buffer *leaf; |
| 2710 | struct btrfs_chunk *chunk; |
| 2711 | struct btrfs_key key; |
| 2712 | struct btrfs_key found_key; |
| 2713 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 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) { |
| 2750 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 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); |
| 3028 | do_div(stripe_length, factor); |
| 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, |
| 3261 | chunk_root->root_key.objectid, |
| 3262 | found_key.objectid, |
| 3263 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3264 | if (ret && ret != -ENOSPC) |
| 3265 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3266 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3267 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3268 | } else { |
| 3269 | spin_lock(&fs_info->balance_lock); |
| 3270 | bctl->stat.completed++; |
| 3271 | spin_unlock(&fs_info->balance_lock); |
| 3272 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3273 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3274 | if (found_key.offset == 0) |
| 3275 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3276 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3277 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3278 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3279 | if (counting) { |
| 3280 | btrfs_release_path(path); |
| 3281 | counting = false; |
| 3282 | goto again; |
| 3283 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3284 | error: |
| 3285 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3286 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3287 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3288 | enospc_errors); |
| 3289 | if (!ret) |
| 3290 | ret = -ENOSPC; |
| 3291 | } |
| 3292 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3293 | return ret; |
| 3294 | } |
| 3295 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3296 | /** |
| 3297 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3298 | * @flags: profile to validate |
| 3299 | * @extended: if true @flags is treated as an extended profile |
| 3300 | */ |
| 3301 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3302 | { |
| 3303 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3304 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3305 | |
| 3306 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3307 | |
| 3308 | /* 1) check that all other bits are zeroed */ |
| 3309 | if (flags & ~mask) |
| 3310 | return 0; |
| 3311 | |
| 3312 | /* 2) see if profile is reduced */ |
| 3313 | if (flags == 0) |
| 3314 | return !extended; /* "0" is valid for usual profiles */ |
| 3315 | |
| 3316 | /* true if exactly one bit set */ |
| 3317 | return (flags & (flags - 1)) == 0; |
| 3318 | } |
| 3319 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3320 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3321 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3322 | /* cancel requested || normal exit path */ |
| 3323 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3324 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3325 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3328 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3329 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3330 | int ret; |
| 3331 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3332 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3333 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3334 | if (ret) |
| 3335 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3336 | |
| 3337 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3338 | } |
| 3339 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3340 | /* |
| 3341 | * Should be called with both balance and volume mutexes held |
| 3342 | */ |
| 3343 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3344 | struct btrfs_ioctl_balance_args *bargs) |
| 3345 | { |
| 3346 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3347 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3348 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3349 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3350 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3351 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3352 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3353 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3354 | atomic_read(&fs_info->balance_pause_req) || |
| 3355 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3356 | ret = -EINVAL; |
| 3357 | goto out; |
| 3358 | } |
| 3359 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3360 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3361 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3362 | mixed = 1; |
| 3363 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3364 | /* |
| 3365 | * In case of mixed groups both data and meta should be picked, |
| 3366 | * and identical options should be given for both of them. |
| 3367 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3368 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3369 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3370 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3371 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3372 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3373 | btrfs_err(fs_info, "with mixed groups data and " |
| 3374 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3375 | ret = -EINVAL; |
| 3376 | goto out; |
| 3377 | } |
| 3378 | } |
| 3379 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3380 | num_devices = fs_info->fs_devices->num_devices; |
| 3381 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3382 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3383 | BUG_ON(num_devices < 1); |
| 3384 | num_devices--; |
| 3385 | } |
| 3386 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3387 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3388 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3389 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3390 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3391 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3392 | if (num_devices > 2) |
| 3393 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3394 | if (num_devices > 3) |
| 3395 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3396 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3397 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3398 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3399 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3400 | btrfs_err(fs_info, "unable to start balance with target " |
| 3401 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3402 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3403 | ret = -EINVAL; |
| 3404 | goto out; |
| 3405 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3406 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3407 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3408 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3409 | btrfs_err(fs_info, |
| 3410 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3411 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3412 | ret = -EINVAL; |
| 3413 | goto out; |
| 3414 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3415 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3416 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3417 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3418 | btrfs_err(fs_info, |
| 3419 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3420 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3421 | ret = -EINVAL; |
| 3422 | goto out; |
| 3423 | } |
| 3424 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3425 | /* allow dup'ed data chunks only in mixed mode */ |
| 3426 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3427 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3428 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3429 | ret = -EINVAL; |
| 3430 | goto out; |
| 3431 | } |
| 3432 | |
| 3433 | /* allow to reduce meta or sys integrity only if force set */ |
| 3434 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3435 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3436 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3437 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3438 | do { |
| 3439 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3440 | |
| 3441 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3442 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3443 | !(bctl->sys.target & allowed)) || |
| 3444 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3445 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3446 | !(bctl->meta.target & allowed))) { |
| 3447 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3448 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3449 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3450 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3451 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3452 | ret = -EINVAL; |
| 3453 | goto out; |
| 3454 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3455 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3456 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3457 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3458 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3459 | int num_tolerated_disk_barrier_failures; |
| 3460 | u64 target = bctl->sys.target; |
| 3461 | |
| 3462 | num_tolerated_disk_barrier_failures = |
| 3463 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3464 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3465 | (target & |
| 3466 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3467 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3468 | num_tolerated_disk_barrier_failures = 0; |
| 3469 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3470 | (target & |
| 3471 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3472 | num_tolerated_disk_barrier_failures = 1; |
| 3473 | |
| 3474 | fs_info->num_tolerated_disk_barrier_failures = |
| 3475 | num_tolerated_disk_barrier_failures; |
| 3476 | } |
| 3477 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3478 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3479 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3480 | goto out; |
| 3481 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3482 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3483 | BUG_ON(ret == -EEXIST); |
| 3484 | set_balance_control(bctl); |
| 3485 | } else { |
| 3486 | BUG_ON(ret != -EEXIST); |
| 3487 | spin_lock(&fs_info->balance_lock); |
| 3488 | update_balance_args(bctl); |
| 3489 | spin_unlock(&fs_info->balance_lock); |
| 3490 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3491 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3492 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3493 | mutex_unlock(&fs_info->balance_mutex); |
| 3494 | |
| 3495 | ret = __btrfs_balance(fs_info); |
| 3496 | |
| 3497 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3498 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3499 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3500 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3501 | fs_info->num_tolerated_disk_barrier_failures = |
| 3502 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3503 | } |
| 3504 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3505 | if (bargs) { |
| 3506 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3507 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3508 | } |
| 3509 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3510 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3511 | balance_need_close(fs_info)) { |
| 3512 | __cancel_balance(fs_info); |
| 3513 | } |
| 3514 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3515 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3516 | |
| 3517 | return ret; |
| 3518 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3519 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3520 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3521 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3522 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3523 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3524 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3525 | return ret; |
| 3526 | } |
| 3527 | |
| 3528 | static int balance_kthread(void *data) |
| 3529 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3530 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3531 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3532 | |
| 3533 | mutex_lock(&fs_info->volume_mutex); |
| 3534 | mutex_lock(&fs_info->balance_mutex); |
| 3535 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3536 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3537 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3538 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3539 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3540 | |
| 3541 | mutex_unlock(&fs_info->balance_mutex); |
| 3542 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3543 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3544 | return ret; |
| 3545 | } |
| 3546 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3547 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3548 | { |
| 3549 | struct task_struct *tsk; |
| 3550 | |
| 3551 | spin_lock(&fs_info->balance_lock); |
| 3552 | if (!fs_info->balance_ctl) { |
| 3553 | spin_unlock(&fs_info->balance_lock); |
| 3554 | return 0; |
| 3555 | } |
| 3556 | spin_unlock(&fs_info->balance_lock); |
| 3557 | |
| 3558 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3559 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3560 | return 0; |
| 3561 | } |
| 3562 | |
| 3563 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3564 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3565 | } |
| 3566 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3567 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3568 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3569 | struct btrfs_balance_control *bctl; |
| 3570 | struct btrfs_balance_item *item; |
| 3571 | struct btrfs_disk_balance_args disk_bargs; |
| 3572 | struct btrfs_path *path; |
| 3573 | struct extent_buffer *leaf; |
| 3574 | struct btrfs_key key; |
| 3575 | int ret; |
| 3576 | |
| 3577 | path = btrfs_alloc_path(); |
| 3578 | if (!path) |
| 3579 | return -ENOMEM; |
| 3580 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3581 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3582 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3583 | key.offset = 0; |
| 3584 | |
| 3585 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3586 | if (ret < 0) |
| 3587 | goto out; |
| 3588 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3589 | ret = 0; |
| 3590 | goto out; |
| 3591 | } |
| 3592 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3593 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3594 | if (!bctl) { |
| 3595 | ret = -ENOMEM; |
| 3596 | goto out; |
| 3597 | } |
| 3598 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3599 | leaf = path->nodes[0]; |
| 3600 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3601 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3602 | bctl->fs_info = fs_info; |
| 3603 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3604 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3605 | |
| 3606 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3607 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3608 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3609 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3610 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3611 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3612 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3613 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3614 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3615 | mutex_lock(&fs_info->volume_mutex); |
| 3616 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3617 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3618 | set_balance_control(bctl); |
| 3619 | |
| 3620 | mutex_unlock(&fs_info->balance_mutex); |
| 3621 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3622 | out: |
| 3623 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3624 | return ret; |
| 3625 | } |
| 3626 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3627 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3628 | { |
| 3629 | int ret = 0; |
| 3630 | |
| 3631 | mutex_lock(&fs_info->balance_mutex); |
| 3632 | if (!fs_info->balance_ctl) { |
| 3633 | mutex_unlock(&fs_info->balance_mutex); |
| 3634 | return -ENOTCONN; |
| 3635 | } |
| 3636 | |
| 3637 | if (atomic_read(&fs_info->balance_running)) { |
| 3638 | atomic_inc(&fs_info->balance_pause_req); |
| 3639 | mutex_unlock(&fs_info->balance_mutex); |
| 3640 | |
| 3641 | wait_event(fs_info->balance_wait_q, |
| 3642 | atomic_read(&fs_info->balance_running) == 0); |
| 3643 | |
| 3644 | mutex_lock(&fs_info->balance_mutex); |
| 3645 | /* we are good with balance_ctl ripped off from under us */ |
| 3646 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3647 | atomic_dec(&fs_info->balance_pause_req); |
| 3648 | } else { |
| 3649 | ret = -ENOTCONN; |
| 3650 | } |
| 3651 | |
| 3652 | mutex_unlock(&fs_info->balance_mutex); |
| 3653 | return ret; |
| 3654 | } |
| 3655 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3656 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3657 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3658 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3659 | return -EROFS; |
| 3660 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3661 | mutex_lock(&fs_info->balance_mutex); |
| 3662 | if (!fs_info->balance_ctl) { |
| 3663 | mutex_unlock(&fs_info->balance_mutex); |
| 3664 | return -ENOTCONN; |
| 3665 | } |
| 3666 | |
| 3667 | atomic_inc(&fs_info->balance_cancel_req); |
| 3668 | /* |
| 3669 | * if we are running just wait and return, balance item is |
| 3670 | * deleted in btrfs_balance in this case |
| 3671 | */ |
| 3672 | if (atomic_read(&fs_info->balance_running)) { |
| 3673 | mutex_unlock(&fs_info->balance_mutex); |
| 3674 | wait_event(fs_info->balance_wait_q, |
| 3675 | atomic_read(&fs_info->balance_running) == 0); |
| 3676 | mutex_lock(&fs_info->balance_mutex); |
| 3677 | } else { |
| 3678 | /* __cancel_balance needs volume_mutex */ |
| 3679 | mutex_unlock(&fs_info->balance_mutex); |
| 3680 | mutex_lock(&fs_info->volume_mutex); |
| 3681 | mutex_lock(&fs_info->balance_mutex); |
| 3682 | |
| 3683 | if (fs_info->balance_ctl) |
| 3684 | __cancel_balance(fs_info); |
| 3685 | |
| 3686 | mutex_unlock(&fs_info->volume_mutex); |
| 3687 | } |
| 3688 | |
| 3689 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3690 | atomic_dec(&fs_info->balance_cancel_req); |
| 3691 | mutex_unlock(&fs_info->balance_mutex); |
| 3692 | return 0; |
| 3693 | } |
| 3694 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3695 | static int btrfs_uuid_scan_kthread(void *data) |
| 3696 | { |
| 3697 | struct btrfs_fs_info *fs_info = data; |
| 3698 | struct btrfs_root *root = fs_info->tree_root; |
| 3699 | struct btrfs_key key; |
| 3700 | struct btrfs_key max_key; |
| 3701 | struct btrfs_path *path = NULL; |
| 3702 | int ret = 0; |
| 3703 | struct extent_buffer *eb; |
| 3704 | int slot; |
| 3705 | struct btrfs_root_item root_item; |
| 3706 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3707 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3708 | |
| 3709 | path = btrfs_alloc_path(); |
| 3710 | if (!path) { |
| 3711 | ret = -ENOMEM; |
| 3712 | goto out; |
| 3713 | } |
| 3714 | |
| 3715 | key.objectid = 0; |
| 3716 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3717 | key.offset = 0; |
| 3718 | |
| 3719 | max_key.objectid = (u64)-1; |
| 3720 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3721 | max_key.offset = (u64)-1; |
| 3722 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3723 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3724 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3725 | if (ret) { |
| 3726 | if (ret > 0) |
| 3727 | ret = 0; |
| 3728 | break; |
| 3729 | } |
| 3730 | |
| 3731 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3732 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3733 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3734 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3735 | goto skip; |
| 3736 | |
| 3737 | eb = path->nodes[0]; |
| 3738 | slot = path->slots[0]; |
| 3739 | item_size = btrfs_item_size_nr(eb, slot); |
| 3740 | if (item_size < sizeof(root_item)) |
| 3741 | goto skip; |
| 3742 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3743 | read_extent_buffer(eb, &root_item, |
| 3744 | btrfs_item_ptr_offset(eb, slot), |
| 3745 | (int)sizeof(root_item)); |
| 3746 | if (btrfs_root_refs(&root_item) == 0) |
| 3747 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3748 | |
| 3749 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3750 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3751 | if (trans) |
| 3752 | goto update_tree; |
| 3753 | |
| 3754 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3755 | /* |
| 3756 | * 1 - subvol uuid item |
| 3757 | * 1 - received_subvol uuid item |
| 3758 | */ |
| 3759 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3760 | if (IS_ERR(trans)) { |
| 3761 | ret = PTR_ERR(trans); |
| 3762 | break; |
| 3763 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3764 | continue; |
| 3765 | } else { |
| 3766 | goto skip; |
| 3767 | } |
| 3768 | update_tree: |
| 3769 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3770 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3771 | root_item.uuid, |
| 3772 | BTRFS_UUID_KEY_SUBVOL, |
| 3773 | key.objectid); |
| 3774 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3775 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3776 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3777 | break; |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3782 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3783 | root_item.received_uuid, |
| 3784 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3785 | key.objectid); |
| 3786 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3787 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3788 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3789 | break; |
| 3790 | } |
| 3791 | } |
| 3792 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3793 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3794 | if (trans) { |
| 3795 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3796 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3797 | if (ret) |
| 3798 | break; |
| 3799 | } |
| 3800 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3801 | btrfs_release_path(path); |
| 3802 | if (key.offset < (u64)-1) { |
| 3803 | key.offset++; |
| 3804 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3805 | key.offset = 0; |
| 3806 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3807 | } else if (key.objectid < (u64)-1) { |
| 3808 | key.offset = 0; |
| 3809 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3810 | key.objectid++; |
| 3811 | } else { |
| 3812 | break; |
| 3813 | } |
| 3814 | cond_resched(); |
| 3815 | } |
| 3816 | |
| 3817 | out: |
| 3818 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3819 | if (trans && !IS_ERR(trans)) |
| 3820 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3821 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3822 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3823 | else |
| 3824 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3825 | up(&fs_info->uuid_tree_rescan_sem); |
| 3826 | return 0; |
| 3827 | } |
| 3828 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3829 | /* |
| 3830 | * Callback for btrfs_uuid_tree_iterate(). |
| 3831 | * returns: |
| 3832 | * 0 check succeeded, the entry is not outdated. |
| 3833 | * < 0 if an error occured. |
| 3834 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3835 | */ |
| 3836 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3837 | u8 *uuid, u8 type, u64 subid) |
| 3838 | { |
| 3839 | struct btrfs_key key; |
| 3840 | int ret = 0; |
| 3841 | struct btrfs_root *subvol_root; |
| 3842 | |
| 3843 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3844 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3845 | goto out; |
| 3846 | |
| 3847 | key.objectid = subid; |
| 3848 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3849 | key.offset = (u64)-1; |
| 3850 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3851 | if (IS_ERR(subvol_root)) { |
| 3852 | ret = PTR_ERR(subvol_root); |
| 3853 | if (ret == -ENOENT) |
| 3854 | ret = 1; |
| 3855 | goto out; |
| 3856 | } |
| 3857 | |
| 3858 | switch (type) { |
| 3859 | case BTRFS_UUID_KEY_SUBVOL: |
| 3860 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3861 | ret = 1; |
| 3862 | break; |
| 3863 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3864 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3865 | BTRFS_UUID_SIZE)) |
| 3866 | ret = 1; |
| 3867 | break; |
| 3868 | } |
| 3869 | |
| 3870 | out: |
| 3871 | return ret; |
| 3872 | } |
| 3873 | |
| 3874 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3875 | { |
| 3876 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3877 | int ret; |
| 3878 | |
| 3879 | /* |
| 3880 | * 1st step is to iterate through the existing UUID tree and |
| 3881 | * to delete all entries that contain outdated data. |
| 3882 | * 2nd step is to add all missing entries to the UUID tree. |
| 3883 | */ |
| 3884 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 3885 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3886 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3887 | up(&fs_info->uuid_tree_rescan_sem); |
| 3888 | return ret; |
| 3889 | } |
| 3890 | return btrfs_uuid_scan_kthread(data); |
| 3891 | } |
| 3892 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3893 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3894 | { |
| 3895 | struct btrfs_trans_handle *trans; |
| 3896 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 3897 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3898 | struct task_struct *task; |
| 3899 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3900 | |
| 3901 | /* |
| 3902 | * 1 - root node |
| 3903 | * 1 - root item |
| 3904 | */ |
| 3905 | trans = btrfs_start_transaction(tree_root, 2); |
| 3906 | if (IS_ERR(trans)) |
| 3907 | return PTR_ERR(trans); |
| 3908 | |
| 3909 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 3910 | BTRFS_UUID_TREE_OBJECTID); |
| 3911 | if (IS_ERR(uuid_root)) { |
| 3912 | btrfs_abort_transaction(trans, tree_root, |
| 3913 | PTR_ERR(uuid_root)); |
| 3914 | return PTR_ERR(uuid_root); |
| 3915 | } |
| 3916 | |
| 3917 | fs_info->uuid_root = uuid_root; |
| 3918 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3919 | ret = btrfs_commit_transaction(trans, tree_root); |
| 3920 | if (ret) |
| 3921 | return ret; |
| 3922 | |
| 3923 | down(&fs_info->uuid_tree_rescan_sem); |
| 3924 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 3925 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3926 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3927 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3928 | up(&fs_info->uuid_tree_rescan_sem); |
| 3929 | return PTR_ERR(task); |
| 3930 | } |
| 3931 | |
| 3932 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3933 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3934 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3935 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3936 | { |
| 3937 | struct task_struct *task; |
| 3938 | |
| 3939 | down(&fs_info->uuid_tree_rescan_sem); |
| 3940 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 3941 | if (IS_ERR(task)) { |
| 3942 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3943 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3944 | up(&fs_info->uuid_tree_rescan_sem); |
| 3945 | return PTR_ERR(task); |
| 3946 | } |
| 3947 | |
| 3948 | return 0; |
| 3949 | } |
| 3950 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3951 | /* |
| 3952 | * shrinking a device means finding all of the device extents past |
| 3953 | * the new size, and then following the back refs to the chunks. |
| 3954 | * The chunk relocation code actually frees the device extent |
| 3955 | */ |
| 3956 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3957 | { |
| 3958 | struct btrfs_trans_handle *trans; |
| 3959 | struct btrfs_root *root = device->dev_root; |
| 3960 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3961 | struct btrfs_path *path; |
| 3962 | u64 length; |
| 3963 | u64 chunk_tree; |
| 3964 | u64 chunk_objectid; |
| 3965 | u64 chunk_offset; |
| 3966 | int ret; |
| 3967 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3968 | int failed = 0; |
| 3969 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3970 | struct extent_buffer *l; |
| 3971 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3972 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3973 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3974 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 3975 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3976 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3977 | if (device->is_tgtdev_for_dev_replace) |
| 3978 | return -EINVAL; |
| 3979 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3980 | path = btrfs_alloc_path(); |
| 3981 | if (!path) |
| 3982 | return -ENOMEM; |
| 3983 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3984 | path->reada = 2; |
| 3985 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3986 | lock_chunks(root); |
| 3987 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3988 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3989 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3990 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3991 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3992 | root->fs_info->free_chunk_space -= diff; |
| 3993 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3994 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3995 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3996 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3997 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3998 | key.objectid = device->devid; |
| 3999 | key.offset = (u64)-1; |
| 4000 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 4001 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4002 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4003 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4004 | if (ret < 0) |
| 4005 | goto done; |
| 4006 | |
| 4007 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 4008 | if (ret < 0) |
| 4009 | goto done; |
| 4010 | if (ret) { |
| 4011 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4012 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4013 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | l = path->nodes[0]; |
| 4017 | slot = path->slots[0]; |
| 4018 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4019 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4020 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4021 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4022 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4023 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4024 | |
| 4025 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4026 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4027 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4028 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4029 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4030 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4031 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4032 | |
| 4033 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 4034 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 4035 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4036 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4037 | |
| 4038 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 4039 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4040 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4041 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4042 | if (ret == -ENOSPC) |
| 4043 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4044 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4045 | |
| 4046 | if (failed && !retried) { |
| 4047 | failed = 0; |
| 4048 | retried = true; |
| 4049 | goto again; |
| 4050 | } else if (failed && retried) { |
| 4051 | ret = -ENOSPC; |
| 4052 | lock_chunks(root); |
| 4053 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4054 | btrfs_device_set_total_bytes(device, old_size); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4055 | if (device->writeable) |
| 4056 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4057 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4058 | root->fs_info->free_chunk_space += diff; |
| 4059 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4060 | unlock_chunks(root); |
| 4061 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4062 | } |
| 4063 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4064 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4065 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4066 | if (IS_ERR(trans)) { |
| 4067 | ret = PTR_ERR(trans); |
| 4068 | goto done; |
| 4069 | } |
| 4070 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4071 | lock_chunks(root); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4072 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4073 | if (list_empty(&device->resized_list)) |
| 4074 | list_add_tail(&device->resized_list, |
| 4075 | &root->fs_info->fs_devices->resized_devices); |
| 4076 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4077 | WARN_ON(diff > old_total); |
| 4078 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4079 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4080 | |
| 4081 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4082 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4083 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4084 | done: |
| 4085 | btrfs_free_path(path); |
| 4086 | return ret; |
| 4087 | } |
| 4088 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4089 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4090 | struct btrfs_key *key, |
| 4091 | struct btrfs_chunk *chunk, int item_size) |
| 4092 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4093 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4094 | struct btrfs_disk_key disk_key; |
| 4095 | u32 array_size; |
| 4096 | u8 *ptr; |
| 4097 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4098 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4099 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4100 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4101 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4102 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4103 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4104 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4105 | |
| 4106 | ptr = super_copy->sys_chunk_array + array_size; |
| 4107 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4108 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4109 | ptr += sizeof(disk_key); |
| 4110 | memcpy(ptr, chunk, item_size); |
| 4111 | item_size += sizeof(disk_key); |
| 4112 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4113 | unlock_chunks(root); |
| 4114 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4115 | return 0; |
| 4116 | } |
| 4117 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4118 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4119 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4120 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4121 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4122 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4123 | const struct btrfs_device_info *di_a = a; |
| 4124 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4125 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4126 | if (di_a->max_avail > di_b->max_avail) |
| 4127 | return -1; |
| 4128 | if (di_a->max_avail < di_b->max_avail) |
| 4129 | return 1; |
| 4130 | if (di_a->total_avail > di_b->total_avail) |
| 4131 | return -1; |
| 4132 | if (di_a->total_avail < di_b->total_avail) |
| 4133 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4134 | return 0; |
| 4135 | } |
| 4136 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4137 | static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 4138 | [BTRFS_RAID_RAID10] = { |
| 4139 | .sub_stripes = 2, |
| 4140 | .dev_stripes = 1, |
| 4141 | .devs_max = 0, /* 0 == as many as possible */ |
| 4142 | .devs_min = 4, |
| 4143 | .devs_increment = 2, |
| 4144 | .ncopies = 2, |
| 4145 | }, |
| 4146 | [BTRFS_RAID_RAID1] = { |
| 4147 | .sub_stripes = 1, |
| 4148 | .dev_stripes = 1, |
| 4149 | .devs_max = 2, |
| 4150 | .devs_min = 2, |
| 4151 | .devs_increment = 2, |
| 4152 | .ncopies = 2, |
| 4153 | }, |
| 4154 | [BTRFS_RAID_DUP] = { |
| 4155 | .sub_stripes = 1, |
| 4156 | .dev_stripes = 2, |
| 4157 | .devs_max = 1, |
| 4158 | .devs_min = 1, |
| 4159 | .devs_increment = 1, |
| 4160 | .ncopies = 2, |
| 4161 | }, |
| 4162 | [BTRFS_RAID_RAID0] = { |
| 4163 | .sub_stripes = 1, |
| 4164 | .dev_stripes = 1, |
| 4165 | .devs_max = 0, |
| 4166 | .devs_min = 2, |
| 4167 | .devs_increment = 1, |
| 4168 | .ncopies = 1, |
| 4169 | }, |
| 4170 | [BTRFS_RAID_SINGLE] = { |
| 4171 | .sub_stripes = 1, |
| 4172 | .dev_stripes = 1, |
| 4173 | .devs_max = 1, |
| 4174 | .devs_min = 1, |
| 4175 | .devs_increment = 1, |
| 4176 | .ncopies = 1, |
| 4177 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4178 | [BTRFS_RAID_RAID5] = { |
| 4179 | .sub_stripes = 1, |
| 4180 | .dev_stripes = 1, |
| 4181 | .devs_max = 0, |
| 4182 | .devs_min = 2, |
| 4183 | .devs_increment = 1, |
| 4184 | .ncopies = 2, |
| 4185 | }, |
| 4186 | [BTRFS_RAID_RAID6] = { |
| 4187 | .sub_stripes = 1, |
| 4188 | .dev_stripes = 1, |
| 4189 | .devs_max = 0, |
| 4190 | .devs_min = 3, |
| 4191 | .devs_increment = 1, |
| 4192 | .ncopies = 3, |
| 4193 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4194 | }; |
| 4195 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4196 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4197 | { |
| 4198 | /* TODO allow them to set a preferred stripe size */ |
| 4199 | return 64 * 1024; |
| 4200 | } |
| 4201 | |
| 4202 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4203 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4204 | if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))) |
| 4205 | return; |
| 4206 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4207 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4208 | } |
| 4209 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4210 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4211 | - sizeof(struct btrfs_item) \ |
| 4212 | - sizeof(struct btrfs_chunk)) \ |
| 4213 | / sizeof(struct btrfs_stripe) + 1) |
| 4214 | |
| 4215 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4216 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4217 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4218 | / sizeof(struct btrfs_stripe) + 1) |
| 4219 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4220 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4221 | struct btrfs_root *extent_root, u64 start, |
| 4222 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4223 | { |
| 4224 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4225 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4226 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4227 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4228 | struct extent_map_tree *em_tree; |
| 4229 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4230 | struct btrfs_device_info *devices_info = NULL; |
| 4231 | u64 total_avail; |
| 4232 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4233 | int data_stripes; /* number of stripes that count for |
| 4234 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4235 | int sub_stripes; /* sub_stripes info for map */ |
| 4236 | int dev_stripes; /* stripes per dev */ |
| 4237 | int devs_max; /* max devs to use */ |
| 4238 | int devs_min; /* min devs needed */ |
| 4239 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4240 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4241 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4242 | u64 max_stripe_size; |
| 4243 | u64 max_chunk_size; |
| 4244 | u64 stripe_size; |
| 4245 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4246 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4247 | int ndevs; |
| 4248 | int i; |
| 4249 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4250 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4251 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4252 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4253 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4254 | if (list_empty(&fs_devices->alloc_list)) |
| 4255 | return -ENOSPC; |
| 4256 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4257 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4258 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4259 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4260 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4261 | devs_max = btrfs_raid_array[index].devs_max; |
| 4262 | devs_min = btrfs_raid_array[index].devs_min; |
| 4263 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4264 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4265 | |
| 4266 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4267 | max_stripe_size = 1024 * 1024 * 1024; |
| 4268 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4269 | if (!devs_max) |
| 4270 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4271 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4272 | /* for larger filesystems, use larger metadata chunks */ |
| 4273 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4274 | max_stripe_size = 1024 * 1024 * 1024; |
| 4275 | else |
| 4276 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4277 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4278 | if (!devs_max) |
| 4279 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4280 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4281 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4282 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4283 | if (!devs_max) |
| 4284 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4285 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4286 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4287 | type); |
| 4288 | BUG_ON(1); |
| 4289 | } |
| 4290 | |
| 4291 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4292 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4293 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4294 | |
| 4295 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 4296 | GFP_NOFS); |
| 4297 | if (!devices_info) |
| 4298 | return -ENOMEM; |
| 4299 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4300 | cur = fs_devices->alloc_list.next; |
| 4301 | |
| 4302 | /* |
| 4303 | * in the first pass through the devices list, we gather information |
| 4304 | * about the available holes on each device. |
| 4305 | */ |
| 4306 | ndevs = 0; |
| 4307 | while (cur != &fs_devices->alloc_list) { |
| 4308 | struct btrfs_device *device; |
| 4309 | u64 max_avail; |
| 4310 | u64 dev_offset; |
| 4311 | |
| 4312 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4313 | |
| 4314 | cur = cur->next; |
| 4315 | |
| 4316 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4317 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4318 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4319 | continue; |
| 4320 | } |
| 4321 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4322 | if (!device->in_fs_metadata || |
| 4323 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4324 | continue; |
| 4325 | |
| 4326 | if (device->total_bytes > device->bytes_used) |
| 4327 | total_avail = device->total_bytes - device->bytes_used; |
| 4328 | else |
| 4329 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4330 | |
| 4331 | /* If there is no space on this device, skip it. */ |
| 4332 | if (total_avail == 0) |
| 4333 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4334 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4335 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4336 | max_stripe_size * dev_stripes, |
| 4337 | &dev_offset, &max_avail); |
| 4338 | if (ret && ret != -ENOSPC) |
| 4339 | goto error; |
| 4340 | |
| 4341 | if (ret == 0) |
| 4342 | max_avail = max_stripe_size * dev_stripes; |
| 4343 | |
| 4344 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4345 | continue; |
| 4346 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4347 | if (ndevs == fs_devices->rw_devices) { |
| 4348 | WARN(1, "%s: found more than %llu devices\n", |
| 4349 | __func__, fs_devices->rw_devices); |
| 4350 | break; |
| 4351 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4352 | devices_info[ndevs].dev_offset = dev_offset; |
| 4353 | devices_info[ndevs].max_avail = max_avail; |
| 4354 | devices_info[ndevs].total_avail = total_avail; |
| 4355 | devices_info[ndevs].dev = device; |
| 4356 | ++ndevs; |
| 4357 | } |
| 4358 | |
| 4359 | /* |
| 4360 | * now sort the devices by hole size / available space |
| 4361 | */ |
| 4362 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4363 | btrfs_cmp_device_info, NULL); |
| 4364 | |
| 4365 | /* round down to number of usable stripes */ |
| 4366 | ndevs -= ndevs % devs_increment; |
| 4367 | |
| 4368 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4369 | ret = -ENOSPC; |
| 4370 | goto error; |
| 4371 | } |
| 4372 | |
| 4373 | if (devs_max && ndevs > devs_max) |
| 4374 | ndevs = devs_max; |
| 4375 | /* |
| 4376 | * the primary goal is to maximize the number of stripes, so use as many |
| 4377 | * devices as possible, even if the stripes are not maximum sized. |
| 4378 | */ |
| 4379 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4380 | num_stripes = ndevs * dev_stripes; |
| 4381 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4382 | /* |
| 4383 | * this will have to be fixed for RAID1 and RAID10 over |
| 4384 | * more drives |
| 4385 | */ |
| 4386 | data_stripes = num_stripes / ncopies; |
| 4387 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4388 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4389 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4390 | btrfs_super_stripesize(info->super_copy)); |
| 4391 | data_stripes = num_stripes - 1; |
| 4392 | } |
| 4393 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4394 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4395 | btrfs_super_stripesize(info->super_copy)); |
| 4396 | data_stripes = num_stripes - 2; |
| 4397 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4398 | |
| 4399 | /* |
| 4400 | * Use the number of data stripes to figure out how big this chunk |
| 4401 | * is really going to be in terms of logical address space, |
| 4402 | * and compare that answer with the max chunk size |
| 4403 | */ |
| 4404 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4405 | u64 mask = (1ULL << 24) - 1; |
| 4406 | stripe_size = max_chunk_size; |
| 4407 | do_div(stripe_size, data_stripes); |
| 4408 | |
| 4409 | /* bump the answer up to a 16MB boundary */ |
| 4410 | stripe_size = (stripe_size + mask) & ~mask; |
| 4411 | |
| 4412 | /* but don't go higher than the limits we found |
| 4413 | * while searching for free extents |
| 4414 | */ |
| 4415 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4416 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4417 | } |
| 4418 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4419 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4420 | |
| 4421 | /* align to BTRFS_STRIPE_LEN */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4422 | do_div(stripe_size, raid_stripe_len); |
| 4423 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4424 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4425 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4426 | if (!map) { |
| 4427 | ret = -ENOMEM; |
| 4428 | goto error; |
| 4429 | } |
| 4430 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4431 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4432 | for (i = 0; i < ndevs; ++i) { |
| 4433 | for (j = 0; j < dev_stripes; ++j) { |
| 4434 | int s = i * dev_stripes + j; |
| 4435 | map->stripes[s].dev = devices_info[i].dev; |
| 4436 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4437 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4438 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4439 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4440 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4441 | map->stripe_len = raid_stripe_len; |
| 4442 | map->io_align = raid_stripe_len; |
| 4443 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4444 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4445 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4446 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4447 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4448 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4449 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4450 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4451 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4452 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4453 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4454 | ret = -ENOMEM; |
| 4455 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4456 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4457 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4458 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4459 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4460 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4461 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4462 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4463 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4464 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4465 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4466 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4467 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4468 | if (!ret) { |
| 4469 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4470 | atomic_inc(&em->refs); |
| 4471 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4472 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4473 | if (ret) { |
| 4474 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4475 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4476 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4477 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4478 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4479 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4480 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4481 | if (ret) |
| 4482 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4483 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4484 | for (i = 0; i < map->num_stripes; i++) { |
| 4485 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4486 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4487 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4488 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4489 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4490 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4491 | map->num_stripes); |
| 4492 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4493 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4494 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4495 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4496 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4497 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4498 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4499 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4500 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4501 | write_lock(&em_tree->lock); |
| 4502 | remove_extent_mapping(em_tree, em); |
| 4503 | write_unlock(&em_tree->lock); |
| 4504 | |
| 4505 | /* One for our allocation */ |
| 4506 | free_extent_map(em); |
| 4507 | /* One for the tree reference */ |
| 4508 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4509 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4510 | kfree(devices_info); |
| 4511 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4512 | } |
| 4513 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4514 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4515 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4516 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4517 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4518 | struct btrfs_key key; |
| 4519 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4520 | struct btrfs_device *device; |
| 4521 | struct btrfs_chunk *chunk; |
| 4522 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4523 | struct extent_map_tree *em_tree; |
| 4524 | struct extent_map *em; |
| 4525 | struct map_lookup *map; |
| 4526 | size_t item_size; |
| 4527 | u64 dev_offset; |
| 4528 | u64 stripe_size; |
| 4529 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4530 | int ret; |
| 4531 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4532 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4533 | read_lock(&em_tree->lock); |
| 4534 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4535 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4536 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4537 | if (!em) { |
| 4538 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4539 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4540 | return -EINVAL; |
| 4541 | } |
| 4542 | |
| 4543 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4544 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4545 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4546 | chunk_size, em->start, em->len); |
| 4547 | free_extent_map(em); |
| 4548 | return -EINVAL; |
| 4549 | } |
| 4550 | |
| 4551 | map = (struct map_lookup *)em->bdev; |
| 4552 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4553 | stripe_size = em->orig_block_len; |
| 4554 | |
| 4555 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4556 | if (!chunk) { |
| 4557 | ret = -ENOMEM; |
| 4558 | goto out; |
| 4559 | } |
| 4560 | |
| 4561 | for (i = 0; i < map->num_stripes; i++) { |
| 4562 | device = map->stripes[i].dev; |
| 4563 | dev_offset = map->stripes[i].physical; |
| 4564 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4565 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4566 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4567 | goto out; |
| 4568 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4569 | chunk_root->root_key.objectid, |
| 4570 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4571 | chunk_offset, dev_offset, |
| 4572 | stripe_size); |
| 4573 | if (ret) |
| 4574 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4575 | } |
| 4576 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4577 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4578 | for (i = 0; i < map->num_stripes; i++) { |
| 4579 | device = map->stripes[i].dev; |
| 4580 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4581 | |
| 4582 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4583 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4584 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4585 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4589 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4590 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4591 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4592 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4593 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4594 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4595 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4596 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4597 | |
| 4598 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4599 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4600 | key.offset = chunk_offset; |
| 4601 | |
| 4602 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4603 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4604 | /* |
| 4605 | * TODO: Cleanup of inserted chunk root in case of |
| 4606 | * failure. |
| 4607 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4608 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4609 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4610 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4611 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4612 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4613 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4614 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4615 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | /* |
| 4619 | * Chunk allocation falls into two parts. The first part does works |
| 4620 | * that make the new allocated chunk useable, but not do any operation |
| 4621 | * that modifies the chunk tree. The second part does the works that |
| 4622 | * require modifying the chunk tree. This division is important for the |
| 4623 | * bootstrap process of adding storage to a seed btrfs. |
| 4624 | */ |
| 4625 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4626 | struct btrfs_root *extent_root, u64 type) |
| 4627 | { |
| 4628 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4629 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4630 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4631 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4632 | } |
| 4633 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4634 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4635 | struct btrfs_root *root, |
| 4636 | struct btrfs_device *device) |
| 4637 | { |
| 4638 | u64 chunk_offset; |
| 4639 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4640 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4641 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4642 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4643 | int ret; |
| 4644 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4645 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4646 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4647 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4648 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4649 | if (ret) |
| 4650 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4651 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4652 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4653 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4654 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4655 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4656 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4657 | } |
| 4658 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4659 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4660 | { |
| 4661 | int max_errors; |
| 4662 | |
| 4663 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4664 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4665 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4666 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4667 | max_errors = 1; |
| 4668 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4669 | max_errors = 2; |
| 4670 | } else { |
| 4671 | max_errors = 0; |
| 4672 | } |
| 4673 | |
| 4674 | return max_errors; |
| 4675 | } |
| 4676 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4677 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4678 | { |
| 4679 | struct extent_map *em; |
| 4680 | struct map_lookup *map; |
| 4681 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4682 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4683 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4684 | int i; |
| 4685 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4686 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4687 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4688 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4689 | if (!em) |
| 4690 | return 1; |
| 4691 | |
| 4692 | map = (struct map_lookup *)em->bdev; |
| 4693 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4694 | if (map->stripes[i].dev->missing) { |
| 4695 | miss_ndevs++; |
| 4696 | continue; |
| 4697 | } |
| 4698 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4699 | if (!map->stripes[i].dev->writeable) { |
| 4700 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4701 | goto end; |
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 | |
| 4705 | /* |
| 4706 | * If the number of missing devices is larger than max errors, |
| 4707 | * we can not write the data into that chunk successfully, so |
| 4708 | * set it readonly. |
| 4709 | */ |
| 4710 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4711 | readonly = 1; |
| 4712 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4713 | free_extent_map(em); |
| 4714 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4718 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4719 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4723 | { |
| 4724 | struct extent_map *em; |
| 4725 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4726 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4727 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4728 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4729 | if (em) |
| 4730 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4731 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4732 | if (!em) |
| 4733 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4734 | /* once for us */ |
| 4735 | free_extent_map(em); |
| 4736 | /* once for the tree */ |
| 4737 | free_extent_map(em); |
| 4738 | } |
| 4739 | } |
| 4740 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4741 | 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] | 4742 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4743 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4744 | struct extent_map *em; |
| 4745 | struct map_lookup *map; |
| 4746 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4747 | int ret; |
| 4748 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4749 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4750 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4751 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4752 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4753 | /* |
| 4754 | * We could return errors for these cases, but that could get ugly and |
| 4755 | * we'd probably do the same thing which is just not do anything else |
| 4756 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4757 | */ |
| 4758 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4759 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4760 | logical+len); |
| 4761 | return 1; |
| 4762 | } |
| 4763 | |
| 4764 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4765 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4766 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4767 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4768 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4769 | return 1; |
| 4770 | } |
| 4771 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4772 | map = (struct map_lookup *)em->bdev; |
| 4773 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4774 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4775 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4776 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4777 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4778 | ret = 2; |
| 4779 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4780 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4781 | else |
| 4782 | ret = 1; |
| 4783 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4784 | |
| 4785 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4786 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4787 | ret++; |
| 4788 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4789 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4790 | return ret; |
| 4791 | } |
| 4792 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4793 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4794 | struct btrfs_mapping_tree *map_tree, |
| 4795 | u64 logical) |
| 4796 | { |
| 4797 | struct extent_map *em; |
| 4798 | struct map_lookup *map; |
| 4799 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4800 | unsigned long len = root->sectorsize; |
| 4801 | |
| 4802 | read_lock(&em_tree->lock); |
| 4803 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4804 | read_unlock(&em_tree->lock); |
| 4805 | BUG_ON(!em); |
| 4806 | |
| 4807 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4808 | map = (struct map_lookup *)em->bdev; |
| 4809 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4810 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4811 | len = map->stripe_len * nr_data_stripes(map); |
| 4812 | } |
| 4813 | free_extent_map(em); |
| 4814 | return len; |
| 4815 | } |
| 4816 | |
| 4817 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4818 | u64 logical, u64 len, int mirror_num) |
| 4819 | { |
| 4820 | struct extent_map *em; |
| 4821 | struct map_lookup *map; |
| 4822 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4823 | int ret = 0; |
| 4824 | |
| 4825 | read_lock(&em_tree->lock); |
| 4826 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4827 | read_unlock(&em_tree->lock); |
| 4828 | BUG_ON(!em); |
| 4829 | |
| 4830 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4831 | map = (struct map_lookup *)em->bdev; |
| 4832 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4833 | BTRFS_BLOCK_GROUP_RAID6)) |
| 4834 | ret = 1; |
| 4835 | free_extent_map(em); |
| 4836 | return ret; |
| 4837 | } |
| 4838 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4839 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4840 | struct map_lookup *map, int first, int num, |
| 4841 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4842 | { |
| 4843 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4844 | int tolerance; |
| 4845 | struct btrfs_device *srcdev; |
| 4846 | |
| 4847 | if (dev_replace_is_ongoing && |
| 4848 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4849 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4850 | srcdev = fs_info->dev_replace.srcdev; |
| 4851 | else |
| 4852 | srcdev = NULL; |
| 4853 | |
| 4854 | /* |
| 4855 | * try to avoid the drive that is the source drive for a |
| 4856 | * dev-replace procedure, only choose it if no other non-missing |
| 4857 | * mirror is available |
| 4858 | */ |
| 4859 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4860 | if (map->stripes[optimal].dev->bdev && |
| 4861 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4862 | return optimal; |
| 4863 | for (i = first; i < first + num; i++) { |
| 4864 | if (map->stripes[i].dev->bdev && |
| 4865 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4866 | return i; |
| 4867 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4868 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4869 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4870 | /* we couldn't find one that doesn't fail. Just return something |
| 4871 | * and the io error handling code will clean up eventually |
| 4872 | */ |
| 4873 | return optimal; |
| 4874 | } |
| 4875 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4876 | static inline int parity_smaller(u64 a, u64 b) |
| 4877 | { |
| 4878 | return a > b; |
| 4879 | } |
| 4880 | |
| 4881 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
| 4882 | static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map) |
| 4883 | { |
| 4884 | struct btrfs_bio_stripe s; |
| 4885 | int i; |
| 4886 | u64 l; |
| 4887 | int again = 1; |
| 4888 | |
| 4889 | while (again) { |
| 4890 | again = 0; |
| 4891 | for (i = 0; i < bbio->num_stripes - 1; i++) { |
| 4892 | if (parity_smaller(raid_map[i], raid_map[i+1])) { |
| 4893 | s = bbio->stripes[i]; |
| 4894 | l = raid_map[i]; |
| 4895 | bbio->stripes[i] = bbio->stripes[i+1]; |
| 4896 | raid_map[i] = raid_map[i+1]; |
| 4897 | bbio->stripes[i+1] = s; |
| 4898 | raid_map[i+1] = l; |
| 4899 | again = 1; |
| 4900 | } |
| 4901 | } |
| 4902 | } |
| 4903 | } |
| 4904 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4905 | 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] | 4906 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4907 | struct btrfs_bio **bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4908 | int mirror_num, u64 **raid_map_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4909 | { |
| 4910 | struct extent_map *em; |
| 4911 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4912 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4913 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4914 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4915 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4916 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4917 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4918 | u64 stripe_nr_orig; |
| 4919 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4920 | u64 stripe_len; |
| 4921 | u64 *raid_map = NULL; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4922 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4923 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4924 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4925 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4926 | int max_errors = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4927 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4928 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4929 | int dev_replace_is_ongoing = 0; |
| 4930 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4931 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4932 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4933 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4934 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4935 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4936 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4937 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4938 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4939 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4940 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4941 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4942 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4943 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4944 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4945 | if (em->start > logical || em->start + em->len < logical) { |
| 4946 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4947 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4948 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4949 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4950 | return -EINVAL; |
| 4951 | } |
| 4952 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4953 | map = (struct map_lookup *)em->bdev; |
| 4954 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4955 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4956 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4957 | stripe_nr = offset; |
| 4958 | /* |
| 4959 | * stripe_nr counts the total number of stripes we have to stride |
| 4960 | * to get to this block |
| 4961 | */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4962 | do_div(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4963 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4964 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4965 | BUG_ON(offset < stripe_offset); |
| 4966 | |
| 4967 | /* stripe_offset is the offset of this block in its stripe*/ |
| 4968 | stripe_offset = offset - stripe_offset; |
| 4969 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4970 | /* if we're here for raid56, we need to know the stripe aligned start */ |
| 4971 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4972 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 4973 | raid56_full_stripe_start = offset; |
| 4974 | |
| 4975 | /* allow a write of a full stripe, but make sure we don't |
| 4976 | * allow straddling of stripes |
| 4977 | */ |
| 4978 | do_div(raid56_full_stripe_start, full_stripe_len); |
| 4979 | raid56_full_stripe_start *= full_stripe_len; |
| 4980 | } |
| 4981 | |
| 4982 | if (rw & REQ_DISCARD) { |
| 4983 | /* we don't discard raid56 yet */ |
| 4984 | if (map->type & |
| 4985 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4986 | ret = -EOPNOTSUPP; |
| 4987 | goto out; |
| 4988 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4989 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4990 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 4991 | u64 max_len; |
| 4992 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 4993 | For other RAID types and for RAID[56] reads, just allow a single |
| 4994 | stripe (on a single disk). */ |
| 4995 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) && |
| 4996 | (rw & REQ_WRITE)) { |
| 4997 | max_len = stripe_len * nr_data_stripes(map) - |
| 4998 | (offset - raid56_full_stripe_start); |
| 4999 | } else { |
| 5000 | /* we limit the length of each bio to what fits in a stripe */ |
| 5001 | max_len = stripe_len - stripe_offset; |
| 5002 | } |
| 5003 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5004 | } else { |
| 5005 | *length = em->len - offset; |
| 5006 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5007 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5008 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5009 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5010 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5011 | goto out; |
| 5012 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5013 | btrfs_dev_replace_lock(dev_replace); |
| 5014 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5015 | if (!dev_replace_is_ongoing) |
| 5016 | btrfs_dev_replace_unlock(dev_replace); |
| 5017 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5018 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5019 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5020 | dev_replace->tgtdev != NULL) { |
| 5021 | /* |
| 5022 | * in dev-replace case, for repair case (that's the only |
| 5023 | * case where the mirror is selected explicitly when |
| 5024 | * calling btrfs_map_block), blocks left of the left cursor |
| 5025 | * can also be read from the target drive. |
| 5026 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5027 | * the last one to the array of stripes. For READ, it also |
| 5028 | * needs to be supported using the same mirror number. |
| 5029 | * If the requested block is not left of the left cursor, |
| 5030 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5031 | * returns one more in the dev-replace case. |
| 5032 | */ |
| 5033 | u64 tmp_length = *length; |
| 5034 | struct btrfs_bio *tmp_bbio = NULL; |
| 5035 | int tmp_num_stripes; |
| 5036 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5037 | int index_srcdev = 0; |
| 5038 | int found = 0; |
| 5039 | u64 physical_of_found = 0; |
| 5040 | |
| 5041 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5042 | logical, &tmp_length, &tmp_bbio, 0, NULL); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5043 | if (ret) { |
| 5044 | WARN_ON(tmp_bbio != NULL); |
| 5045 | goto out; |
| 5046 | } |
| 5047 | |
| 5048 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5049 | if (mirror_num > tmp_num_stripes) { |
| 5050 | /* |
| 5051 | * REQ_GET_READ_MIRRORS does not contain this |
| 5052 | * mirror, that means that the requested area |
| 5053 | * is not left of the left cursor |
| 5054 | */ |
| 5055 | ret = -EIO; |
| 5056 | kfree(tmp_bbio); |
| 5057 | goto out; |
| 5058 | } |
| 5059 | |
| 5060 | /* |
| 5061 | * process the rest of the function using the mirror_num |
| 5062 | * of the source drive. Therefore look it up first. |
| 5063 | * At the end, patch the device pointer to the one of the |
| 5064 | * target drive. |
| 5065 | */ |
| 5066 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5067 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5068 | /* |
| 5069 | * In case of DUP, in order to keep it |
| 5070 | * simple, only add the mirror with the |
| 5071 | * lowest physical address |
| 5072 | */ |
| 5073 | if (found && |
| 5074 | physical_of_found <= |
| 5075 | tmp_bbio->stripes[i].physical) |
| 5076 | continue; |
| 5077 | index_srcdev = i; |
| 5078 | found = 1; |
| 5079 | physical_of_found = |
| 5080 | tmp_bbio->stripes[i].physical; |
| 5081 | } |
| 5082 | } |
| 5083 | |
| 5084 | if (found) { |
| 5085 | mirror_num = index_srcdev + 1; |
| 5086 | patch_the_first_stripe_for_dev_replace = 1; |
| 5087 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5088 | } else { |
| 5089 | WARN_ON(1); |
| 5090 | ret = -EIO; |
| 5091 | kfree(tmp_bbio); |
| 5092 | goto out; |
| 5093 | } |
| 5094 | |
| 5095 | kfree(tmp_bbio); |
| 5096 | } else if (mirror_num > map->num_stripes) { |
| 5097 | mirror_num = 0; |
| 5098 | } |
| 5099 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5100 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5101 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5102 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5103 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5104 | do_div(stripe_nr_end, map->stripe_len); |
| 5105 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5106 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5107 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5108 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5109 | if (rw & REQ_DISCARD) |
| 5110 | num_stripes = min_t(u64, map->num_stripes, |
| 5111 | stripe_nr_end - stripe_nr_orig); |
| 5112 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5113 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5114 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5115 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5116 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5117 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5118 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5119 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5120 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5121 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5122 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5123 | current->pid % map->num_stripes, |
| 5124 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5125 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5126 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5127 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5128 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5129 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5130 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5131 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5132 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5133 | } else { |
| 5134 | mirror_num = 1; |
| 5135 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5136 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5137 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5138 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5139 | |
| 5140 | stripe_index = do_div(stripe_nr, factor); |
| 5141 | stripe_index *= map->sub_stripes; |
| 5142 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5143 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5144 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5145 | else if (rw & REQ_DISCARD) |
| 5146 | num_stripes = min_t(u64, map->sub_stripes * |
| 5147 | (stripe_nr_end - stripe_nr_orig), |
| 5148 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5149 | else if (mirror_num) |
| 5150 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5151 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5152 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5153 | stripe_index = find_live_mirror(fs_info, map, |
| 5154 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5155 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5156 | current->pid % map->sub_stripes, |
| 5157 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5158 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5159 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5160 | |
| 5161 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5162 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5163 | u64 tmp; |
| 5164 | |
| 5165 | if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1) |
| 5166 | && raid_map_ret) { |
| 5167 | int i, rot; |
| 5168 | |
| 5169 | /* push stripe_nr back to the start of the full stripe */ |
| 5170 | stripe_nr = raid56_full_stripe_start; |
| 5171 | do_div(stripe_nr, stripe_len); |
| 5172 | |
| 5173 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5174 | |
| 5175 | /* RAID[56] write or recovery. Return all stripes */ |
| 5176 | num_stripes = map->num_stripes; |
| 5177 | max_errors = nr_parity_stripes(map); |
| 5178 | |
Dulshani Gunawardhana | d9b0d9b | 2013-10-31 10:32:18 +0530 | [diff] [blame] | 5179 | raid_map = kmalloc_array(num_stripes, sizeof(u64), |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5180 | GFP_NOFS); |
| 5181 | if (!raid_map) { |
| 5182 | ret = -ENOMEM; |
| 5183 | goto out; |
| 5184 | } |
| 5185 | |
| 5186 | /* Work out the disk rotation on this stripe-set */ |
| 5187 | tmp = stripe_nr; |
| 5188 | rot = do_div(tmp, num_stripes); |
| 5189 | |
| 5190 | /* Fill in the logical address of each stripe */ |
| 5191 | tmp = stripe_nr * nr_data_stripes(map); |
| 5192 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5193 | raid_map[(i+rot) % num_stripes] = |
| 5194 | em->start + (tmp + i) * map->stripe_len; |
| 5195 | |
| 5196 | raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5197 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5198 | raid_map[(i+rot+1) % num_stripes] = |
| 5199 | RAID6_Q_STRIPE; |
| 5200 | |
| 5201 | *length = map->stripe_len; |
| 5202 | stripe_index = 0; |
| 5203 | stripe_offset = 0; |
| 5204 | } else { |
| 5205 | /* |
| 5206 | * Mirror #0 or #1 means the original data block. |
| 5207 | * Mirror #2 is RAID5 parity block. |
| 5208 | * Mirror #3 is RAID6 Q block. |
| 5209 | */ |
| 5210 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5211 | if (mirror_num > 1) |
| 5212 | stripe_index = nr_data_stripes(map) + |
| 5213 | mirror_num - 2; |
| 5214 | |
| 5215 | /* We distribute the parity blocks across stripes */ |
| 5216 | tmp = stripe_nr + stripe_index; |
| 5217 | stripe_index = do_div(tmp, map->num_stripes); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5218 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5219 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5220 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5221 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5222 | } else { |
| 5223 | /* |
| 5224 | * after this do_div call, stripe_nr is the number of stripes |
| 5225 | * on this device we have to walk to find the data, and |
| 5226 | * stripe_index is the number of our device in the stripe array |
| 5227 | */ |
| 5228 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5229 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5230 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5231 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5232 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5233 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5234 | if (dev_replace_is_ongoing) { |
| 5235 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5236 | num_alloc_stripes <<= 1; |
| 5237 | if (rw & REQ_GET_READ_MIRRORS) |
| 5238 | num_alloc_stripes++; |
| 5239 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5240 | bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5241 | if (!bbio) { |
Dave Jones | eb2067f | 2013-07-30 13:42:17 -0400 | [diff] [blame] | 5242 | kfree(raid_map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5243 | ret = -ENOMEM; |
| 5244 | goto out; |
| 5245 | } |
| 5246 | atomic_set(&bbio->error, 0); |
| 5247 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5248 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5249 | int factor = 0; |
| 5250 | int sub_stripes = 0; |
| 5251 | u64 stripes_per_dev = 0; |
| 5252 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5253 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5254 | |
| 5255 | if (map->type & |
| 5256 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5257 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5258 | sub_stripes = 1; |
| 5259 | else |
| 5260 | sub_stripes = map->sub_stripes; |
| 5261 | |
| 5262 | factor = map->num_stripes / sub_stripes; |
| 5263 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5264 | stripe_nr_orig, |
| 5265 | factor, |
| 5266 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5267 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5268 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5269 | } |
| 5270 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5271 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5272 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5273 | map->stripes[stripe_index].physical + |
| 5274 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5275 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5276 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5277 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5278 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5279 | bbio->stripes[i].length = stripes_per_dev * |
| 5280 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5281 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5282 | if (i / sub_stripes < remaining_stripes) |
| 5283 | bbio->stripes[i].length += |
| 5284 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5285 | |
| 5286 | /* |
| 5287 | * Special for the first stripe and |
| 5288 | * the last stripe: |
| 5289 | * |
| 5290 | * |-------|...|-------| |
| 5291 | * |----------| |
| 5292 | * off end_off |
| 5293 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5294 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5295 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5296 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5297 | |
| 5298 | if (stripe_index >= last_stripe && |
| 5299 | stripe_index <= (last_stripe + |
| 5300 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5301 | bbio->stripes[i].length -= |
| 5302 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5303 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5304 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5305 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5306 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5307 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5308 | |
| 5309 | stripe_index++; |
| 5310 | if (stripe_index == map->num_stripes) { |
| 5311 | /* This could only happen for RAID0/10 */ |
| 5312 | stripe_index = 0; |
| 5313 | stripe_nr++; |
| 5314 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5315 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5316 | } else { |
| 5317 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5318 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5319 | map->stripes[stripe_index].physical + |
| 5320 | stripe_offset + |
| 5321 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5322 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5323 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5324 | stripe_index++; |
| 5325 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5326 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5327 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5328 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5329 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5330 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5331 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5332 | dev_replace->tgtdev != NULL) { |
| 5333 | int index_where_to_add; |
| 5334 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5335 | |
| 5336 | /* |
| 5337 | * duplicate the write operations while the dev replace |
| 5338 | * procedure is running. Since the copying of the old disk |
| 5339 | * to the new disk takes place at run time while the |
| 5340 | * filesystem is mounted writable, the regular write |
| 5341 | * operations to the old disk have to be duplicated to go |
| 5342 | * to the new disk as well. |
| 5343 | * Note that device->missing is handled by the caller, and |
| 5344 | * that the write to the old disk is already set up in the |
| 5345 | * stripes array. |
| 5346 | */ |
| 5347 | index_where_to_add = num_stripes; |
| 5348 | for (i = 0; i < num_stripes; i++) { |
| 5349 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5350 | /* write to new disk, too */ |
| 5351 | struct btrfs_bio_stripe *new = |
| 5352 | bbio->stripes + index_where_to_add; |
| 5353 | struct btrfs_bio_stripe *old = |
| 5354 | bbio->stripes + i; |
| 5355 | |
| 5356 | new->physical = old->physical; |
| 5357 | new->length = old->length; |
| 5358 | new->dev = dev_replace->tgtdev; |
| 5359 | index_where_to_add++; |
| 5360 | max_errors++; |
| 5361 | } |
| 5362 | } |
| 5363 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5364 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5365 | dev_replace->tgtdev != NULL) { |
| 5366 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5367 | int index_srcdev = 0; |
| 5368 | int found = 0; |
| 5369 | u64 physical_of_found = 0; |
| 5370 | |
| 5371 | /* |
| 5372 | * During the dev-replace procedure, the target drive can |
| 5373 | * also be used to read data in case it is needed to repair |
| 5374 | * a corrupt block elsewhere. This is possible if the |
| 5375 | * requested area is left of the left cursor. In this area, |
| 5376 | * the target drive is a full copy of the source drive. |
| 5377 | */ |
| 5378 | for (i = 0; i < num_stripes; i++) { |
| 5379 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5380 | /* |
| 5381 | * In case of DUP, in order to keep it |
| 5382 | * simple, only add the mirror with the |
| 5383 | * lowest physical address |
| 5384 | */ |
| 5385 | if (found && |
| 5386 | physical_of_found <= |
| 5387 | bbio->stripes[i].physical) |
| 5388 | continue; |
| 5389 | index_srcdev = i; |
| 5390 | found = 1; |
| 5391 | physical_of_found = bbio->stripes[i].physical; |
| 5392 | } |
| 5393 | } |
| 5394 | if (found) { |
| 5395 | u64 length = map->stripe_len; |
| 5396 | |
| 5397 | if (physical_of_found + length <= |
| 5398 | dev_replace->cursor_left) { |
| 5399 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5400 | bbio->stripes + num_stripes; |
| 5401 | |
| 5402 | tgtdev_stripe->physical = physical_of_found; |
| 5403 | tgtdev_stripe->length = |
| 5404 | bbio->stripes[index_srcdev].length; |
| 5405 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5406 | |
| 5407 | num_stripes++; |
| 5408 | } |
| 5409 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5410 | } |
| 5411 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5412 | *bbio_ret = bbio; |
| 5413 | bbio->num_stripes = num_stripes; |
| 5414 | bbio->max_errors = max_errors; |
| 5415 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5416 | |
| 5417 | /* |
| 5418 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5419 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5420 | * available as a mirror |
| 5421 | */ |
| 5422 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5423 | WARN_ON(num_stripes > 1); |
| 5424 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5425 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5426 | bbio->mirror_num = map->num_stripes + 1; |
| 5427 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5428 | if (raid_map) { |
| 5429 | sort_parity_stripes(bbio, raid_map); |
| 5430 | *raid_map_ret = raid_map; |
| 5431 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5432 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5433 | if (dev_replace_is_ongoing) |
| 5434 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5435 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5436 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5437 | } |
| 5438 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5439 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5440 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5441 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5442 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5443 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5444 | mirror_num, NULL); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5445 | } |
| 5446 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5447 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5448 | u64 chunk_start, u64 physical, u64 devid, |
| 5449 | u64 **logical, int *naddrs, int *stripe_len) |
| 5450 | { |
| 5451 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5452 | struct extent_map *em; |
| 5453 | struct map_lookup *map; |
| 5454 | u64 *buf; |
| 5455 | u64 bytenr; |
| 5456 | u64 length; |
| 5457 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5458 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5459 | int i, j, nr = 0; |
| 5460 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5461 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5462 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5463 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5464 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5465 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5466 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5467 | chunk_start); |
| 5468 | return -EIO; |
| 5469 | } |
| 5470 | |
| 5471 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5472 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5473 | em->start, chunk_start); |
| 5474 | free_extent_map(em); |
| 5475 | return -EIO; |
| 5476 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5477 | map = (struct map_lookup *)em->bdev; |
| 5478 | |
| 5479 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5480 | rmap_len = map->stripe_len; |
| 5481 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5482 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5483 | do_div(length, map->num_stripes / map->sub_stripes); |
| 5484 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5485 | do_div(length, map->num_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5486 | else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5487 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5488 | do_div(length, nr_data_stripes(map)); |
| 5489 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5490 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5491 | |
| 5492 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5493 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5494 | |
| 5495 | for (i = 0; i < map->num_stripes; i++) { |
| 5496 | if (devid && map->stripes[i].dev->devid != devid) |
| 5497 | continue; |
| 5498 | if (map->stripes[i].physical > physical || |
| 5499 | map->stripes[i].physical + length <= physical) |
| 5500 | continue; |
| 5501 | |
| 5502 | stripe_nr = physical - map->stripes[i].physical; |
| 5503 | do_div(stripe_nr, map->stripe_len); |
| 5504 | |
| 5505 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5506 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 5507 | do_div(stripe_nr, map->sub_stripes); |
| 5508 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5509 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5510 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5511 | * Alternatively, just use rmap_len below instead of |
| 5512 | * map->stripe_len */ |
| 5513 | |
| 5514 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5515 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5516 | for (j = 0; j < nr; j++) { |
| 5517 | if (buf[j] == bytenr) |
| 5518 | break; |
| 5519 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5520 | if (j == nr) { |
| 5521 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5522 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5523 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5524 | } |
| 5525 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5526 | *logical = buf; |
| 5527 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5528 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5529 | |
| 5530 | free_extent_map(em); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5534 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5535 | { |
| 5536 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5537 | bio_endio_nodec(bio, err); |
| 5538 | else |
| 5539 | bio_endio(bio, err); |
| 5540 | kfree(bbio); |
| 5541 | } |
| 5542 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5543 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5544 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5545 | struct btrfs_bio *bbio = bio->bi_private; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5546 | struct btrfs_device *dev = bbio->stripes[0].dev; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5547 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5548 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5549 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5550 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5551 | if (err == -EIO || err == -EREMOTEIO) { |
| 5552 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5553 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5554 | |
| 5555 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5556 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5557 | if (dev->bdev) { |
| 5558 | if (bio->bi_rw & WRITE) |
| 5559 | btrfs_dev_stat_inc(dev, |
| 5560 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5561 | else |
| 5562 | btrfs_dev_stat_inc(dev, |
| 5563 | BTRFS_DEV_STAT_READ_ERRS); |
| 5564 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5565 | btrfs_dev_stat_inc(dev, |
| 5566 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5567 | btrfs_dev_stat_print_on_error(dev); |
| 5568 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5569 | } |
| 5570 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5571 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5572 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5573 | is_orig_bio = 1; |
| 5574 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5575 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5576 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5577 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5578 | if (!is_orig_bio) { |
| 5579 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5580 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5581 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5582 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5583 | bio->bi_private = bbio->private; |
| 5584 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5585 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5586 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5587 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5588 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5589 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5590 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5591 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5592 | /* |
| 5593 | * this bio is actually up to date, we didn't |
| 5594 | * go over the max number of errors |
| 5595 | */ |
| 5596 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5597 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5598 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5599 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5600 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5601 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5602 | bio_put(bio); |
| 5603 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5604 | } |
| 5605 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5606 | /* |
| 5607 | * see run_scheduled_bios for a description of why bios are collected for |
| 5608 | * async submit. |
| 5609 | * |
| 5610 | * This will add one bio to the pending list for a device and make sure |
| 5611 | * the work struct is scheduled. |
| 5612 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5613 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5614 | struct btrfs_device *device, |
| 5615 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5616 | { |
| 5617 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5618 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5619 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5620 | if (device->missing || !device->bdev) { |
| 5621 | bio_endio(bio, -EIO); |
| 5622 | return; |
| 5623 | } |
| 5624 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5625 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5626 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5627 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5628 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5629 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5630 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5634 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5635 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5636 | * made progress against dirty pages when we've really just put it |
| 5637 | * on a queue for later |
| 5638 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5639 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5640 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5641 | bio->bi_next = NULL; |
| 5642 | bio->bi_rw |= rw; |
| 5643 | |
| 5644 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5645 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5646 | pending_bios = &device->pending_sync_bios; |
| 5647 | else |
| 5648 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5649 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5650 | if (pending_bios->tail) |
| 5651 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5652 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5653 | pending_bios->tail = bio; |
| 5654 | if (!pending_bios->head) |
| 5655 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5656 | if (device->running_pending) |
| 5657 | should_queue = 0; |
| 5658 | |
| 5659 | spin_unlock(&device->io_lock); |
| 5660 | |
| 5661 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5662 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5663 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5664 | } |
| 5665 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5666 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5667 | sector_t sector) |
| 5668 | { |
| 5669 | struct bio_vec *prev; |
| 5670 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5671 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5672 | struct bvec_merge_data bvm = { |
| 5673 | .bi_bdev = bdev, |
| 5674 | .bi_sector = sector, |
| 5675 | .bi_rw = bio->bi_rw, |
| 5676 | }; |
| 5677 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5678 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5679 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5680 | |
| 5681 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5682 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5683 | return 0; |
| 5684 | |
| 5685 | if (!q->merge_bvec_fn) |
| 5686 | return 1; |
| 5687 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5688 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5689 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5690 | return 0; |
| 5691 | return 1; |
| 5692 | } |
| 5693 | |
| 5694 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5695 | struct bio *bio, u64 physical, int dev_nr, |
| 5696 | int rw, int async) |
| 5697 | { |
| 5698 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5699 | |
| 5700 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5701 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5702 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5703 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5704 | #ifdef DEBUG |
| 5705 | { |
| 5706 | struct rcu_string *name; |
| 5707 | |
| 5708 | rcu_read_lock(); |
| 5709 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5710 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5711 | "(%s id %llu), size=%u\n", rw, |
| 5712 | (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev, |
| 5713 | name->str, dev->devid, bio->bi_size); |
| 5714 | rcu_read_unlock(); |
| 5715 | } |
| 5716 | #endif |
| 5717 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5718 | |
| 5719 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5720 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5721 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5722 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5723 | else |
| 5724 | btrfsic_submit_bio(rw, bio); |
| 5725 | } |
| 5726 | |
| 5727 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5728 | struct bio *first_bio, struct btrfs_device *dev, |
| 5729 | int dev_nr, int rw, int async) |
| 5730 | { |
| 5731 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5732 | struct bio *bio; |
| 5733 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5734 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5735 | |
| 5736 | again: |
| 5737 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5738 | if (!bio) |
| 5739 | return -ENOMEM; |
| 5740 | |
| 5741 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5742 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5743 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5744 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5745 | |
| 5746 | atomic_inc(&bbio->stripes_pending); |
| 5747 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5748 | rw, async); |
| 5749 | physical += len; |
| 5750 | goto again; |
| 5751 | } |
| 5752 | bvec++; |
| 5753 | } |
| 5754 | |
| 5755 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5756 | return 0; |
| 5757 | } |
| 5758 | |
| 5759 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5760 | { |
| 5761 | atomic_inc(&bbio->error); |
| 5762 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5763 | /* Shoud be the original bio. */ |
| 5764 | WARN_ON(bio != bbio->orig_bio); |
| 5765 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5766 | bio->bi_private = bbio->private; |
| 5767 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5768 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5769 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5770 | |
| 5771 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5772 | } |
| 5773 | } |
| 5774 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5775 | 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] | 5776 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5777 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5778 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5779 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5780 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5781 | u64 length = 0; |
| 5782 | u64 map_length; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5783 | u64 *raid_map = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5784 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5785 | int dev_nr = 0; |
| 5786 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5787 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5788 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5789 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5790 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5791 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5792 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5793 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
| 5794 | mirror_num, &raid_map); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5795 | if (ret) { |
| 5796 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5797 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5798 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5799 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5800 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5801 | bbio->orig_bio = first_bio; |
| 5802 | bbio->private = first_bio->bi_private; |
| 5803 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5804 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5805 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5806 | |
| 5807 | if (raid_map) { |
| 5808 | /* In this case, map_length has been set to the length of |
| 5809 | a single stripe; not the whole write */ |
| 5810 | if (rw & WRITE) { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5811 | ret = raid56_parity_write(root, bio, bbio, |
| 5812 | raid_map, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5813 | } else { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5814 | ret = raid56_parity_recover(root, bio, bbio, |
| 5815 | raid_map, map_length, |
| 5816 | mirror_num); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5817 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5818 | /* |
| 5819 | * FIXME, replace dosen't support raid56 yet, please fix |
| 5820 | * it in the future. |
| 5821 | */ |
| 5822 | btrfs_bio_counter_dec(root->fs_info); |
| 5823 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5824 | } |
| 5825 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5826 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5827 | 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] | 5828 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5829 | BUG(); |
| 5830 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5831 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5832 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5833 | dev = bbio->stripes[dev_nr].dev; |
| 5834 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5835 | bbio_error(bbio, first_bio, logical); |
| 5836 | dev_nr++; |
| 5837 | continue; |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | * Check and see if we're ok with this bio based on it's size |
| 5842 | * and offset with the given device. |
| 5843 | */ |
| 5844 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5845 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5846 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5847 | dev_nr, rw, async_submit); |
| 5848 | BUG_ON(ret); |
| 5849 | dev_nr++; |
| 5850 | continue; |
| 5851 | } |
| 5852 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5853 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5854 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5855 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5856 | } else { |
| 5857 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5858 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5859 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5860 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5861 | submit_stripe_bio(root, bbio, bio, |
| 5862 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5863 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5864 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5865 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5866 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5867 | return 0; |
| 5868 | } |
| 5869 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5870 | 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] | 5871 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5872 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5873 | struct btrfs_device *device; |
| 5874 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5875 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5876 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5877 | while (cur_devices) { |
| 5878 | if (!fsid || |
| 5879 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5880 | device = __find_device(&cur_devices->devices, |
| 5881 | devid, uuid); |
| 5882 | if (device) |
| 5883 | return device; |
| 5884 | } |
| 5885 | cur_devices = cur_devices->seed; |
| 5886 | } |
| 5887 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5888 | } |
| 5889 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5890 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 5891 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5892 | u64 devid, u8 *dev_uuid) |
| 5893 | { |
| 5894 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5895 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5896 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 5897 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5898 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5899 | |
| 5900 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5901 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5902 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5903 | |
| 5904 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5905 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5906 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5907 | return device; |
| 5908 | } |
| 5909 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5910 | /** |
| 5911 | * btrfs_alloc_device - allocate struct btrfs_device |
| 5912 | * @fs_info: used only for generating a new devid, can be NULL if |
| 5913 | * devid is provided (i.e. @devid != NULL). |
| 5914 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 5915 | * is generated. |
| 5916 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 5917 | * is generated. |
| 5918 | * |
| 5919 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 5920 | * on error. Returned struct is not linked onto any lists and can be |
| 5921 | * destroyed with kfree() right away. |
| 5922 | */ |
| 5923 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 5924 | const u64 *devid, |
| 5925 | const u8 *uuid) |
| 5926 | { |
| 5927 | struct btrfs_device *dev; |
| 5928 | u64 tmp; |
| 5929 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5930 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5931 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5932 | |
| 5933 | dev = __alloc_device(); |
| 5934 | if (IS_ERR(dev)) |
| 5935 | return dev; |
| 5936 | |
| 5937 | if (devid) |
| 5938 | tmp = *devid; |
| 5939 | else { |
| 5940 | int ret; |
| 5941 | |
| 5942 | ret = find_next_devid(fs_info, &tmp); |
| 5943 | if (ret) { |
| 5944 | kfree(dev); |
| 5945 | return ERR_PTR(ret); |
| 5946 | } |
| 5947 | } |
| 5948 | dev->devid = tmp; |
| 5949 | |
| 5950 | if (uuid) |
| 5951 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 5952 | else |
| 5953 | generate_random_uuid(dev->uuid); |
| 5954 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 5955 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 5956 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5957 | |
| 5958 | return dev; |
| 5959 | } |
| 5960 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5961 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 5962 | struct extent_buffer *leaf, |
| 5963 | struct btrfs_chunk *chunk) |
| 5964 | { |
| 5965 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5966 | struct map_lookup *map; |
| 5967 | struct extent_map *em; |
| 5968 | u64 logical; |
| 5969 | u64 length; |
| 5970 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5971 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5972 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5973 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5974 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5975 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5976 | logical = key->offset; |
| 5977 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5978 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5979 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5980 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5981 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5982 | |
| 5983 | /* already mapped? */ |
| 5984 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 5985 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5986 | return 0; |
| 5987 | } else if (em) { |
| 5988 | free_extent_map(em); |
| 5989 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5990 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5991 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5992 | if (!em) |
| 5993 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5994 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 5995 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5996 | if (!map) { |
| 5997 | free_extent_map(em); |
| 5998 | return -ENOMEM; |
| 5999 | } |
| 6000 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6001 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6002 | em->bdev = (struct block_device *)map; |
| 6003 | em->start = logical; |
| 6004 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6005 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6006 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6007 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6008 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6009 | map->num_stripes = num_stripes; |
| 6010 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6011 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6012 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6013 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6014 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6015 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6016 | for (i = 0; i < num_stripes; i++) { |
| 6017 | map->stripes[i].physical = |
| 6018 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6019 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6020 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6021 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6022 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6023 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6024 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6025 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6026 | free_extent_map(em); |
| 6027 | return -EIO; |
| 6028 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6029 | if (!map->stripes[i].dev) { |
| 6030 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6031 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6032 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6033 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6034 | free_extent_map(em); |
| 6035 | return -EIO; |
| 6036 | } |
| 6037 | } |
| 6038 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6039 | } |
| 6040 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6041 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6042 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6043 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6044 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6045 | free_extent_map(em); |
| 6046 | |
| 6047 | return 0; |
| 6048 | } |
| 6049 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6050 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6051 | struct btrfs_dev_item *dev_item, |
| 6052 | struct btrfs_device *device) |
| 6053 | { |
| 6054 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6055 | |
| 6056 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6057 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6058 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6059 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6060 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6061 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6062 | device->type = btrfs_device_type(leaf, dev_item); |
| 6063 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6064 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6065 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6066 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6067 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6068 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6069 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6070 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6071 | } |
| 6072 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6073 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6074 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6075 | { |
| 6076 | struct btrfs_fs_devices *fs_devices; |
| 6077 | int ret; |
| 6078 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6079 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6080 | |
| 6081 | fs_devices = root->fs_info->fs_devices->seed; |
| 6082 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6083 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6084 | return fs_devices; |
| 6085 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6086 | fs_devices = fs_devices->seed; |
| 6087 | } |
| 6088 | |
| 6089 | fs_devices = find_fsid(fsid); |
| 6090 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6091 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6092 | return ERR_PTR(-ENOENT); |
| 6093 | |
| 6094 | fs_devices = alloc_fs_devices(fsid); |
| 6095 | if (IS_ERR(fs_devices)) |
| 6096 | return fs_devices; |
| 6097 | |
| 6098 | fs_devices->seeding = 1; |
| 6099 | fs_devices->opened = 1; |
| 6100 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6101 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6102 | |
| 6103 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6104 | if (IS_ERR(fs_devices)) |
| 6105 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6106 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6107 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6108 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6109 | if (ret) { |
| 6110 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6111 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6112 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6113 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6114 | |
| 6115 | if (!fs_devices->seeding) { |
| 6116 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6117 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6118 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6119 | goto out; |
| 6120 | } |
| 6121 | |
| 6122 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6123 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6124 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6125 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6126 | } |
| 6127 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6128 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6129 | struct extent_buffer *leaf, |
| 6130 | struct btrfs_dev_item *dev_item) |
| 6131 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6132 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6133 | struct btrfs_device *device; |
| 6134 | u64 devid; |
| 6135 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6136 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6137 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6138 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6139 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6140 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6141 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6142 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6143 | BTRFS_UUID_SIZE); |
| 6144 | |
| 6145 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6146 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6147 | if (IS_ERR(fs_devices)) |
| 6148 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6149 | } |
| 6150 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6151 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6152 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6153 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6154 | return -EIO; |
| 6155 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6156 | btrfs_warn(root->fs_info, "devid %llu missing", devid); |
| 6157 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6158 | if (!device) |
| 6159 | return -ENOMEM; |
| 6160 | } else { |
| 6161 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6162 | return -EIO; |
| 6163 | |
| 6164 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6165 | /* |
| 6166 | * this happens when a device that was properly setup |
| 6167 | * in the device info lists suddenly goes bad. |
| 6168 | * device->bdev is NULL, and so we have to set |
| 6169 | * device->missing to one here |
| 6170 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6171 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6172 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6173 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6174 | |
| 6175 | /* Move the device to its own fs_devices */ |
| 6176 | if (device->fs_devices != fs_devices) { |
| 6177 | ASSERT(device->missing); |
| 6178 | |
| 6179 | list_move(&device->dev_list, &fs_devices->devices); |
| 6180 | device->fs_devices->num_devices--; |
| 6181 | fs_devices->num_devices++; |
| 6182 | |
| 6183 | device->fs_devices->missing_devices--; |
| 6184 | fs_devices->missing_devices++; |
| 6185 | |
| 6186 | device->fs_devices = fs_devices; |
| 6187 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6188 | } |
| 6189 | |
| 6190 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6191 | BUG_ON(device->writeable); |
| 6192 | if (device->generation != |
| 6193 | btrfs_device_generation(leaf, dev_item)) |
| 6194 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6195 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6196 | |
| 6197 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6198 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6199 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6200 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6201 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6202 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6203 | device->bytes_used; |
| 6204 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6205 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6206 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6207 | return ret; |
| 6208 | } |
| 6209 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6210 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6211 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6212 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6213 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6214 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6215 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6216 | u8 *ptr; |
| 6217 | unsigned long sb_ptr; |
| 6218 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6219 | u32 num_stripes; |
| 6220 | u32 array_size; |
| 6221 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6222 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6223 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6224 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6225 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6226 | BTRFS_SUPER_INFO_SIZE); |
| 6227 | if (!sb) |
| 6228 | return -ENOMEM; |
| 6229 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6230 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6231 | /* |
| 6232 | * The sb extent buffer is artifical and just used to read the system array. |
| 6233 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6234 | * pages up-to-date when the page is larger: extent does not cover the |
| 6235 | * whole page and consequently check_page_uptodate does not find all |
| 6236 | * the page's extents up-to-date (the hole beyond sb), |
| 6237 | * write_extent_buffer then triggers a WARN_ON. |
| 6238 | * |
| 6239 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6240 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6241 | * to silence the warning eg. on PowerPC 64. |
| 6242 | */ |
| 6243 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6244 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6245 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6246 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6247 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6248 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6249 | ptr = super_copy->sys_chunk_array; |
| 6250 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6251 | cur = 0; |
| 6252 | |
| 6253 | while (cur < array_size) { |
| 6254 | disk_key = (struct btrfs_disk_key *)ptr; |
| 6255 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6256 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6257 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6258 | sb_ptr += len; |
| 6259 | cur += len; |
| 6260 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6261 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6262 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6263 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6264 | if (ret) |
| 6265 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6266 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6267 | len = btrfs_chunk_item_size(num_stripes); |
| 6268 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6269 | ret = -EIO; |
| 6270 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6271 | } |
| 6272 | ptr += len; |
| 6273 | sb_ptr += len; |
| 6274 | cur += len; |
| 6275 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6276 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6277 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6278 | } |
| 6279 | |
| 6280 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6281 | { |
| 6282 | struct btrfs_path *path; |
| 6283 | struct extent_buffer *leaf; |
| 6284 | struct btrfs_key key; |
| 6285 | struct btrfs_key found_key; |
| 6286 | int ret; |
| 6287 | int slot; |
| 6288 | |
| 6289 | root = root->fs_info->chunk_root; |
| 6290 | |
| 6291 | path = btrfs_alloc_path(); |
| 6292 | if (!path) |
| 6293 | return -ENOMEM; |
| 6294 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6295 | mutex_lock(&uuid_mutex); |
| 6296 | lock_chunks(root); |
| 6297 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6298 | /* |
| 6299 | * Read all device items, and then all the chunk items. All |
| 6300 | * device items are found before any chunk item (their object id |
| 6301 | * is smaller than the lowest possible object id for a chunk |
| 6302 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6303 | */ |
| 6304 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6305 | key.offset = 0; |
| 6306 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6307 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6308 | if (ret < 0) |
| 6309 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6310 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6311 | leaf = path->nodes[0]; |
| 6312 | slot = path->slots[0]; |
| 6313 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6314 | ret = btrfs_next_leaf(root, path); |
| 6315 | if (ret == 0) |
| 6316 | continue; |
| 6317 | if (ret < 0) |
| 6318 | goto error; |
| 6319 | break; |
| 6320 | } |
| 6321 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6322 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6323 | struct btrfs_dev_item *dev_item; |
| 6324 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6325 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6326 | ret = read_one_dev(root, leaf, dev_item); |
| 6327 | if (ret) |
| 6328 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6329 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6330 | struct btrfs_chunk *chunk; |
| 6331 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6332 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6333 | if (ret) |
| 6334 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6335 | } |
| 6336 | path->slots[0]++; |
| 6337 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6338 | ret = 0; |
| 6339 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6340 | unlock_chunks(root); |
| 6341 | mutex_unlock(&uuid_mutex); |
| 6342 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6343 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6344 | return ret; |
| 6345 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6346 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6347 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6348 | { |
| 6349 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6350 | struct btrfs_device *device; |
| 6351 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6352 | while (fs_devices) { |
| 6353 | mutex_lock(&fs_devices->device_list_mutex); |
| 6354 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6355 | device->dev_root = fs_info->dev_root; |
| 6356 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6357 | |
| 6358 | fs_devices = fs_devices->seed; |
| 6359 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6360 | } |
| 6361 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6362 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6363 | { |
| 6364 | int i; |
| 6365 | |
| 6366 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6367 | btrfs_dev_stat_reset(dev, i); |
| 6368 | } |
| 6369 | |
| 6370 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6371 | { |
| 6372 | struct btrfs_key key; |
| 6373 | struct btrfs_key found_key; |
| 6374 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6375 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6376 | struct extent_buffer *eb; |
| 6377 | int slot; |
| 6378 | int ret = 0; |
| 6379 | struct btrfs_device *device; |
| 6380 | struct btrfs_path *path = NULL; |
| 6381 | int i; |
| 6382 | |
| 6383 | path = btrfs_alloc_path(); |
| 6384 | if (!path) { |
| 6385 | ret = -ENOMEM; |
| 6386 | goto out; |
| 6387 | } |
| 6388 | |
| 6389 | mutex_lock(&fs_devices->device_list_mutex); |
| 6390 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6391 | int item_size; |
| 6392 | struct btrfs_dev_stats_item *ptr; |
| 6393 | |
| 6394 | key.objectid = 0; |
| 6395 | key.type = BTRFS_DEV_STATS_KEY; |
| 6396 | key.offset = device->devid; |
| 6397 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6398 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6399 | __btrfs_reset_dev_stats(device); |
| 6400 | device->dev_stats_valid = 1; |
| 6401 | btrfs_release_path(path); |
| 6402 | continue; |
| 6403 | } |
| 6404 | slot = path->slots[0]; |
| 6405 | eb = path->nodes[0]; |
| 6406 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6407 | item_size = btrfs_item_size_nr(eb, slot); |
| 6408 | |
| 6409 | ptr = btrfs_item_ptr(eb, slot, |
| 6410 | struct btrfs_dev_stats_item); |
| 6411 | |
| 6412 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6413 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6414 | btrfs_dev_stat_set(device, i, |
| 6415 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6416 | else |
| 6417 | btrfs_dev_stat_reset(device, i); |
| 6418 | } |
| 6419 | |
| 6420 | device->dev_stats_valid = 1; |
| 6421 | btrfs_dev_stat_print_on_load(device); |
| 6422 | btrfs_release_path(path); |
| 6423 | } |
| 6424 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6425 | |
| 6426 | out: |
| 6427 | btrfs_free_path(path); |
| 6428 | return ret < 0 ? ret : 0; |
| 6429 | } |
| 6430 | |
| 6431 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6432 | struct btrfs_root *dev_root, |
| 6433 | struct btrfs_device *device) |
| 6434 | { |
| 6435 | struct btrfs_path *path; |
| 6436 | struct btrfs_key key; |
| 6437 | struct extent_buffer *eb; |
| 6438 | struct btrfs_dev_stats_item *ptr; |
| 6439 | int ret; |
| 6440 | int i; |
| 6441 | |
| 6442 | key.objectid = 0; |
| 6443 | key.type = BTRFS_DEV_STATS_KEY; |
| 6444 | key.offset = device->devid; |
| 6445 | |
| 6446 | path = btrfs_alloc_path(); |
| 6447 | BUG_ON(!path); |
| 6448 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6449 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6450 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6451 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6452 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6453 | goto out; |
| 6454 | } |
| 6455 | |
| 6456 | if (ret == 0 && |
| 6457 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6458 | /* need to delete old one and insert a new one */ |
| 6459 | ret = btrfs_del_item(trans, dev_root, path); |
| 6460 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6461 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6462 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6463 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6464 | goto out; |
| 6465 | } |
| 6466 | ret = 1; |
| 6467 | } |
| 6468 | |
| 6469 | if (ret == 1) { |
| 6470 | /* need to insert a new item */ |
| 6471 | btrfs_release_path(path); |
| 6472 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6473 | &key, sizeof(*ptr)); |
| 6474 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6475 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6476 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6477 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6478 | goto out; |
| 6479 | } |
| 6480 | } |
| 6481 | |
| 6482 | eb = path->nodes[0]; |
| 6483 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6484 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6485 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6486 | btrfs_dev_stat_read(device, i)); |
| 6487 | btrfs_mark_buffer_dirty(eb); |
| 6488 | |
| 6489 | out: |
| 6490 | btrfs_free_path(path); |
| 6491 | return ret; |
| 6492 | } |
| 6493 | |
| 6494 | /* |
| 6495 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6496 | */ |
| 6497 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6498 | struct btrfs_fs_info *fs_info) |
| 6499 | { |
| 6500 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6501 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6502 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6503 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6504 | int ret = 0; |
| 6505 | |
| 6506 | mutex_lock(&fs_devices->device_list_mutex); |
| 6507 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6508 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6509 | continue; |
| 6510 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6511 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6512 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6513 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6514 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6515 | } |
| 6516 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6517 | |
| 6518 | return ret; |
| 6519 | } |
| 6520 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6521 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6522 | { |
| 6523 | btrfs_dev_stat_inc(dev, index); |
| 6524 | btrfs_dev_stat_print_on_error(dev); |
| 6525 | } |
| 6526 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6527 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6528 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6529 | if (!dev->dev_stats_valid) |
| 6530 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6531 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6532 | "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] | 6533 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6534 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6535 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6536 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6537 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6538 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6539 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6540 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6541 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6542 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6543 | int i; |
| 6544 | |
| 6545 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6546 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6547 | break; |
| 6548 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6549 | return; /* all values == 0, suppress message */ |
| 6550 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6551 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6552 | "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] | 6553 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6554 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6555 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6556 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6557 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6558 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6559 | } |
| 6560 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6561 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6562 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6563 | { |
| 6564 | struct btrfs_device *dev; |
| 6565 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6566 | int i; |
| 6567 | |
| 6568 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6569 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6570 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6571 | |
| 6572 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6573 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6574 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6575 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6576 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6577 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6578 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6579 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6580 | if (stats->nr_items > i) |
| 6581 | stats->values[i] = |
| 6582 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6583 | else |
| 6584 | btrfs_dev_stat_reset(dev, i); |
| 6585 | } |
| 6586 | } else { |
| 6587 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6588 | if (stats->nr_items > i) |
| 6589 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6590 | } |
| 6591 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6592 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6593 | return 0; |
| 6594 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6595 | |
| 6596 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6597 | { |
| 6598 | struct buffer_head *bh; |
| 6599 | struct btrfs_super_block *disk_super; |
| 6600 | |
| 6601 | bh = btrfs_read_dev_super(device->bdev); |
| 6602 | if (!bh) |
| 6603 | return -EINVAL; |
| 6604 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6605 | |
| 6606 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6607 | set_buffer_dirty(bh); |
| 6608 | sync_dirty_buffer(bh); |
| 6609 | brelse(bh); |
| 6610 | |
| 6611 | return 0; |
| 6612 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6613 | |
| 6614 | /* |
| 6615 | * Update the size of all devices, which is used for writing out the |
| 6616 | * super blocks. |
| 6617 | */ |
| 6618 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6619 | { |
| 6620 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6621 | struct btrfs_device *curr, *next; |
| 6622 | |
| 6623 | if (list_empty(&fs_devices->resized_devices)) |
| 6624 | return; |
| 6625 | |
| 6626 | mutex_lock(&fs_devices->device_list_mutex); |
| 6627 | lock_chunks(fs_info->dev_root); |
| 6628 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6629 | resized_list) { |
| 6630 | list_del_init(&curr->resized_list); |
| 6631 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6632 | } |
| 6633 | unlock_chunks(fs_info->dev_root); |
| 6634 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6635 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6636 | |
| 6637 | /* Must be invoked during the transaction commit */ |
| 6638 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6639 | struct btrfs_transaction *transaction) |
| 6640 | { |
| 6641 | struct extent_map *em; |
| 6642 | struct map_lookup *map; |
| 6643 | struct btrfs_device *dev; |
| 6644 | int i; |
| 6645 | |
| 6646 | if (list_empty(&transaction->pending_chunks)) |
| 6647 | return; |
| 6648 | |
| 6649 | /* In order to kick the device replace finish process */ |
| 6650 | lock_chunks(root); |
| 6651 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6652 | map = (struct map_lookup *)em->bdev; |
| 6653 | |
| 6654 | for (i = 0; i < map->num_stripes; i++) { |
| 6655 | dev = map->stripes[i].dev; |
| 6656 | dev->commit_bytes_used = dev->bytes_used; |
| 6657 | } |
| 6658 | } |
| 6659 | unlock_chunks(root); |
| 6660 | } |