Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/bio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 21 | #include <linux/buffer_head.h> |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 23 | #include <linux/random.h> |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 24 | #include <linux/iocontext.h> |
Ben Hutchings | 6f88a44 | 2010-12-29 14:55:03 +0000 | [diff] [blame] | 25 | #include <linux/capability.h> |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 26 | #include <linux/ratelimit.h> |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 27 | #include <linux/kthread.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 28 | #include <linux/raid/pq.h> |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 29 | #include <linux/semaphore.h> |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 30 | #include <asm/div64.h> |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | #include "ctree.h" |
| 32 | #include "extent_map.h" |
| 33 | #include "disk-io.h" |
| 34 | #include "transaction.h" |
| 35 | #include "print-tree.h" |
| 36 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 37 | #include "raid56.h" |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 38 | #include "async-thread.h" |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 39 | #include "check-integrity.h" |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 40 | #include "rcu-string.h" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 41 | #include "math.h" |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 42 | #include "dev-replace.h" |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 43 | #include "sysfs.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 44 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 45 | static int init_first_rw_device(struct btrfs_trans_handle *trans, |
| 46 | struct btrfs_root *root, |
| 47 | struct btrfs_device *device); |
| 48 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 49 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 50 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 51 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 52 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 53 | DEFINE_MUTEX(uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 54 | static LIST_HEAD(fs_uuids); |
| 55 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 56 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 57 | { |
| 58 | struct btrfs_fs_devices *fs_devs; |
| 59 | |
| 60 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 61 | if (!fs_devs) |
| 62 | return ERR_PTR(-ENOMEM); |
| 63 | |
| 64 | mutex_init(&fs_devs->device_list_mutex); |
| 65 | |
| 66 | INIT_LIST_HEAD(&fs_devs->devices); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 67 | INIT_LIST_HEAD(&fs_devs->resized_devices); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 68 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 69 | INIT_LIST_HEAD(&fs_devs->list); |
| 70 | |
| 71 | return fs_devs; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 76 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 77 | * generated. |
| 78 | * |
| 79 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 80 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 81 | * can be destroyed with kfree() right away. |
| 82 | */ |
| 83 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 84 | { |
| 85 | struct btrfs_fs_devices *fs_devs; |
| 86 | |
| 87 | fs_devs = __alloc_fs_devices(); |
| 88 | if (IS_ERR(fs_devs)) |
| 89 | return fs_devs; |
| 90 | |
| 91 | if (fsid) |
| 92 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 93 | else |
| 94 | generate_random_uuid(fs_devs->fsid); |
| 95 | |
| 96 | return fs_devs; |
| 97 | } |
| 98 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 99 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 100 | { |
| 101 | struct btrfs_device *device; |
| 102 | WARN_ON(fs_devices->opened); |
| 103 | while (!list_empty(&fs_devices->devices)) { |
| 104 | device = list_entry(fs_devices->devices.next, |
| 105 | struct btrfs_device, dev_list); |
| 106 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 107 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 108 | kfree(device); |
| 109 | } |
| 110 | kfree(fs_devices); |
| 111 | } |
| 112 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 113 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 114 | enum kobject_action action) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 119 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 120 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 121 | action, |
| 122 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 123 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 124 | } |
| 125 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 126 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 127 | { |
| 128 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 129 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 130 | while (!list_empty(&fs_uuids)) { |
| 131 | fs_devices = list_entry(fs_uuids.next, |
| 132 | struct btrfs_fs_devices, list); |
| 133 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 134 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 135 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 138 | static struct btrfs_device *__alloc_device(void) |
| 139 | { |
| 140 | struct btrfs_device *dev; |
| 141 | |
| 142 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 143 | if (!dev) |
| 144 | return ERR_PTR(-ENOMEM); |
| 145 | |
| 146 | INIT_LIST_HEAD(&dev->dev_list); |
| 147 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 148 | INIT_LIST_HEAD(&dev->resized_list); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 149 | |
| 150 | spin_lock_init(&dev->io_lock); |
| 151 | |
| 152 | spin_lock_init(&dev->reada_lock); |
| 153 | atomic_set(&dev->reada_in_flight, 0); |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 154 | atomic_set(&dev->dev_stats_ccnt, 0); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 155 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 156 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 157 | |
| 158 | return dev; |
| 159 | } |
| 160 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 161 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 162 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 163 | { |
| 164 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 165 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 166 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 167 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 168 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 169 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 170 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 171 | } |
| 172 | return NULL; |
| 173 | } |
| 174 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 175 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 176 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 177 | struct btrfs_fs_devices *fs_devices; |
| 178 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 179 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 180 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 181 | return fs_devices; |
| 182 | } |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 186 | static int |
| 187 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 188 | int flush, struct block_device **bdev, |
| 189 | struct buffer_head **bh) |
| 190 | { |
| 191 | int ret; |
| 192 | |
| 193 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 194 | |
| 195 | if (IS_ERR(*bdev)) { |
| 196 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 197 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 198 | goto error; |
| 199 | } |
| 200 | |
| 201 | if (flush) |
| 202 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 203 | ret = set_blocksize(*bdev, 4096); |
| 204 | if (ret) { |
| 205 | blkdev_put(*bdev, flags); |
| 206 | goto error; |
| 207 | } |
| 208 | invalidate_bdev(*bdev); |
| 209 | *bh = btrfs_read_dev_super(*bdev); |
| 210 | if (!*bh) { |
| 211 | ret = -EINVAL; |
| 212 | blkdev_put(*bdev, flags); |
| 213 | goto error; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | |
| 218 | error: |
| 219 | *bdev = NULL; |
| 220 | *bh = NULL; |
| 221 | return ret; |
| 222 | } |
| 223 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 224 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 225 | struct bio *head, struct bio *tail) |
| 226 | { |
| 227 | |
| 228 | struct bio *old_head; |
| 229 | |
| 230 | old_head = pending_bios->head; |
| 231 | pending_bios->head = head; |
| 232 | if (pending_bios->tail) |
| 233 | tail->bi_next = old_head; |
| 234 | else |
| 235 | pending_bios->tail = tail; |
| 236 | } |
| 237 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 238 | /* |
| 239 | * we try to collect pending bios for a device so we don't get a large |
| 240 | * number of procs sending bios down to the same device. This greatly |
| 241 | * improves the schedulers ability to collect and merge the bios. |
| 242 | * |
| 243 | * But, it also turns into a long list of bios to process and that is sure |
| 244 | * to eventually make the worker thread block. The solution here is to |
| 245 | * make some progress and then put this work struct back at the end of |
| 246 | * the list if the block device is congested. This way, multiple devices |
| 247 | * can make progress from a single worker thread. |
| 248 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 249 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 250 | { |
| 251 | struct bio *pending; |
| 252 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 253 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 254 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 255 | struct bio *tail; |
| 256 | struct bio *cur; |
| 257 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 258 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 259 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 260 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 261 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 262 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 263 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 264 | struct blk_plug plug; |
| 265 | |
| 266 | /* |
| 267 | * this function runs all the bios we've collected for |
| 268 | * a particular device. We don't want to wander off to |
| 269 | * another device without first sending all of these down. |
| 270 | * So, setup a plug here and finish it off before we return |
| 271 | */ |
| 272 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 273 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 274 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 275 | fs_info = device->dev_root->fs_info; |
| 276 | limit = btrfs_async_submit_limit(fs_info); |
| 277 | limit = limit * 2 / 3; |
| 278 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 279 | loop: |
| 280 | spin_lock(&device->io_lock); |
| 281 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 282 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 283 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 284 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 285 | /* take all the bios off the list at once and process them |
| 286 | * later on (without the lock held). But, remember the |
| 287 | * tail and other pointers so the bios can be properly reinserted |
| 288 | * into the list if we hit congestion |
| 289 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 290 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 291 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 292 | force_reg = 1; |
| 293 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 294 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 295 | force_reg = 0; |
| 296 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 297 | |
| 298 | pending = pending_bios->head; |
| 299 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 300 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * if pending was null this time around, no bios need processing |
| 304 | * at all and we can stop. Otherwise it'll loop back up again |
| 305 | * and do an additional check so no bios are missed. |
| 306 | * |
| 307 | * device->running_pending is used to synchronize with the |
| 308 | * schedule_bio code. |
| 309 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 310 | if (device->pending_sync_bios.head == NULL && |
| 311 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 312 | again = 0; |
| 313 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 314 | } else { |
| 315 | again = 1; |
| 316 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 317 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 318 | |
| 319 | pending_bios->head = NULL; |
| 320 | pending_bios->tail = NULL; |
| 321 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 322 | spin_unlock(&device->io_lock); |
| 323 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 324 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 325 | |
| 326 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 327 | /* we want to work on both lists, but do more bios on the |
| 328 | * sync list than the regular list |
| 329 | */ |
| 330 | if ((num_run > 32 && |
| 331 | pending_bios != &device->pending_sync_bios && |
| 332 | device->pending_sync_bios.head) || |
| 333 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 334 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 335 | spin_lock(&device->io_lock); |
| 336 | requeue_list(pending_bios, pending, tail); |
| 337 | goto loop_lock; |
| 338 | } |
| 339 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 340 | cur = pending; |
| 341 | pending = pending->bi_next; |
| 342 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 343 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 344 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 345 | waitqueue_active(&fs_info->async_submit_wait)) |
| 346 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 347 | |
| 348 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 349 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 350 | /* |
| 351 | * if we're doing the sync list, record that our |
| 352 | * plug has some sync requests on it |
| 353 | * |
| 354 | * If we're doing the regular list and there are |
| 355 | * sync requests sitting around, unplug before |
| 356 | * we add more |
| 357 | */ |
| 358 | if (pending_bios == &device->pending_sync_bios) { |
| 359 | sync_pending = 1; |
| 360 | } else if (sync_pending) { |
| 361 | blk_finish_plug(&plug); |
| 362 | blk_start_plug(&plug); |
| 363 | sync_pending = 0; |
| 364 | } |
| 365 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 366 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 367 | num_run++; |
| 368 | batch_run++; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 369 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 370 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * we made progress, there is more work to do and the bdi |
| 374 | * is now congested. Back off and let other work structs |
| 375 | * run instead |
| 376 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 377 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 378 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 379 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 380 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 381 | ioc = current->io_context; |
| 382 | |
| 383 | /* |
| 384 | * the main goal here is that we don't want to |
| 385 | * block if we're going to be able to submit |
| 386 | * more requests without blocking. |
| 387 | * |
| 388 | * This code does two great things, it pokes into |
| 389 | * the elevator code from a filesystem _and_ |
| 390 | * it makes assumptions about how batching works. |
| 391 | */ |
| 392 | if (ioc && ioc->nr_batch_requests > 0 && |
| 393 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 394 | (last_waited == 0 || |
| 395 | ioc->last_waited == last_waited)) { |
| 396 | /* |
| 397 | * we want to go through our batch of |
| 398 | * requests and stop. So, we copy out |
| 399 | * the ioc->last_waited time and test |
| 400 | * against it before looping |
| 401 | */ |
| 402 | last_waited = ioc->last_waited; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 403 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 404 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 405 | continue; |
| 406 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 407 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 408 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 409 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 410 | |
| 411 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 412 | btrfs_queue_work(fs_info->submit_workers, |
| 413 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 414 | goto done; |
| 415 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 416 | /* unplug every 64 requests just for good measure */ |
| 417 | if (batch_run % 64 == 0) { |
| 418 | blk_finish_plug(&plug); |
| 419 | blk_start_plug(&plug); |
| 420 | sync_pending = 0; |
| 421 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 422 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 423 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 424 | cond_resched(); |
| 425 | if (again) |
| 426 | goto loop; |
| 427 | |
| 428 | spin_lock(&device->io_lock); |
| 429 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 430 | goto loop_lock; |
| 431 | spin_unlock(&device->io_lock); |
| 432 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 433 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 434 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 435 | } |
| 436 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 437 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 438 | { |
| 439 | struct btrfs_device *device; |
| 440 | |
| 441 | device = container_of(work, struct btrfs_device, work); |
| 442 | run_scheduled_bios(device); |
| 443 | } |
| 444 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 445 | /* |
| 446 | * Add new device to list of registered devices |
| 447 | * |
| 448 | * Returns: |
| 449 | * 1 - first time device is seen |
| 450 | * 0 - device already known |
| 451 | * < 0 - error |
| 452 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 453 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 454 | struct btrfs_super_block *disk_super, |
| 455 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 456 | { |
| 457 | struct btrfs_device *device; |
| 458 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 459 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 460 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 461 | u64 found_transid = btrfs_super_generation(disk_super); |
| 462 | |
| 463 | fs_devices = find_fsid(disk_super->fsid); |
| 464 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 465 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 466 | if (IS_ERR(fs_devices)) |
| 467 | return PTR_ERR(fs_devices); |
| 468 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 469 | list_add(&fs_devices->list, &fs_uuids); |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 470 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 471 | device = NULL; |
| 472 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 473 | device = __find_device(&fs_devices->devices, devid, |
| 474 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 475 | } |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 476 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 477 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 478 | if (fs_devices->opened) |
| 479 | return -EBUSY; |
| 480 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 481 | device = btrfs_alloc_device(NULL, &devid, |
| 482 | disk_super->dev_item.uuid); |
| 483 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 484 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 485 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 486 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 487 | |
| 488 | name = rcu_string_strdup(path, GFP_NOFS); |
| 489 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 490 | kfree(device); |
| 491 | return -ENOMEM; |
| 492 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 493 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 494 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 495 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 496 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 497 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 498 | mutex_unlock(&fs_devices->device_list_mutex); |
| 499 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 500 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 501 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 502 | } else if (!device->name || strcmp(device->name->str, path)) { |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 503 | /* |
| 504 | * When FS is already mounted. |
| 505 | * 1. If you are here and if the device->name is NULL that |
| 506 | * means this device was missing at time of FS mount. |
| 507 | * 2. If you are here and if the device->name is different |
| 508 | * from 'path' that means either |
| 509 | * a. The same device disappeared and reappeared with |
| 510 | * different name. or |
| 511 | * b. The missing-disk-which-was-replaced, has |
| 512 | * reappeared now. |
| 513 | * |
| 514 | * We must allow 1 and 2a above. But 2b would be a spurious |
| 515 | * and unintentional. |
| 516 | * |
| 517 | * Further in case of 1 and 2a above, the disk at 'path' |
| 518 | * would have missed some transaction when it was away and |
| 519 | * in case of 2a the stale bdev has to be updated as well. |
| 520 | * 2b must not be allowed at all time. |
| 521 | */ |
| 522 | |
| 523 | /* |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 524 | * For now, we do allow update to btrfs_fs_device through the |
| 525 | * btrfs dev scan cli after FS has been mounted. We're still |
| 526 | * tracking a problem where systems fail mount by subvolume id |
| 527 | * when we reject replacement on a mounted FS. |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 528 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 529 | if (!fs_devices->opened && found_transid < device->generation) { |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 530 | /* |
| 531 | * That is if the FS is _not_ mounted and if you |
| 532 | * are here, that means there is more than one |
| 533 | * disk with same uuid and devid.We keep the one |
| 534 | * with larger generation number or the last-in if |
| 535 | * generation are equal. |
| 536 | */ |
Chris Mason | 0f23ae7 | 2014-09-18 07:49:05 -0700 | [diff] [blame] | 537 | return -EEXIST; |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 538 | } |
Anand Jain | b96de00 | 2014-07-03 18:22:05 +0800 | [diff] [blame] | 539 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 540 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 541 | if (!name) |
| 542 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 543 | rcu_string_free(device->name); |
| 544 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 545 | if (device->missing) { |
| 546 | fs_devices->missing_devices--; |
| 547 | device->missing = 0; |
| 548 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Anand Jain | 77bdae4 | 2014-07-03 18:22:06 +0800 | [diff] [blame] | 551 | /* |
| 552 | * Unmount does not free the btrfs_device struct but would zero |
| 553 | * generation along with most of the other members. So just update |
| 554 | * it back. We need it to pick the disk with largest generation |
| 555 | * (as above). |
| 556 | */ |
| 557 | if (!fs_devices->opened) |
| 558 | device->generation = found_transid; |
| 559 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 560 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 561 | |
| 562 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 563 | } |
| 564 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 565 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 566 | { |
| 567 | struct btrfs_fs_devices *fs_devices; |
| 568 | struct btrfs_device *device; |
| 569 | struct btrfs_device *orig_dev; |
| 570 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 571 | fs_devices = alloc_fs_devices(orig->fsid); |
| 572 | if (IS_ERR(fs_devices)) |
| 573 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 574 | |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 575 | mutex_lock(&orig->device_list_mutex); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 576 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 577 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 578 | /* 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] | 579 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 580 | struct rcu_string *name; |
| 581 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 582 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 583 | orig_dev->uuid); |
| 584 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 585 | goto error; |
| 586 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 587 | /* |
| 588 | * This is ok to do without rcu read locked because we hold the |
| 589 | * uuid mutex so nothing we touch in here is going to disappear. |
| 590 | */ |
Anand Jain | e755f78 | 2014-06-30 17:12:47 +0800 | [diff] [blame] | 591 | if (orig_dev->name) { |
| 592 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 593 | if (!name) { |
| 594 | kfree(device); |
| 595 | goto error; |
| 596 | } |
| 597 | rcu_assign_pointer(device->name, name); |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 598 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 599 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 600 | list_add(&device->dev_list, &fs_devices->devices); |
| 601 | device->fs_devices = fs_devices; |
| 602 | fs_devices->num_devices++; |
| 603 | } |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 604 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 605 | return fs_devices; |
| 606 | error: |
Miao Xie | adbbb86 | 2014-09-03 21:35:42 +0800 | [diff] [blame] | 607 | mutex_unlock(&orig->device_list_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 608 | free_fs_devices(fs_devices); |
| 609 | return ERR_PTR(-ENOMEM); |
| 610 | } |
| 611 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 612 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 613 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 614 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 615 | struct btrfs_device *device, *next; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 616 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 617 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 618 | mutex_lock(&uuid_mutex); |
| 619 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 620 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 621 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 622 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 623 | if (!device->is_tgtdev_for_dev_replace && |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 624 | (!latest_dev || |
| 625 | device->generation > latest_dev->generation)) { |
| 626 | latest_dev = device; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 627 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 628 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 629 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 630 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 631 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 632 | /* |
| 633 | * In the first step, keep the device which has |
| 634 | * the correct fsid and the devid that is used |
| 635 | * for the dev_replace procedure. |
| 636 | * In the second step, the dev_replace state is |
| 637 | * read from the device tree and it is known |
| 638 | * whether the procedure is really active or |
| 639 | * not, which means whether this device is |
| 640 | * used or whether it should be removed. |
| 641 | */ |
| 642 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 643 | continue; |
| 644 | } |
| 645 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 646 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 647 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 648 | device->bdev = NULL; |
| 649 | fs_devices->open_devices--; |
| 650 | } |
| 651 | if (device->writeable) { |
| 652 | list_del_init(&device->dev_alloc_list); |
| 653 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 654 | if (!device->is_tgtdev_for_dev_replace) |
| 655 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 656 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 657 | list_del_init(&device->dev_list); |
| 658 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 659 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 660 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 661 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 662 | |
| 663 | if (fs_devices->seed) { |
| 664 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 665 | goto again; |
| 666 | } |
| 667 | |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 668 | fs_devices->latest_bdev = latest_dev->bdev; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 669 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 670 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 671 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 672 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 673 | static void __free_device(struct work_struct *work) |
| 674 | { |
| 675 | struct btrfs_device *device; |
| 676 | |
| 677 | device = container_of(work, struct btrfs_device, rcu_work); |
| 678 | |
| 679 | if (device->bdev) |
| 680 | blkdev_put(device->bdev, device->mode); |
| 681 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 682 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 683 | kfree(device); |
| 684 | } |
| 685 | |
| 686 | static void free_device(struct rcu_head *head) |
| 687 | { |
| 688 | struct btrfs_device *device; |
| 689 | |
| 690 | device = container_of(head, struct btrfs_device, rcu); |
| 691 | |
| 692 | INIT_WORK(&device->rcu_work, __free_device); |
| 693 | schedule_work(&device->rcu_work); |
| 694 | } |
| 695 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 696 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 697 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 698 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 699 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 700 | if (--fs_devices->opened > 0) |
| 701 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 702 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 703 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 704 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 705 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 706 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 707 | |
| 708 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 709 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 710 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 711 | if (device->writeable && |
| 712 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 713 | list_del_init(&device->dev_alloc_list); |
| 714 | fs_devices->rw_devices--; |
| 715 | } |
| 716 | |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 717 | if (device->missing) |
| 718 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 719 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 720 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 721 | device->uuid); |
| 722 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 723 | |
| 724 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 725 | if (device->name) { |
| 726 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 727 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 728 | rcu_assign_pointer(new_device->name, name); |
| 729 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 730 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 731 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 732 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 733 | |
| 734 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 735 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 736 | mutex_unlock(&fs_devices->device_list_mutex); |
| 737 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 738 | WARN_ON(fs_devices->open_devices); |
| 739 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 740 | fs_devices->opened = 0; |
| 741 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 742 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 746 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 747 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 748 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 749 | int ret; |
| 750 | |
| 751 | mutex_lock(&uuid_mutex); |
| 752 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 753 | if (!fs_devices->opened) { |
| 754 | seed_devices = fs_devices->seed; |
| 755 | fs_devices->seed = NULL; |
| 756 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 757 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 758 | |
| 759 | while (seed_devices) { |
| 760 | fs_devices = seed_devices; |
| 761 | seed_devices = fs_devices->seed; |
| 762 | __btrfs_close_devices(fs_devices); |
| 763 | free_fs_devices(fs_devices); |
| 764 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 765 | /* |
| 766 | * Wait for rcu kworkers under __btrfs_close_devices |
| 767 | * to finish all blkdev_puts so device is really |
| 768 | * free when umount is done. |
| 769 | */ |
| 770 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 771 | return ret; |
| 772 | } |
| 773 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 774 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 775 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 776 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 777 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 778 | struct block_device *bdev; |
| 779 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 780 | struct btrfs_device *device; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 781 | struct btrfs_device *latest_dev = NULL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 782 | struct buffer_head *bh; |
| 783 | struct btrfs_super_block *disk_super; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 784 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 785 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 786 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 787 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 788 | flags |= FMODE_EXCL; |
| 789 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 790 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 791 | if (device->bdev) |
| 792 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 793 | if (!device->name) |
| 794 | continue; |
| 795 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 796 | /* Just open everything we can; ignore failures here */ |
| 797 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 798 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 799 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 800 | |
| 801 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 802 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 803 | if (devid != device->devid) |
| 804 | goto error_brelse; |
| 805 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 806 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 807 | BTRFS_UUID_SIZE)) |
| 808 | goto error_brelse; |
| 809 | |
| 810 | device->generation = btrfs_super_generation(disk_super); |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 811 | if (!latest_dev || |
| 812 | device->generation > latest_dev->generation) |
| 813 | latest_dev = device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 814 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 815 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 816 | device->writeable = 0; |
| 817 | } else { |
| 818 | device->writeable = !bdev_read_only(bdev); |
| 819 | seeding = 0; |
| 820 | } |
| 821 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 822 | q = bdev_get_queue(bdev); |
Miao Xie | 90180da | 2014-09-03 21:35:30 +0800 | [diff] [blame] | 823 | if (blk_queue_discard(q)) |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 824 | device->can_discard = 1; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 825 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 826 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 827 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 828 | device->mode = flags; |
| 829 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 830 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 831 | fs_devices->rotating = 1; |
| 832 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 833 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 834 | if (device->writeable && |
| 835 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 836 | fs_devices->rw_devices++; |
| 837 | list_add(&device->dev_alloc_list, |
| 838 | &fs_devices->alloc_list); |
| 839 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 840 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 841 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 842 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 843 | error_brelse: |
| 844 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 845 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 846 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 847 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 848 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 849 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 850 | goto out; |
| 851 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 852 | fs_devices->seeding = seeding; |
| 853 | fs_devices->opened = 1; |
Miao Xie | 443f24f | 2014-07-24 11:37:15 +0800 | [diff] [blame] | 854 | fs_devices->latest_bdev = latest_dev->bdev; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 855 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 856 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 861 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 862 | { |
| 863 | int ret; |
| 864 | |
| 865 | mutex_lock(&uuid_mutex); |
| 866 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 867 | fs_devices->opened++; |
| 868 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 869 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 870 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 871 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 872 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 873 | return ret; |
| 874 | } |
| 875 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 876 | /* |
| 877 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 878 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 879 | * is read via pagecache |
| 880 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 881 | 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] | 882 | struct btrfs_fs_devices **fs_devices_ret) |
| 883 | { |
| 884 | struct btrfs_super_block *disk_super; |
| 885 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 886 | struct page *page; |
| 887 | void *p; |
| 888 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 889 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 890 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 891 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 892 | u64 bytenr; |
| 893 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 894 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 895 | /* |
| 896 | * we would like to check all the supers, but that would make |
| 897 | * a btrfs mount succeed after a mkfs from a different FS. |
| 898 | * So, we need to add a special mount option to scan for |
| 899 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 900 | */ |
| 901 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 902 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 903 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 904 | |
| 905 | bdev = blkdev_get_by_path(path, flags, holder); |
| 906 | |
| 907 | if (IS_ERR(bdev)) { |
| 908 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 909 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* make sure our super fits in the device */ |
| 913 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 914 | goto error_bdev_put; |
| 915 | |
| 916 | /* make sure our super fits in the page */ |
| 917 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 918 | goto error_bdev_put; |
| 919 | |
| 920 | /* make sure our super doesn't straddle pages on disk */ |
| 921 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 922 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 923 | goto error_bdev_put; |
| 924 | |
| 925 | /* pull in the page with our super */ |
| 926 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 927 | index, GFP_NOFS); |
| 928 | |
| 929 | if (IS_ERR_OR_NULL(page)) |
| 930 | goto error_bdev_put; |
| 931 | |
| 932 | p = kmap(page); |
| 933 | |
| 934 | /* align our pointer to the offset of the super block */ |
| 935 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 936 | |
| 937 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 938 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 939 | goto error_unmap; |
| 940 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 941 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 942 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 943 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 944 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 945 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 946 | if (ret > 0) { |
| 947 | if (disk_super->label[0]) { |
| 948 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 949 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 950 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 951 | } else { |
| 952 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 953 | } |
| 954 | |
| 955 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 956 | ret = 0; |
| 957 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 958 | if (!ret && fs_devices_ret) |
| 959 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 960 | |
| 961 | error_unmap: |
| 962 | kunmap(page); |
| 963 | page_cache_release(page); |
| 964 | |
| 965 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 966 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 967 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 968 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 969 | return ret; |
| 970 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 971 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 972 | /* helper to account the used device space in the range */ |
| 973 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 974 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 975 | { |
| 976 | struct btrfs_key key; |
| 977 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 978 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 979 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 980 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 981 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 982 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 983 | struct extent_buffer *l; |
| 984 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 985 | *length = 0; |
| 986 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 987 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 988 | return 0; |
| 989 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 990 | path = btrfs_alloc_path(); |
| 991 | if (!path) |
| 992 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 993 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 994 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 995 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 996 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 997 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 998 | |
| 999 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1000 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1001 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1002 | if (ret > 0) { |
| 1003 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1004 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1005 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 1006 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1007 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1008 | while (1) { |
| 1009 | l = path->nodes[0]; |
| 1010 | slot = path->slots[0]; |
| 1011 | if (slot >= btrfs_header_nritems(l)) { |
| 1012 | ret = btrfs_next_leaf(root, path); |
| 1013 | if (ret == 0) |
| 1014 | continue; |
| 1015 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1016 | goto out; |
| 1017 | |
| 1018 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1019 | } |
| 1020 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1021 | |
| 1022 | if (key.objectid < device->devid) |
| 1023 | goto next; |
| 1024 | |
| 1025 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1026 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1027 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1028 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1029 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1030 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1031 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1032 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1033 | dev_extent); |
| 1034 | if (key.offset <= start && extent_end > end) { |
| 1035 | *length = end - start + 1; |
| 1036 | break; |
| 1037 | } else if (key.offset <= start && extent_end > start) |
| 1038 | *length += extent_end - start; |
| 1039 | else if (key.offset > start && extent_end <= end) |
| 1040 | *length += extent_end - key.offset; |
| 1041 | else if (key.offset > start && key.offset <= end) { |
| 1042 | *length += end - key.offset + 1; |
| 1043 | break; |
| 1044 | } else if (key.offset > end) |
| 1045 | break; |
| 1046 | |
| 1047 | next: |
| 1048 | path->slots[0]++; |
| 1049 | } |
| 1050 | ret = 0; |
| 1051 | out: |
| 1052 | btrfs_free_path(path); |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1056 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1057 | struct btrfs_device *device, |
| 1058 | u64 *start, u64 len) |
| 1059 | { |
| 1060 | struct extent_map *em; |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1061 | struct list_head *search_list = &trans->transaction->pending_chunks; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1062 | int ret = 0; |
| 1063 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1064 | again: |
| 1065 | list_for_each_entry(em, search_list, list) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1066 | struct map_lookup *map; |
| 1067 | int i; |
| 1068 | |
| 1069 | map = (struct map_lookup *)em->bdev; |
| 1070 | for (i = 0; i < map->num_stripes; i++) { |
| 1071 | if (map->stripes[i].dev != device) |
| 1072 | continue; |
| 1073 | if (map->stripes[i].physical >= *start + len || |
| 1074 | map->stripes[i].physical + em->orig_block_len <= |
| 1075 | *start) |
| 1076 | continue; |
| 1077 | *start = map->stripes[i].physical + |
| 1078 | em->orig_block_len; |
| 1079 | ret = 1; |
| 1080 | } |
| 1081 | } |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 1082 | if (search_list == &trans->transaction->pending_chunks) { |
| 1083 | search_list = &trans->root->fs_info->pinned_chunks; |
| 1084 | goto again; |
| 1085 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1086 | |
| 1087 | return ret; |
| 1088 | } |
| 1089 | |
| 1090 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1091 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1092 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1093 | * @device: the device which we search the free space in |
| 1094 | * @num_bytes: the size of the free space that we need |
| 1095 | * @start: store the start of the free space. |
| 1096 | * @len: the size of the free space. that we find, or the size of the max |
| 1097 | * free space if we don't find suitable free space |
| 1098 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1099 | * this uses a pretty simple search, the expectation is that it is |
| 1100 | * called very infrequently and that a given device has a small number |
| 1101 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1102 | * |
| 1103 | * @start is used to store the start of the free space if we find. But if we |
| 1104 | * don't find suitable free space, it will be used to store the start position |
| 1105 | * of the max free space. |
| 1106 | * |
| 1107 | * @len is used to store the size of the free space that we find. |
| 1108 | * But if we don't find suitable free space, it is used to store the size of |
| 1109 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1110 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1111 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1112 | struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1113 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1114 | { |
| 1115 | struct btrfs_key key; |
| 1116 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1117 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1118 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1119 | u64 hole_size; |
| 1120 | u64 max_hole_start; |
| 1121 | u64 max_hole_size; |
| 1122 | u64 extent_end; |
| 1123 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1124 | u64 search_end = device->total_bytes; |
| 1125 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1126 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1127 | struct extent_buffer *l; |
| 1128 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1129 | /* FIXME use last free of some kind */ |
| 1130 | |
| 1131 | /* we don't want to overwrite the superblock on the drive, |
| 1132 | * so we make sure to start at an offset of at least 1MB |
| 1133 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1134 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1135 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1136 | path = btrfs_alloc_path(); |
| 1137 | if (!path) |
| 1138 | return -ENOMEM; |
| 1139 | again: |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1140 | max_hole_start = search_start; |
| 1141 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1142 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1143 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1144 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1145 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1146 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1149 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1150 | path->search_commit_root = 1; |
| 1151 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1152 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1153 | key.objectid = device->devid; |
| 1154 | key.offset = search_start; |
| 1155 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1156 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1157 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1158 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1159 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1160 | if (ret > 0) { |
| 1161 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1162 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1163 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1164 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1165 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1166 | while (1) { |
| 1167 | l = path->nodes[0]; |
| 1168 | slot = path->slots[0]; |
| 1169 | if (slot >= btrfs_header_nritems(l)) { |
| 1170 | ret = btrfs_next_leaf(root, path); |
| 1171 | if (ret == 0) |
| 1172 | continue; |
| 1173 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1174 | goto out; |
| 1175 | |
| 1176 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1177 | } |
| 1178 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1179 | |
| 1180 | if (key.objectid < device->devid) |
| 1181 | goto next; |
| 1182 | |
| 1183 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1184 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1185 | |
David Sterba | 962a298 | 2014-06-04 18:41:45 +0200 | [diff] [blame] | 1186 | if (key.type != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1187 | goto next; |
| 1188 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1189 | if (key.offset > search_start) { |
| 1190 | hole_size = key.offset - search_start; |
| 1191 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1192 | /* |
| 1193 | * Have to check before we set max_hole_start, otherwise |
| 1194 | * we could end up sending back this offset anyway. |
| 1195 | */ |
| 1196 | if (contains_pending_extent(trans, device, |
| 1197 | &search_start, |
| 1198 | hole_size)) |
| 1199 | hole_size = 0; |
| 1200 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1201 | if (hole_size > max_hole_size) { |
| 1202 | max_hole_start = search_start; |
| 1203 | max_hole_size = hole_size; |
| 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * If this free space is greater than which we need, |
| 1208 | * it must be the max free space that we have found |
| 1209 | * until now, so max_hole_start must point to the start |
| 1210 | * of this free space and the length of this free space |
| 1211 | * is stored in max_hole_size. Thus, we return |
| 1212 | * max_hole_start and max_hole_size and go back to the |
| 1213 | * caller. |
| 1214 | */ |
| 1215 | if (hole_size >= num_bytes) { |
| 1216 | ret = 0; |
| 1217 | goto out; |
| 1218 | } |
| 1219 | } |
| 1220 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1221 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1222 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1223 | dev_extent); |
| 1224 | if (extent_end > search_start) |
| 1225 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1226 | next: |
| 1227 | path->slots[0]++; |
| 1228 | cond_resched(); |
| 1229 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1230 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1231 | /* |
| 1232 | * At this point, search_start should be the end of |
| 1233 | * allocated dev extents, and when shrinking the device, |
| 1234 | * search_end may be smaller than search_start. |
| 1235 | */ |
| 1236 | if (search_end > search_start) |
| 1237 | hole_size = search_end - search_start; |
| 1238 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1239 | if (hole_size > max_hole_size) { |
| 1240 | max_hole_start = search_start; |
| 1241 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1242 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1243 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1244 | if (contains_pending_extent(trans, device, &search_start, hole_size)) { |
| 1245 | btrfs_release_path(path); |
| 1246 | goto again; |
| 1247 | } |
| 1248 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1249 | /* See above. */ |
| 1250 | if (hole_size < num_bytes) |
| 1251 | ret = -ENOSPC; |
| 1252 | else |
| 1253 | ret = 0; |
| 1254 | |
| 1255 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1256 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1257 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1258 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1259 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1260 | return ret; |
| 1261 | } |
| 1262 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1263 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1264 | struct btrfs_device *device, |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1265 | u64 start, u64 *dev_extent_len) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1266 | { |
| 1267 | int ret; |
| 1268 | struct btrfs_path *path; |
| 1269 | struct btrfs_root *root = device->dev_root; |
| 1270 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1271 | struct btrfs_key found_key; |
| 1272 | struct extent_buffer *leaf = NULL; |
| 1273 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1274 | |
| 1275 | path = btrfs_alloc_path(); |
| 1276 | if (!path) |
| 1277 | return -ENOMEM; |
| 1278 | |
| 1279 | key.objectid = device->devid; |
| 1280 | key.offset = start; |
| 1281 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1282 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1283 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1284 | if (ret > 0) { |
| 1285 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1286 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1287 | if (ret) |
| 1288 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1289 | leaf = path->nodes[0]; |
| 1290 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1291 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1292 | struct btrfs_dev_extent); |
| 1293 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1294 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1295 | key = found_key; |
| 1296 | btrfs_release_path(path); |
| 1297 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1298 | } else if (ret == 0) { |
| 1299 | leaf = path->nodes[0]; |
| 1300 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1301 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1302 | } else { |
| 1303 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1304 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1305 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1306 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1307 | *dev_extent_len = btrfs_dev_extent_length(leaf, extent); |
| 1308 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1309 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1310 | if (ret) { |
| 1311 | btrfs_error(root->fs_info, ret, |
| 1312 | "Failed to remove dev extent item"); |
| 1313 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1314 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1315 | btrfs_free_path(path); |
| 1316 | return ret; |
| 1317 | } |
| 1318 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1319 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1320 | struct btrfs_device *device, |
| 1321 | u64 chunk_tree, u64 chunk_objectid, |
| 1322 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1323 | { |
| 1324 | int ret; |
| 1325 | struct btrfs_path *path; |
| 1326 | struct btrfs_root *root = device->dev_root; |
| 1327 | struct btrfs_dev_extent *extent; |
| 1328 | struct extent_buffer *leaf; |
| 1329 | struct btrfs_key key; |
| 1330 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1331 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1332 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1333 | path = btrfs_alloc_path(); |
| 1334 | if (!path) |
| 1335 | return -ENOMEM; |
| 1336 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1337 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1338 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1339 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1340 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1341 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1342 | if (ret) |
| 1343 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1344 | |
| 1345 | leaf = path->nodes[0]; |
| 1346 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1347 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1348 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1349 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1350 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1351 | |
| 1352 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1353 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1354 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1355 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1356 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1357 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1358 | btrfs_free_path(path); |
| 1359 | return ret; |
| 1360 | } |
| 1361 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1362 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1363 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1364 | struct extent_map_tree *em_tree; |
| 1365 | struct extent_map *em; |
| 1366 | struct rb_node *n; |
| 1367 | u64 ret = 0; |
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 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1370 | read_lock(&em_tree->lock); |
| 1371 | n = rb_last(&em_tree->map); |
| 1372 | if (n) { |
| 1373 | em = rb_entry(n, struct extent_map, rb_node); |
| 1374 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1375 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1376 | read_unlock(&em_tree->lock); |
| 1377 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1378 | return ret; |
| 1379 | } |
| 1380 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1381 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1382 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1383 | { |
| 1384 | int ret; |
| 1385 | struct btrfs_key key; |
| 1386 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1387 | struct btrfs_path *path; |
| 1388 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1389 | path = btrfs_alloc_path(); |
| 1390 | if (!path) |
| 1391 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1392 | |
| 1393 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1394 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1395 | key.offset = (u64)-1; |
| 1396 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1397 | 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] | 1398 | if (ret < 0) |
| 1399 | goto error; |
| 1400 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1401 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1402 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1403 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1404 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1405 | BTRFS_DEV_ITEM_KEY); |
| 1406 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1407 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1408 | } else { |
| 1409 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1410 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1411 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1412 | } |
| 1413 | ret = 0; |
| 1414 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1415 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1416 | return ret; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
| 1420 | * the device information is stored in the chunk root |
| 1421 | * the btrfs_device struct should be fully filled in |
| 1422 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1423 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1424 | struct btrfs_root *root, |
| 1425 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1426 | { |
| 1427 | int ret; |
| 1428 | struct btrfs_path *path; |
| 1429 | struct btrfs_dev_item *dev_item; |
| 1430 | struct extent_buffer *leaf; |
| 1431 | struct btrfs_key key; |
| 1432 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1433 | |
| 1434 | root = root->fs_info->chunk_root; |
| 1435 | |
| 1436 | path = btrfs_alloc_path(); |
| 1437 | if (!path) |
| 1438 | return -ENOMEM; |
| 1439 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1440 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1441 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1442 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1443 | |
| 1444 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1445 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1446 | if (ret) |
| 1447 | goto out; |
| 1448 | |
| 1449 | leaf = path->nodes[0]; |
| 1450 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1451 | |
| 1452 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1453 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1454 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1455 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1456 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1457 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 1458 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 1459 | btrfs_device_get_disk_total_bytes(device)); |
| 1460 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 1461 | btrfs_device_get_bytes_used(device)); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1462 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1463 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1464 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1465 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1466 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1467 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1468 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1469 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1470 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1471 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1472 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1473 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1474 | out: |
| 1475 | btrfs_free_path(path); |
| 1476 | return ret; |
| 1477 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1478 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1479 | /* |
| 1480 | * Function to update ctime/mtime for a given device path. |
| 1481 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1482 | */ |
| 1483 | static void update_dev_time(char *path_name) |
| 1484 | { |
| 1485 | struct file *filp; |
| 1486 | |
| 1487 | filp = filp_open(path_name, O_RDWR, 0); |
Al Viro | 98af592 | 2014-12-14 02:59:17 -0500 | [diff] [blame] | 1488 | if (IS_ERR(filp)) |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1489 | return; |
| 1490 | file_update_time(filp); |
| 1491 | filp_close(filp, NULL); |
| 1492 | return; |
| 1493 | } |
| 1494 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1495 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1496 | struct btrfs_device *device) |
| 1497 | { |
| 1498 | int ret; |
| 1499 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1500 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1501 | struct btrfs_trans_handle *trans; |
| 1502 | |
| 1503 | root = root->fs_info->chunk_root; |
| 1504 | |
| 1505 | path = btrfs_alloc_path(); |
| 1506 | if (!path) |
| 1507 | return -ENOMEM; |
| 1508 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1509 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1510 | if (IS_ERR(trans)) { |
| 1511 | btrfs_free_path(path); |
| 1512 | return PTR_ERR(trans); |
| 1513 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1514 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1515 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1516 | key.offset = device->devid; |
| 1517 | |
| 1518 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1519 | if (ret < 0) |
| 1520 | goto out; |
| 1521 | |
| 1522 | if (ret > 0) { |
| 1523 | ret = -ENOENT; |
| 1524 | goto out; |
| 1525 | } |
| 1526 | |
| 1527 | ret = btrfs_del_item(trans, root, path); |
| 1528 | if (ret) |
| 1529 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1530 | out: |
| 1531 | btrfs_free_path(path); |
| 1532 | btrfs_commit_transaction(trans, root); |
| 1533 | return ret; |
| 1534 | } |
| 1535 | |
| 1536 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1537 | { |
| 1538 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1539 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1540 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1541 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1542 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1543 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1544 | u64 all_avail; |
| 1545 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1546 | u64 num_devices; |
| 1547 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1548 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1549 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1550 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1551 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1552 | mutex_lock(&uuid_mutex); |
| 1553 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1554 | do { |
| 1555 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1556 | |
| 1557 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1558 | root->fs_info->avail_system_alloc_bits | |
| 1559 | root->fs_info->avail_metadata_alloc_bits; |
| 1560 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1561 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1562 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1563 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1564 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1565 | WARN_ON(num_devices < 1); |
| 1566 | num_devices--; |
| 1567 | } |
| 1568 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1569 | |
| 1570 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1571 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1572 | goto out; |
| 1573 | } |
| 1574 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1575 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1576 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1577 | goto out; |
| 1578 | } |
| 1579 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1580 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1581 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1582 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1583 | goto out; |
| 1584 | } |
| 1585 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1586 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1587 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1588 | goto out; |
| 1589 | } |
| 1590 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1591 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1592 | struct list_head *devices; |
| 1593 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1594 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1595 | device = NULL; |
| 1596 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1597 | /* |
| 1598 | * It is safe to read the devices since the volume_mutex |
| 1599 | * is held. |
| 1600 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1601 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1602 | if (tmp->in_fs_metadata && |
| 1603 | !tmp->is_tgtdev_for_dev_replace && |
| 1604 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1605 | device = tmp; |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | bdev = NULL; |
| 1610 | bh = NULL; |
| 1611 | disk_super = NULL; |
| 1612 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1613 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1614 | goto out; |
| 1615 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1616 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1617 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1618 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1619 | root->fs_info->bdev_holder, 0, |
| 1620 | &bdev, &bh); |
| 1621 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1622 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1623 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1624 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1625 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1626 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1627 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1628 | if (!device) { |
| 1629 | ret = -ENOENT; |
| 1630 | goto error_brelse; |
| 1631 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1632 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1633 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1634 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1635 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1636 | goto error_brelse; |
| 1637 | } |
| 1638 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1639 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1640 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1641 | goto error_brelse; |
| 1642 | } |
| 1643 | |
| 1644 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1645 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1646 | list_del_init(&device->dev_alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1647 | device->fs_devices->rw_devices--; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1648 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1649 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1650 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1651 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1652 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1653 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1654 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1655 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1656 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1657 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1658 | /* |
| 1659 | * TODO: the superblock still includes this device in its num_devices |
| 1660 | * counter although write_all_supers() is not locked out. This |
| 1661 | * could give a filesystem state which requires a degraded mount. |
| 1662 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1663 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1664 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1665 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1666 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1667 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1668 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1669 | |
| 1670 | /* |
| 1671 | * the device list mutex makes sure that we don't change |
| 1672 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1673 | * the device supers. Whoever is writing all supers, should |
| 1674 | * lock the device list mutex before getting the number of |
| 1675 | * devices in the super block (super_copy). Conversely, |
| 1676 | * whoever updates the number of devices in the super block |
| 1677 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1678 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1679 | |
| 1680 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1681 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1682 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1683 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1684 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1685 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1686 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1687 | if (device->missing) |
Miao Xie | 3a7d55c | 2014-07-16 18:38:01 +0800 | [diff] [blame] | 1688 | device->fs_devices->missing_devices--; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1689 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1690 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1691 | struct btrfs_device, dev_list); |
| 1692 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1693 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1694 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1695 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1696 | |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1697 | if (device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1698 | device->fs_devices->open_devices--; |
Eric Sandeen | 0bfaa9c | 2014-07-07 12:34:49 -0500 | [diff] [blame] | 1699 | /* remove sysfs entry */ |
| 1700 | btrfs_kobj_rm_device(root->fs_info, device); |
| 1701 | } |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame] | 1702 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1703 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1704 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1705 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1706 | 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] | 1707 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1708 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1709 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1710 | struct btrfs_fs_devices *fs_devices; |
| 1711 | fs_devices = root->fs_info->fs_devices; |
| 1712 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1713 | if (fs_devices->seed == cur_devices) { |
| 1714 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1715 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1716 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1717 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1718 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1719 | cur_devices->seed = NULL; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1720 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1721 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1722 | } |
| 1723 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1724 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1725 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1726 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1727 | /* |
| 1728 | * at this point, the device is zero sized. We want to |
| 1729 | * remove it from the devices list and zero out the old super |
| 1730 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1731 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1732 | u64 bytenr; |
| 1733 | int i; |
| 1734 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1735 | /* make sure this device isn't detected as part of |
| 1736 | * the FS anymore |
| 1737 | */ |
| 1738 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1739 | set_buffer_dirty(bh); |
| 1740 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1741 | |
| 1742 | /* clear the mirror copies of super block on the disk |
| 1743 | * being removed, 0th copy is been taken care above and |
| 1744 | * the below would take of the rest |
| 1745 | */ |
| 1746 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1747 | bytenr = btrfs_sb_offset(i); |
| 1748 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1749 | i_size_read(bdev->bd_inode)) |
| 1750 | break; |
| 1751 | |
| 1752 | brelse(bh); |
| 1753 | bh = __bread(bdev, bytenr / 4096, |
| 1754 | BTRFS_SUPER_INFO_SIZE); |
| 1755 | if (!bh) |
| 1756 | continue; |
| 1757 | |
| 1758 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1759 | |
| 1760 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1761 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1762 | continue; |
| 1763 | } |
| 1764 | memset(&disk_super->magic, 0, |
| 1765 | sizeof(disk_super->magic)); |
| 1766 | set_buffer_dirty(bh); |
| 1767 | sync_dirty_buffer(bh); |
| 1768 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1769 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1770 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1771 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1772 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1773 | if (bdev) { |
| 1774 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1775 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1776 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1777 | /* Update ctime/mtime for device path for libblkid */ |
| 1778 | update_dev_time(device_path); |
| 1779 | } |
| 1780 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1781 | error_brelse: |
| 1782 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1783 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1784 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1785 | out: |
| 1786 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1787 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1788 | error_undo: |
| 1789 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1790 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1791 | list_add(&device->dev_alloc_list, |
| 1792 | &root->fs_info->fs_devices->alloc_list); |
Miao Xie | c3929c3 | 2014-09-03 21:35:47 +0800 | [diff] [blame] | 1793 | device->fs_devices->rw_devices++; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1794 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1795 | } |
| 1796 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1797 | } |
| 1798 | |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1799 | void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info, |
| 1800 | struct btrfs_device *srcdev) |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1801 | { |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1802 | struct btrfs_fs_devices *fs_devices; |
| 1803 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1804 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1805 | |
Anand Jain | 25e8e91 | 2014-08-20 10:56:56 +0800 | [diff] [blame] | 1806 | /* |
| 1807 | * in case of fs with no seed, srcdev->fs_devices will point |
| 1808 | * to fs_devices of fs_info. However when the dev being replaced is |
| 1809 | * a seed dev it will point to the seed's local fs_devices. In short |
| 1810 | * srcdev will have its correct fs_devices in both the cases. |
| 1811 | */ |
| 1812 | fs_devices = srcdev->fs_devices; |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1813 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1814 | list_del_rcu(&srcdev->dev_list); |
| 1815 | list_del_rcu(&srcdev->dev_alloc_list); |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1816 | fs_devices->num_devices--; |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1817 | if (srcdev->missing) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1818 | fs_devices->missing_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1819 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1820 | if (srcdev->writeable) { |
| 1821 | fs_devices->rw_devices--; |
| 1822 | /* zero out the old super if it is writable */ |
| 1823 | btrfs_scratch_superblock(srcdev); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1824 | } |
| 1825 | |
Miao Xie | 82372bc | 2014-09-03 21:35:44 +0800 | [diff] [blame] | 1826 | if (srcdev->bdev) |
Anand Jain | d51908c | 2014-08-13 14:24:19 +0800 | [diff] [blame] | 1827 | fs_devices->open_devices--; |
Qu Wenruo | 084b6e7c | 2014-10-30 16:52:31 +0800 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info, |
| 1831 | struct btrfs_device *srcdev) |
| 1832 | { |
| 1833 | struct btrfs_fs_devices *fs_devices = srcdev->fs_devices; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1834 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1835 | call_rcu(&srcdev->rcu, free_device); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1836 | |
| 1837 | /* |
| 1838 | * unless fs_devices is seed fs, num_devices shouldn't go |
| 1839 | * zero |
| 1840 | */ |
| 1841 | BUG_ON(!fs_devices->num_devices && !fs_devices->seeding); |
| 1842 | |
| 1843 | /* if this is no devs we rather delete the fs_devices */ |
| 1844 | if (!fs_devices->num_devices) { |
| 1845 | struct btrfs_fs_devices *tmp_fs_devices; |
| 1846 | |
| 1847 | tmp_fs_devices = fs_info->fs_devices; |
| 1848 | while (tmp_fs_devices) { |
| 1849 | if (tmp_fs_devices->seed == fs_devices) { |
| 1850 | tmp_fs_devices->seed = fs_devices->seed; |
| 1851 | break; |
| 1852 | } |
| 1853 | tmp_fs_devices = tmp_fs_devices->seed; |
| 1854 | } |
| 1855 | fs_devices->seed = NULL; |
Anand Jain | 8bef840 | 2014-08-13 14:24:23 +0800 | [diff] [blame] | 1856 | __btrfs_close_devices(fs_devices); |
| 1857 | free_fs_devices(fs_devices); |
Anand Jain | 94d5f0c | 2014-08-13 14:24:22 +0800 | [diff] [blame] | 1858 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1859 | } |
| 1860 | |
| 1861 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1862 | struct btrfs_device *tgtdev) |
| 1863 | { |
| 1864 | struct btrfs_device *next_device; |
| 1865 | |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1866 | mutex_lock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1867 | WARN_ON(!tgtdev); |
| 1868 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1869 | if (tgtdev->bdev) { |
| 1870 | btrfs_scratch_superblock(tgtdev); |
| 1871 | fs_info->fs_devices->open_devices--; |
| 1872 | } |
| 1873 | fs_info->fs_devices->num_devices--; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1874 | |
| 1875 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1876 | struct btrfs_device, dev_list); |
| 1877 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1878 | fs_info->sb->s_bdev = next_device->bdev; |
| 1879 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1880 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1881 | list_del_rcu(&tgtdev->dev_list); |
| 1882 | |
| 1883 | call_rcu(&tgtdev->rcu, free_device); |
| 1884 | |
| 1885 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
Miao Xie | 67a2c45 | 2014-09-03 21:35:43 +0800 | [diff] [blame] | 1886 | mutex_unlock(&uuid_mutex); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1887 | } |
| 1888 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1889 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1890 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1891 | { |
| 1892 | int ret = 0; |
| 1893 | struct btrfs_super_block *disk_super; |
| 1894 | u64 devid; |
| 1895 | u8 *dev_uuid; |
| 1896 | struct block_device *bdev; |
| 1897 | struct buffer_head *bh; |
| 1898 | |
| 1899 | *device = NULL; |
| 1900 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1901 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1902 | if (ret) |
| 1903 | return ret; |
| 1904 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1905 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1906 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1907 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1908 | disk_super->fsid); |
| 1909 | brelse(bh); |
| 1910 | if (!*device) |
| 1911 | ret = -ENOENT; |
| 1912 | blkdev_put(bdev, FMODE_READ); |
| 1913 | return ret; |
| 1914 | } |
| 1915 | |
| 1916 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1917 | char *device_path, |
| 1918 | struct btrfs_device **device) |
| 1919 | { |
| 1920 | *device = NULL; |
| 1921 | if (strcmp(device_path, "missing") == 0) { |
| 1922 | struct list_head *devices; |
| 1923 | struct btrfs_device *tmp; |
| 1924 | |
| 1925 | devices = &root->fs_info->fs_devices->devices; |
| 1926 | /* |
| 1927 | * It is safe to read the devices since the volume_mutex |
| 1928 | * is held by the caller. |
| 1929 | */ |
| 1930 | list_for_each_entry(tmp, devices, dev_list) { |
| 1931 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1932 | *device = tmp; |
| 1933 | break; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1938 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1939 | return -ENOENT; |
| 1940 | } |
| 1941 | |
| 1942 | return 0; |
| 1943 | } else { |
| 1944 | return btrfs_find_device_by_path(root, device_path, device); |
| 1945 | } |
| 1946 | } |
| 1947 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1948 | /* |
| 1949 | * does all the dirty work required for changing file system's UUID. |
| 1950 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1951 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1952 | { |
| 1953 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1954 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1955 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1956 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1957 | struct btrfs_device *device; |
| 1958 | u64 super_flags; |
| 1959 | |
| 1960 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1961 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1962 | return -EINVAL; |
| 1963 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1964 | seed_devices = __alloc_fs_devices(); |
| 1965 | if (IS_ERR(seed_devices)) |
| 1966 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1967 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1968 | old_devices = clone_fs_devices(fs_devices); |
| 1969 | if (IS_ERR(old_devices)) { |
| 1970 | kfree(seed_devices); |
| 1971 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1972 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1973 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1974 | list_add(&old_devices->list, &fs_uuids); |
| 1975 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1976 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1977 | seed_devices->opened = 1; |
| 1978 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1979 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1980 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1981 | |
| 1982 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1983 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1984 | synchronize_rcu); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1985 | list_for_each_entry(device, &seed_devices->devices, dev_list) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1986 | device->fs_devices = seed_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 1987 | |
| 1988 | lock_chunks(root); |
| 1989 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1990 | unlock_chunks(root); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1991 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1992 | fs_devices->seeding = 0; |
| 1993 | fs_devices->num_devices = 0; |
| 1994 | fs_devices->open_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 1995 | fs_devices->missing_devices = 0; |
Miao Xie | 69611ac | 2014-07-03 18:22:12 +0800 | [diff] [blame] | 1996 | fs_devices->rotating = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1997 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1998 | |
| 1999 | generate_random_uuid(fs_devices->fsid); |
| 2000 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 2001 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 2002 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2003 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2004 | super_flags = btrfs_super_flags(disk_super) & |
| 2005 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 2006 | btrfs_set_super_flags(disk_super, super_flags); |
| 2007 | |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
| 2011 | /* |
| 2012 | * strore the expected generation for seed devices in device items. |
| 2013 | */ |
| 2014 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 2015 | struct btrfs_root *root) |
| 2016 | { |
| 2017 | struct btrfs_path *path; |
| 2018 | struct extent_buffer *leaf; |
| 2019 | struct btrfs_dev_item *dev_item; |
| 2020 | struct btrfs_device *device; |
| 2021 | struct btrfs_key key; |
| 2022 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 2023 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 2024 | u64 devid; |
| 2025 | int ret; |
| 2026 | |
| 2027 | path = btrfs_alloc_path(); |
| 2028 | if (!path) |
| 2029 | return -ENOMEM; |
| 2030 | |
| 2031 | root = root->fs_info->chunk_root; |
| 2032 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2033 | key.offset = 0; |
| 2034 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2035 | |
| 2036 | while (1) { |
| 2037 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2038 | if (ret < 0) |
| 2039 | goto error; |
| 2040 | |
| 2041 | leaf = path->nodes[0]; |
| 2042 | next_slot: |
| 2043 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2044 | ret = btrfs_next_leaf(root, path); |
| 2045 | if (ret > 0) |
| 2046 | break; |
| 2047 | if (ret < 0) |
| 2048 | goto error; |
| 2049 | leaf = path->nodes[0]; |
| 2050 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2051 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2052 | continue; |
| 2053 | } |
| 2054 | |
| 2055 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2056 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2057 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2058 | break; |
| 2059 | |
| 2060 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2061 | struct btrfs_dev_item); |
| 2062 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2063 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2064 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2065 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2066 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2067 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2068 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2069 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2070 | |
| 2071 | if (device->fs_devices->seeding) { |
| 2072 | btrfs_set_device_generation(leaf, dev_item, |
| 2073 | device->generation); |
| 2074 | btrfs_mark_buffer_dirty(leaf); |
| 2075 | } |
| 2076 | |
| 2077 | path->slots[0]++; |
| 2078 | goto next_slot; |
| 2079 | } |
| 2080 | ret = 0; |
| 2081 | error: |
| 2082 | btrfs_free_path(path); |
| 2083 | return ret; |
| 2084 | } |
| 2085 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2086 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2087 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2088 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2089 | struct btrfs_trans_handle *trans; |
| 2090 | struct btrfs_device *device; |
| 2091 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2092 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2093 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2094 | struct rcu_string *name; |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2095 | u64 tmp; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2096 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2097 | int ret = 0; |
| 2098 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2099 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2100 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2101 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2102 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2103 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2104 | if (IS_ERR(bdev)) |
| 2105 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2106 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2107 | if (root->fs_info->fs_devices->seeding) { |
| 2108 | seeding_dev = 1; |
| 2109 | down_write(&sb->s_umount); |
| 2110 | mutex_lock(&uuid_mutex); |
| 2111 | } |
| 2112 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2113 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2114 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2115 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2116 | |
| 2117 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2118 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2119 | if (device->bdev == bdev) { |
| 2120 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2121 | mutex_unlock( |
| 2122 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2123 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2124 | } |
| 2125 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2126 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2127 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2128 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2129 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2130 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2131 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2132 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2133 | } |
| 2134 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2135 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2136 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2137 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2138 | ret = -ENOMEM; |
| 2139 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2140 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2141 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2142 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2143 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2144 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2145 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2146 | kfree(device); |
| 2147 | ret = PTR_ERR(trans); |
| 2148 | goto error; |
| 2149 | } |
| 2150 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2151 | q = bdev_get_queue(bdev); |
| 2152 | if (blk_queue_discard(q)) |
| 2153 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2154 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2155 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2156 | device->io_width = root->sectorsize; |
| 2157 | device->io_align = root->sectorsize; |
| 2158 | device->sector_size = root->sectorsize; |
| 2159 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2160 | device->disk_total_bytes = device->total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2161 | device->commit_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2162 | device->dev_root = root->fs_info->dev_root; |
| 2163 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2164 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2165 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2166 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2167 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2168 | set_blocksize(device->bdev, 4096); |
| 2169 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2170 | if (seeding_dev) { |
| 2171 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2172 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2173 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2177 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2178 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2179 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2180 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2181 | list_add(&device->dev_alloc_list, |
| 2182 | &root->fs_info->fs_devices->alloc_list); |
| 2183 | root->fs_info->fs_devices->num_devices++; |
| 2184 | root->fs_info->fs_devices->open_devices++; |
| 2185 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2186 | root->fs_info->fs_devices->total_devices++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2187 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2188 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2189 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2190 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2191 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2192 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2193 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2194 | root->fs_info->fs_devices->rotating = 1; |
| 2195 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2196 | tmp = btrfs_super_total_bytes(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2197 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2198 | tmp + device->total_bytes); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2199 | |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2200 | tmp = btrfs_super_num_devices(root->fs_info->super_copy); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2201 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Anand Jain | 3c1dbdf | 2014-08-20 10:54:17 +0800 | [diff] [blame] | 2202 | tmp + 1); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2203 | |
| 2204 | /* add sysfs device entry */ |
| 2205 | btrfs_kobj_add_device(root->fs_info, device); |
| 2206 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2207 | /* |
| 2208 | * we've got more storage, clear any full flags on the space |
| 2209 | * infos |
| 2210 | */ |
| 2211 | btrfs_clear_space_info_full(root->fs_info); |
| 2212 | |
| 2213 | unlock_chunks(root); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2214 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2215 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2216 | if (seeding_dev) { |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2217 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2218 | ret = init_first_rw_device(trans, root, device); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2219 | unlock_chunks(root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2220 | if (ret) { |
| 2221 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2222 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2223 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | ret = btrfs_add_device(trans, root, device); |
| 2227 | if (ret) { |
| 2228 | btrfs_abort_transaction(trans, root, ret); |
| 2229 | goto error_trans; |
| 2230 | } |
| 2231 | |
| 2232 | if (seeding_dev) { |
| 2233 | char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; |
| 2234 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2235 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2236 | if (ret) { |
| 2237 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2238 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2239 | } |
Anand Jain | b2373f2 | 2014-06-03 11:36:03 +0800 | [diff] [blame] | 2240 | |
| 2241 | /* Sprouting would change fsid of the mounted root, |
| 2242 | * so rename the fsid on the sysfs |
| 2243 | */ |
| 2244 | snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", |
| 2245 | root->fs_info->fsid); |
| 2246 | if (kobject_rename(&root->fs_info->super_kobj, fsid_buf)) |
| 2247 | goto error_trans; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2248 | } |
| 2249 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2250 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2251 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2252 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2253 | |
| 2254 | if (seeding_dev) { |
| 2255 | mutex_unlock(&uuid_mutex); |
| 2256 | up_write(&sb->s_umount); |
| 2257 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2258 | if (ret) /* transaction commit */ |
| 2259 | return ret; |
| 2260 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2261 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2262 | if (ret < 0) |
| 2263 | btrfs_error(root->fs_info, ret, |
| 2264 | "Failed to relocate sys chunks after " |
| 2265 | "device initialization. This can be fixed " |
| 2266 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2267 | trans = btrfs_attach_transaction(root); |
| 2268 | if (IS_ERR(trans)) { |
| 2269 | if (PTR_ERR(trans) == -ENOENT) |
| 2270 | return 0; |
| 2271 | return PTR_ERR(trans); |
| 2272 | } |
| 2273 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2274 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2275 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2276 | /* Update ctime/mtime for libblkid */ |
| 2277 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2278 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2279 | |
| 2280 | error_trans: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2281 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2282 | rcu_string_free(device->name); |
Anand Jain | 0d39376 | 2014-06-03 11:36:01 +0800 | [diff] [blame] | 2283 | btrfs_kobj_rm_device(root->fs_info, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2284 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2285 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2286 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2287 | if (seeding_dev) { |
| 2288 | mutex_unlock(&uuid_mutex); |
| 2289 | up_write(&sb->s_umount); |
| 2290 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2291 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2292 | } |
| 2293 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2294 | 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] | 2295 | struct btrfs_device *srcdev, |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2296 | struct btrfs_device **device_out) |
| 2297 | { |
| 2298 | struct request_queue *q; |
| 2299 | struct btrfs_device *device; |
| 2300 | struct block_device *bdev; |
| 2301 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2302 | struct list_head *devices; |
| 2303 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2304 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2305 | int ret = 0; |
| 2306 | |
| 2307 | *device_out = NULL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2308 | if (fs_info->fs_devices->seeding) { |
| 2309 | btrfs_err(fs_info, "the filesystem is a seed filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2310 | return -EINVAL; |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2311 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2312 | |
| 2313 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2314 | fs_info->bdev_holder); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2315 | if (IS_ERR(bdev)) { |
| 2316 | btrfs_err(fs_info, "target device %s is invalid!", device_path); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2317 | return PTR_ERR(bdev); |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2318 | } |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2319 | |
| 2320 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2321 | |
| 2322 | devices = &fs_info->fs_devices->devices; |
| 2323 | list_for_each_entry(device, devices, dev_list) { |
| 2324 | if (device->bdev == bdev) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2325 | btrfs_err(fs_info, "target device is in the filesystem!"); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2326 | ret = -EEXIST; |
| 2327 | goto error; |
| 2328 | } |
| 2329 | } |
| 2330 | |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2331 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2332 | if (i_size_read(bdev->bd_inode) < |
| 2333 | btrfs_device_get_total_bytes(srcdev)) { |
Miao Xie | 1c43366 | 2014-09-03 21:35:32 +0800 | [diff] [blame] | 2334 | btrfs_err(fs_info, "target device is smaller than source device!"); |
| 2335 | ret = -EINVAL; |
| 2336 | goto error; |
| 2337 | } |
| 2338 | |
| 2339 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2340 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2341 | if (IS_ERR(device)) { |
| 2342 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2343 | goto error; |
| 2344 | } |
| 2345 | |
| 2346 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2347 | if (!name) { |
| 2348 | kfree(device); |
| 2349 | ret = -ENOMEM; |
| 2350 | goto error; |
| 2351 | } |
| 2352 | rcu_assign_pointer(device->name, name); |
| 2353 | |
| 2354 | q = bdev_get_queue(bdev); |
| 2355 | if (blk_queue_discard(q)) |
| 2356 | device->can_discard = 1; |
| 2357 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2358 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2359 | device->generation = 0; |
| 2360 | device->io_width = root->sectorsize; |
| 2361 | device->io_align = root->sectorsize; |
| 2362 | device->sector_size = root->sectorsize; |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2363 | device->total_bytes = btrfs_device_get_total_bytes(srcdev); |
| 2364 | device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); |
| 2365 | device->bytes_used = btrfs_device_get_bytes_used(srcdev); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2366 | ASSERT(list_empty(&srcdev->resized_list)); |
| 2367 | device->commit_total_bytes = srcdev->commit_total_bytes; |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 2368 | device->commit_bytes_used = device->bytes_used; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2369 | device->dev_root = fs_info->dev_root; |
| 2370 | device->bdev = bdev; |
| 2371 | device->in_fs_metadata = 1; |
| 2372 | device->is_tgtdev_for_dev_replace = 1; |
| 2373 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2374 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2375 | set_blocksize(device->bdev, 4096); |
| 2376 | device->fs_devices = fs_info->fs_devices; |
| 2377 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2378 | fs_info->fs_devices->num_devices++; |
| 2379 | fs_info->fs_devices->open_devices++; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2380 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2381 | |
| 2382 | *device_out = device; |
| 2383 | return ret; |
| 2384 | |
| 2385 | error: |
| 2386 | blkdev_put(bdev, FMODE_EXCL); |
| 2387 | return ret; |
| 2388 | } |
| 2389 | |
| 2390 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2391 | struct btrfs_device *tgtdev) |
| 2392 | { |
| 2393 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2394 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2395 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2396 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2397 | tgtdev->dev_root = fs_info->dev_root; |
| 2398 | tgtdev->in_fs_metadata = 1; |
| 2399 | } |
| 2400 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2401 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2402 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2403 | { |
| 2404 | int ret; |
| 2405 | struct btrfs_path *path; |
| 2406 | struct btrfs_root *root; |
| 2407 | struct btrfs_dev_item *dev_item; |
| 2408 | struct extent_buffer *leaf; |
| 2409 | struct btrfs_key key; |
| 2410 | |
| 2411 | root = device->dev_root->fs_info->chunk_root; |
| 2412 | |
| 2413 | path = btrfs_alloc_path(); |
| 2414 | if (!path) |
| 2415 | return -ENOMEM; |
| 2416 | |
| 2417 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2418 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2419 | key.offset = device->devid; |
| 2420 | |
| 2421 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2422 | if (ret < 0) |
| 2423 | goto out; |
| 2424 | |
| 2425 | if (ret > 0) { |
| 2426 | ret = -ENOENT; |
| 2427 | goto out; |
| 2428 | } |
| 2429 | |
| 2430 | leaf = path->nodes[0]; |
| 2431 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2432 | |
| 2433 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2434 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2435 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2436 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2437 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2438 | btrfs_set_device_total_bytes(leaf, dev_item, |
| 2439 | btrfs_device_get_disk_total_bytes(device)); |
| 2440 | btrfs_set_device_bytes_used(leaf, dev_item, |
| 2441 | btrfs_device_get_bytes_used(device)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2442 | btrfs_mark_buffer_dirty(leaf); |
| 2443 | |
| 2444 | out: |
| 2445 | btrfs_free_path(path); |
| 2446 | return ret; |
| 2447 | } |
| 2448 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2449 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2450 | struct btrfs_device *device, u64 new_size) |
| 2451 | { |
| 2452 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2453 | device->dev_root->fs_info->super_copy; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2454 | struct btrfs_fs_devices *fs_devices; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2455 | u64 old_total; |
| 2456 | u64 diff; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2457 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2458 | if (!device->writeable) |
| 2459 | return -EACCES; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2460 | |
| 2461 | lock_chunks(device->dev_root); |
| 2462 | old_total = btrfs_super_total_bytes(super_copy); |
| 2463 | diff = new_size - device->total_bytes; |
| 2464 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2465 | if (new_size <= device->total_bytes || |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2466 | device->is_tgtdev_for_dev_replace) { |
| 2467 | unlock_chunks(device->dev_root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2468 | return -EINVAL; |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2469 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2470 | |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2471 | fs_devices = device->dev_root->fs_info->fs_devices; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2472 | |
| 2473 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2474 | device->fs_devices->total_rw_bytes += diff; |
| 2475 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 2476 | btrfs_device_set_total_bytes(device, new_size); |
| 2477 | btrfs_device_set_disk_total_bytes(device, new_size); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2478 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 2479 | if (list_empty(&device->resized_list)) |
| 2480 | list_add_tail(&device->resized_list, |
| 2481 | &fs_devices->resized_devices); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2482 | unlock_chunks(device->dev_root); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2483 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2484 | return btrfs_update_device(trans, device); |
| 2485 | } |
| 2486 | |
| 2487 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2488 | struct btrfs_root *root, |
| 2489 | u64 chunk_tree, u64 chunk_objectid, |
| 2490 | u64 chunk_offset) |
| 2491 | { |
| 2492 | int ret; |
| 2493 | struct btrfs_path *path; |
| 2494 | struct btrfs_key key; |
| 2495 | |
| 2496 | root = root->fs_info->chunk_root; |
| 2497 | path = btrfs_alloc_path(); |
| 2498 | if (!path) |
| 2499 | return -ENOMEM; |
| 2500 | |
| 2501 | key.objectid = chunk_objectid; |
| 2502 | key.offset = chunk_offset; |
| 2503 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2504 | |
| 2505 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2506 | if (ret < 0) |
| 2507 | goto out; |
| 2508 | else if (ret > 0) { /* Logic error or corruption */ |
| 2509 | btrfs_error(root->fs_info, -ENOENT, |
| 2510 | "Failed lookup while freeing chunk."); |
| 2511 | ret = -ENOENT; |
| 2512 | goto out; |
| 2513 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2514 | |
| 2515 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2516 | if (ret < 0) |
| 2517 | btrfs_error(root->fs_info, ret, |
| 2518 | "Failed to delete chunk item."); |
| 2519 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2520 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2521 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2522 | } |
| 2523 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2524 | 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] | 2525 | chunk_offset) |
| 2526 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2527 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2528 | struct btrfs_disk_key *disk_key; |
| 2529 | struct btrfs_chunk *chunk; |
| 2530 | u8 *ptr; |
| 2531 | int ret = 0; |
| 2532 | u32 num_stripes; |
| 2533 | u32 array_size; |
| 2534 | u32 len = 0; |
| 2535 | u32 cur; |
| 2536 | struct btrfs_key key; |
| 2537 | |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2538 | lock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2539 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2540 | |
| 2541 | ptr = super_copy->sys_chunk_array; |
| 2542 | cur = 0; |
| 2543 | |
| 2544 | while (cur < array_size) { |
| 2545 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2546 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2547 | |
| 2548 | len = sizeof(*disk_key); |
| 2549 | |
| 2550 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2551 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2552 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2553 | len += btrfs_chunk_item_size(num_stripes); |
| 2554 | } else { |
| 2555 | ret = -EIO; |
| 2556 | break; |
| 2557 | } |
| 2558 | if (key.objectid == chunk_objectid && |
| 2559 | key.offset == chunk_offset) { |
| 2560 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2561 | array_size -= len; |
| 2562 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2563 | } else { |
| 2564 | ptr += len; |
| 2565 | cur += len; |
| 2566 | } |
| 2567 | } |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 2568 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2569 | return ret; |
| 2570 | } |
| 2571 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2572 | int btrfs_remove_chunk(struct btrfs_trans_handle *trans, |
| 2573 | struct btrfs_root *root, u64 chunk_offset) |
| 2574 | { |
| 2575 | struct extent_map_tree *em_tree; |
| 2576 | struct extent_map *em; |
| 2577 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 2578 | struct map_lookup *map; |
| 2579 | u64 dev_extent_len = 0; |
| 2580 | u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2581 | u64 chunk_tree = root->fs_info->chunk_root->objectid; |
| 2582 | int i, ret = 0; |
| 2583 | |
| 2584 | /* Just in case */ |
| 2585 | root = root->fs_info->chunk_root; |
| 2586 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2587 | |
| 2588 | read_lock(&em_tree->lock); |
| 2589 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
| 2590 | read_unlock(&em_tree->lock); |
| 2591 | |
| 2592 | if (!em || em->start > chunk_offset || |
| 2593 | em->start + em->len < chunk_offset) { |
| 2594 | /* |
| 2595 | * This is a logic error, but we don't want to just rely on the |
| 2596 | * user having built with ASSERT enabled, so if ASSERT doens't |
| 2597 | * do anything we still error out. |
| 2598 | */ |
| 2599 | ASSERT(0); |
| 2600 | if (em) |
| 2601 | free_extent_map(em); |
| 2602 | return -EINVAL; |
| 2603 | } |
| 2604 | map = (struct map_lookup *)em->bdev; |
| 2605 | |
| 2606 | for (i = 0; i < map->num_stripes; i++) { |
| 2607 | struct btrfs_device *device = map->stripes[i].dev; |
| 2608 | ret = btrfs_free_dev_extent(trans, device, |
| 2609 | map->stripes[i].physical, |
| 2610 | &dev_extent_len); |
| 2611 | if (ret) { |
| 2612 | btrfs_abort_transaction(trans, root, ret); |
| 2613 | goto out; |
| 2614 | } |
| 2615 | |
| 2616 | if (device->bytes_used > 0) { |
| 2617 | lock_chunks(root); |
| 2618 | btrfs_device_set_bytes_used(device, |
| 2619 | device->bytes_used - dev_extent_len); |
| 2620 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2621 | root->fs_info->free_chunk_space += dev_extent_len; |
| 2622 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2623 | btrfs_clear_space_info_full(root->fs_info); |
| 2624 | unlock_chunks(root); |
| 2625 | } |
| 2626 | |
| 2627 | if (map->stripes[i].dev) { |
| 2628 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2629 | if (ret) { |
| 2630 | btrfs_abort_transaction(trans, root, ret); |
| 2631 | goto out; |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2636 | chunk_offset); |
| 2637 | if (ret) { |
| 2638 | btrfs_abort_transaction(trans, root, ret); |
| 2639 | goto out; |
| 2640 | } |
| 2641 | |
| 2642 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2643 | |
| 2644 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2645 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2646 | if (ret) { |
| 2647 | btrfs_abort_transaction(trans, root, ret); |
| 2648 | goto out; |
| 2649 | } |
| 2650 | } |
| 2651 | |
Filipe Manana | 0421682 | 2014-11-27 21:14:15 +0000 | [diff] [blame] | 2652 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2653 | if (ret) { |
| 2654 | btrfs_abort_transaction(trans, extent_root, ret); |
| 2655 | goto out; |
| 2656 | } |
| 2657 | |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2658 | out: |
| 2659 | /* once for us */ |
| 2660 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2661 | return ret; |
| 2662 | } |
| 2663 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2664 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2665 | u64 chunk_tree, u64 chunk_objectid, |
| 2666 | u64 chunk_offset) |
| 2667 | { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2668 | struct btrfs_root *extent_root; |
| 2669 | struct btrfs_trans_handle *trans; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2670 | int ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2671 | |
| 2672 | root = root->fs_info->chunk_root; |
| 2673 | extent_root = root->fs_info->extent_root; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2674 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2675 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2676 | if (ret) |
| 2677 | return -ENOSPC; |
| 2678 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2679 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2680 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2681 | if (ret) |
| 2682 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2683 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2684 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2685 | if (IS_ERR(trans)) { |
| 2686 | ret = PTR_ERR(trans); |
| 2687 | btrfs_std_error(root->fs_info, ret); |
| 2688 | return ret; |
| 2689 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2690 | |
| 2691 | /* |
| 2692 | * step two, delete the device extents and the |
| 2693 | * chunk tree entries |
| 2694 | */ |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2695 | ret = btrfs_remove_chunk(trans, root, chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2696 | btrfs_end_transaction(trans, root); |
Josef Bacik | 47ab2a6 | 2014-09-18 11:20:02 -0400 | [diff] [blame] | 2697 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2698 | } |
| 2699 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2700 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2701 | { |
| 2702 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2703 | struct btrfs_path *path; |
| 2704 | struct extent_buffer *leaf; |
| 2705 | struct btrfs_chunk *chunk; |
| 2706 | struct btrfs_key key; |
| 2707 | struct btrfs_key found_key; |
| 2708 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2709 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2710 | bool retried = false; |
| 2711 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2712 | int ret; |
| 2713 | |
| 2714 | path = btrfs_alloc_path(); |
| 2715 | if (!path) |
| 2716 | return -ENOMEM; |
| 2717 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2718 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2719 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2720 | key.offset = (u64)-1; |
| 2721 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2722 | |
| 2723 | while (1) { |
| 2724 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2725 | if (ret < 0) |
| 2726 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2727 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2728 | |
| 2729 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2730 | key.type); |
| 2731 | if (ret < 0) |
| 2732 | goto error; |
| 2733 | if (ret > 0) |
| 2734 | break; |
| 2735 | |
| 2736 | leaf = path->nodes[0]; |
| 2737 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2738 | |
| 2739 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2740 | struct btrfs_chunk); |
| 2741 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2742 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2743 | |
| 2744 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2745 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2746 | found_key.objectid, |
| 2747 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2748 | if (ret == -ENOSPC) |
| 2749 | failed++; |
HIMANGI SARAOGI | 1458665 | 2014-07-09 03:51:41 +0530 | [diff] [blame] | 2750 | else |
| 2751 | BUG_ON(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | if (found_key.offset == 0) |
| 2755 | break; |
| 2756 | key.offset = found_key.offset - 1; |
| 2757 | } |
| 2758 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2759 | if (failed && !retried) { |
| 2760 | failed = 0; |
| 2761 | retried = true; |
| 2762 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2763 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2764 | ret = -ENOSPC; |
| 2765 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2766 | error: |
| 2767 | btrfs_free_path(path); |
| 2768 | return ret; |
| 2769 | } |
| 2770 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2771 | static int insert_balance_item(struct btrfs_root *root, |
| 2772 | struct btrfs_balance_control *bctl) |
| 2773 | { |
| 2774 | struct btrfs_trans_handle *trans; |
| 2775 | struct btrfs_balance_item *item; |
| 2776 | struct btrfs_disk_balance_args disk_bargs; |
| 2777 | struct btrfs_path *path; |
| 2778 | struct extent_buffer *leaf; |
| 2779 | struct btrfs_key key; |
| 2780 | int ret, err; |
| 2781 | |
| 2782 | path = btrfs_alloc_path(); |
| 2783 | if (!path) |
| 2784 | return -ENOMEM; |
| 2785 | |
| 2786 | trans = btrfs_start_transaction(root, 0); |
| 2787 | if (IS_ERR(trans)) { |
| 2788 | btrfs_free_path(path); |
| 2789 | return PTR_ERR(trans); |
| 2790 | } |
| 2791 | |
| 2792 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2793 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2794 | key.offset = 0; |
| 2795 | |
| 2796 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2797 | sizeof(*item)); |
| 2798 | if (ret) |
| 2799 | goto out; |
| 2800 | |
| 2801 | leaf = path->nodes[0]; |
| 2802 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2803 | |
| 2804 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2805 | |
| 2806 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2807 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2808 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2809 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2810 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2811 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2812 | |
| 2813 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2814 | |
| 2815 | btrfs_mark_buffer_dirty(leaf); |
| 2816 | out: |
| 2817 | btrfs_free_path(path); |
| 2818 | err = btrfs_commit_transaction(trans, root); |
| 2819 | if (err && !ret) |
| 2820 | ret = err; |
| 2821 | return ret; |
| 2822 | } |
| 2823 | |
| 2824 | static int del_balance_item(struct btrfs_root *root) |
| 2825 | { |
| 2826 | struct btrfs_trans_handle *trans; |
| 2827 | struct btrfs_path *path; |
| 2828 | struct btrfs_key key; |
| 2829 | int ret, err; |
| 2830 | |
| 2831 | path = btrfs_alloc_path(); |
| 2832 | if (!path) |
| 2833 | return -ENOMEM; |
| 2834 | |
| 2835 | trans = btrfs_start_transaction(root, 0); |
| 2836 | if (IS_ERR(trans)) { |
| 2837 | btrfs_free_path(path); |
| 2838 | return PTR_ERR(trans); |
| 2839 | } |
| 2840 | |
| 2841 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2842 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2843 | key.offset = 0; |
| 2844 | |
| 2845 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2846 | if (ret < 0) |
| 2847 | goto out; |
| 2848 | if (ret > 0) { |
| 2849 | ret = -ENOENT; |
| 2850 | goto out; |
| 2851 | } |
| 2852 | |
| 2853 | ret = btrfs_del_item(trans, root, path); |
| 2854 | out: |
| 2855 | btrfs_free_path(path); |
| 2856 | err = btrfs_commit_transaction(trans, root); |
| 2857 | if (err && !ret) |
| 2858 | ret = err; |
| 2859 | return ret; |
| 2860 | } |
| 2861 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2862 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2863 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2864 | * resume after balance was interrupted. |
| 2865 | */ |
| 2866 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2867 | { |
| 2868 | /* |
| 2869 | * Turn on soft mode for chunk types that were being converted. |
| 2870 | */ |
| 2871 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2872 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2873 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2874 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2875 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2876 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2877 | |
| 2878 | /* |
| 2879 | * Turn on usage filter if is not already used. The idea is |
| 2880 | * that chunks that we have already balanced should be |
| 2881 | * reasonably full. Don't do it for chunks that are being |
| 2882 | * converted - that will keep us from relocating unconverted |
| 2883 | * (albeit full) chunks. |
| 2884 | */ |
| 2885 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2886 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2887 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2888 | bctl->data.usage = 90; |
| 2889 | } |
| 2890 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2891 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2892 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2893 | bctl->sys.usage = 90; |
| 2894 | } |
| 2895 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2896 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2897 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2898 | bctl->meta.usage = 90; |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2903 | * Should be called with both balance and volume mutexes held to |
| 2904 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2905 | * restriper. Same goes for unset_balance_control. |
| 2906 | */ |
| 2907 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2908 | { |
| 2909 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2910 | |
| 2911 | BUG_ON(fs_info->balance_ctl); |
| 2912 | |
| 2913 | spin_lock(&fs_info->balance_lock); |
| 2914 | fs_info->balance_ctl = bctl; |
| 2915 | spin_unlock(&fs_info->balance_lock); |
| 2916 | } |
| 2917 | |
| 2918 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2919 | { |
| 2920 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2921 | |
| 2922 | BUG_ON(!fs_info->balance_ctl); |
| 2923 | |
| 2924 | spin_lock(&fs_info->balance_lock); |
| 2925 | fs_info->balance_ctl = NULL; |
| 2926 | spin_unlock(&fs_info->balance_lock); |
| 2927 | |
| 2928 | kfree(bctl); |
| 2929 | } |
| 2930 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2931 | /* |
| 2932 | * Balance filters. Return 1 if chunk should be filtered out |
| 2933 | * (should not be balanced). |
| 2934 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2935 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2936 | struct btrfs_balance_args *bargs) |
| 2937 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2938 | chunk_type = chunk_to_extended(chunk_type) & |
| 2939 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2940 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2941 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2942 | return 0; |
| 2943 | |
| 2944 | return 1; |
| 2945 | } |
| 2946 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2947 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2948 | struct btrfs_balance_args *bargs) |
| 2949 | { |
| 2950 | struct btrfs_block_group_cache *cache; |
| 2951 | u64 chunk_used, user_thresh; |
| 2952 | int ret = 1; |
| 2953 | |
| 2954 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2955 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2956 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2957 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 2958 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2959 | else if (bargs->usage > 100) |
| 2960 | user_thresh = cache->key.offset; |
| 2961 | else |
| 2962 | user_thresh = div_factor_fine(cache->key.offset, |
| 2963 | bargs->usage); |
| 2964 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2965 | if (chunk_used < user_thresh) |
| 2966 | ret = 0; |
| 2967 | |
| 2968 | btrfs_put_block_group(cache); |
| 2969 | return ret; |
| 2970 | } |
| 2971 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2972 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2973 | struct btrfs_chunk *chunk, |
| 2974 | struct btrfs_balance_args *bargs) |
| 2975 | { |
| 2976 | struct btrfs_stripe *stripe; |
| 2977 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2978 | int i; |
| 2979 | |
| 2980 | for (i = 0; i < num_stripes; i++) { |
| 2981 | stripe = btrfs_stripe_nr(chunk, i); |
| 2982 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2983 | return 0; |
| 2984 | } |
| 2985 | |
| 2986 | return 1; |
| 2987 | } |
| 2988 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2989 | /* [pstart, pend) */ |
| 2990 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2991 | struct btrfs_chunk *chunk, |
| 2992 | u64 chunk_offset, |
| 2993 | struct btrfs_balance_args *bargs) |
| 2994 | { |
| 2995 | struct btrfs_stripe *stripe; |
| 2996 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2997 | u64 stripe_offset; |
| 2998 | u64 stripe_length; |
| 2999 | int factor; |
| 3000 | int i; |
| 3001 | |
| 3002 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 3003 | return 0; |
| 3004 | |
| 3005 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3006 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 3007 | factor = num_stripes / 2; |
| 3008 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 3009 | factor = num_stripes - 1; |
| 3010 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 3011 | factor = num_stripes - 2; |
| 3012 | } else { |
| 3013 | factor = num_stripes; |
| 3014 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3015 | |
| 3016 | for (i = 0; i < num_stripes; i++) { |
| 3017 | stripe = btrfs_stripe_nr(chunk, i); |
| 3018 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 3019 | continue; |
| 3020 | |
| 3021 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 3022 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 3023 | do_div(stripe_length, factor); |
| 3024 | |
| 3025 | if (stripe_offset < bargs->pend && |
| 3026 | stripe_offset + stripe_length > bargs->pstart) |
| 3027 | return 0; |
| 3028 | } |
| 3029 | |
| 3030 | return 1; |
| 3031 | } |
| 3032 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3033 | /* [vstart, vend) */ |
| 3034 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 3035 | struct btrfs_chunk *chunk, |
| 3036 | u64 chunk_offset, |
| 3037 | struct btrfs_balance_args *bargs) |
| 3038 | { |
| 3039 | if (chunk_offset < bargs->vend && |
| 3040 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 3041 | /* at least part of the chunk is inside this vrange */ |
| 3042 | return 0; |
| 3043 | |
| 3044 | return 1; |
| 3045 | } |
| 3046 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3047 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3048 | struct btrfs_balance_args *bargs) |
| 3049 | { |
| 3050 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 3051 | return 0; |
| 3052 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3053 | chunk_type = chunk_to_extended(chunk_type) & |
| 3054 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3055 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3056 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3057 | return 1; |
| 3058 | |
| 3059 | return 0; |
| 3060 | } |
| 3061 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3062 | static int should_balance_chunk(struct btrfs_root *root, |
| 3063 | struct extent_buffer *leaf, |
| 3064 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 3065 | { |
| 3066 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 3067 | struct btrfs_balance_args *bargs = NULL; |
| 3068 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 3069 | |
| 3070 | /* type filter */ |
| 3071 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 3072 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 3073 | return 0; |
| 3074 | } |
| 3075 | |
| 3076 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 3077 | bargs = &bctl->data; |
| 3078 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3079 | bargs = &bctl->sys; |
| 3080 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 3081 | bargs = &bctl->meta; |
| 3082 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3083 | /* profiles filter */ |
| 3084 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 3085 | chunk_profiles_filter(chunk_type, bargs)) { |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3089 | /* usage filter */ |
| 3090 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 3091 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 3092 | return 0; |
| 3093 | } |
| 3094 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3095 | /* devid filter */ |
| 3096 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 3097 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3101 | /* drange filter, makes sense only with devid filter */ |
| 3102 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 3103 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3104 | return 0; |
| 3105 | } |
| 3106 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3107 | /* vrange filter */ |
| 3108 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 3109 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 3110 | return 0; |
| 3111 | } |
| 3112 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3113 | /* soft profile changing mode */ |
| 3114 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 3115 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 3116 | return 0; |
| 3117 | } |
| 3118 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3119 | /* |
| 3120 | * limited by count, must be the last filter |
| 3121 | */ |
| 3122 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 3123 | if (bargs->limit == 0) |
| 3124 | return 0; |
| 3125 | else |
| 3126 | bargs->limit--; |
| 3127 | } |
| 3128 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3129 | return 1; |
| 3130 | } |
| 3131 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3132 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3133 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3134 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3135 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 3136 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 3137 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3138 | struct btrfs_device *device; |
| 3139 | u64 old_size; |
| 3140 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3141 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3142 | struct btrfs_path *path; |
| 3143 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3144 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3145 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3146 | struct extent_buffer *leaf; |
| 3147 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3148 | int ret; |
| 3149 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3150 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3151 | u64 limit_data = bctl->data.limit; |
| 3152 | u64 limit_meta = bctl->meta.limit; |
| 3153 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3154 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3155 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3156 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3157 | list_for_each_entry(device, devices, dev_list) { |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3158 | old_size = btrfs_device_get_total_bytes(device); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3159 | size_to_free = div_factor(old_size, 1); |
| 3160 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3161 | if (!device->writeable || |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3162 | btrfs_device_get_total_bytes(device) - |
| 3163 | btrfs_device_get_bytes_used(device) > size_to_free || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3164 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3165 | continue; |
| 3166 | |
| 3167 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3168 | if (ret == -ENOSPC) |
| 3169 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3170 | BUG_ON(ret); |
| 3171 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3172 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3173 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3174 | |
| 3175 | ret = btrfs_grow_device(trans, device, old_size); |
| 3176 | BUG_ON(ret); |
| 3177 | |
| 3178 | btrfs_end_transaction(trans, dev_root); |
| 3179 | } |
| 3180 | |
| 3181 | /* step two, relocate all the chunks */ |
| 3182 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3183 | if (!path) { |
| 3184 | ret = -ENOMEM; |
| 3185 | goto error; |
| 3186 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3187 | |
| 3188 | /* zero out stat counters */ |
| 3189 | spin_lock(&fs_info->balance_lock); |
| 3190 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3191 | spin_unlock(&fs_info->balance_lock); |
| 3192 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3193 | if (!counting) { |
| 3194 | bctl->data.limit = limit_data; |
| 3195 | bctl->meta.limit = limit_meta; |
| 3196 | bctl->sys.limit = limit_sys; |
| 3197 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3198 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3199 | key.offset = (u64)-1; |
| 3200 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3201 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3202 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3203 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3204 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3205 | ret = -ECANCELED; |
| 3206 | goto error; |
| 3207 | } |
| 3208 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3209 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 3210 | if (ret < 0) |
| 3211 | goto error; |
| 3212 | |
| 3213 | /* |
| 3214 | * this shouldn't happen, it means the last relocate |
| 3215 | * failed |
| 3216 | */ |
| 3217 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3218 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3219 | |
| 3220 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3221 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3222 | if (ret) { |
| 3223 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3224 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3225 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3226 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3227 | leaf = path->nodes[0]; |
| 3228 | slot = path->slots[0]; |
| 3229 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3230 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3231 | if (found_key.objectid != key.objectid) |
| 3232 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3233 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3234 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3235 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3236 | if (!counting) { |
| 3237 | spin_lock(&fs_info->balance_lock); |
| 3238 | bctl->stat.considered++; |
| 3239 | spin_unlock(&fs_info->balance_lock); |
| 3240 | } |
| 3241 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3242 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3243 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3244 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3245 | if (!ret) |
| 3246 | goto loop; |
| 3247 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3248 | if (counting) { |
| 3249 | spin_lock(&fs_info->balance_lock); |
| 3250 | bctl->stat.expected++; |
| 3251 | spin_unlock(&fs_info->balance_lock); |
| 3252 | goto loop; |
| 3253 | } |
| 3254 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3255 | ret = btrfs_relocate_chunk(chunk_root, |
| 3256 | chunk_root->root_key.objectid, |
| 3257 | found_key.objectid, |
| 3258 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3259 | if (ret && ret != -ENOSPC) |
| 3260 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3261 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3262 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3263 | } else { |
| 3264 | spin_lock(&fs_info->balance_lock); |
| 3265 | bctl->stat.completed++; |
| 3266 | spin_unlock(&fs_info->balance_lock); |
| 3267 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3268 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3269 | if (found_key.offset == 0) |
| 3270 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3271 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3272 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3273 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3274 | if (counting) { |
| 3275 | btrfs_release_path(path); |
| 3276 | counting = false; |
| 3277 | goto again; |
| 3278 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3279 | error: |
| 3280 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3281 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3282 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3283 | enospc_errors); |
| 3284 | if (!ret) |
| 3285 | ret = -ENOSPC; |
| 3286 | } |
| 3287 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3288 | return ret; |
| 3289 | } |
| 3290 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3291 | /** |
| 3292 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3293 | * @flags: profile to validate |
| 3294 | * @extended: if true @flags is treated as an extended profile |
| 3295 | */ |
| 3296 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3297 | { |
| 3298 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3299 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3300 | |
| 3301 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3302 | |
| 3303 | /* 1) check that all other bits are zeroed */ |
| 3304 | if (flags & ~mask) |
| 3305 | return 0; |
| 3306 | |
| 3307 | /* 2) see if profile is reduced */ |
| 3308 | if (flags == 0) |
| 3309 | return !extended; /* "0" is valid for usual profiles */ |
| 3310 | |
| 3311 | /* true if exactly one bit set */ |
| 3312 | return (flags & (flags - 1)) == 0; |
| 3313 | } |
| 3314 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3315 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3316 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3317 | /* cancel requested || normal exit path */ |
| 3318 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3319 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3320 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3321 | } |
| 3322 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3323 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3324 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3325 | int ret; |
| 3326 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3327 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3328 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3329 | if (ret) |
| 3330 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3331 | |
| 3332 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3333 | } |
| 3334 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3335 | /* |
| 3336 | * Should be called with both balance and volume mutexes held |
| 3337 | */ |
| 3338 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3339 | struct btrfs_ioctl_balance_args *bargs) |
| 3340 | { |
| 3341 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3342 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3343 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3344 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3345 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3346 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3347 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3348 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3349 | atomic_read(&fs_info->balance_pause_req) || |
| 3350 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3351 | ret = -EINVAL; |
| 3352 | goto out; |
| 3353 | } |
| 3354 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3355 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3356 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3357 | mixed = 1; |
| 3358 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3359 | /* |
| 3360 | * In case of mixed groups both data and meta should be picked, |
| 3361 | * and identical options should be given for both of them. |
| 3362 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3363 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3364 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3365 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3366 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3367 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3368 | btrfs_err(fs_info, "with mixed groups data and " |
| 3369 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3370 | ret = -EINVAL; |
| 3371 | goto out; |
| 3372 | } |
| 3373 | } |
| 3374 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3375 | num_devices = fs_info->fs_devices->num_devices; |
| 3376 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3377 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3378 | BUG_ON(num_devices < 1); |
| 3379 | num_devices--; |
| 3380 | } |
| 3381 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3382 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3383 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3384 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3385 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3386 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3387 | if (num_devices > 2) |
| 3388 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3389 | if (num_devices > 3) |
| 3390 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3391 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3392 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3393 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3394 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3395 | btrfs_err(fs_info, "unable to start balance with target " |
| 3396 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3397 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3398 | ret = -EINVAL; |
| 3399 | goto out; |
| 3400 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3401 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3402 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3403 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3404 | btrfs_err(fs_info, |
| 3405 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3406 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3407 | ret = -EINVAL; |
| 3408 | goto out; |
| 3409 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3410 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3411 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3412 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3413 | btrfs_err(fs_info, |
| 3414 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3415 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3416 | ret = -EINVAL; |
| 3417 | goto out; |
| 3418 | } |
| 3419 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3420 | /* allow dup'ed data chunks only in mixed mode */ |
| 3421 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3422 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3423 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3424 | ret = -EINVAL; |
| 3425 | goto out; |
| 3426 | } |
| 3427 | |
| 3428 | /* allow to reduce meta or sys integrity only if force set */ |
| 3429 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3430 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3431 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3432 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3433 | do { |
| 3434 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3435 | |
| 3436 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3437 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3438 | !(bctl->sys.target & allowed)) || |
| 3439 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3440 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3441 | !(bctl->meta.target & allowed))) { |
| 3442 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3443 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3444 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3445 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3446 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3447 | ret = -EINVAL; |
| 3448 | goto out; |
| 3449 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3450 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3451 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3452 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3453 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3454 | int num_tolerated_disk_barrier_failures; |
| 3455 | u64 target = bctl->sys.target; |
| 3456 | |
| 3457 | num_tolerated_disk_barrier_failures = |
| 3458 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3459 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3460 | (target & |
| 3461 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3462 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3463 | num_tolerated_disk_barrier_failures = 0; |
| 3464 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3465 | (target & |
| 3466 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3467 | num_tolerated_disk_barrier_failures = 1; |
| 3468 | |
| 3469 | fs_info->num_tolerated_disk_barrier_failures = |
| 3470 | num_tolerated_disk_barrier_failures; |
| 3471 | } |
| 3472 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3473 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3474 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3475 | goto out; |
| 3476 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3477 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3478 | BUG_ON(ret == -EEXIST); |
| 3479 | set_balance_control(bctl); |
| 3480 | } else { |
| 3481 | BUG_ON(ret != -EEXIST); |
| 3482 | spin_lock(&fs_info->balance_lock); |
| 3483 | update_balance_args(bctl); |
| 3484 | spin_unlock(&fs_info->balance_lock); |
| 3485 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3486 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3487 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3488 | mutex_unlock(&fs_info->balance_mutex); |
| 3489 | |
| 3490 | ret = __btrfs_balance(fs_info); |
| 3491 | |
| 3492 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3493 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3494 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3495 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3496 | fs_info->num_tolerated_disk_barrier_failures = |
| 3497 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3498 | } |
| 3499 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3500 | if (bargs) { |
| 3501 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3502 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3503 | } |
| 3504 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3505 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3506 | balance_need_close(fs_info)) { |
| 3507 | __cancel_balance(fs_info); |
| 3508 | } |
| 3509 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3510 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3511 | |
| 3512 | return ret; |
| 3513 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3514 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3515 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3516 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3517 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3518 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3519 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3520 | return ret; |
| 3521 | } |
| 3522 | |
| 3523 | static int balance_kthread(void *data) |
| 3524 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3525 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3526 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3527 | |
| 3528 | mutex_lock(&fs_info->volume_mutex); |
| 3529 | mutex_lock(&fs_info->balance_mutex); |
| 3530 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3531 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3532 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3533 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3534 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3535 | |
| 3536 | mutex_unlock(&fs_info->balance_mutex); |
| 3537 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3538 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3539 | return ret; |
| 3540 | } |
| 3541 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3542 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3543 | { |
| 3544 | struct task_struct *tsk; |
| 3545 | |
| 3546 | spin_lock(&fs_info->balance_lock); |
| 3547 | if (!fs_info->balance_ctl) { |
| 3548 | spin_unlock(&fs_info->balance_lock); |
| 3549 | return 0; |
| 3550 | } |
| 3551 | spin_unlock(&fs_info->balance_lock); |
| 3552 | |
| 3553 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3554 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3555 | return 0; |
| 3556 | } |
| 3557 | |
| 3558 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3559 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3560 | } |
| 3561 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3562 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3563 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3564 | struct btrfs_balance_control *bctl; |
| 3565 | struct btrfs_balance_item *item; |
| 3566 | struct btrfs_disk_balance_args disk_bargs; |
| 3567 | struct btrfs_path *path; |
| 3568 | struct extent_buffer *leaf; |
| 3569 | struct btrfs_key key; |
| 3570 | int ret; |
| 3571 | |
| 3572 | path = btrfs_alloc_path(); |
| 3573 | if (!path) |
| 3574 | return -ENOMEM; |
| 3575 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3576 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3577 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3578 | key.offset = 0; |
| 3579 | |
| 3580 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3581 | if (ret < 0) |
| 3582 | goto out; |
| 3583 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3584 | ret = 0; |
| 3585 | goto out; |
| 3586 | } |
| 3587 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3588 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3589 | if (!bctl) { |
| 3590 | ret = -ENOMEM; |
| 3591 | goto out; |
| 3592 | } |
| 3593 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3594 | leaf = path->nodes[0]; |
| 3595 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3596 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3597 | bctl->fs_info = fs_info; |
| 3598 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3599 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3600 | |
| 3601 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3602 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3603 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3604 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3605 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3606 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3607 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3608 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3609 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3610 | mutex_lock(&fs_info->volume_mutex); |
| 3611 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3612 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3613 | set_balance_control(bctl); |
| 3614 | |
| 3615 | mutex_unlock(&fs_info->balance_mutex); |
| 3616 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3617 | out: |
| 3618 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3619 | return ret; |
| 3620 | } |
| 3621 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3622 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3623 | { |
| 3624 | int ret = 0; |
| 3625 | |
| 3626 | mutex_lock(&fs_info->balance_mutex); |
| 3627 | if (!fs_info->balance_ctl) { |
| 3628 | mutex_unlock(&fs_info->balance_mutex); |
| 3629 | return -ENOTCONN; |
| 3630 | } |
| 3631 | |
| 3632 | if (atomic_read(&fs_info->balance_running)) { |
| 3633 | atomic_inc(&fs_info->balance_pause_req); |
| 3634 | mutex_unlock(&fs_info->balance_mutex); |
| 3635 | |
| 3636 | wait_event(fs_info->balance_wait_q, |
| 3637 | atomic_read(&fs_info->balance_running) == 0); |
| 3638 | |
| 3639 | mutex_lock(&fs_info->balance_mutex); |
| 3640 | /* we are good with balance_ctl ripped off from under us */ |
| 3641 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3642 | atomic_dec(&fs_info->balance_pause_req); |
| 3643 | } else { |
| 3644 | ret = -ENOTCONN; |
| 3645 | } |
| 3646 | |
| 3647 | mutex_unlock(&fs_info->balance_mutex); |
| 3648 | return ret; |
| 3649 | } |
| 3650 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3651 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3652 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3653 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3654 | return -EROFS; |
| 3655 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3656 | mutex_lock(&fs_info->balance_mutex); |
| 3657 | if (!fs_info->balance_ctl) { |
| 3658 | mutex_unlock(&fs_info->balance_mutex); |
| 3659 | return -ENOTCONN; |
| 3660 | } |
| 3661 | |
| 3662 | atomic_inc(&fs_info->balance_cancel_req); |
| 3663 | /* |
| 3664 | * if we are running just wait and return, balance item is |
| 3665 | * deleted in btrfs_balance in this case |
| 3666 | */ |
| 3667 | if (atomic_read(&fs_info->balance_running)) { |
| 3668 | mutex_unlock(&fs_info->balance_mutex); |
| 3669 | wait_event(fs_info->balance_wait_q, |
| 3670 | atomic_read(&fs_info->balance_running) == 0); |
| 3671 | mutex_lock(&fs_info->balance_mutex); |
| 3672 | } else { |
| 3673 | /* __cancel_balance needs volume_mutex */ |
| 3674 | mutex_unlock(&fs_info->balance_mutex); |
| 3675 | mutex_lock(&fs_info->volume_mutex); |
| 3676 | mutex_lock(&fs_info->balance_mutex); |
| 3677 | |
| 3678 | if (fs_info->balance_ctl) |
| 3679 | __cancel_balance(fs_info); |
| 3680 | |
| 3681 | mutex_unlock(&fs_info->volume_mutex); |
| 3682 | } |
| 3683 | |
| 3684 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3685 | atomic_dec(&fs_info->balance_cancel_req); |
| 3686 | mutex_unlock(&fs_info->balance_mutex); |
| 3687 | return 0; |
| 3688 | } |
| 3689 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3690 | static int btrfs_uuid_scan_kthread(void *data) |
| 3691 | { |
| 3692 | struct btrfs_fs_info *fs_info = data; |
| 3693 | struct btrfs_root *root = fs_info->tree_root; |
| 3694 | struct btrfs_key key; |
| 3695 | struct btrfs_key max_key; |
| 3696 | struct btrfs_path *path = NULL; |
| 3697 | int ret = 0; |
| 3698 | struct extent_buffer *eb; |
| 3699 | int slot; |
| 3700 | struct btrfs_root_item root_item; |
| 3701 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3702 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3703 | |
| 3704 | path = btrfs_alloc_path(); |
| 3705 | if (!path) { |
| 3706 | ret = -ENOMEM; |
| 3707 | goto out; |
| 3708 | } |
| 3709 | |
| 3710 | key.objectid = 0; |
| 3711 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3712 | key.offset = 0; |
| 3713 | |
| 3714 | max_key.objectid = (u64)-1; |
| 3715 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3716 | max_key.offset = (u64)-1; |
| 3717 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3718 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3719 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3720 | if (ret) { |
| 3721 | if (ret > 0) |
| 3722 | ret = 0; |
| 3723 | break; |
| 3724 | } |
| 3725 | |
| 3726 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3727 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3728 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3729 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3730 | goto skip; |
| 3731 | |
| 3732 | eb = path->nodes[0]; |
| 3733 | slot = path->slots[0]; |
| 3734 | item_size = btrfs_item_size_nr(eb, slot); |
| 3735 | if (item_size < sizeof(root_item)) |
| 3736 | goto skip; |
| 3737 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3738 | read_extent_buffer(eb, &root_item, |
| 3739 | btrfs_item_ptr_offset(eb, slot), |
| 3740 | (int)sizeof(root_item)); |
| 3741 | if (btrfs_root_refs(&root_item) == 0) |
| 3742 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3743 | |
| 3744 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3745 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3746 | if (trans) |
| 3747 | goto update_tree; |
| 3748 | |
| 3749 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3750 | /* |
| 3751 | * 1 - subvol uuid item |
| 3752 | * 1 - received_subvol uuid item |
| 3753 | */ |
| 3754 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3755 | if (IS_ERR(trans)) { |
| 3756 | ret = PTR_ERR(trans); |
| 3757 | break; |
| 3758 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3759 | continue; |
| 3760 | } else { |
| 3761 | goto skip; |
| 3762 | } |
| 3763 | update_tree: |
| 3764 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3765 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3766 | root_item.uuid, |
| 3767 | BTRFS_UUID_KEY_SUBVOL, |
| 3768 | key.objectid); |
| 3769 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3770 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3771 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3772 | break; |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3777 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3778 | root_item.received_uuid, |
| 3779 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3780 | key.objectid); |
| 3781 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3782 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3783 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3784 | break; |
| 3785 | } |
| 3786 | } |
| 3787 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3788 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3789 | if (trans) { |
| 3790 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3791 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3792 | if (ret) |
| 3793 | break; |
| 3794 | } |
| 3795 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3796 | btrfs_release_path(path); |
| 3797 | if (key.offset < (u64)-1) { |
| 3798 | key.offset++; |
| 3799 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3800 | key.offset = 0; |
| 3801 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3802 | } else if (key.objectid < (u64)-1) { |
| 3803 | key.offset = 0; |
| 3804 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3805 | key.objectid++; |
| 3806 | } else { |
| 3807 | break; |
| 3808 | } |
| 3809 | cond_resched(); |
| 3810 | } |
| 3811 | |
| 3812 | out: |
| 3813 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3814 | if (trans && !IS_ERR(trans)) |
| 3815 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3816 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3817 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3818 | else |
| 3819 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3820 | up(&fs_info->uuid_tree_rescan_sem); |
| 3821 | return 0; |
| 3822 | } |
| 3823 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3824 | /* |
| 3825 | * Callback for btrfs_uuid_tree_iterate(). |
| 3826 | * returns: |
| 3827 | * 0 check succeeded, the entry is not outdated. |
| 3828 | * < 0 if an error occured. |
| 3829 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3830 | */ |
| 3831 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3832 | u8 *uuid, u8 type, u64 subid) |
| 3833 | { |
| 3834 | struct btrfs_key key; |
| 3835 | int ret = 0; |
| 3836 | struct btrfs_root *subvol_root; |
| 3837 | |
| 3838 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3839 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3840 | goto out; |
| 3841 | |
| 3842 | key.objectid = subid; |
| 3843 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3844 | key.offset = (u64)-1; |
| 3845 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3846 | if (IS_ERR(subvol_root)) { |
| 3847 | ret = PTR_ERR(subvol_root); |
| 3848 | if (ret == -ENOENT) |
| 3849 | ret = 1; |
| 3850 | goto out; |
| 3851 | } |
| 3852 | |
| 3853 | switch (type) { |
| 3854 | case BTRFS_UUID_KEY_SUBVOL: |
| 3855 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3856 | ret = 1; |
| 3857 | break; |
| 3858 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3859 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3860 | BTRFS_UUID_SIZE)) |
| 3861 | ret = 1; |
| 3862 | break; |
| 3863 | } |
| 3864 | |
| 3865 | out: |
| 3866 | return ret; |
| 3867 | } |
| 3868 | |
| 3869 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3870 | { |
| 3871 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3872 | int ret; |
| 3873 | |
| 3874 | /* |
| 3875 | * 1st step is to iterate through the existing UUID tree and |
| 3876 | * to delete all entries that contain outdated data. |
| 3877 | * 2nd step is to add all missing entries to the UUID tree. |
| 3878 | */ |
| 3879 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 3880 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3881 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3882 | up(&fs_info->uuid_tree_rescan_sem); |
| 3883 | return ret; |
| 3884 | } |
| 3885 | return btrfs_uuid_scan_kthread(data); |
| 3886 | } |
| 3887 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3888 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3889 | { |
| 3890 | struct btrfs_trans_handle *trans; |
| 3891 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 3892 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3893 | struct task_struct *task; |
| 3894 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3895 | |
| 3896 | /* |
| 3897 | * 1 - root node |
| 3898 | * 1 - root item |
| 3899 | */ |
| 3900 | trans = btrfs_start_transaction(tree_root, 2); |
| 3901 | if (IS_ERR(trans)) |
| 3902 | return PTR_ERR(trans); |
| 3903 | |
| 3904 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 3905 | BTRFS_UUID_TREE_OBJECTID); |
| 3906 | if (IS_ERR(uuid_root)) { |
| 3907 | btrfs_abort_transaction(trans, tree_root, |
| 3908 | PTR_ERR(uuid_root)); |
| 3909 | return PTR_ERR(uuid_root); |
| 3910 | } |
| 3911 | |
| 3912 | fs_info->uuid_root = uuid_root; |
| 3913 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3914 | ret = btrfs_commit_transaction(trans, tree_root); |
| 3915 | if (ret) |
| 3916 | return ret; |
| 3917 | |
| 3918 | down(&fs_info->uuid_tree_rescan_sem); |
| 3919 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 3920 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3921 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3922 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3923 | up(&fs_info->uuid_tree_rescan_sem); |
| 3924 | return PTR_ERR(task); |
| 3925 | } |
| 3926 | |
| 3927 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3928 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3929 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3930 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3931 | { |
| 3932 | struct task_struct *task; |
| 3933 | |
| 3934 | down(&fs_info->uuid_tree_rescan_sem); |
| 3935 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 3936 | if (IS_ERR(task)) { |
| 3937 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3938 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3939 | up(&fs_info->uuid_tree_rescan_sem); |
| 3940 | return PTR_ERR(task); |
| 3941 | } |
| 3942 | |
| 3943 | return 0; |
| 3944 | } |
| 3945 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3946 | /* |
| 3947 | * shrinking a device means finding all of the device extents past |
| 3948 | * the new size, and then following the back refs to the chunks. |
| 3949 | * The chunk relocation code actually frees the device extent |
| 3950 | */ |
| 3951 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3952 | { |
| 3953 | struct btrfs_trans_handle *trans; |
| 3954 | struct btrfs_root *root = device->dev_root; |
| 3955 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3956 | struct btrfs_path *path; |
| 3957 | u64 length; |
| 3958 | u64 chunk_tree; |
| 3959 | u64 chunk_objectid; |
| 3960 | u64 chunk_offset; |
| 3961 | int ret; |
| 3962 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3963 | int failed = 0; |
| 3964 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3965 | struct extent_buffer *l; |
| 3966 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3967 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3968 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3969 | u64 old_size = btrfs_device_get_total_bytes(device); |
| 3970 | u64 diff = old_size - new_size; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3971 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3972 | if (device->is_tgtdev_for_dev_replace) |
| 3973 | return -EINVAL; |
| 3974 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3975 | path = btrfs_alloc_path(); |
| 3976 | if (!path) |
| 3977 | return -ENOMEM; |
| 3978 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3979 | path->reada = 2; |
| 3980 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3981 | lock_chunks(root); |
| 3982 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 3983 | btrfs_device_set_total_bytes(device, new_size); |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3984 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3985 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3986 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3987 | root->fs_info->free_chunk_space -= diff; |
| 3988 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3989 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3990 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3991 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3992 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3993 | key.objectid = device->devid; |
| 3994 | key.offset = (u64)-1; |
| 3995 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3996 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3997 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3998 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3999 | if (ret < 0) |
| 4000 | goto done; |
| 4001 | |
| 4002 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 4003 | if (ret < 0) |
| 4004 | goto done; |
| 4005 | if (ret) { |
| 4006 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4007 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4008 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | l = path->nodes[0]; |
| 4012 | slot = path->slots[0]; |
| 4013 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 4014 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4015 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4016 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 4017 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4018 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4019 | |
| 4020 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 4021 | length = btrfs_dev_extent_length(l, dev_extent); |
| 4022 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4023 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4024 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4025 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4026 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4027 | |
| 4028 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 4029 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 4030 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4031 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4032 | |
| 4033 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 4034 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4035 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4036 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4037 | if (ret == -ENOSPC) |
| 4038 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 4039 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4040 | |
| 4041 | if (failed && !retried) { |
| 4042 | failed = 0; |
| 4043 | retried = true; |
| 4044 | goto again; |
| 4045 | } else if (failed && retried) { |
| 4046 | ret = -ENOSPC; |
| 4047 | lock_chunks(root); |
| 4048 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4049 | btrfs_device_set_total_bytes(device, old_size); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4050 | if (device->writeable) |
| 4051 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4052 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4053 | root->fs_info->free_chunk_space += diff; |
| 4054 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4055 | unlock_chunks(root); |
| 4056 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4057 | } |
| 4058 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4059 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4060 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 4061 | if (IS_ERR(trans)) { |
| 4062 | ret = PTR_ERR(trans); |
| 4063 | goto done; |
| 4064 | } |
| 4065 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4066 | lock_chunks(root); |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4067 | btrfs_device_set_disk_total_bytes(device, new_size); |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 4068 | if (list_empty(&device->resized_list)) |
| 4069 | list_add_tail(&device->resized_list, |
| 4070 | &root->fs_info->fs_devices->resized_devices); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4071 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4072 | WARN_ON(diff > old_total); |
| 4073 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 4074 | unlock_chunks(root); |
Miao Xie | 2196d6e | 2014-09-03 21:35:41 +0800 | [diff] [blame] | 4075 | |
| 4076 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 4077 | ret = btrfs_update_device(trans, device); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 4078 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4079 | done: |
| 4080 | btrfs_free_path(path); |
| 4081 | return ret; |
| 4082 | } |
| 4083 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4084 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4085 | struct btrfs_key *key, |
| 4086 | struct btrfs_chunk *chunk, int item_size) |
| 4087 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4088 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4089 | struct btrfs_disk_key disk_key; |
| 4090 | u32 array_size; |
| 4091 | u8 *ptr; |
| 4092 | |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4093 | lock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4094 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 4095 | if (array_size + item_size + sizeof(disk_key) |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4096 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { |
| 4097 | unlock_chunks(root); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4098 | return -EFBIG; |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4099 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4100 | |
| 4101 | ptr = super_copy->sys_chunk_array + array_size; |
| 4102 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 4103 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 4104 | ptr += sizeof(disk_key); |
| 4105 | memcpy(ptr, chunk, item_size); |
| 4106 | item_size += sizeof(disk_key); |
| 4107 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
Miao Xie | fe48a5c | 2014-09-03 21:35:39 +0800 | [diff] [blame] | 4108 | unlock_chunks(root); |
| 4109 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4110 | return 0; |
| 4111 | } |
| 4112 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4113 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4114 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4115 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4116 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4117 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4118 | const struct btrfs_device_info *di_a = a; |
| 4119 | const struct btrfs_device_info *di_b = b; |
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 | if (di_a->max_avail > di_b->max_avail) |
| 4122 | return -1; |
| 4123 | if (di_a->max_avail < di_b->max_avail) |
| 4124 | return 1; |
| 4125 | if (di_a->total_avail > di_b->total_avail) |
| 4126 | return -1; |
| 4127 | if (di_a->total_avail < di_b->total_avail) |
| 4128 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4129 | return 0; |
| 4130 | } |
| 4131 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4132 | static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 4133 | [BTRFS_RAID_RAID10] = { |
| 4134 | .sub_stripes = 2, |
| 4135 | .dev_stripes = 1, |
| 4136 | .devs_max = 0, /* 0 == as many as possible */ |
| 4137 | .devs_min = 4, |
| 4138 | .devs_increment = 2, |
| 4139 | .ncopies = 2, |
| 4140 | }, |
| 4141 | [BTRFS_RAID_RAID1] = { |
| 4142 | .sub_stripes = 1, |
| 4143 | .dev_stripes = 1, |
| 4144 | .devs_max = 2, |
| 4145 | .devs_min = 2, |
| 4146 | .devs_increment = 2, |
| 4147 | .ncopies = 2, |
| 4148 | }, |
| 4149 | [BTRFS_RAID_DUP] = { |
| 4150 | .sub_stripes = 1, |
| 4151 | .dev_stripes = 2, |
| 4152 | .devs_max = 1, |
| 4153 | .devs_min = 1, |
| 4154 | .devs_increment = 1, |
| 4155 | .ncopies = 2, |
| 4156 | }, |
| 4157 | [BTRFS_RAID_RAID0] = { |
| 4158 | .sub_stripes = 1, |
| 4159 | .dev_stripes = 1, |
| 4160 | .devs_max = 0, |
| 4161 | .devs_min = 2, |
| 4162 | .devs_increment = 1, |
| 4163 | .ncopies = 1, |
| 4164 | }, |
| 4165 | [BTRFS_RAID_SINGLE] = { |
| 4166 | .sub_stripes = 1, |
| 4167 | .dev_stripes = 1, |
| 4168 | .devs_max = 1, |
| 4169 | .devs_min = 1, |
| 4170 | .devs_increment = 1, |
| 4171 | .ncopies = 1, |
| 4172 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4173 | [BTRFS_RAID_RAID5] = { |
| 4174 | .sub_stripes = 1, |
| 4175 | .dev_stripes = 1, |
| 4176 | .devs_max = 0, |
| 4177 | .devs_min = 2, |
| 4178 | .devs_increment = 1, |
| 4179 | .ncopies = 2, |
| 4180 | }, |
| 4181 | [BTRFS_RAID_RAID6] = { |
| 4182 | .sub_stripes = 1, |
| 4183 | .dev_stripes = 1, |
| 4184 | .devs_max = 0, |
| 4185 | .devs_min = 3, |
| 4186 | .devs_increment = 1, |
| 4187 | .ncopies = 3, |
| 4188 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4189 | }; |
| 4190 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4191 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4192 | { |
| 4193 | /* TODO allow them to set a preferred stripe size */ |
| 4194 | return 64 * 1024; |
| 4195 | } |
| 4196 | |
| 4197 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4198 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4199 | if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))) |
| 4200 | return; |
| 4201 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4202 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4203 | } |
| 4204 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4205 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4206 | - sizeof(struct btrfs_item) \ |
| 4207 | - sizeof(struct btrfs_chunk)) \ |
| 4208 | / sizeof(struct btrfs_stripe) + 1) |
| 4209 | |
| 4210 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4211 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4212 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4213 | / sizeof(struct btrfs_stripe) + 1) |
| 4214 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4215 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4216 | struct btrfs_root *extent_root, u64 start, |
| 4217 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4218 | { |
| 4219 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4220 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4221 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4222 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4223 | struct extent_map_tree *em_tree; |
| 4224 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4225 | struct btrfs_device_info *devices_info = NULL; |
| 4226 | u64 total_avail; |
| 4227 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4228 | int data_stripes; /* number of stripes that count for |
| 4229 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4230 | int sub_stripes; /* sub_stripes info for map */ |
| 4231 | int dev_stripes; /* stripes per dev */ |
| 4232 | int devs_max; /* max devs to use */ |
| 4233 | int devs_min; /* min devs needed */ |
| 4234 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4235 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4236 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4237 | u64 max_stripe_size; |
| 4238 | u64 max_chunk_size; |
| 4239 | u64 stripe_size; |
| 4240 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4241 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4242 | int ndevs; |
| 4243 | int i; |
| 4244 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4245 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4246 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4247 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4248 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4249 | if (list_empty(&fs_devices->alloc_list)) |
| 4250 | return -ENOSPC; |
| 4251 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4252 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4253 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4254 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4255 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4256 | devs_max = btrfs_raid_array[index].devs_max; |
| 4257 | devs_min = btrfs_raid_array[index].devs_min; |
| 4258 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4259 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4260 | |
| 4261 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4262 | max_stripe_size = 1024 * 1024 * 1024; |
| 4263 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4264 | if (!devs_max) |
| 4265 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4266 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4267 | /* for larger filesystems, use larger metadata chunks */ |
| 4268 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4269 | max_stripe_size = 1024 * 1024 * 1024; |
| 4270 | else |
| 4271 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4272 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4273 | if (!devs_max) |
| 4274 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4275 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4276 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4277 | max_chunk_size = 2 * 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_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4280 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4281 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4282 | type); |
| 4283 | BUG_ON(1); |
| 4284 | } |
| 4285 | |
| 4286 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4287 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4288 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4289 | |
| 4290 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 4291 | GFP_NOFS); |
| 4292 | if (!devices_info) |
| 4293 | return -ENOMEM; |
| 4294 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4295 | cur = fs_devices->alloc_list.next; |
| 4296 | |
| 4297 | /* |
| 4298 | * in the first pass through the devices list, we gather information |
| 4299 | * about the available holes on each device. |
| 4300 | */ |
| 4301 | ndevs = 0; |
| 4302 | while (cur != &fs_devices->alloc_list) { |
| 4303 | struct btrfs_device *device; |
| 4304 | u64 max_avail; |
| 4305 | u64 dev_offset; |
| 4306 | |
| 4307 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4308 | |
| 4309 | cur = cur->next; |
| 4310 | |
| 4311 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4312 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4313 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4314 | continue; |
| 4315 | } |
| 4316 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4317 | if (!device->in_fs_metadata || |
| 4318 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4319 | continue; |
| 4320 | |
| 4321 | if (device->total_bytes > device->bytes_used) |
| 4322 | total_avail = device->total_bytes - device->bytes_used; |
| 4323 | else |
| 4324 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4325 | |
| 4326 | /* If there is no space on this device, skip it. */ |
| 4327 | if (total_avail == 0) |
| 4328 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4329 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4330 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4331 | max_stripe_size * dev_stripes, |
| 4332 | &dev_offset, &max_avail); |
| 4333 | if (ret && ret != -ENOSPC) |
| 4334 | goto error; |
| 4335 | |
| 4336 | if (ret == 0) |
| 4337 | max_avail = max_stripe_size * dev_stripes; |
| 4338 | |
| 4339 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4340 | continue; |
| 4341 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4342 | if (ndevs == fs_devices->rw_devices) { |
| 4343 | WARN(1, "%s: found more than %llu devices\n", |
| 4344 | __func__, fs_devices->rw_devices); |
| 4345 | break; |
| 4346 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4347 | devices_info[ndevs].dev_offset = dev_offset; |
| 4348 | devices_info[ndevs].max_avail = max_avail; |
| 4349 | devices_info[ndevs].total_avail = total_avail; |
| 4350 | devices_info[ndevs].dev = device; |
| 4351 | ++ndevs; |
| 4352 | } |
| 4353 | |
| 4354 | /* |
| 4355 | * now sort the devices by hole size / available space |
| 4356 | */ |
| 4357 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4358 | btrfs_cmp_device_info, NULL); |
| 4359 | |
| 4360 | /* round down to number of usable stripes */ |
| 4361 | ndevs -= ndevs % devs_increment; |
| 4362 | |
| 4363 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4364 | ret = -ENOSPC; |
| 4365 | goto error; |
| 4366 | } |
| 4367 | |
| 4368 | if (devs_max && ndevs > devs_max) |
| 4369 | ndevs = devs_max; |
| 4370 | /* |
| 4371 | * the primary goal is to maximize the number of stripes, so use as many |
| 4372 | * devices as possible, even if the stripes are not maximum sized. |
| 4373 | */ |
| 4374 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4375 | num_stripes = ndevs * dev_stripes; |
| 4376 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4377 | /* |
| 4378 | * this will have to be fixed for RAID1 and RAID10 over |
| 4379 | * more drives |
| 4380 | */ |
| 4381 | data_stripes = num_stripes / ncopies; |
| 4382 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4383 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4384 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4385 | btrfs_super_stripesize(info->super_copy)); |
| 4386 | data_stripes = num_stripes - 1; |
| 4387 | } |
| 4388 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4389 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4390 | btrfs_super_stripesize(info->super_copy)); |
| 4391 | data_stripes = num_stripes - 2; |
| 4392 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4393 | |
| 4394 | /* |
| 4395 | * Use the number of data stripes to figure out how big this chunk |
| 4396 | * is really going to be in terms of logical address space, |
| 4397 | * and compare that answer with the max chunk size |
| 4398 | */ |
| 4399 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4400 | u64 mask = (1ULL << 24) - 1; |
| 4401 | stripe_size = max_chunk_size; |
| 4402 | do_div(stripe_size, data_stripes); |
| 4403 | |
| 4404 | /* bump the answer up to a 16MB boundary */ |
| 4405 | stripe_size = (stripe_size + mask) & ~mask; |
| 4406 | |
| 4407 | /* but don't go higher than the limits we found |
| 4408 | * while searching for free extents |
| 4409 | */ |
| 4410 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4411 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4412 | } |
| 4413 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4414 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4415 | |
| 4416 | /* align to BTRFS_STRIPE_LEN */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4417 | do_div(stripe_size, raid_stripe_len); |
| 4418 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4419 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4420 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4421 | if (!map) { |
| 4422 | ret = -ENOMEM; |
| 4423 | goto error; |
| 4424 | } |
| 4425 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4426 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4427 | for (i = 0; i < ndevs; ++i) { |
| 4428 | for (j = 0; j < dev_stripes; ++j) { |
| 4429 | int s = i * dev_stripes + j; |
| 4430 | map->stripes[s].dev = devices_info[i].dev; |
| 4431 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4432 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4433 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4434 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4435 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4436 | map->stripe_len = raid_stripe_len; |
| 4437 | map->io_align = raid_stripe_len; |
| 4438 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4439 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4440 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4441 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4442 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4443 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4444 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4445 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4446 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4447 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4448 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4449 | ret = -ENOMEM; |
| 4450 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4451 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4452 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4453 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4454 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4455 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4456 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4457 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4458 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4459 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4460 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4461 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4462 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4463 | if (!ret) { |
| 4464 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4465 | atomic_inc(&em->refs); |
| 4466 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4467 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4468 | if (ret) { |
| 4469 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4470 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4471 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4472 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4473 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4474 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4475 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4476 | if (ret) |
| 4477 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4478 | |
Miao Xie | 7cc8e58 | 2014-09-03 21:35:38 +0800 | [diff] [blame] | 4479 | for (i = 0; i < map->num_stripes; i++) { |
| 4480 | num_bytes = map->stripes[i].dev->bytes_used + stripe_size; |
| 4481 | btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); |
| 4482 | } |
Miao Xie | 43530c4 | 2014-09-03 21:35:36 +0800 | [diff] [blame] | 4483 | |
Miao Xie | 1c11618 | 2014-09-03 21:35:37 +0800 | [diff] [blame] | 4484 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4485 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4486 | map->num_stripes); |
| 4487 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4488 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4489 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4490 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4491 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4492 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4493 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4494 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4495 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4496 | write_lock(&em_tree->lock); |
| 4497 | remove_extent_mapping(em_tree, em); |
| 4498 | write_unlock(&em_tree->lock); |
| 4499 | |
| 4500 | /* One for our allocation */ |
| 4501 | free_extent_map(em); |
| 4502 | /* One for the tree reference */ |
| 4503 | free_extent_map(em); |
Filipe Manana | 495e64f | 2014-12-02 18:07:30 +0000 | [diff] [blame] | 4504 | /* One for the pending_chunks list reference */ |
| 4505 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4506 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4507 | kfree(devices_info); |
| 4508 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4509 | } |
| 4510 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4511 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4512 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4513 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4514 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4515 | struct btrfs_key key; |
| 4516 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4517 | struct btrfs_device *device; |
| 4518 | struct btrfs_chunk *chunk; |
| 4519 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4520 | struct extent_map_tree *em_tree; |
| 4521 | struct extent_map *em; |
| 4522 | struct map_lookup *map; |
| 4523 | size_t item_size; |
| 4524 | u64 dev_offset; |
| 4525 | u64 stripe_size; |
| 4526 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4527 | int ret; |
| 4528 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4529 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4530 | read_lock(&em_tree->lock); |
| 4531 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4532 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4533 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4534 | if (!em) { |
| 4535 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4536 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4537 | return -EINVAL; |
| 4538 | } |
| 4539 | |
| 4540 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4541 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4542 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4543 | chunk_size, em->start, em->len); |
| 4544 | free_extent_map(em); |
| 4545 | return -EINVAL; |
| 4546 | } |
| 4547 | |
| 4548 | map = (struct map_lookup *)em->bdev; |
| 4549 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4550 | stripe_size = em->orig_block_len; |
| 4551 | |
| 4552 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4553 | if (!chunk) { |
| 4554 | ret = -ENOMEM; |
| 4555 | goto out; |
| 4556 | } |
| 4557 | |
| 4558 | for (i = 0; i < map->num_stripes; i++) { |
| 4559 | device = map->stripes[i].dev; |
| 4560 | dev_offset = map->stripes[i].physical; |
| 4561 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4562 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4563 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4564 | goto out; |
| 4565 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4566 | chunk_root->root_key.objectid, |
| 4567 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4568 | chunk_offset, dev_offset, |
| 4569 | stripe_size); |
| 4570 | if (ret) |
| 4571 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4572 | } |
| 4573 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4574 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4575 | for (i = 0; i < map->num_stripes; i++) { |
| 4576 | device = map->stripes[i].dev; |
| 4577 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4578 | |
| 4579 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4580 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4581 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4582 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4586 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4587 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4588 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4589 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4590 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4591 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4592 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4593 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4594 | |
| 4595 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4596 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4597 | key.offset = chunk_offset; |
| 4598 | |
| 4599 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4600 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4601 | /* |
| 4602 | * TODO: Cleanup of inserted chunk root in case of |
| 4603 | * failure. |
| 4604 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4605 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4606 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4607 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4608 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4609 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4610 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4611 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4612 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | /* |
| 4616 | * Chunk allocation falls into two parts. The first part does works |
| 4617 | * that make the new allocated chunk useable, but not do any operation |
| 4618 | * that modifies the chunk tree. The second part does the works that |
| 4619 | * require modifying the chunk tree. This division is important for the |
| 4620 | * bootstrap process of adding storage to a seed btrfs. |
| 4621 | */ |
| 4622 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4623 | struct btrfs_root *extent_root, u64 type) |
| 4624 | { |
| 4625 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4626 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4627 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4628 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4629 | } |
| 4630 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4631 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4632 | struct btrfs_root *root, |
| 4633 | struct btrfs_device *device) |
| 4634 | { |
| 4635 | u64 chunk_offset; |
| 4636 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4637 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4638 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4639 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4640 | int ret; |
| 4641 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4642 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4643 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4644 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4645 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4646 | if (ret) |
| 4647 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4648 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4649 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4650 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4651 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4652 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4653 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4654 | } |
| 4655 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4656 | static inline int btrfs_chunk_max_errors(struct map_lookup *map) |
| 4657 | { |
| 4658 | int max_errors; |
| 4659 | |
| 4660 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4661 | BTRFS_BLOCK_GROUP_RAID10 | |
| 4662 | BTRFS_BLOCK_GROUP_RAID5 | |
| 4663 | BTRFS_BLOCK_GROUP_DUP)) { |
| 4664 | max_errors = 1; |
| 4665 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4666 | max_errors = 2; |
| 4667 | } else { |
| 4668 | max_errors = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4669 | } |
| 4670 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4671 | return max_errors; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4675 | { |
| 4676 | struct extent_map *em; |
| 4677 | struct map_lookup *map; |
| 4678 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4679 | int readonly = 0; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4680 | int miss_ndevs = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4681 | int i; |
| 4682 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4683 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4684 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4685 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4686 | if (!em) |
| 4687 | return 1; |
| 4688 | |
| 4689 | map = (struct map_lookup *)em->bdev; |
| 4690 | for (i = 0; i < map->num_stripes; i++) { |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4691 | if (map->stripes[i].dev->missing) { |
| 4692 | miss_ndevs++; |
| 4693 | continue; |
| 4694 | } |
| 4695 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4696 | if (!map->stripes[i].dev->writeable) { |
| 4697 | readonly = 1; |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4698 | goto end; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4699 | } |
| 4700 | } |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 4701 | |
| 4702 | /* |
| 4703 | * If the number of missing devices is larger than max errors, |
| 4704 | * we can not write the data into that chunk successfully, so |
| 4705 | * set it readonly. |
| 4706 | */ |
| 4707 | if (miss_ndevs > btrfs_chunk_max_errors(map)) |
| 4708 | readonly = 1; |
| 4709 | end: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4710 | free_extent_map(em); |
| 4711 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4715 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4716 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4720 | { |
| 4721 | struct extent_map *em; |
| 4722 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4723 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4724 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4725 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4726 | if (em) |
| 4727 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4728 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4729 | if (!em) |
| 4730 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4731 | /* once for us */ |
| 4732 | free_extent_map(em); |
| 4733 | /* once for the tree */ |
| 4734 | free_extent_map(em); |
| 4735 | } |
| 4736 | } |
| 4737 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4738 | 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] | 4739 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4740 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4741 | struct extent_map *em; |
| 4742 | struct map_lookup *map; |
| 4743 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4744 | int ret; |
| 4745 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4746 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4747 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4748 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4749 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4750 | /* |
| 4751 | * We could return errors for these cases, but that could get ugly and |
| 4752 | * we'd probably do the same thing which is just not do anything else |
| 4753 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4754 | */ |
| 4755 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4756 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4757 | logical+len); |
| 4758 | return 1; |
| 4759 | } |
| 4760 | |
| 4761 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4762 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4763 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4764 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4765 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4766 | return 1; |
| 4767 | } |
| 4768 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4769 | map = (struct map_lookup *)em->bdev; |
| 4770 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4771 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4772 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4773 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4774 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4775 | ret = 2; |
| 4776 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4777 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4778 | else |
| 4779 | ret = 1; |
| 4780 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4781 | |
| 4782 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4783 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4784 | ret++; |
| 4785 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4786 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4787 | return ret; |
| 4788 | } |
| 4789 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4790 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4791 | struct btrfs_mapping_tree *map_tree, |
| 4792 | u64 logical) |
| 4793 | { |
| 4794 | struct extent_map *em; |
| 4795 | struct map_lookup *map; |
| 4796 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4797 | unsigned long len = root->sectorsize; |
| 4798 | |
| 4799 | read_lock(&em_tree->lock); |
| 4800 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4801 | read_unlock(&em_tree->lock); |
| 4802 | BUG_ON(!em); |
| 4803 | |
| 4804 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4805 | map = (struct map_lookup *)em->bdev; |
| 4806 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4807 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4808 | len = map->stripe_len * nr_data_stripes(map); |
| 4809 | } |
| 4810 | free_extent_map(em); |
| 4811 | return len; |
| 4812 | } |
| 4813 | |
| 4814 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4815 | u64 logical, u64 len, int mirror_num) |
| 4816 | { |
| 4817 | struct extent_map *em; |
| 4818 | struct map_lookup *map; |
| 4819 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4820 | int ret = 0; |
| 4821 | |
| 4822 | read_lock(&em_tree->lock); |
| 4823 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4824 | read_unlock(&em_tree->lock); |
| 4825 | BUG_ON(!em); |
| 4826 | |
| 4827 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4828 | map = (struct map_lookup *)em->bdev; |
| 4829 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4830 | BTRFS_BLOCK_GROUP_RAID6)) |
| 4831 | ret = 1; |
| 4832 | free_extent_map(em); |
| 4833 | return ret; |
| 4834 | } |
| 4835 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4836 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4837 | struct map_lookup *map, int first, int num, |
| 4838 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4839 | { |
| 4840 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4841 | int tolerance; |
| 4842 | struct btrfs_device *srcdev; |
| 4843 | |
| 4844 | if (dev_replace_is_ongoing && |
| 4845 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4846 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4847 | srcdev = fs_info->dev_replace.srcdev; |
| 4848 | else |
| 4849 | srcdev = NULL; |
| 4850 | |
| 4851 | /* |
| 4852 | * try to avoid the drive that is the source drive for a |
| 4853 | * dev-replace procedure, only choose it if no other non-missing |
| 4854 | * mirror is available |
| 4855 | */ |
| 4856 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4857 | if (map->stripes[optimal].dev->bdev && |
| 4858 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4859 | return optimal; |
| 4860 | for (i = first; i < first + num; i++) { |
| 4861 | if (map->stripes[i].dev->bdev && |
| 4862 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4863 | return i; |
| 4864 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4865 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4866 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4867 | /* we couldn't find one that doesn't fail. Just return something |
| 4868 | * and the io error handling code will clean up eventually |
| 4869 | */ |
| 4870 | return optimal; |
| 4871 | } |
| 4872 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4873 | static inline int parity_smaller(u64 a, u64 b) |
| 4874 | { |
| 4875 | return a > b; |
| 4876 | } |
| 4877 | |
| 4878 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
| 4879 | static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map) |
| 4880 | { |
| 4881 | struct btrfs_bio_stripe s; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4882 | int real_stripes = bbio->num_stripes - bbio->num_tgtdevs; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4883 | int i; |
| 4884 | u64 l; |
| 4885 | int again = 1; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4886 | int m; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4887 | |
| 4888 | while (again) { |
| 4889 | again = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4890 | for (i = 0; i < real_stripes - 1; i++) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4891 | if (parity_smaller(raid_map[i], raid_map[i+1])) { |
| 4892 | s = bbio->stripes[i]; |
| 4893 | l = raid_map[i]; |
| 4894 | bbio->stripes[i] = bbio->stripes[i+1]; |
| 4895 | raid_map[i] = raid_map[i+1]; |
| 4896 | bbio->stripes[i+1] = s; |
| 4897 | raid_map[i+1] = l; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4898 | |
| 4899 | if (bbio->tgtdev_map) { |
| 4900 | m = bbio->tgtdev_map[i]; |
| 4901 | bbio->tgtdev_map[i] = |
| 4902 | bbio->tgtdev_map[i + 1]; |
| 4903 | bbio->tgtdev_map[i + 1] = m; |
| 4904 | } |
| 4905 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4906 | again = 1; |
| 4907 | } |
| 4908 | } |
| 4909 | } |
| 4910 | } |
| 4911 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4912 | 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] | 4913 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4914 | struct btrfs_bio **bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4915 | int mirror_num, u64 **raid_map_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4916 | { |
| 4917 | struct extent_map *em; |
| 4918 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4919 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4920 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4921 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4922 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4923 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4924 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4925 | u64 stripe_nr_orig; |
| 4926 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4927 | u64 stripe_len; |
| 4928 | u64 *raid_map = NULL; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4929 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4930 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4931 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4932 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4933 | int max_errors = 0; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 4934 | int tgtdev_indexes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4935 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4936 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4937 | int dev_replace_is_ongoing = 0; |
| 4938 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4939 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4940 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4941 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4942 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4943 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4944 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4945 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4946 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4947 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4948 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4949 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4950 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4951 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4952 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4953 | if (em->start > logical || em->start + em->len < logical) { |
| 4954 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4955 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4956 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4957 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4958 | return -EINVAL; |
| 4959 | } |
| 4960 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4961 | map = (struct map_lookup *)em->bdev; |
| 4962 | offset = logical - em->start; |
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_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4965 | stripe_nr = offset; |
| 4966 | /* |
| 4967 | * stripe_nr counts the total number of stripes we have to stride |
| 4968 | * to get to this block |
| 4969 | */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4970 | do_div(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4971 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4972 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4973 | BUG_ON(offset < stripe_offset); |
| 4974 | |
| 4975 | /* stripe_offset is the offset of this block in its stripe*/ |
| 4976 | stripe_offset = offset - stripe_offset; |
| 4977 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4978 | /* if we're here for raid56, we need to know the stripe aligned start */ |
| 4979 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4980 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 4981 | raid56_full_stripe_start = offset; |
| 4982 | |
| 4983 | /* allow a write of a full stripe, but make sure we don't |
| 4984 | * allow straddling of stripes |
| 4985 | */ |
| 4986 | do_div(raid56_full_stripe_start, full_stripe_len); |
| 4987 | raid56_full_stripe_start *= full_stripe_len; |
| 4988 | } |
| 4989 | |
| 4990 | if (rw & REQ_DISCARD) { |
| 4991 | /* we don't discard raid56 yet */ |
| 4992 | if (map->type & |
| 4993 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4994 | ret = -EOPNOTSUPP; |
| 4995 | goto out; |
| 4996 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4997 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4998 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 4999 | u64 max_len; |
| 5000 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 5001 | For other RAID types and for RAID[56] reads, just allow a single |
| 5002 | stripe (on a single disk). */ |
| 5003 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) && |
| 5004 | (rw & REQ_WRITE)) { |
| 5005 | max_len = stripe_len * nr_data_stripes(map) - |
| 5006 | (offset - raid56_full_stripe_start); |
| 5007 | } else { |
| 5008 | /* we limit the length of each bio to what fits in a stripe */ |
| 5009 | max_len = stripe_len - stripe_offset; |
| 5010 | } |
| 5011 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5012 | } else { |
| 5013 | *length = em->len - offset; |
| 5014 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5015 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5016 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 5017 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5018 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5019 | goto out; |
| 5020 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5021 | btrfs_dev_replace_lock(dev_replace); |
| 5022 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 5023 | if (!dev_replace_is_ongoing) |
| 5024 | btrfs_dev_replace_unlock(dev_replace); |
| 5025 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5026 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 5027 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 5028 | dev_replace->tgtdev != NULL) { |
| 5029 | /* |
| 5030 | * in dev-replace case, for repair case (that's the only |
| 5031 | * case where the mirror is selected explicitly when |
| 5032 | * calling btrfs_map_block), blocks left of the left cursor |
| 5033 | * can also be read from the target drive. |
| 5034 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 5035 | * the last one to the array of stripes. For READ, it also |
| 5036 | * needs to be supported using the same mirror number. |
| 5037 | * If the requested block is not left of the left cursor, |
| 5038 | * EIO is returned. This can happen because btrfs_num_copies() |
| 5039 | * returns one more in the dev-replace case. |
| 5040 | */ |
| 5041 | u64 tmp_length = *length; |
| 5042 | struct btrfs_bio *tmp_bbio = NULL; |
| 5043 | int tmp_num_stripes; |
| 5044 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5045 | int index_srcdev = 0; |
| 5046 | int found = 0; |
| 5047 | u64 physical_of_found = 0; |
| 5048 | |
| 5049 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5050 | logical, &tmp_length, &tmp_bbio, 0, NULL); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5051 | if (ret) { |
| 5052 | WARN_ON(tmp_bbio != NULL); |
| 5053 | goto out; |
| 5054 | } |
| 5055 | |
| 5056 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 5057 | if (mirror_num > tmp_num_stripes) { |
| 5058 | /* |
| 5059 | * REQ_GET_READ_MIRRORS does not contain this |
| 5060 | * mirror, that means that the requested area |
| 5061 | * is not left of the left cursor |
| 5062 | */ |
| 5063 | ret = -EIO; |
| 5064 | kfree(tmp_bbio); |
| 5065 | goto out; |
| 5066 | } |
| 5067 | |
| 5068 | /* |
| 5069 | * process the rest of the function using the mirror_num |
| 5070 | * of the source drive. Therefore look it up first. |
| 5071 | * At the end, patch the device pointer to the one of the |
| 5072 | * target drive. |
| 5073 | */ |
| 5074 | for (i = 0; i < tmp_num_stripes; i++) { |
| 5075 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5076 | /* |
| 5077 | * In case of DUP, in order to keep it |
| 5078 | * simple, only add the mirror with the |
| 5079 | * lowest physical address |
| 5080 | */ |
| 5081 | if (found && |
| 5082 | physical_of_found <= |
| 5083 | tmp_bbio->stripes[i].physical) |
| 5084 | continue; |
| 5085 | index_srcdev = i; |
| 5086 | found = 1; |
| 5087 | physical_of_found = |
| 5088 | tmp_bbio->stripes[i].physical; |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | if (found) { |
| 5093 | mirror_num = index_srcdev + 1; |
| 5094 | patch_the_first_stripe_for_dev_replace = 1; |
| 5095 | physical_to_patch_in_first_stripe = physical_of_found; |
| 5096 | } else { |
| 5097 | WARN_ON(1); |
| 5098 | ret = -EIO; |
| 5099 | kfree(tmp_bbio); |
| 5100 | goto out; |
| 5101 | } |
| 5102 | |
| 5103 | kfree(tmp_bbio); |
| 5104 | } else if (mirror_num > map->num_stripes) { |
| 5105 | mirror_num = 0; |
| 5106 | } |
| 5107 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5108 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5109 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5110 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 5111 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5112 | do_div(stripe_nr_end, map->stripe_len); |
| 5113 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 5114 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5115 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5116 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5117 | if (rw & REQ_DISCARD) |
| 5118 | num_stripes = min_t(u64, map->num_stripes, |
| 5119 | stripe_nr_end - stripe_nr_orig); |
| 5120 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5121 | if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))) |
| 5122 | mirror_num = 1; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5123 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5124 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5125 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5126 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5127 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5128 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5129 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5130 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5131 | current->pid % map->num_stripes, |
| 5132 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5133 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5134 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5135 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5136 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5137 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5138 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5139 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5140 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5141 | } else { |
| 5142 | mirror_num = 1; |
| 5143 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 5144 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5145 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5146 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5147 | |
| 5148 | stripe_index = do_div(stripe_nr, factor); |
| 5149 | stripe_index *= map->sub_stripes; |
| 5150 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5151 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5152 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5153 | else if (rw & REQ_DISCARD) |
| 5154 | num_stripes = min_t(u64, map->sub_stripes * |
| 5155 | (stripe_nr_end - stripe_nr_orig), |
| 5156 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5157 | else if (mirror_num) |
| 5158 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5159 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5160 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5161 | stripe_index = find_live_mirror(fs_info, map, |
| 5162 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5163 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 5164 | current->pid % map->sub_stripes, |
| 5165 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 5166 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5167 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5168 | |
| 5169 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5170 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5171 | u64 tmp; |
| 5172 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5173 | if (raid_map_ret && |
| 5174 | ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) || |
| 5175 | mirror_num > 1)) { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5176 | int i, rot; |
| 5177 | |
| 5178 | /* push stripe_nr back to the start of the full stripe */ |
| 5179 | stripe_nr = raid56_full_stripe_start; |
Zhao Lei | 6de6565 | 2014-11-13 11:45:40 +0800 | [diff] [blame] | 5180 | do_div(stripe_nr, stripe_len * nr_data_stripes(map)); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5181 | |
| 5182 | /* RAID[56] write or recovery. Return all stripes */ |
| 5183 | num_stripes = map->num_stripes; |
| 5184 | max_errors = nr_parity_stripes(map); |
| 5185 | |
Dulshani Gunawardhana | d9b0d9b | 2013-10-31 10:32:18 +0530 | [diff] [blame] | 5186 | raid_map = kmalloc_array(num_stripes, sizeof(u64), |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5187 | GFP_NOFS); |
| 5188 | if (!raid_map) { |
| 5189 | ret = -ENOMEM; |
| 5190 | goto out; |
| 5191 | } |
| 5192 | |
| 5193 | /* Work out the disk rotation on this stripe-set */ |
| 5194 | tmp = stripe_nr; |
| 5195 | rot = do_div(tmp, num_stripes); |
| 5196 | |
| 5197 | /* Fill in the logical address of each stripe */ |
| 5198 | tmp = stripe_nr * nr_data_stripes(map); |
| 5199 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5200 | raid_map[(i+rot) % num_stripes] = |
| 5201 | em->start + (tmp + i) * map->stripe_len; |
| 5202 | |
| 5203 | raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5204 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5205 | raid_map[(i+rot+1) % num_stripes] = |
| 5206 | RAID6_Q_STRIPE; |
| 5207 | |
| 5208 | *length = map->stripe_len; |
| 5209 | stripe_index = 0; |
| 5210 | stripe_offset = 0; |
| 5211 | } else { |
| 5212 | /* |
| 5213 | * Mirror #0 or #1 means the original data block. |
| 5214 | * Mirror #2 is RAID5 parity block. |
| 5215 | * Mirror #3 is RAID6 Q block. |
| 5216 | */ |
| 5217 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5218 | if (mirror_num > 1) |
| 5219 | stripe_index = nr_data_stripes(map) + |
| 5220 | mirror_num - 2; |
| 5221 | |
| 5222 | /* We distribute the parity blocks across stripes */ |
| 5223 | tmp = stripe_nr + stripe_index; |
| 5224 | stripe_index = do_div(tmp, map->num_stripes); |
Miao Xie | 28e1cc7 | 2014-09-12 18:44:02 +0800 | [diff] [blame] | 5225 | if (!(rw & (REQ_WRITE | REQ_DISCARD | |
| 5226 | REQ_GET_READ_MIRRORS)) && mirror_num <= 1) |
| 5227 | mirror_num = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5228 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5229 | } else { |
| 5230 | /* |
| 5231 | * after this do_div call, stripe_nr is the number of stripes |
| 5232 | * on this device we have to walk to find the data, and |
| 5233 | * stripe_index is the number of our device in the stripe array |
| 5234 | */ |
| 5235 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5236 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5237 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5238 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5239 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5240 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5241 | if (dev_replace_is_ongoing) { |
| 5242 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5243 | num_alloc_stripes <<= 1; |
| 5244 | if (rw & REQ_GET_READ_MIRRORS) |
| 5245 | num_alloc_stripes++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5246 | tgtdev_indexes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5247 | } |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5248 | |
| 5249 | bbio = kzalloc(btrfs_bio_size(num_alloc_stripes, tgtdev_indexes), |
| 5250 | GFP_NOFS); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5251 | if (!bbio) { |
Dave Jones | eb2067f | 2013-07-30 13:42:17 -0400 | [diff] [blame] | 5252 | kfree(raid_map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5253 | ret = -ENOMEM; |
| 5254 | goto out; |
| 5255 | } |
| 5256 | atomic_set(&bbio->error, 0); |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5257 | if (dev_replace_is_ongoing) |
| 5258 | bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5259 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5260 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5261 | int factor = 0; |
| 5262 | int sub_stripes = 0; |
| 5263 | u64 stripes_per_dev = 0; |
| 5264 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5265 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5266 | |
| 5267 | if (map->type & |
| 5268 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5269 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5270 | sub_stripes = 1; |
| 5271 | else |
| 5272 | sub_stripes = map->sub_stripes; |
| 5273 | |
| 5274 | factor = map->num_stripes / sub_stripes; |
| 5275 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5276 | stripe_nr_orig, |
| 5277 | factor, |
| 5278 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5279 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5280 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5281 | } |
| 5282 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5283 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5284 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5285 | map->stripes[stripe_index].physical + |
| 5286 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5287 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5288 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5289 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5290 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5291 | bbio->stripes[i].length = stripes_per_dev * |
| 5292 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5293 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5294 | if (i / sub_stripes < remaining_stripes) |
| 5295 | bbio->stripes[i].length += |
| 5296 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5297 | |
| 5298 | /* |
| 5299 | * Special for the first stripe and |
| 5300 | * the last stripe: |
| 5301 | * |
| 5302 | * |-------|...|-------| |
| 5303 | * |----------| |
| 5304 | * off end_off |
| 5305 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5306 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5307 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5308 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5309 | |
| 5310 | if (stripe_index >= last_stripe && |
| 5311 | stripe_index <= (last_stripe + |
| 5312 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5313 | bbio->stripes[i].length -= |
| 5314 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5315 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5316 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5317 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5318 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5319 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5320 | |
| 5321 | stripe_index++; |
| 5322 | if (stripe_index == map->num_stripes) { |
| 5323 | /* This could only happen for RAID0/10 */ |
| 5324 | stripe_index = 0; |
| 5325 | stripe_nr++; |
| 5326 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5327 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5328 | } else { |
| 5329 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5330 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5331 | map->stripes[stripe_index].physical + |
| 5332 | stripe_offset + |
| 5333 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5334 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5335 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5336 | stripe_index++; |
| 5337 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5338 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5339 | |
Miao Xie | d20983b | 2014-07-03 18:22:13 +0800 | [diff] [blame] | 5340 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
| 5341 | max_errors = btrfs_chunk_max_errors(map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5342 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5343 | tgtdev_indexes = 0; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5344 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5345 | dev_replace->tgtdev != NULL) { |
| 5346 | int index_where_to_add; |
| 5347 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5348 | |
| 5349 | /* |
| 5350 | * duplicate the write operations while the dev replace |
| 5351 | * procedure is running. Since the copying of the old disk |
| 5352 | * to the new disk takes place at run time while the |
| 5353 | * filesystem is mounted writable, the regular write |
| 5354 | * operations to the old disk have to be duplicated to go |
| 5355 | * to the new disk as well. |
| 5356 | * Note that device->missing is handled by the caller, and |
| 5357 | * that the write to the old disk is already set up in the |
| 5358 | * stripes array. |
| 5359 | */ |
| 5360 | index_where_to_add = num_stripes; |
| 5361 | for (i = 0; i < num_stripes; i++) { |
| 5362 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5363 | /* write to new disk, too */ |
| 5364 | struct btrfs_bio_stripe *new = |
| 5365 | bbio->stripes + index_where_to_add; |
| 5366 | struct btrfs_bio_stripe *old = |
| 5367 | bbio->stripes + i; |
| 5368 | |
| 5369 | new->physical = old->physical; |
| 5370 | new->length = old->length; |
| 5371 | new->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5372 | bbio->tgtdev_map[i] = index_where_to_add; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5373 | index_where_to_add++; |
| 5374 | max_errors++; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5375 | tgtdev_indexes++; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5376 | } |
| 5377 | } |
| 5378 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5379 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5380 | dev_replace->tgtdev != NULL) { |
| 5381 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5382 | int index_srcdev = 0; |
| 5383 | int found = 0; |
| 5384 | u64 physical_of_found = 0; |
| 5385 | |
| 5386 | /* |
| 5387 | * During the dev-replace procedure, the target drive can |
| 5388 | * also be used to read data in case it is needed to repair |
| 5389 | * a corrupt block elsewhere. This is possible if the |
| 5390 | * requested area is left of the left cursor. In this area, |
| 5391 | * the target drive is a full copy of the source drive. |
| 5392 | */ |
| 5393 | for (i = 0; i < num_stripes; i++) { |
| 5394 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5395 | /* |
| 5396 | * In case of DUP, in order to keep it |
| 5397 | * simple, only add the mirror with the |
| 5398 | * lowest physical address |
| 5399 | */ |
| 5400 | if (found && |
| 5401 | physical_of_found <= |
| 5402 | bbio->stripes[i].physical) |
| 5403 | continue; |
| 5404 | index_srcdev = i; |
| 5405 | found = 1; |
| 5406 | physical_of_found = bbio->stripes[i].physical; |
| 5407 | } |
| 5408 | } |
| 5409 | if (found) { |
| 5410 | u64 length = map->stripe_len; |
| 5411 | |
| 5412 | if (physical_of_found + length <= |
| 5413 | dev_replace->cursor_left) { |
| 5414 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5415 | bbio->stripes + num_stripes; |
| 5416 | |
| 5417 | tgtdev_stripe->physical = physical_of_found; |
| 5418 | tgtdev_stripe->length = |
| 5419 | bbio->stripes[index_srcdev].length; |
| 5420 | tgtdev_stripe->dev = dev_replace->tgtdev; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5421 | bbio->tgtdev_map[index_srcdev] = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5422 | |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5423 | tgtdev_indexes++; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5424 | num_stripes++; |
| 5425 | } |
| 5426 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5427 | } |
| 5428 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5429 | *bbio_ret = bbio; |
| 5430 | bbio->num_stripes = num_stripes; |
| 5431 | bbio->max_errors = max_errors; |
| 5432 | bbio->mirror_num = mirror_num; |
Miao Xie | 2c8cdd6 | 2014-11-14 16:06:25 +0800 | [diff] [blame] | 5433 | bbio->num_tgtdevs = tgtdev_indexes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5434 | |
| 5435 | /* |
| 5436 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5437 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5438 | * available as a mirror |
| 5439 | */ |
| 5440 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5441 | WARN_ON(num_stripes > 1); |
| 5442 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5443 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5444 | bbio->mirror_num = map->num_stripes + 1; |
| 5445 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5446 | if (raid_map) { |
| 5447 | sort_parity_stripes(bbio, raid_map); |
| 5448 | *raid_map_ret = raid_map; |
| 5449 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5450 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5451 | if (dev_replace_is_ongoing) |
| 5452 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5453 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5454 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5455 | } |
| 5456 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5457 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5458 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5459 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5460 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5461 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5462 | mirror_num, NULL); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5463 | } |
| 5464 | |
Miao Xie | af8e2d1 | 2014-10-23 14:42:50 +0800 | [diff] [blame] | 5465 | /* For Scrub/replace */ |
| 5466 | int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw, |
| 5467 | u64 logical, u64 *length, |
| 5468 | struct btrfs_bio **bbio_ret, int mirror_num, |
| 5469 | u64 **raid_map_ret) |
| 5470 | { |
| 5471 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
| 5472 | mirror_num, raid_map_ret); |
| 5473 | } |
| 5474 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5475 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5476 | u64 chunk_start, u64 physical, u64 devid, |
| 5477 | u64 **logical, int *naddrs, int *stripe_len) |
| 5478 | { |
| 5479 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5480 | struct extent_map *em; |
| 5481 | struct map_lookup *map; |
| 5482 | u64 *buf; |
| 5483 | u64 bytenr; |
| 5484 | u64 length; |
| 5485 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5486 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5487 | int i, j, nr = 0; |
| 5488 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5489 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5490 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5491 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5492 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5493 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5494 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5495 | chunk_start); |
| 5496 | return -EIO; |
| 5497 | } |
| 5498 | |
| 5499 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5500 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5501 | em->start, chunk_start); |
| 5502 | free_extent_map(em); |
| 5503 | return -EIO; |
| 5504 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5505 | map = (struct map_lookup *)em->bdev; |
| 5506 | |
| 5507 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5508 | rmap_len = map->stripe_len; |
| 5509 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5510 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5511 | do_div(length, map->num_stripes / map->sub_stripes); |
| 5512 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5513 | do_div(length, map->num_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5514 | else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5515 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5516 | do_div(length, nr_data_stripes(map)); |
| 5517 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5518 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5519 | |
| 5520 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5521 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5522 | |
| 5523 | for (i = 0; i < map->num_stripes; i++) { |
| 5524 | if (devid && map->stripes[i].dev->devid != devid) |
| 5525 | continue; |
| 5526 | if (map->stripes[i].physical > physical || |
| 5527 | map->stripes[i].physical + length <= physical) |
| 5528 | continue; |
| 5529 | |
| 5530 | stripe_nr = physical - map->stripes[i].physical; |
| 5531 | do_div(stripe_nr, map->stripe_len); |
| 5532 | |
| 5533 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5534 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 5535 | do_div(stripe_nr, map->sub_stripes); |
| 5536 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5537 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5538 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5539 | * Alternatively, just use rmap_len below instead of |
| 5540 | * map->stripe_len */ |
| 5541 | |
| 5542 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5543 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5544 | for (j = 0; j < nr; j++) { |
| 5545 | if (buf[j] == bytenr) |
| 5546 | break; |
| 5547 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5548 | if (j == nr) { |
| 5549 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5550 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5551 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5552 | } |
| 5553 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5554 | *logical = buf; |
| 5555 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5556 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5557 | |
| 5558 | free_extent_map(em); |
| 5559 | return 0; |
| 5560 | } |
| 5561 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5562 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5563 | { |
| 5564 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5565 | bio_endio_nodec(bio, err); |
| 5566 | else |
| 5567 | bio_endio(bio, err); |
| 5568 | kfree(bbio); |
| 5569 | } |
| 5570 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5571 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5572 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5573 | struct btrfs_bio *bbio = bio->bi_private; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5574 | struct btrfs_device *dev = bbio->stripes[0].dev; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5575 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5576 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5577 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5578 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5579 | if (err == -EIO || err == -EREMOTEIO) { |
| 5580 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5581 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5582 | |
| 5583 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5584 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5585 | if (dev->bdev) { |
| 5586 | if (bio->bi_rw & WRITE) |
| 5587 | btrfs_dev_stat_inc(dev, |
| 5588 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5589 | else |
| 5590 | btrfs_dev_stat_inc(dev, |
| 5591 | BTRFS_DEV_STAT_READ_ERRS); |
| 5592 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5593 | btrfs_dev_stat_inc(dev, |
| 5594 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5595 | btrfs_dev_stat_print_on_error(dev); |
| 5596 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5597 | } |
| 5598 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5599 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5600 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5601 | is_orig_bio = 1; |
| 5602 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5603 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5604 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5605 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5606 | if (!is_orig_bio) { |
| 5607 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5608 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5609 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5610 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5611 | bio->bi_private = bbio->private; |
| 5612 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5613 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5614 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5615 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5616 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5617 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5618 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5619 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5620 | /* |
| 5621 | * this bio is actually up to date, we didn't |
| 5622 | * go over the max number of errors |
| 5623 | */ |
| 5624 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5625 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5626 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5627 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5628 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5629 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5630 | bio_put(bio); |
| 5631 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5632 | } |
| 5633 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5634 | /* |
| 5635 | * see run_scheduled_bios for a description of why bios are collected for |
| 5636 | * async submit. |
| 5637 | * |
| 5638 | * This will add one bio to the pending list for a device and make sure |
| 5639 | * the work struct is scheduled. |
| 5640 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5641 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5642 | struct btrfs_device *device, |
| 5643 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5644 | { |
| 5645 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5646 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5647 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5648 | if (device->missing || !device->bdev) { |
| 5649 | bio_endio(bio, -EIO); |
| 5650 | return; |
| 5651 | } |
| 5652 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5653 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5654 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5655 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5656 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5657 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5658 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5659 | } |
| 5660 | |
| 5661 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5662 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5663 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5664 | * made progress against dirty pages when we've really just put it |
| 5665 | * on a queue for later |
| 5666 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5667 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5668 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5669 | bio->bi_next = NULL; |
| 5670 | bio->bi_rw |= rw; |
| 5671 | |
| 5672 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5673 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5674 | pending_bios = &device->pending_sync_bios; |
| 5675 | else |
| 5676 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5677 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5678 | if (pending_bios->tail) |
| 5679 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5680 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5681 | pending_bios->tail = bio; |
| 5682 | if (!pending_bios->head) |
| 5683 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5684 | if (device->running_pending) |
| 5685 | should_queue = 0; |
| 5686 | |
| 5687 | spin_unlock(&device->io_lock); |
| 5688 | |
| 5689 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5690 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5691 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5692 | } |
| 5693 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5694 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5695 | sector_t sector) |
| 5696 | { |
| 5697 | struct bio_vec *prev; |
| 5698 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5699 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5700 | struct bvec_merge_data bvm = { |
| 5701 | .bi_bdev = bdev, |
| 5702 | .bi_sector = sector, |
| 5703 | .bi_rw = bio->bi_rw, |
| 5704 | }; |
| 5705 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5706 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5707 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5708 | |
| 5709 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5710 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5711 | return 0; |
| 5712 | |
| 5713 | if (!q->merge_bvec_fn) |
| 5714 | return 1; |
| 5715 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5716 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5717 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5718 | return 0; |
| 5719 | return 1; |
| 5720 | } |
| 5721 | |
| 5722 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5723 | struct bio *bio, u64 physical, int dev_nr, |
| 5724 | int rw, int async) |
| 5725 | { |
| 5726 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5727 | |
| 5728 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5729 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5730 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5731 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5732 | #ifdef DEBUG |
| 5733 | { |
| 5734 | struct rcu_string *name; |
| 5735 | |
| 5736 | rcu_read_lock(); |
| 5737 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5738 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5739 | "(%s id %llu), size=%u\n", rw, |
Fabian Frederick | 1b6e446 | 2014-09-24 20:23:05 +0200 | [diff] [blame] | 5740 | (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev, |
| 5741 | name->str, dev->devid, bio->bi_iter.bi_size); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5742 | rcu_read_unlock(); |
| 5743 | } |
| 5744 | #endif |
| 5745 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5746 | |
| 5747 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5748 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5749 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5750 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5751 | else |
| 5752 | btrfsic_submit_bio(rw, bio); |
| 5753 | } |
| 5754 | |
| 5755 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5756 | struct bio *first_bio, struct btrfs_device *dev, |
| 5757 | int dev_nr, int rw, int async) |
| 5758 | { |
| 5759 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5760 | struct bio *bio; |
| 5761 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5762 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5763 | |
| 5764 | again: |
| 5765 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5766 | if (!bio) |
| 5767 | return -ENOMEM; |
| 5768 | |
| 5769 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5770 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5771 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5772 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5773 | |
| 5774 | atomic_inc(&bbio->stripes_pending); |
| 5775 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5776 | rw, async); |
| 5777 | physical += len; |
| 5778 | goto again; |
| 5779 | } |
| 5780 | bvec++; |
| 5781 | } |
| 5782 | |
| 5783 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5784 | return 0; |
| 5785 | } |
| 5786 | |
| 5787 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5788 | { |
| 5789 | atomic_inc(&bbio->error); |
| 5790 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5791 | /* Shoud be the original bio. */ |
| 5792 | WARN_ON(bio != bbio->orig_bio); |
| 5793 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5794 | bio->bi_private = bbio->private; |
| 5795 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5796 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5797 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5798 | |
| 5799 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5800 | } |
| 5801 | } |
| 5802 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5803 | 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] | 5804 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5805 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5806 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5807 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5808 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5809 | u64 length = 0; |
| 5810 | u64 map_length; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5811 | u64 *raid_map = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5812 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5813 | int dev_nr = 0; |
| 5814 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5815 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5816 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5817 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5818 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5819 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5820 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5821 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
| 5822 | mirror_num, &raid_map); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5823 | if (ret) { |
| 5824 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5825 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5826 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5827 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5828 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5829 | bbio->orig_bio = first_bio; |
| 5830 | bbio->private = first_bio->bi_private; |
| 5831 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5832 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5833 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5834 | |
| 5835 | if (raid_map) { |
| 5836 | /* In this case, map_length has been set to the length of |
| 5837 | a single stripe; not the whole write */ |
| 5838 | if (rw & WRITE) { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5839 | ret = raid56_parity_write(root, bio, bbio, |
| 5840 | raid_map, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5841 | } else { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5842 | ret = raid56_parity_recover(root, bio, bbio, |
| 5843 | raid_map, map_length, |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5844 | mirror_num, 1); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5845 | } |
Miao Xie | 4245215 | 2014-11-25 16:39:28 +0800 | [diff] [blame] | 5846 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5847 | btrfs_bio_counter_dec(root->fs_info); |
| 5848 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5849 | } |
| 5850 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5851 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5852 | 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] | 5853 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5854 | BUG(); |
| 5855 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5856 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5857 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5858 | dev = bbio->stripes[dev_nr].dev; |
| 5859 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5860 | bbio_error(bbio, first_bio, logical); |
| 5861 | dev_nr++; |
| 5862 | continue; |
| 5863 | } |
| 5864 | |
| 5865 | /* |
| 5866 | * Check and see if we're ok with this bio based on it's size |
| 5867 | * and offset with the given device. |
| 5868 | */ |
| 5869 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5870 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5871 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5872 | dev_nr, rw, async_submit); |
| 5873 | BUG_ON(ret); |
| 5874 | dev_nr++; |
| 5875 | continue; |
| 5876 | } |
| 5877 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5878 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5879 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5880 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5881 | } else { |
| 5882 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5883 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5884 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5885 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5886 | submit_stripe_bio(root, bbio, bio, |
| 5887 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5888 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5889 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5890 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5891 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5892 | return 0; |
| 5893 | } |
| 5894 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5895 | 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] | 5896 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5897 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5898 | struct btrfs_device *device; |
| 5899 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5900 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5901 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5902 | while (cur_devices) { |
| 5903 | if (!fsid || |
| 5904 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5905 | device = __find_device(&cur_devices->devices, |
| 5906 | devid, uuid); |
| 5907 | if (device) |
| 5908 | return device; |
| 5909 | } |
| 5910 | cur_devices = cur_devices->seed; |
| 5911 | } |
| 5912 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5913 | } |
| 5914 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5915 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 5916 | struct btrfs_fs_devices *fs_devices, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5917 | u64 devid, u8 *dev_uuid) |
| 5918 | { |
| 5919 | struct btrfs_device *device; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5920 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5921 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 5922 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5923 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5924 | |
| 5925 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5926 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5927 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5928 | |
| 5929 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5930 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5931 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5932 | return device; |
| 5933 | } |
| 5934 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5935 | /** |
| 5936 | * btrfs_alloc_device - allocate struct btrfs_device |
| 5937 | * @fs_info: used only for generating a new devid, can be NULL if |
| 5938 | * devid is provided (i.e. @devid != NULL). |
| 5939 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 5940 | * is generated. |
| 5941 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 5942 | * is generated. |
| 5943 | * |
| 5944 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 5945 | * on error. Returned struct is not linked onto any lists and can be |
| 5946 | * destroyed with kfree() right away. |
| 5947 | */ |
| 5948 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 5949 | const u64 *devid, |
| 5950 | const u8 *uuid) |
| 5951 | { |
| 5952 | struct btrfs_device *dev; |
| 5953 | u64 tmp; |
| 5954 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5955 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5956 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5957 | |
| 5958 | dev = __alloc_device(); |
| 5959 | if (IS_ERR(dev)) |
| 5960 | return dev; |
| 5961 | |
| 5962 | if (devid) |
| 5963 | tmp = *devid; |
| 5964 | else { |
| 5965 | int ret; |
| 5966 | |
| 5967 | ret = find_next_devid(fs_info, &tmp); |
| 5968 | if (ret) { |
| 5969 | kfree(dev); |
| 5970 | return ERR_PTR(ret); |
| 5971 | } |
| 5972 | } |
| 5973 | dev->devid = tmp; |
| 5974 | |
| 5975 | if (uuid) |
| 5976 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 5977 | else |
| 5978 | generate_random_uuid(dev->uuid); |
| 5979 | |
Liu Bo | 9e0af23 | 2014-08-15 23:36:53 +0800 | [diff] [blame] | 5980 | btrfs_init_work(&dev->work, btrfs_submit_helper, |
| 5981 | pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5982 | |
| 5983 | return dev; |
| 5984 | } |
| 5985 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5986 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 5987 | struct extent_buffer *leaf, |
| 5988 | struct btrfs_chunk *chunk) |
| 5989 | { |
| 5990 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5991 | struct map_lookup *map; |
| 5992 | struct extent_map *em; |
| 5993 | u64 logical; |
| 5994 | u64 length; |
| 5995 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5996 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5997 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5998 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5999 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6000 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6001 | logical = key->offset; |
| 6002 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6003 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6004 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6005 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6006 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6007 | |
| 6008 | /* already mapped? */ |
| 6009 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 6010 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6011 | return 0; |
| 6012 | } else if (em) { |
| 6013 | free_extent_map(em); |
| 6014 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6015 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 6016 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6017 | if (!em) |
| 6018 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6019 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 6020 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6021 | if (!map) { |
| 6022 | free_extent_map(em); |
| 6023 | return -ENOMEM; |
| 6024 | } |
| 6025 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 6026 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6027 | em->bdev = (struct block_device *)map; |
| 6028 | em->start = logical; |
| 6029 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 6030 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6031 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6032 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6033 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6034 | map->num_stripes = num_stripes; |
| 6035 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 6036 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 6037 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 6038 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 6039 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 6040 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6041 | for (i = 0; i < num_stripes; i++) { |
| 6042 | map->stripes[i].physical = |
| 6043 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 6044 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6045 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 6046 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 6047 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6048 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 6049 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6050 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 6051 | free_extent_map(em); |
| 6052 | return -EIO; |
| 6053 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6054 | if (!map->stripes[i].dev) { |
| 6055 | map->stripes[i].dev = |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6056 | add_missing_dev(root, root->fs_info->fs_devices, |
| 6057 | devid, uuid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6058 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6059 | free_extent_map(em); |
| 6060 | return -EIO; |
| 6061 | } |
| 6062 | } |
| 6063 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6064 | } |
| 6065 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6066 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 6067 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6068 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6069 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6070 | free_extent_map(em); |
| 6071 | |
| 6072 | return 0; |
| 6073 | } |
| 6074 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 6075 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6076 | struct btrfs_dev_item *dev_item, |
| 6077 | struct btrfs_device *device) |
| 6078 | { |
| 6079 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6080 | |
| 6081 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 6082 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 6083 | device->total_bytes = device->disk_total_bytes; |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6084 | device->commit_total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6085 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6086 | device->commit_bytes_used = device->bytes_used; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6087 | device->type = btrfs_device_type(leaf, dev_item); |
| 6088 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 6089 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 6090 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 6091 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6092 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6093 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6094 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6095 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6096 | } |
| 6097 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6098 | static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, |
| 6099 | u8 *fsid) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6100 | { |
| 6101 | struct btrfs_fs_devices *fs_devices; |
| 6102 | int ret; |
| 6103 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6104 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6105 | |
| 6106 | fs_devices = root->fs_info->fs_devices->seed; |
| 6107 | while (fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6108 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) |
| 6109 | return fs_devices; |
| 6110 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6111 | fs_devices = fs_devices->seed; |
| 6112 | } |
| 6113 | |
| 6114 | fs_devices = find_fsid(fsid); |
| 6115 | if (!fs_devices) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6116 | if (!btrfs_test_opt(root, DEGRADED)) |
| 6117 | return ERR_PTR(-ENOENT); |
| 6118 | |
| 6119 | fs_devices = alloc_fs_devices(fsid); |
| 6120 | if (IS_ERR(fs_devices)) |
| 6121 | return fs_devices; |
| 6122 | |
| 6123 | fs_devices->seeding = 1; |
| 6124 | fs_devices->opened = 1; |
| 6125 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6126 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6127 | |
| 6128 | fs_devices = clone_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6129 | if (IS_ERR(fs_devices)) |
| 6130 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6131 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 6132 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 6133 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6134 | if (ret) { |
| 6135 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6136 | fs_devices = ERR_PTR(ret); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6137 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 6138 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6139 | |
| 6140 | if (!fs_devices->seeding) { |
| 6141 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6142 | free_fs_devices(fs_devices); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6143 | fs_devices = ERR_PTR(-EINVAL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6144 | goto out; |
| 6145 | } |
| 6146 | |
| 6147 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 6148 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6149 | out: |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6150 | return fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6151 | } |
| 6152 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6153 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6154 | struct extent_buffer *leaf, |
| 6155 | struct btrfs_dev_item *dev_item) |
| 6156 | { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6157 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6158 | struct btrfs_device *device; |
| 6159 | u64 devid; |
| 6160 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6161 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6162 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 6163 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6164 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 6165 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 6166 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 6167 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6168 | BTRFS_UUID_SIZE); |
| 6169 | |
| 6170 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6171 | fs_devices = open_seed_devices(root, fs_uuid); |
| 6172 | if (IS_ERR(fs_devices)) |
| 6173 | return PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6174 | } |
| 6175 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6176 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6177 | if (!device) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6178 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6179 | return -EIO; |
| 6180 | |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6181 | btrfs_warn(root->fs_info, "devid %llu missing", devid); |
| 6182 | device = add_missing_dev(root, fs_devices, devid, dev_uuid); |
| 6183 | if (!device) |
| 6184 | return -ENOMEM; |
| 6185 | } else { |
| 6186 | if (!device->bdev && !btrfs_test_opt(root, DEGRADED)) |
| 6187 | return -EIO; |
| 6188 | |
| 6189 | if(!device->bdev && !device->missing) { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6190 | /* |
| 6191 | * this happens when a device that was properly setup |
| 6192 | * in the device info lists suddenly goes bad. |
| 6193 | * device->bdev is NULL, and so we have to set |
| 6194 | * device->missing to one here |
| 6195 | */ |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6196 | device->fs_devices->missing_devices++; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6197 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6198 | } |
Miao Xie | 5f37583 | 2014-09-03 21:35:46 +0800 | [diff] [blame] | 6199 | |
| 6200 | /* Move the device to its own fs_devices */ |
| 6201 | if (device->fs_devices != fs_devices) { |
| 6202 | ASSERT(device->missing); |
| 6203 | |
| 6204 | list_move(&device->dev_list, &fs_devices->devices); |
| 6205 | device->fs_devices->num_devices--; |
| 6206 | fs_devices->num_devices++; |
| 6207 | |
| 6208 | device->fs_devices->missing_devices--; |
| 6209 | fs_devices->missing_devices++; |
| 6210 | |
| 6211 | device->fs_devices = fs_devices; |
| 6212 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 6216 | BUG_ON(device->writeable); |
| 6217 | if (device->generation != |
| 6218 | btrfs_device_generation(leaf, dev_item)) |
| 6219 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6220 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6221 | |
| 6222 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6223 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6224 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6225 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6226 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6227 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6228 | device->bytes_used; |
| 6229 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6230 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6231 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6232 | return ret; |
| 6233 | } |
| 6234 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6235 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6236 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6237 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6238 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6239 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6240 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6241 | u8 *ptr; |
| 6242 | unsigned long sb_ptr; |
| 6243 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6244 | u32 num_stripes; |
| 6245 | u32 array_size; |
| 6246 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6247 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6248 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6249 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6250 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6251 | BTRFS_SUPER_INFO_SIZE); |
| 6252 | if (!sb) |
| 6253 | return -ENOMEM; |
| 6254 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6255 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6256 | /* |
| 6257 | * The sb extent buffer is artifical and just used to read the system array. |
| 6258 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6259 | * pages up-to-date when the page is larger: extent does not cover the |
| 6260 | * whole page and consequently check_page_uptodate does not find all |
| 6261 | * the page's extents up-to-date (the hole beyond sb), |
| 6262 | * write_extent_buffer then triggers a WARN_ON. |
| 6263 | * |
| 6264 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6265 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6266 | * to silence the warning eg. on PowerPC 64. |
| 6267 | */ |
| 6268 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6269 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6270 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6271 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6272 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6273 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6274 | ptr = super_copy->sys_chunk_array; |
| 6275 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6276 | cur = 0; |
| 6277 | |
| 6278 | while (cur < array_size) { |
| 6279 | disk_key = (struct btrfs_disk_key *)ptr; |
| 6280 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6281 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6282 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6283 | sb_ptr += len; |
| 6284 | cur += len; |
| 6285 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6286 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6287 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6288 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6289 | if (ret) |
| 6290 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6291 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6292 | len = btrfs_chunk_item_size(num_stripes); |
| 6293 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6294 | ret = -EIO; |
| 6295 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6296 | } |
| 6297 | ptr += len; |
| 6298 | sb_ptr += len; |
| 6299 | cur += len; |
| 6300 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6301 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6302 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6306 | { |
| 6307 | struct btrfs_path *path; |
| 6308 | struct extent_buffer *leaf; |
| 6309 | struct btrfs_key key; |
| 6310 | struct btrfs_key found_key; |
| 6311 | int ret; |
| 6312 | int slot; |
| 6313 | |
| 6314 | root = root->fs_info->chunk_root; |
| 6315 | |
| 6316 | path = btrfs_alloc_path(); |
| 6317 | if (!path) |
| 6318 | return -ENOMEM; |
| 6319 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6320 | mutex_lock(&uuid_mutex); |
| 6321 | lock_chunks(root); |
| 6322 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6323 | /* |
| 6324 | * Read all device items, and then all the chunk items. All |
| 6325 | * device items are found before any chunk item (their object id |
| 6326 | * is smaller than the lowest possible object id for a chunk |
| 6327 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6328 | */ |
| 6329 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6330 | key.offset = 0; |
| 6331 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6332 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6333 | if (ret < 0) |
| 6334 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6335 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6336 | leaf = path->nodes[0]; |
| 6337 | slot = path->slots[0]; |
| 6338 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6339 | ret = btrfs_next_leaf(root, path); |
| 6340 | if (ret == 0) |
| 6341 | continue; |
| 6342 | if (ret < 0) |
| 6343 | goto error; |
| 6344 | break; |
| 6345 | } |
| 6346 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6347 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6348 | struct btrfs_dev_item *dev_item; |
| 6349 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6350 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6351 | ret = read_one_dev(root, leaf, dev_item); |
| 6352 | if (ret) |
| 6353 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6354 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6355 | struct btrfs_chunk *chunk; |
| 6356 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6357 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6358 | if (ret) |
| 6359 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6360 | } |
| 6361 | path->slots[0]++; |
| 6362 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6363 | ret = 0; |
| 6364 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6365 | unlock_chunks(root); |
| 6366 | mutex_unlock(&uuid_mutex); |
| 6367 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6368 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6369 | return ret; |
| 6370 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6371 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6372 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6373 | { |
| 6374 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6375 | struct btrfs_device *device; |
| 6376 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6377 | while (fs_devices) { |
| 6378 | mutex_lock(&fs_devices->device_list_mutex); |
| 6379 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6380 | device->dev_root = fs_info->dev_root; |
| 6381 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6382 | |
| 6383 | fs_devices = fs_devices->seed; |
| 6384 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6385 | } |
| 6386 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6387 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6388 | { |
| 6389 | int i; |
| 6390 | |
| 6391 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6392 | btrfs_dev_stat_reset(dev, i); |
| 6393 | } |
| 6394 | |
| 6395 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6396 | { |
| 6397 | struct btrfs_key key; |
| 6398 | struct btrfs_key found_key; |
| 6399 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6400 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6401 | struct extent_buffer *eb; |
| 6402 | int slot; |
| 6403 | int ret = 0; |
| 6404 | struct btrfs_device *device; |
| 6405 | struct btrfs_path *path = NULL; |
| 6406 | int i; |
| 6407 | |
| 6408 | path = btrfs_alloc_path(); |
| 6409 | if (!path) { |
| 6410 | ret = -ENOMEM; |
| 6411 | goto out; |
| 6412 | } |
| 6413 | |
| 6414 | mutex_lock(&fs_devices->device_list_mutex); |
| 6415 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6416 | int item_size; |
| 6417 | struct btrfs_dev_stats_item *ptr; |
| 6418 | |
| 6419 | key.objectid = 0; |
| 6420 | key.type = BTRFS_DEV_STATS_KEY; |
| 6421 | key.offset = device->devid; |
| 6422 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6423 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6424 | __btrfs_reset_dev_stats(device); |
| 6425 | device->dev_stats_valid = 1; |
| 6426 | btrfs_release_path(path); |
| 6427 | continue; |
| 6428 | } |
| 6429 | slot = path->slots[0]; |
| 6430 | eb = path->nodes[0]; |
| 6431 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6432 | item_size = btrfs_item_size_nr(eb, slot); |
| 6433 | |
| 6434 | ptr = btrfs_item_ptr(eb, slot, |
| 6435 | struct btrfs_dev_stats_item); |
| 6436 | |
| 6437 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6438 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6439 | btrfs_dev_stat_set(device, i, |
| 6440 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6441 | else |
| 6442 | btrfs_dev_stat_reset(device, i); |
| 6443 | } |
| 6444 | |
| 6445 | device->dev_stats_valid = 1; |
| 6446 | btrfs_dev_stat_print_on_load(device); |
| 6447 | btrfs_release_path(path); |
| 6448 | } |
| 6449 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6450 | |
| 6451 | out: |
| 6452 | btrfs_free_path(path); |
| 6453 | return ret < 0 ? ret : 0; |
| 6454 | } |
| 6455 | |
| 6456 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6457 | struct btrfs_root *dev_root, |
| 6458 | struct btrfs_device *device) |
| 6459 | { |
| 6460 | struct btrfs_path *path; |
| 6461 | struct btrfs_key key; |
| 6462 | struct extent_buffer *eb; |
| 6463 | struct btrfs_dev_stats_item *ptr; |
| 6464 | int ret; |
| 6465 | int i; |
| 6466 | |
| 6467 | key.objectid = 0; |
| 6468 | key.type = BTRFS_DEV_STATS_KEY; |
| 6469 | key.offset = device->devid; |
| 6470 | |
| 6471 | path = btrfs_alloc_path(); |
| 6472 | BUG_ON(!path); |
| 6473 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6474 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6475 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6476 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6477 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6478 | goto out; |
| 6479 | } |
| 6480 | |
| 6481 | if (ret == 0 && |
| 6482 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6483 | /* need to delete old one and insert a new one */ |
| 6484 | ret = btrfs_del_item(trans, dev_root, path); |
| 6485 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6486 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6487 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6488 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6489 | goto out; |
| 6490 | } |
| 6491 | ret = 1; |
| 6492 | } |
| 6493 | |
| 6494 | if (ret == 1) { |
| 6495 | /* need to insert a new item */ |
| 6496 | btrfs_release_path(path); |
| 6497 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6498 | &key, sizeof(*ptr)); |
| 6499 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6500 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6501 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6502 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6503 | goto out; |
| 6504 | } |
| 6505 | } |
| 6506 | |
| 6507 | eb = path->nodes[0]; |
| 6508 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6509 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6510 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6511 | btrfs_dev_stat_read(device, i)); |
| 6512 | btrfs_mark_buffer_dirty(eb); |
| 6513 | |
| 6514 | out: |
| 6515 | btrfs_free_path(path); |
| 6516 | return ret; |
| 6517 | } |
| 6518 | |
| 6519 | /* |
| 6520 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6521 | */ |
| 6522 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6523 | struct btrfs_fs_info *fs_info) |
| 6524 | { |
| 6525 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6526 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6527 | struct btrfs_device *device; |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6528 | int stats_cnt; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6529 | int ret = 0; |
| 6530 | |
| 6531 | mutex_lock(&fs_devices->device_list_mutex); |
| 6532 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6533 | if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device)) |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6534 | continue; |
| 6535 | |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6536 | stats_cnt = atomic_read(&device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6537 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6538 | if (!ret) |
Miao Xie | addc3fa | 2014-07-24 11:37:11 +0800 | [diff] [blame] | 6539 | atomic_sub(stats_cnt, &device->dev_stats_ccnt); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6540 | } |
| 6541 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6542 | |
| 6543 | return ret; |
| 6544 | } |
| 6545 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6546 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6547 | { |
| 6548 | btrfs_dev_stat_inc(dev, index); |
| 6549 | btrfs_dev_stat_print_on_error(dev); |
| 6550 | } |
| 6551 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6552 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6553 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6554 | if (!dev->dev_stats_valid) |
| 6555 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6556 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6557 | "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] | 6558 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6559 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6560 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6561 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6562 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6563 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6564 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6565 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6566 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6567 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6568 | int i; |
| 6569 | |
| 6570 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6571 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6572 | break; |
| 6573 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6574 | return; /* all values == 0, suppress message */ |
| 6575 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6576 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6577 | "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] | 6578 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6579 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6580 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6581 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6582 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6583 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6584 | } |
| 6585 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6586 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6587 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6588 | { |
| 6589 | struct btrfs_device *dev; |
| 6590 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6591 | int i; |
| 6592 | |
| 6593 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6594 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6595 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6596 | |
| 6597 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6598 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6599 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6600 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6601 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6602 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6603 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6604 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6605 | if (stats->nr_items > i) |
| 6606 | stats->values[i] = |
| 6607 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6608 | else |
| 6609 | btrfs_dev_stat_reset(dev, i); |
| 6610 | } |
| 6611 | } else { |
| 6612 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6613 | if (stats->nr_items > i) |
| 6614 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6615 | } |
| 6616 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6617 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6618 | return 0; |
| 6619 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6620 | |
| 6621 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6622 | { |
| 6623 | struct buffer_head *bh; |
| 6624 | struct btrfs_super_block *disk_super; |
| 6625 | |
| 6626 | bh = btrfs_read_dev_super(device->bdev); |
| 6627 | if (!bh) |
| 6628 | return -EINVAL; |
| 6629 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6630 | |
| 6631 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6632 | set_buffer_dirty(bh); |
| 6633 | sync_dirty_buffer(bh); |
| 6634 | brelse(bh); |
| 6635 | |
| 6636 | return 0; |
| 6637 | } |
Miao Xie | 935e5cc | 2014-09-03 21:35:33 +0800 | [diff] [blame] | 6638 | |
| 6639 | /* |
| 6640 | * Update the size of all devices, which is used for writing out the |
| 6641 | * super blocks. |
| 6642 | */ |
| 6643 | void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info) |
| 6644 | { |
| 6645 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6646 | struct btrfs_device *curr, *next; |
| 6647 | |
| 6648 | if (list_empty(&fs_devices->resized_devices)) |
| 6649 | return; |
| 6650 | |
| 6651 | mutex_lock(&fs_devices->device_list_mutex); |
| 6652 | lock_chunks(fs_info->dev_root); |
| 6653 | list_for_each_entry_safe(curr, next, &fs_devices->resized_devices, |
| 6654 | resized_list) { |
| 6655 | list_del_init(&curr->resized_list); |
| 6656 | curr->commit_total_bytes = curr->disk_total_bytes; |
| 6657 | } |
| 6658 | unlock_chunks(fs_info->dev_root); |
| 6659 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6660 | } |
Miao Xie | ce7213c | 2014-09-03 21:35:34 +0800 | [diff] [blame] | 6661 | |
| 6662 | /* Must be invoked during the transaction commit */ |
| 6663 | void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, |
| 6664 | struct btrfs_transaction *transaction) |
| 6665 | { |
| 6666 | struct extent_map *em; |
| 6667 | struct map_lookup *map; |
| 6668 | struct btrfs_device *dev; |
| 6669 | int i; |
| 6670 | |
| 6671 | if (list_empty(&transaction->pending_chunks)) |
| 6672 | return; |
| 6673 | |
| 6674 | /* In order to kick the device replace finish process */ |
| 6675 | lock_chunks(root); |
| 6676 | list_for_each_entry(em, &transaction->pending_chunks, list) { |
| 6677 | map = (struct map_lookup *)em->bdev; |
| 6678 | |
| 6679 | for (i = 0; i < map->num_stripes; i++) { |
| 6680 | dev = map->stripes[i].dev; |
| 6681 | dev->commit_bytes_used = dev->bytes_used; |
| 6682 | } |
| 6683 | } |
| 6684 | unlock_chunks(root); |
| 6685 | } |