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 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 53 | static DEFINE_MUTEX(uuid_mutex); |
| 54 | static LIST_HEAD(fs_uuids); |
| 55 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 56 | static void lock_chunks(struct btrfs_root *root) |
| 57 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 58 | mutex_lock(&root->fs_info->chunk_mutex); |
| 59 | } |
| 60 | |
| 61 | static void unlock_chunks(struct btrfs_root *root) |
| 62 | { |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 63 | mutex_unlock(&root->fs_info->chunk_mutex); |
| 64 | } |
| 65 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 66 | static struct btrfs_fs_devices *__alloc_fs_devices(void) |
| 67 | { |
| 68 | struct btrfs_fs_devices *fs_devs; |
| 69 | |
| 70 | fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS); |
| 71 | if (!fs_devs) |
| 72 | return ERR_PTR(-ENOMEM); |
| 73 | |
| 74 | mutex_init(&fs_devs->device_list_mutex); |
| 75 | |
| 76 | INIT_LIST_HEAD(&fs_devs->devices); |
| 77 | INIT_LIST_HEAD(&fs_devs->alloc_list); |
| 78 | INIT_LIST_HEAD(&fs_devs->list); |
| 79 | |
| 80 | return fs_devs; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * alloc_fs_devices - allocate struct btrfs_fs_devices |
| 85 | * @fsid: a pointer to UUID for this FS. If NULL a new UUID is |
| 86 | * generated. |
| 87 | * |
| 88 | * Return: a pointer to a new &struct btrfs_fs_devices on success; |
| 89 | * ERR_PTR() on error. Returned struct is not linked onto any lists and |
| 90 | * can be destroyed with kfree() right away. |
| 91 | */ |
| 92 | static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid) |
| 93 | { |
| 94 | struct btrfs_fs_devices *fs_devs; |
| 95 | |
| 96 | fs_devs = __alloc_fs_devices(); |
| 97 | if (IS_ERR(fs_devs)) |
| 98 | return fs_devs; |
| 99 | |
| 100 | if (fsid) |
| 101 | memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE); |
| 102 | else |
| 103 | generate_random_uuid(fs_devs->fsid); |
| 104 | |
| 105 | return fs_devs; |
| 106 | } |
| 107 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 108 | static void free_fs_devices(struct btrfs_fs_devices *fs_devices) |
| 109 | { |
| 110 | struct btrfs_device *device; |
| 111 | WARN_ON(fs_devices->opened); |
| 112 | while (!list_empty(&fs_devices->devices)) { |
| 113 | device = list_entry(fs_devices->devices.next, |
| 114 | struct btrfs_device, dev_list); |
| 115 | list_del(&device->dev_list); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 116 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 117 | kfree(device); |
| 118 | } |
| 119 | kfree(fs_devices); |
| 120 | } |
| 121 | |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 122 | static void btrfs_kobject_uevent(struct block_device *bdev, |
| 123 | enum kobject_action action) |
| 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action); |
| 128 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 129 | pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n", |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 130 | action, |
| 131 | kobject_name(&disk_to_dev(bdev->bd_disk)->kobj), |
| 132 | &disk_to_dev(bdev->bd_disk)->kobj); |
| 133 | } |
| 134 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 135 | void btrfs_cleanup_fs_uuids(void) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 136 | { |
| 137 | struct btrfs_fs_devices *fs_devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 138 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 139 | while (!list_empty(&fs_uuids)) { |
| 140 | fs_devices = list_entry(fs_uuids.next, |
| 141 | struct btrfs_fs_devices, list); |
| 142 | list_del(&fs_devices->list); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 143 | free_fs_devices(fs_devices); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 144 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 147 | static struct btrfs_device *__alloc_device(void) |
| 148 | { |
| 149 | struct btrfs_device *dev; |
| 150 | |
| 151 | dev = kzalloc(sizeof(*dev), GFP_NOFS); |
| 152 | if (!dev) |
| 153 | return ERR_PTR(-ENOMEM); |
| 154 | |
| 155 | INIT_LIST_HEAD(&dev->dev_list); |
| 156 | INIT_LIST_HEAD(&dev->dev_alloc_list); |
| 157 | |
| 158 | spin_lock_init(&dev->io_lock); |
| 159 | |
| 160 | spin_lock_init(&dev->reada_lock); |
| 161 | atomic_set(&dev->reada_in_flight, 0); |
| 162 | INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT); |
| 163 | INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT); |
| 164 | |
| 165 | return dev; |
| 166 | } |
| 167 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 168 | static noinline struct btrfs_device *__find_device(struct list_head *head, |
| 169 | u64 devid, u8 *uuid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 170 | { |
| 171 | struct btrfs_device *dev; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 172 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 173 | list_for_each_entry(dev, head, dev_list) { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 174 | if (dev->devid == devid && |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 175 | (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 176 | return dev; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 177 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 178 | } |
| 179 | return NULL; |
| 180 | } |
| 181 | |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 182 | static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 183 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 184 | struct btrfs_fs_devices *fs_devices; |
| 185 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 186 | list_for_each_entry(fs_devices, &fs_uuids, list) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 187 | if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0) |
| 188 | return fs_devices; |
| 189 | } |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 193 | static int |
| 194 | btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder, |
| 195 | int flush, struct block_device **bdev, |
| 196 | struct buffer_head **bh) |
| 197 | { |
| 198 | int ret; |
| 199 | |
| 200 | *bdev = blkdev_get_by_path(device_path, flags, holder); |
| 201 | |
| 202 | if (IS_ERR(*bdev)) { |
| 203 | ret = PTR_ERR(*bdev); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 204 | printk(KERN_INFO "BTRFS: open %s failed\n", device_path); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 205 | goto error; |
| 206 | } |
| 207 | |
| 208 | if (flush) |
| 209 | filemap_write_and_wait((*bdev)->bd_inode->i_mapping); |
| 210 | ret = set_blocksize(*bdev, 4096); |
| 211 | if (ret) { |
| 212 | blkdev_put(*bdev, flags); |
| 213 | goto error; |
| 214 | } |
| 215 | invalidate_bdev(*bdev); |
| 216 | *bh = btrfs_read_dev_super(*bdev); |
| 217 | if (!*bh) { |
| 218 | ret = -EINVAL; |
| 219 | blkdev_put(*bdev, flags); |
| 220 | goto error; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | |
| 225 | error: |
| 226 | *bdev = NULL; |
| 227 | *bh = NULL; |
| 228 | return ret; |
| 229 | } |
| 230 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 231 | static void requeue_list(struct btrfs_pending_bios *pending_bios, |
| 232 | struct bio *head, struct bio *tail) |
| 233 | { |
| 234 | |
| 235 | struct bio *old_head; |
| 236 | |
| 237 | old_head = pending_bios->head; |
| 238 | pending_bios->head = head; |
| 239 | if (pending_bios->tail) |
| 240 | tail->bi_next = old_head; |
| 241 | else |
| 242 | pending_bios->tail = tail; |
| 243 | } |
| 244 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 245 | /* |
| 246 | * we try to collect pending bios for a device so we don't get a large |
| 247 | * number of procs sending bios down to the same device. This greatly |
| 248 | * improves the schedulers ability to collect and merge the bios. |
| 249 | * |
| 250 | * But, it also turns into a long list of bios to process and that is sure |
| 251 | * to eventually make the worker thread block. The solution here is to |
| 252 | * make some progress and then put this work struct back at the end of |
| 253 | * the list if the block device is congested. This way, multiple devices |
| 254 | * can make progress from a single worker thread. |
| 255 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 256 | static noinline void run_scheduled_bios(struct btrfs_device *device) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 257 | { |
| 258 | struct bio *pending; |
| 259 | struct backing_dev_info *bdi; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 260 | struct btrfs_fs_info *fs_info; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 261 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 262 | struct bio *tail; |
| 263 | struct bio *cur; |
| 264 | int again = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 265 | unsigned long num_run; |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 266 | unsigned long batch_run = 0; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 267 | unsigned long limit; |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 268 | unsigned long last_waited = 0; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 269 | int force_reg = 0; |
Miao Xie | 0e58885 | 2011-08-05 09:32:37 +0000 | [diff] [blame] | 270 | int sync_pending = 0; |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 271 | struct blk_plug plug; |
| 272 | |
| 273 | /* |
| 274 | * this function runs all the bios we've collected for |
| 275 | * a particular device. We don't want to wander off to |
| 276 | * another device without first sending all of these down. |
| 277 | * So, setup a plug here and finish it off before we return |
| 278 | */ |
| 279 | blk_start_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 280 | |
Chris Mason | bedf762 | 2009-04-03 10:32:58 -0400 | [diff] [blame] | 281 | bdi = blk_get_backing_dev_info(device->bdev); |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 282 | fs_info = device->dev_root->fs_info; |
| 283 | limit = btrfs_async_submit_limit(fs_info); |
| 284 | limit = limit * 2 / 3; |
| 285 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 286 | loop: |
| 287 | spin_lock(&device->io_lock); |
| 288 | |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 289 | loop_lock: |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 290 | num_run = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 291 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 292 | /* take all the bios off the list at once and process them |
| 293 | * later on (without the lock held). But, remember the |
| 294 | * tail and other pointers so the bios can be properly reinserted |
| 295 | * into the list if we hit congestion |
| 296 | */ |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 297 | if (!force_reg && device->pending_sync_bios.head) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 298 | pending_bios = &device->pending_sync_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 299 | force_reg = 1; |
| 300 | } else { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 301 | pending_bios = &device->pending_bios; |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 302 | force_reg = 0; |
| 303 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 304 | |
| 305 | pending = pending_bios->head; |
| 306 | tail = pending_bios->tail; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 307 | WARN_ON(pending && !tail); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * if pending was null this time around, no bios need processing |
| 311 | * at all and we can stop. Otherwise it'll loop back up again |
| 312 | * and do an additional check so no bios are missed. |
| 313 | * |
| 314 | * device->running_pending is used to synchronize with the |
| 315 | * schedule_bio code. |
| 316 | */ |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 317 | if (device->pending_sync_bios.head == NULL && |
| 318 | device->pending_bios.head == NULL) { |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 319 | again = 0; |
| 320 | device->running_pending = 0; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 321 | } else { |
| 322 | again = 1; |
| 323 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 324 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 325 | |
| 326 | pending_bios->head = NULL; |
| 327 | pending_bios->tail = NULL; |
| 328 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 329 | spin_unlock(&device->io_lock); |
| 330 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 331 | while (pending) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 332 | |
| 333 | rmb(); |
Chris Mason | d84275c | 2009-06-09 15:39:08 -0400 | [diff] [blame] | 334 | /* we want to work on both lists, but do more bios on the |
| 335 | * sync list than the regular list |
| 336 | */ |
| 337 | if ((num_run > 32 && |
| 338 | pending_bios != &device->pending_sync_bios && |
| 339 | device->pending_sync_bios.head) || |
| 340 | (num_run > 64 && pending_bios == &device->pending_sync_bios && |
| 341 | device->pending_bios.head)) { |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 342 | spin_lock(&device->io_lock); |
| 343 | requeue_list(pending_bios, pending, tail); |
| 344 | goto loop_lock; |
| 345 | } |
| 346 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 347 | cur = pending; |
| 348 | pending = pending->bi_next; |
| 349 | cur->bi_next = NULL; |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 350 | |
Josef Bacik | 66657b3 | 2012-08-01 15:36:24 -0400 | [diff] [blame] | 351 | if (atomic_dec_return(&fs_info->nr_async_bios) < limit && |
Chris Mason | b64a285 | 2008-08-20 13:39:41 -0400 | [diff] [blame] | 352 | waitqueue_active(&fs_info->async_submit_wait)) |
| 353 | wake_up(&fs_info->async_submit_wait); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 354 | |
| 355 | BUG_ON(atomic_read(&cur->bi_cnt) == 0); |
Chris Mason | d644d8a | 2009-06-09 15:59:22 -0400 | [diff] [blame] | 356 | |
Chris Mason | 2ab1ba6 | 2011-08-04 14:28:36 -0400 | [diff] [blame] | 357 | /* |
| 358 | * if we're doing the sync list, record that our |
| 359 | * plug has some sync requests on it |
| 360 | * |
| 361 | * If we're doing the regular list and there are |
| 362 | * sync requests sitting around, unplug before |
| 363 | * we add more |
| 364 | */ |
| 365 | if (pending_bios == &device->pending_sync_bios) { |
| 366 | sync_pending = 1; |
| 367 | } else if (sync_pending) { |
| 368 | blk_finish_plug(&plug); |
| 369 | blk_start_plug(&plug); |
| 370 | sync_pending = 0; |
| 371 | } |
| 372 | |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 373 | btrfsic_submit_bio(cur->bi_rw, cur); |
Chris Mason | 5ff7ba3 | 2010-03-15 10:21:30 -0400 | [diff] [blame] | 374 | num_run++; |
| 375 | batch_run++; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 376 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 377 | cond_resched(); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * we made progress, there is more work to do and the bdi |
| 381 | * is now congested. Back off and let other work structs |
| 382 | * run instead |
| 383 | */ |
Chris Mason | 57fd5a5 | 2009-08-07 09:59:15 -0400 | [diff] [blame] | 384 | if (pending && bdi_write_congested(bdi) && batch_run > 8 && |
Chris Mason | 5f2cc08 | 2008-11-07 18:22:45 -0500 | [diff] [blame] | 385 | fs_info->fs_devices->open_devices > 1) { |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 386 | struct io_context *ioc; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 387 | |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 388 | ioc = current->io_context; |
| 389 | |
| 390 | /* |
| 391 | * the main goal here is that we don't want to |
| 392 | * block if we're going to be able to submit |
| 393 | * more requests without blocking. |
| 394 | * |
| 395 | * This code does two great things, it pokes into |
| 396 | * the elevator code from a filesystem _and_ |
| 397 | * it makes assumptions about how batching works. |
| 398 | */ |
| 399 | if (ioc && ioc->nr_batch_requests > 0 && |
| 400 | time_before(jiffies, ioc->last_waited + HZ/50UL) && |
| 401 | (last_waited == 0 || |
| 402 | ioc->last_waited == last_waited)) { |
| 403 | /* |
| 404 | * we want to go through our batch of |
| 405 | * requests and stop. So, we copy out |
| 406 | * the ioc->last_waited time and test |
| 407 | * against it before looping |
| 408 | */ |
| 409 | last_waited = ioc->last_waited; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 410 | if (need_resched()) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 411 | cond_resched(); |
Chris Mason | b765ead | 2009-04-03 10:27:10 -0400 | [diff] [blame] | 412 | continue; |
| 413 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 414 | spin_lock(&device->io_lock); |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 415 | requeue_list(pending_bios, pending, tail); |
Chris Mason | a683705 | 2009-02-04 09:19:41 -0500 | [diff] [blame] | 416 | device->running_pending = 1; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 417 | |
| 418 | spin_unlock(&device->io_lock); |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 419 | btrfs_queue_work(fs_info->submit_workers, |
| 420 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 421 | goto done; |
| 422 | } |
Chris Mason | d85c8a6f | 2011-12-15 15:38:41 -0500 | [diff] [blame] | 423 | /* unplug every 64 requests just for good measure */ |
| 424 | if (batch_run % 64 == 0) { |
| 425 | blk_finish_plug(&plug); |
| 426 | blk_start_plug(&plug); |
| 427 | sync_pending = 0; |
| 428 | } |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 429 | } |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 430 | |
Chris Mason | 5168408 | 2010-03-10 15:33:32 -0500 | [diff] [blame] | 431 | cond_resched(); |
| 432 | if (again) |
| 433 | goto loop; |
| 434 | |
| 435 | spin_lock(&device->io_lock); |
| 436 | if (device->pending_bios.head || device->pending_sync_bios.head) |
| 437 | goto loop_lock; |
| 438 | spin_unlock(&device->io_lock); |
| 439 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 440 | done: |
Chris Mason | 211588a | 2011-04-19 20:12:40 -0400 | [diff] [blame] | 441 | blk_finish_plug(&plug); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 444 | static void pending_bios_fn(struct btrfs_work *work) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 445 | { |
| 446 | struct btrfs_device *device; |
| 447 | |
| 448 | device = container_of(work, struct btrfs_device, work); |
| 449 | run_scheduled_bios(device); |
| 450 | } |
| 451 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 452 | /* |
| 453 | * Add new device to list of registered devices |
| 454 | * |
| 455 | * Returns: |
| 456 | * 1 - first time device is seen |
| 457 | * 0 - device already known |
| 458 | * < 0 - error |
| 459 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 460 | static noinline int device_list_add(const char *path, |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 461 | struct btrfs_super_block *disk_super, |
| 462 | u64 devid, struct btrfs_fs_devices **fs_devices_ret) |
| 463 | { |
| 464 | struct btrfs_device *device; |
| 465 | struct btrfs_fs_devices *fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 466 | struct rcu_string *name; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 467 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 468 | u64 found_transid = btrfs_super_generation(disk_super); |
| 469 | |
| 470 | fs_devices = find_fsid(disk_super->fsid); |
| 471 | if (!fs_devices) { |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 472 | fs_devices = alloc_fs_devices(disk_super->fsid); |
| 473 | if (IS_ERR(fs_devices)) |
| 474 | return PTR_ERR(fs_devices); |
| 475 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 476 | list_add(&fs_devices->list, &fs_uuids); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 477 | fs_devices->latest_devid = devid; |
| 478 | fs_devices->latest_trans = found_transid; |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 479 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 480 | device = NULL; |
| 481 | } else { |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 482 | device = __find_device(&fs_devices->devices, devid, |
| 483 | disk_super->dev_item.uuid); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 484 | } |
| 485 | if (!device) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 486 | if (fs_devices->opened) |
| 487 | return -EBUSY; |
| 488 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 489 | device = btrfs_alloc_device(NULL, &devid, |
| 490 | disk_super->dev_item.uuid); |
| 491 | if (IS_ERR(device)) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 492 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 493 | return PTR_ERR(device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 494 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 495 | |
| 496 | name = rcu_string_strdup(path, GFP_NOFS); |
| 497 | if (!name) { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 498 | kfree(device); |
| 499 | return -ENOMEM; |
| 500 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 501 | rcu_assign_pointer(device->name, name); |
Arne Jansen | 90519d6 | 2011-05-23 14:30:00 +0200 | [diff] [blame] | 502 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 503 | mutex_lock(&fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 504 | list_add_rcu(&device->dev_list, &fs_devices->devices); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 505 | fs_devices->num_devices++; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 506 | mutex_unlock(&fs_devices->device_list_mutex); |
| 507 | |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 508 | ret = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 509 | device->fs_devices = fs_devices; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 510 | } else if (!device->name || strcmp(device->name->str, path)) { |
| 511 | name = rcu_string_strdup(path, GFP_NOFS); |
TARUISI Hiroaki | 3a0524d | 2010-02-09 06:36:45 +0000 | [diff] [blame] | 512 | if (!name) |
| 513 | return -ENOMEM; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 514 | rcu_string_free(device->name); |
| 515 | rcu_assign_pointer(device->name, name); |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 516 | if (device->missing) { |
| 517 | fs_devices->missing_devices--; |
| 518 | device->missing = 0; |
| 519 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (found_transid > fs_devices->latest_trans) { |
| 523 | fs_devices->latest_devid = devid; |
| 524 | fs_devices->latest_trans = found_transid; |
| 525 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 526 | *fs_devices_ret = fs_devices; |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 527 | |
| 528 | return ret; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 531 | static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig) |
| 532 | { |
| 533 | struct btrfs_fs_devices *fs_devices; |
| 534 | struct btrfs_device *device; |
| 535 | struct btrfs_device *orig_dev; |
| 536 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 537 | fs_devices = alloc_fs_devices(orig->fsid); |
| 538 | if (IS_ERR(fs_devices)) |
| 539 | return fs_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 540 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 541 | fs_devices->latest_devid = orig->latest_devid; |
| 542 | fs_devices->latest_trans = orig->latest_trans; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 543 | fs_devices->total_devices = orig->total_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 544 | |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 545 | /* 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] | 546 | list_for_each_entry(orig_dev, &orig->devices, dev_list) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 547 | struct rcu_string *name; |
| 548 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 549 | device = btrfs_alloc_device(NULL, &orig_dev->devid, |
| 550 | orig_dev->uuid); |
| 551 | if (IS_ERR(device)) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 552 | goto error; |
| 553 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 554 | /* |
| 555 | * This is ok to do without rcu read locked because we hold the |
| 556 | * uuid mutex so nothing we touch in here is going to disappear. |
| 557 | */ |
| 558 | name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS); |
| 559 | if (!name) { |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 560 | kfree(device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 561 | goto error; |
Julia Lawall | fd2696f | 2009-09-29 13:51:04 -0400 | [diff] [blame] | 562 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 563 | rcu_assign_pointer(device->name, name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 564 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 565 | list_add(&device->dev_list, &fs_devices->devices); |
| 566 | device->fs_devices = fs_devices; |
| 567 | fs_devices->num_devices++; |
| 568 | } |
| 569 | return fs_devices; |
| 570 | error: |
| 571 | free_fs_devices(fs_devices); |
| 572 | return ERR_PTR(-ENOMEM); |
| 573 | } |
| 574 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 575 | void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info, |
| 576 | struct btrfs_fs_devices *fs_devices, int step) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 577 | { |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 578 | struct btrfs_device *device, *next; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 579 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 580 | struct block_device *latest_bdev = NULL; |
| 581 | u64 latest_devid = 0; |
| 582 | u64 latest_transid = 0; |
| 583 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 584 | mutex_lock(&uuid_mutex); |
| 585 | again: |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 586 | /* This is the initialized path, it is safe to release the devices. */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 587 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 588 | if (device->in_fs_metadata) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 589 | if (!device->is_tgtdev_for_dev_replace && |
| 590 | (!latest_transid || |
| 591 | device->generation > latest_transid)) { |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 592 | latest_devid = device->devid; |
| 593 | latest_transid = device->generation; |
| 594 | latest_bdev = device->bdev; |
| 595 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 596 | continue; |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 597 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 598 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 599 | if (device->devid == BTRFS_DEV_REPLACE_DEVID) { |
| 600 | /* |
| 601 | * In the first step, keep the device which has |
| 602 | * the correct fsid and the devid that is used |
| 603 | * for the dev_replace procedure. |
| 604 | * In the second step, the dev_replace state is |
| 605 | * read from the device tree and it is known |
| 606 | * whether the procedure is really active or |
| 607 | * not, which means whether this device is |
| 608 | * used or whether it should be removed. |
| 609 | */ |
| 610 | if (step == 0 || device->is_tgtdev_for_dev_replace) { |
| 611 | continue; |
| 612 | } |
| 613 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 614 | if (device->bdev) { |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 615 | blkdev_put(device->bdev, device->mode); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 616 | device->bdev = NULL; |
| 617 | fs_devices->open_devices--; |
| 618 | } |
| 619 | if (device->writeable) { |
| 620 | list_del_init(&device->dev_alloc_list); |
| 621 | device->writeable = 0; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 622 | if (!device->is_tgtdev_for_dev_replace) |
| 623 | fs_devices->rw_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 624 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 625 | list_del_init(&device->dev_list); |
| 626 | fs_devices->num_devices--; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 627 | rcu_string_free(device->name); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 628 | kfree(device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 629 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 630 | |
| 631 | if (fs_devices->seed) { |
| 632 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 633 | goto again; |
| 634 | } |
| 635 | |
Chris Mason | a6b0d5c | 2012-02-20 20:53:43 -0500 | [diff] [blame] | 636 | fs_devices->latest_bdev = latest_bdev; |
| 637 | fs_devices->latest_devid = latest_devid; |
| 638 | fs_devices->latest_trans = latest_transid; |
| 639 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 640 | mutex_unlock(&uuid_mutex); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 641 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 642 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 643 | static void __free_device(struct work_struct *work) |
| 644 | { |
| 645 | struct btrfs_device *device; |
| 646 | |
| 647 | device = container_of(work, struct btrfs_device, rcu_work); |
| 648 | |
| 649 | if (device->bdev) |
| 650 | blkdev_put(device->bdev, device->mode); |
| 651 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 652 | rcu_string_free(device->name); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 653 | kfree(device); |
| 654 | } |
| 655 | |
| 656 | static void free_device(struct rcu_head *head) |
| 657 | { |
| 658 | struct btrfs_device *device; |
| 659 | |
| 660 | device = container_of(head, struct btrfs_device, rcu); |
| 661 | |
| 662 | INIT_WORK(&device->rcu_work, __free_device); |
| 663 | schedule_work(&device->rcu_work); |
| 664 | } |
| 665 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 666 | static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 667 | { |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 668 | struct btrfs_device *device; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 669 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 670 | if (--fs_devices->opened > 0) |
| 671 | return 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 672 | |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 673 | mutex_lock(&fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 674 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 675 | struct btrfs_device *new_device; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 676 | struct rcu_string *name; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 677 | |
| 678 | if (device->bdev) |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 679 | fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 680 | |
Ilya Dryomov | f747cab | 2013-10-10 20:37:29 +0300 | [diff] [blame] | 681 | if (device->writeable && |
| 682 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 683 | list_del_init(&device->dev_alloc_list); |
| 684 | fs_devices->rw_devices--; |
| 685 | } |
| 686 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 687 | if (device->can_discard) |
| 688 | fs_devices->num_can_discard--; |
Josef Bacik | 726551e | 2013-08-26 13:45:53 -0400 | [diff] [blame] | 689 | if (device->missing) |
| 690 | fs_devices->missing_devices--; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 691 | |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 692 | new_device = btrfs_alloc_device(NULL, &device->devid, |
| 693 | device->uuid); |
| 694 | BUG_ON(IS_ERR(new_device)); /* -ENOMEM */ |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 695 | |
| 696 | /* Safe because we are under uuid_mutex */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 697 | if (device->name) { |
| 698 | name = rcu_string_strdup(device->name->str, GFP_NOFS); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 699 | BUG_ON(!name); /* -ENOMEM */ |
Josef Bacik | 99f5944 | 2012-08-02 10:23:59 -0400 | [diff] [blame] | 700 | rcu_assign_pointer(new_device->name, name); |
| 701 | } |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 702 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 703 | list_replace_rcu(&device->dev_list, &new_device->dev_list); |
Ilya Dryomov | a1e8780 | 2013-08-12 14:33:04 +0300 | [diff] [blame] | 704 | new_device->fs_devices = device->fs_devices; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 705 | |
| 706 | call_rcu(&device->rcu, free_device); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 707 | } |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 708 | mutex_unlock(&fs_devices->device_list_mutex); |
| 709 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 710 | WARN_ON(fs_devices->open_devices); |
| 711 | WARN_ON(fs_devices->rw_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 712 | fs_devices->opened = 0; |
| 713 | fs_devices->seeding = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 714 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 715 | return 0; |
| 716 | } |
| 717 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 718 | int btrfs_close_devices(struct btrfs_fs_devices *fs_devices) |
| 719 | { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 720 | struct btrfs_fs_devices *seed_devices = NULL; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 721 | int ret; |
| 722 | |
| 723 | mutex_lock(&uuid_mutex); |
| 724 | ret = __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 725 | if (!fs_devices->opened) { |
| 726 | seed_devices = fs_devices->seed; |
| 727 | fs_devices->seed = NULL; |
| 728 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 729 | mutex_unlock(&uuid_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 730 | |
| 731 | while (seed_devices) { |
| 732 | fs_devices = seed_devices; |
| 733 | seed_devices = fs_devices->seed; |
| 734 | __btrfs_close_devices(fs_devices); |
| 735 | free_fs_devices(fs_devices); |
| 736 | } |
Eric Sandeen | bc17862 | 2013-03-09 15:18:39 +0000 | [diff] [blame] | 737 | /* |
| 738 | * Wait for rcu kworkers under __btrfs_close_devices |
| 739 | * to finish all blkdev_puts so device is really |
| 740 | * free when umount is done. |
| 741 | */ |
| 742 | rcu_barrier(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 743 | return ret; |
| 744 | } |
| 745 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 746 | static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
| 747 | fmode_t flags, void *holder) |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 748 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 749 | struct request_queue *q; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 750 | struct block_device *bdev; |
| 751 | struct list_head *head = &fs_devices->devices; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 752 | struct btrfs_device *device; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 753 | struct block_device *latest_bdev = NULL; |
| 754 | struct buffer_head *bh; |
| 755 | struct btrfs_super_block *disk_super; |
| 756 | u64 latest_devid = 0; |
| 757 | u64 latest_transid = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 758 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 759 | int seeding = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 760 | int ret = 0; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 761 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 762 | flags |= FMODE_EXCL; |
| 763 | |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 764 | list_for_each_entry(device, head, dev_list) { |
Chris Mason | c1c4d91 | 2008-05-08 15:05:58 -0400 | [diff] [blame] | 765 | if (device->bdev) |
| 766 | continue; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 767 | if (!device->name) |
| 768 | continue; |
| 769 | |
Eric Sandeen | f63e0cc | 2013-04-04 20:45:08 +0000 | [diff] [blame] | 770 | /* Just open everything we can; ignore failures here */ |
| 771 | if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1, |
| 772 | &bdev, &bh)) |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 773 | continue; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 774 | |
| 775 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 776 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 777 | if (devid != device->devid) |
| 778 | goto error_brelse; |
| 779 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 780 | if (memcmp(device->uuid, disk_super->dev_item.uuid, |
| 781 | BTRFS_UUID_SIZE)) |
| 782 | goto error_brelse; |
| 783 | |
| 784 | device->generation = btrfs_super_generation(disk_super); |
| 785 | if (!latest_transid || device->generation > latest_transid) { |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 786 | latest_devid = devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 787 | latest_transid = device->generation; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 788 | latest_bdev = bdev; |
| 789 | } |
| 790 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 791 | if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) { |
| 792 | device->writeable = 0; |
| 793 | } else { |
| 794 | device->writeable = !bdev_read_only(bdev); |
| 795 | seeding = 0; |
| 796 | } |
| 797 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 798 | q = bdev_get_queue(bdev); |
| 799 | if (blk_queue_discard(q)) { |
| 800 | device->can_discard = 1; |
| 801 | fs_devices->num_can_discard++; |
| 802 | } |
| 803 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 804 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 805 | device->in_fs_metadata = 0; |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 806 | device->mode = flags; |
| 807 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 808 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 809 | fs_devices->rotating = 1; |
| 810 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 811 | fs_devices->open_devices++; |
Ilya Dryomov | 55e50e4 | 2013-09-01 18:56:44 +0300 | [diff] [blame] | 812 | if (device->writeable && |
| 813 | device->devid != BTRFS_DEV_REPLACE_DEVID) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 814 | fs_devices->rw_devices++; |
| 815 | list_add(&device->dev_alloc_list, |
| 816 | &fs_devices->alloc_list); |
| 817 | } |
Xiao Guangrong | 4f6c932 | 2011-04-20 10:06:40 +0000 | [diff] [blame] | 818 | brelse(bh); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 819 | continue; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 820 | |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 821 | error_brelse: |
| 822 | brelse(bh); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 823 | blkdev_put(bdev, flags); |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 824 | continue; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 825 | } |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 826 | if (fs_devices->open_devices == 0) { |
Ilya Dryomov | 20bcd64 | 2011-10-20 00:06:20 +0300 | [diff] [blame] | 827 | ret = -EINVAL; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 828 | goto out; |
| 829 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 830 | fs_devices->seeding = seeding; |
| 831 | fs_devices->opened = 1; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 832 | fs_devices->latest_bdev = latest_bdev; |
| 833 | fs_devices->latest_devid = latest_devid; |
| 834 | fs_devices->latest_trans = latest_transid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 835 | fs_devices->total_rw_bytes = 0; |
Chris Mason | a0af469 | 2008-05-13 16:03:06 -0400 | [diff] [blame] | 836 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 841 | fmode_t flags, void *holder) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 842 | { |
| 843 | int ret; |
| 844 | |
| 845 | mutex_lock(&uuid_mutex); |
| 846 | if (fs_devices->opened) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 847 | fs_devices->opened++; |
| 848 | ret = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 849 | } else { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 850 | ret = __btrfs_open_devices(fs_devices, flags, holder); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 851 | } |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 852 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 853 | return ret; |
| 854 | } |
| 855 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 856 | /* |
| 857 | * Look for a btrfs signature on a device. This may be called out of the mount path |
| 858 | * and we are not allowed to call set_blocksize during the scan. The superblock |
| 859 | * is read via pagecache |
| 860 | */ |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 861 | 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] | 862 | struct btrfs_fs_devices **fs_devices_ret) |
| 863 | { |
| 864 | struct btrfs_super_block *disk_super; |
| 865 | struct block_device *bdev; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 866 | struct page *page; |
| 867 | void *p; |
| 868 | int ret = -EINVAL; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 869 | u64 devid; |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 870 | u64 transid; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 871 | u64 total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 872 | u64 bytenr; |
| 873 | pgoff_t index; |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 874 | |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 875 | /* |
| 876 | * we would like to check all the supers, but that would make |
| 877 | * a btrfs mount succeed after a mkfs from a different FS. |
| 878 | * So, we need to add a special mount option to scan for |
| 879 | * later supers, using BTRFS_SUPER_MIRROR_MAX instead |
| 880 | */ |
| 881 | bytenr = btrfs_sb_offset(0); |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 882 | flags |= FMODE_EXCL; |
Al Viro | 10f6327 | 2011-11-17 15:05:22 -0500 | [diff] [blame] | 883 | mutex_lock(&uuid_mutex); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 884 | |
| 885 | bdev = blkdev_get_by_path(path, flags, holder); |
| 886 | |
| 887 | if (IS_ERR(bdev)) { |
| 888 | ret = PTR_ERR(bdev); |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 889 | goto error; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* make sure our super fits in the device */ |
| 893 | if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode)) |
| 894 | goto error_bdev_put; |
| 895 | |
| 896 | /* make sure our super fits in the page */ |
| 897 | if (sizeof(*disk_super) > PAGE_CACHE_SIZE) |
| 898 | goto error_bdev_put; |
| 899 | |
| 900 | /* make sure our super doesn't straddle pages on disk */ |
| 901 | index = bytenr >> PAGE_CACHE_SHIFT; |
| 902 | if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index) |
| 903 | goto error_bdev_put; |
| 904 | |
| 905 | /* pull in the page with our super */ |
| 906 | page = read_cache_page_gfp(bdev->bd_inode->i_mapping, |
| 907 | index, GFP_NOFS); |
| 908 | |
| 909 | if (IS_ERR_OR_NULL(page)) |
| 910 | goto error_bdev_put; |
| 911 | |
| 912 | p = kmap(page); |
| 913 | |
| 914 | /* align our pointer to the offset of the super block */ |
| 915 | disk_super = p + (bytenr & ~PAGE_CACHE_MASK); |
| 916 | |
| 917 | if (btrfs_super_bytenr(disk_super) != bytenr || |
Qu Wenruo | 3cae210 | 2013-07-16 11:19:18 +0800 | [diff] [blame] | 918 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 919 | goto error_unmap; |
| 920 | |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 921 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Chris Mason | f298446 | 2008-04-10 16:19:33 -0400 | [diff] [blame] | 922 | transid = btrfs_super_generation(disk_super); |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 923 | total_devices = btrfs_super_num_devices(disk_super); |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 924 | |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 925 | ret = device_list_add(path, disk_super, devid, fs_devices_ret); |
David Sterba | 60999ca | 2014-03-26 18:26:36 +0100 | [diff] [blame] | 926 | if (ret > 0) { |
| 927 | if (disk_super->label[0]) { |
| 928 | if (disk_super->label[BTRFS_LABEL_SIZE - 1]) |
| 929 | disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0'; |
| 930 | printk(KERN_INFO "BTRFS: device label %s ", disk_super->label); |
| 931 | } else { |
| 932 | printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid); |
| 933 | } |
| 934 | |
| 935 | printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path); |
| 936 | ret = 0; |
| 937 | } |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 938 | if (!ret && fs_devices_ret) |
| 939 | (*fs_devices_ret)->total_devices = total_devices; |
David Sterba | 6f60cbd | 2013-02-15 11:31:02 -0700 | [diff] [blame] | 940 | |
| 941 | error_unmap: |
| 942 | kunmap(page); |
| 943 | page_cache_release(page); |
| 944 | |
| 945 | error_bdev_put: |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 946 | blkdev_put(bdev, flags); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 947 | error: |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 948 | mutex_unlock(&uuid_mutex); |
Chris Mason | 8a4b83c | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 949 | return ret; |
| 950 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 951 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 952 | /* helper to account the used device space in the range */ |
| 953 | int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start, |
| 954 | u64 end, u64 *length) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 955 | { |
| 956 | struct btrfs_key key; |
| 957 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 958 | struct btrfs_dev_extent *dev_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 959 | struct btrfs_path *path; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 960 | u64 extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 961 | int ret; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 962 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 963 | struct extent_buffer *l; |
| 964 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 965 | *length = 0; |
| 966 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 967 | if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 968 | return 0; |
| 969 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 970 | path = btrfs_alloc_path(); |
| 971 | if (!path) |
| 972 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 973 | path->reada = 2; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 974 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 975 | key.objectid = device->devid; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 976 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 977 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 978 | |
| 979 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 980 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 981 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 982 | if (ret > 0) { |
| 983 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 984 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 985 | goto out; |
Yan Zheng | 1fcbac5 | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 986 | } |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 987 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 988 | while (1) { |
| 989 | l = path->nodes[0]; |
| 990 | slot = path->slots[0]; |
| 991 | if (slot >= btrfs_header_nritems(l)) { |
| 992 | ret = btrfs_next_leaf(root, path); |
| 993 | if (ret == 0) |
| 994 | continue; |
| 995 | if (ret < 0) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 996 | goto out; |
| 997 | |
| 998 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 999 | } |
| 1000 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1001 | |
| 1002 | if (key.objectid < device->devid) |
| 1003 | goto next; |
| 1004 | |
| 1005 | if (key.objectid > device->devid) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1006 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1007 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1008 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1009 | goto next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1010 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1011 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 1012 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1013 | dev_extent); |
| 1014 | if (key.offset <= start && extent_end > end) { |
| 1015 | *length = end - start + 1; |
| 1016 | break; |
| 1017 | } else if (key.offset <= start && extent_end > start) |
| 1018 | *length += extent_end - start; |
| 1019 | else if (key.offset > start && extent_end <= end) |
| 1020 | *length += extent_end - key.offset; |
| 1021 | else if (key.offset > start && key.offset <= end) { |
| 1022 | *length += end - key.offset + 1; |
| 1023 | break; |
| 1024 | } else if (key.offset > end) |
| 1025 | break; |
| 1026 | |
| 1027 | next: |
| 1028 | path->slots[0]++; |
| 1029 | } |
| 1030 | ret = 0; |
| 1031 | out: |
| 1032 | btrfs_free_path(path); |
| 1033 | return ret; |
| 1034 | } |
| 1035 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1036 | static int contains_pending_extent(struct btrfs_trans_handle *trans, |
| 1037 | struct btrfs_device *device, |
| 1038 | u64 *start, u64 len) |
| 1039 | { |
| 1040 | struct extent_map *em; |
| 1041 | int ret = 0; |
| 1042 | |
| 1043 | list_for_each_entry(em, &trans->transaction->pending_chunks, list) { |
| 1044 | struct map_lookup *map; |
| 1045 | int i; |
| 1046 | |
| 1047 | map = (struct map_lookup *)em->bdev; |
| 1048 | for (i = 0; i < map->num_stripes; i++) { |
| 1049 | if (map->stripes[i].dev != device) |
| 1050 | continue; |
| 1051 | if (map->stripes[i].physical >= *start + len || |
| 1052 | map->stripes[i].physical + em->orig_block_len <= |
| 1053 | *start) |
| 1054 | continue; |
| 1055 | *start = map->stripes[i].physical + |
| 1056 | em->orig_block_len; |
| 1057 | ret = 1; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1065 | /* |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1066 | * find_free_dev_extent - find free space in the specified device |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1067 | * @device: the device which we search the free space in |
| 1068 | * @num_bytes: the size of the free space that we need |
| 1069 | * @start: store the start of the free space. |
| 1070 | * @len: the size of the free space. that we find, or the size of the max |
| 1071 | * free space if we don't find suitable free space |
| 1072 | * |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1073 | * this uses a pretty simple search, the expectation is that it is |
| 1074 | * called very infrequently and that a given device has a small number |
| 1075 | * of extents |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1076 | * |
| 1077 | * @start is used to store the start of the free space if we find. But if we |
| 1078 | * don't find suitable free space, it will be used to store the start position |
| 1079 | * of the max free space. |
| 1080 | * |
| 1081 | * @len is used to store the size of the free space that we find. |
| 1082 | * But if we don't find suitable free space, it is used to store the size of |
| 1083 | * the max free space. |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1084 | */ |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1085 | int find_free_dev_extent(struct btrfs_trans_handle *trans, |
| 1086 | struct btrfs_device *device, u64 num_bytes, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1087 | u64 *start, u64 *len) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1088 | { |
| 1089 | struct btrfs_key key; |
| 1090 | struct btrfs_root *root = device->dev_root; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1091 | struct btrfs_dev_extent *dev_extent; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1092 | struct btrfs_path *path; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1093 | u64 hole_size; |
| 1094 | u64 max_hole_start; |
| 1095 | u64 max_hole_size; |
| 1096 | u64 extent_end; |
| 1097 | u64 search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1098 | u64 search_end = device->total_bytes; |
| 1099 | int ret; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1100 | int slot; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1101 | struct extent_buffer *l; |
| 1102 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1103 | /* FIXME use last free of some kind */ |
| 1104 | |
| 1105 | /* we don't want to overwrite the superblock on the drive, |
| 1106 | * so we make sure to start at an offset of at least 1MB |
| 1107 | */ |
Arne Jansen | a9c9bf6 | 2011-04-12 11:01:20 +0200 | [diff] [blame] | 1108 | search_start = max(root->fs_info->alloc_start, 1024ull * 1024); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1109 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1110 | path = btrfs_alloc_path(); |
| 1111 | if (!path) |
| 1112 | return -ENOMEM; |
| 1113 | again: |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1114 | max_hole_start = search_start; |
| 1115 | max_hole_size = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1116 | hole_size = 0; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1117 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1118 | if (search_start >= search_end || device->is_tgtdev_for_dev_replace) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1119 | ret = -ENOSPC; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1120 | goto out; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1123 | path->reada = 2; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1124 | path->search_commit_root = 1; |
| 1125 | path->skip_locking = 1; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1126 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1127 | key.objectid = device->devid; |
| 1128 | key.offset = search_start; |
| 1129 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1130 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1131 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1132 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1133 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1134 | if (ret > 0) { |
| 1135 | ret = btrfs_previous_item(root, path, key.objectid, key.type); |
| 1136 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1137 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1138 | } |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1139 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1140 | while (1) { |
| 1141 | l = path->nodes[0]; |
| 1142 | slot = path->slots[0]; |
| 1143 | if (slot >= btrfs_header_nritems(l)) { |
| 1144 | ret = btrfs_next_leaf(root, path); |
| 1145 | if (ret == 0) |
| 1146 | continue; |
| 1147 | if (ret < 0) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1148 | goto out; |
| 1149 | |
| 1150 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1151 | } |
| 1152 | btrfs_item_key_to_cpu(l, &key, slot); |
| 1153 | |
| 1154 | if (key.objectid < device->devid) |
| 1155 | goto next; |
| 1156 | |
| 1157 | if (key.objectid > device->devid) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1158 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1159 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1160 | if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) |
| 1161 | goto next; |
| 1162 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1163 | if (key.offset > search_start) { |
| 1164 | hole_size = key.offset - search_start; |
| 1165 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1166 | /* |
| 1167 | * Have to check before we set max_hole_start, otherwise |
| 1168 | * we could end up sending back this offset anyway. |
| 1169 | */ |
| 1170 | if (contains_pending_extent(trans, device, |
| 1171 | &search_start, |
| 1172 | hole_size)) |
| 1173 | hole_size = 0; |
| 1174 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1175 | if (hole_size > max_hole_size) { |
| 1176 | max_hole_start = search_start; |
| 1177 | max_hole_size = hole_size; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * If this free space is greater than which we need, |
| 1182 | * it must be the max free space that we have found |
| 1183 | * until now, so max_hole_start must point to the start |
| 1184 | * of this free space and the length of this free space |
| 1185 | * is stored in max_hole_size. Thus, we return |
| 1186 | * max_hole_start and max_hole_size and go back to the |
| 1187 | * caller. |
| 1188 | */ |
| 1189 | if (hole_size >= num_bytes) { |
| 1190 | ret = 0; |
| 1191 | goto out; |
| 1192 | } |
| 1193 | } |
| 1194 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1195 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1196 | extent_end = key.offset + btrfs_dev_extent_length(l, |
| 1197 | dev_extent); |
| 1198 | if (extent_end > search_start) |
| 1199 | search_start = extent_end; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1200 | next: |
| 1201 | path->slots[0]++; |
| 1202 | cond_resched(); |
| 1203 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1204 | |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 1205 | /* |
| 1206 | * At this point, search_start should be the end of |
| 1207 | * allocated dev extents, and when shrinking the device, |
| 1208 | * search_end may be smaller than search_start. |
| 1209 | */ |
| 1210 | if (search_end > search_start) |
| 1211 | hole_size = search_end - search_start; |
| 1212 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1213 | if (hole_size > max_hole_size) { |
| 1214 | max_hole_start = search_start; |
| 1215 | max_hole_size = hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1216 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1217 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1218 | if (contains_pending_extent(trans, device, &search_start, hole_size)) { |
| 1219 | btrfs_release_path(path); |
| 1220 | goto again; |
| 1221 | } |
| 1222 | |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1223 | /* See above. */ |
| 1224 | if (hole_size < num_bytes) |
| 1225 | ret = -ENOSPC; |
| 1226 | else |
| 1227 | ret = 0; |
| 1228 | |
| 1229 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1230 | btrfs_free_path(path); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1231 | *start = max_hole_start; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 1232 | if (len) |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 1233 | *len = max_hole_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1234 | return ret; |
| 1235 | } |
| 1236 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 1237 | static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1238 | struct btrfs_device *device, |
| 1239 | u64 start) |
| 1240 | { |
| 1241 | int ret; |
| 1242 | struct btrfs_path *path; |
| 1243 | struct btrfs_root *root = device->dev_root; |
| 1244 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1245 | struct btrfs_key found_key; |
| 1246 | struct extent_buffer *leaf = NULL; |
| 1247 | struct btrfs_dev_extent *extent = NULL; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1248 | |
| 1249 | path = btrfs_alloc_path(); |
| 1250 | if (!path) |
| 1251 | return -ENOMEM; |
| 1252 | |
| 1253 | key.objectid = device->devid; |
| 1254 | key.offset = start; |
| 1255 | key.type = BTRFS_DEV_EXTENT_KEY; |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1256 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1257 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1258 | if (ret > 0) { |
| 1259 | ret = btrfs_previous_item(root, path, key.objectid, |
| 1260 | BTRFS_DEV_EXTENT_KEY); |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1261 | if (ret) |
| 1262 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1263 | leaf = path->nodes[0]; |
| 1264 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 1265 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1266 | struct btrfs_dev_extent); |
| 1267 | BUG_ON(found_key.offset > start || found_key.offset + |
| 1268 | btrfs_dev_extent_length(leaf, extent) < start); |
Miao Xie | 924cd8f | 2011-11-10 20:45:04 -0500 | [diff] [blame] | 1269 | key = found_key; |
| 1270 | btrfs_release_path(path); |
| 1271 | goto again; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1272 | } else if (ret == 0) { |
| 1273 | leaf = path->nodes[0]; |
| 1274 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1275 | struct btrfs_dev_extent); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1276 | } else { |
| 1277 | btrfs_error(root->fs_info, ret, "Slot search failed"); |
| 1278 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1279 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1280 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1281 | if (device->bytes_used > 0) { |
| 1282 | u64 len = btrfs_dev_extent_length(leaf, extent); |
| 1283 | device->bytes_used -= len; |
| 1284 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1285 | root->fs_info->free_chunk_space += len; |
| 1286 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1287 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1288 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1289 | if (ret) { |
| 1290 | btrfs_error(root->fs_info, ret, |
| 1291 | "Failed to remove dev extent item"); |
| 1292 | } |
Tsutomu Itoh | b0b802d | 2011-05-19 07:03:42 +0000 | [diff] [blame] | 1293 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1294 | btrfs_free_path(path); |
| 1295 | return ret; |
| 1296 | } |
| 1297 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1298 | static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, |
| 1299 | struct btrfs_device *device, |
| 1300 | u64 chunk_tree, u64 chunk_objectid, |
| 1301 | u64 chunk_offset, u64 start, u64 num_bytes) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1302 | { |
| 1303 | int ret; |
| 1304 | struct btrfs_path *path; |
| 1305 | struct btrfs_root *root = device->dev_root; |
| 1306 | struct btrfs_dev_extent *extent; |
| 1307 | struct extent_buffer *leaf; |
| 1308 | struct btrfs_key key; |
| 1309 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1310 | WARN_ON(!device->in_fs_metadata); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1311 | WARN_ON(device->is_tgtdev_for_dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1312 | path = btrfs_alloc_path(); |
| 1313 | if (!path) |
| 1314 | return -ENOMEM; |
| 1315 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1316 | key.objectid = device->devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1317 | key.offset = start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1318 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 1319 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1320 | sizeof(*extent)); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1321 | if (ret) |
| 1322 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1323 | |
| 1324 | leaf = path->nodes[0]; |
| 1325 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 1326 | struct btrfs_dev_extent); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1327 | btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree); |
| 1328 | btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); |
| 1329 | btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); |
| 1330 | |
| 1331 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
Geert Uytterhoeven | 231e88f | 2013-08-20 13:20:13 +0200 | [diff] [blame] | 1332 | btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1333 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1334 | btrfs_set_dev_extent_length(leaf, extent, num_bytes); |
| 1335 | btrfs_mark_buffer_dirty(leaf); |
Mark Fasheh | 2cdcecb | 2011-09-08 17:14:32 -0700 | [diff] [blame] | 1336 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1337 | btrfs_free_path(path); |
| 1338 | return ret; |
| 1339 | } |
| 1340 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1341 | static u64 find_next_chunk(struct btrfs_fs_info *fs_info) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1342 | { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1343 | struct extent_map_tree *em_tree; |
| 1344 | struct extent_map *em; |
| 1345 | struct rb_node *n; |
| 1346 | u64 ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1347 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1348 | em_tree = &fs_info->mapping_tree.map_tree; |
| 1349 | read_lock(&em_tree->lock); |
| 1350 | n = rb_last(&em_tree->map); |
| 1351 | if (n) { |
| 1352 | em = rb_entry(n, struct extent_map, rb_node); |
| 1353 | ret = em->start + em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1354 | } |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 1355 | read_unlock(&em_tree->lock); |
| 1356 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1357 | return ret; |
| 1358 | } |
| 1359 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1360 | static noinline int find_next_devid(struct btrfs_fs_info *fs_info, |
| 1361 | u64 *devid_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1362 | { |
| 1363 | int ret; |
| 1364 | struct btrfs_key key; |
| 1365 | struct btrfs_key found_key; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1366 | struct btrfs_path *path; |
| 1367 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1368 | path = btrfs_alloc_path(); |
| 1369 | if (!path) |
| 1370 | return -ENOMEM; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1371 | |
| 1372 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1373 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1374 | key.offset = (u64)-1; |
| 1375 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1376 | 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] | 1377 | if (ret < 0) |
| 1378 | goto error; |
| 1379 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1380 | BUG_ON(ret == 0); /* Corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1381 | |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1382 | ret = btrfs_previous_item(fs_info->chunk_root, path, |
| 1383 | BTRFS_DEV_ITEMS_OBJECTID, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1384 | BTRFS_DEV_ITEM_KEY); |
| 1385 | if (ret) { |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1386 | *devid_ret = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1387 | } else { |
| 1388 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1389 | path->slots[0]); |
Ilya Dryomov | 53f1065 | 2013-08-12 14:33:01 +0300 | [diff] [blame] | 1390 | *devid_ret = found_key.offset + 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1391 | } |
| 1392 | ret = 0; |
| 1393 | error: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1394 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1395 | return ret; |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | * the device information is stored in the chunk root |
| 1400 | * the btrfs_device struct should be fully filled in |
| 1401 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1402 | static int btrfs_add_device(struct btrfs_trans_handle *trans, |
| 1403 | struct btrfs_root *root, |
| 1404 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1405 | { |
| 1406 | int ret; |
| 1407 | struct btrfs_path *path; |
| 1408 | struct btrfs_dev_item *dev_item; |
| 1409 | struct extent_buffer *leaf; |
| 1410 | struct btrfs_key key; |
| 1411 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1412 | |
| 1413 | root = root->fs_info->chunk_root; |
| 1414 | |
| 1415 | path = btrfs_alloc_path(); |
| 1416 | if (!path) |
| 1417 | return -ENOMEM; |
| 1418 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1419 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1420 | key.type = BTRFS_DEV_ITEM_KEY; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1421 | key.offset = device->devid; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1422 | |
| 1423 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1424 | sizeof(*dev_item)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1425 | if (ret) |
| 1426 | goto out; |
| 1427 | |
| 1428 | leaf = path->nodes[0]; |
| 1429 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 1430 | |
| 1431 | btrfs_set_device_id(leaf, dev_item, device->devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1432 | btrfs_set_device_generation(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1433 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 1434 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 1435 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 1436 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1437 | btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes); |
| 1438 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1439 | btrfs_set_device_group(leaf, dev_item, 0); |
| 1440 | btrfs_set_device_seek_speed(leaf, dev_item, 0); |
| 1441 | btrfs_set_device_bandwidth(leaf, dev_item, 0); |
Chris Mason | c3027eb | 2008-12-08 16:40:21 -0500 | [diff] [blame] | 1442 | btrfs_set_device_start_offset(leaf, dev_item, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1443 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 1444 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1445 | write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 1446 | ptr = btrfs_device_fsid(dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1447 | write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1448 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1449 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1450 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1451 | out: |
| 1452 | btrfs_free_path(path); |
| 1453 | return ret; |
| 1454 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1455 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1456 | /* |
| 1457 | * Function to update ctime/mtime for a given device path. |
| 1458 | * Mainly used for ctime/mtime based probe like libblkid. |
| 1459 | */ |
| 1460 | static void update_dev_time(char *path_name) |
| 1461 | { |
| 1462 | struct file *filp; |
| 1463 | |
| 1464 | filp = filp_open(path_name, O_RDWR, 0); |
| 1465 | if (!filp) |
| 1466 | return; |
| 1467 | file_update_time(filp); |
| 1468 | filp_close(filp, NULL); |
| 1469 | return; |
| 1470 | } |
| 1471 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1472 | static int btrfs_rm_dev_item(struct btrfs_root *root, |
| 1473 | struct btrfs_device *device) |
| 1474 | { |
| 1475 | int ret; |
| 1476 | struct btrfs_path *path; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1477 | struct btrfs_key key; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1478 | struct btrfs_trans_handle *trans; |
| 1479 | |
| 1480 | root = root->fs_info->chunk_root; |
| 1481 | |
| 1482 | path = btrfs_alloc_path(); |
| 1483 | if (!path) |
| 1484 | return -ENOMEM; |
| 1485 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1486 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 1487 | if (IS_ERR(trans)) { |
| 1488 | btrfs_free_path(path); |
| 1489 | return PTR_ERR(trans); |
| 1490 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1491 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1492 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1493 | key.offset = device->devid; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1494 | lock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1495 | |
| 1496 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1497 | if (ret < 0) |
| 1498 | goto out; |
| 1499 | |
| 1500 | if (ret > 0) { |
| 1501 | ret = -ENOENT; |
| 1502 | goto out; |
| 1503 | } |
| 1504 | |
| 1505 | ret = btrfs_del_item(trans, root, path); |
| 1506 | if (ret) |
| 1507 | goto out; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1508 | out: |
| 1509 | btrfs_free_path(path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1510 | unlock_chunks(root); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1511 | btrfs_commit_transaction(trans, root); |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
| 1515 | int btrfs_rm_device(struct btrfs_root *root, char *device_path) |
| 1516 | { |
| 1517 | struct btrfs_device *device; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1518 | struct btrfs_device *next_device; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1519 | struct block_device *bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1520 | struct buffer_head *bh = NULL; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1521 | struct btrfs_super_block *disk_super; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1522 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1523 | u64 all_avail; |
| 1524 | u64 devid; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1525 | u64 num_devices; |
| 1526 | u8 *dev_uuid; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1527 | unsigned seq; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1528 | int ret = 0; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1529 | bool clear_super = false; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1530 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1531 | mutex_lock(&uuid_mutex); |
| 1532 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 1533 | do { |
| 1534 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 1535 | |
| 1536 | all_avail = root->fs_info->avail_data_alloc_bits | |
| 1537 | root->fs_info->avail_system_alloc_bits | |
| 1538 | root->fs_info->avail_metadata_alloc_bits; |
| 1539 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1540 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1541 | num_devices = root->fs_info->fs_devices->num_devices; |
| 1542 | btrfs_dev_replace_lock(&root->fs_info->dev_replace); |
| 1543 | if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { |
| 1544 | WARN_ON(num_devices < 1); |
| 1545 | num_devices--; |
| 1546 | } |
| 1547 | btrfs_dev_replace_unlock(&root->fs_info->dev_replace); |
| 1548 | |
| 1549 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1550 | ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1551 | goto out; |
| 1552 | } |
| 1553 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 1554 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1555 | ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1556 | goto out; |
| 1557 | } |
| 1558 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1559 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) && |
| 1560 | root->fs_info->fs_devices->rw_devices <= 2) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1561 | ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1562 | goto out; |
| 1563 | } |
| 1564 | if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) && |
| 1565 | root->fs_info->fs_devices->rw_devices <= 3) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1566 | ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1567 | goto out; |
| 1568 | } |
| 1569 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1570 | if (strcmp(device_path, "missing") == 0) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1571 | struct list_head *devices; |
| 1572 | struct btrfs_device *tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1573 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1574 | device = NULL; |
| 1575 | devices = &root->fs_info->fs_devices->devices; |
Xiao Guangrong | 4622470 | 2011-04-20 10:08:47 +0000 | [diff] [blame] | 1576 | /* |
| 1577 | * It is safe to read the devices since the volume_mutex |
| 1578 | * is held. |
| 1579 | */ |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 1580 | list_for_each_entry(tmp, devices, dev_list) { |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1581 | if (tmp->in_fs_metadata && |
| 1582 | !tmp->is_tgtdev_for_dev_replace && |
| 1583 | !tmp->bdev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1584 | device = tmp; |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | bdev = NULL; |
| 1589 | bh = NULL; |
| 1590 | disk_super = NULL; |
| 1591 | if (!device) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1592 | ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1593 | goto out; |
| 1594 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1595 | } else { |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1596 | ret = btrfs_get_bdev_and_sb(device_path, |
Lukas Czerner | cc975eb | 2012-12-07 10:09:19 +0000 | [diff] [blame] | 1597 | FMODE_WRITE | FMODE_EXCL, |
Stefan Behrens | beaf8ab | 2012-11-12 14:03:45 +0100 | [diff] [blame] | 1598 | root->fs_info->bdev_holder, 0, |
| 1599 | &bdev, &bh); |
| 1600 | if (ret) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1601 | goto out; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1602 | disk_super = (struct btrfs_super_block *)bh->b_data; |
Xiao Guangrong | a343832 | 2010-01-06 11:48:18 +0000 | [diff] [blame] | 1603 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1604 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1605 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1606 | disk_super->fsid); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1607 | if (!device) { |
| 1608 | ret = -ENOENT; |
| 1609 | goto error_brelse; |
| 1610 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1611 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1612 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1613 | if (device->is_tgtdev_for_dev_replace) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1614 | ret = BTRFS_ERROR_DEV_TGT_REPLACE; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1615 | goto error_brelse; |
| 1616 | } |
| 1617 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1618 | if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { |
Anand Jain | 183860f | 2013-05-17 10:52:45 +0000 | [diff] [blame] | 1619 | ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1620 | goto error_brelse; |
| 1621 | } |
| 1622 | |
| 1623 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1624 | lock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1625 | list_del_init(&device->dev_alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1626 | unlock_chunks(root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1627 | root->fs_info->fs_devices->rw_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1628 | clear_super = true; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1629 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1630 | |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1631 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1632 | ret = btrfs_shrink_device(device, 0); |
Carey Underwood | d790155 | 2013-03-04 16:37:06 -0600 | [diff] [blame] | 1633 | mutex_lock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1634 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1635 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1636 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 1637 | /* |
| 1638 | * TODO: the superblock still includes this device in its num_devices |
| 1639 | * counter although write_all_supers() is not locked out. This |
| 1640 | * could give a filesystem state which requires a degraded mount. |
| 1641 | */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1642 | ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device); |
| 1643 | if (ret) |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1644 | goto error_undo; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1645 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 1646 | spin_lock(&root->fs_info->free_chunk_lock); |
| 1647 | root->fs_info->free_chunk_space = device->total_bytes - |
| 1648 | device->bytes_used; |
| 1649 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 1650 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1651 | device->in_fs_metadata = 0; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1652 | btrfs_scrub_cancel_dev(root->fs_info, device); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1653 | |
| 1654 | /* |
| 1655 | * the device list mutex makes sure that we don't change |
| 1656 | * the device list while someone else is writing out all |
Filipe David Borba Manana | d730680 | 2013-08-09 15:41:36 +0100 | [diff] [blame] | 1657 | * the device supers. Whoever is writing all supers, should |
| 1658 | * lock the device list mutex before getting the number of |
| 1659 | * devices in the super block (super_copy). Conversely, |
| 1660 | * whoever updates the number of devices in the super block |
| 1661 | * (super_copy) should hold the device list mutex. |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1662 | */ |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1663 | |
| 1664 | cur_devices = device->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1665 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1666 | list_del_rcu(&device->dev_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1667 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1668 | device->fs_devices->num_devices--; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 1669 | device->fs_devices->total_devices--; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1670 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 1671 | if (device->missing) |
| 1672 | root->fs_info->fs_devices->missing_devices--; |
| 1673 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1674 | next_device = list_entry(root->fs_info->fs_devices->devices.next, |
| 1675 | struct btrfs_device, dev_list); |
| 1676 | if (device->bdev == root->fs_info->sb->s_bdev) |
| 1677 | root->fs_info->sb->s_bdev = next_device->bdev; |
| 1678 | if (device->bdev == root->fs_info->fs_devices->latest_bdev) |
| 1679 | root->fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1680 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1681 | if (device->bdev) |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1682 | device->fs_devices->open_devices--; |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1683 | |
Anand Jain | 99994cd | 2014-06-03 11:36:00 +0800 | [diff] [blame^] | 1684 | /* remove sysfs entry */ |
| 1685 | btrfs_kobj_rm_device(root->fs_info, device); |
| 1686 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1687 | call_rcu(&device->rcu, free_device); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1688 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1689 | num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; |
| 1690 | 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] | 1691 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1692 | |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1693 | if (cur_devices->open_devices == 0) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1694 | struct btrfs_fs_devices *fs_devices; |
| 1695 | fs_devices = root->fs_info->fs_devices; |
| 1696 | while (fs_devices) { |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1697 | if (fs_devices->seed == cur_devices) { |
| 1698 | fs_devices->seed = cur_devices->seed; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1699 | break; |
Rickard Strandqvist | 8321cf2 | 2014-05-22 22:43:43 +0200 | [diff] [blame] | 1700 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1701 | fs_devices = fs_devices->seed; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1702 | } |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1703 | cur_devices->seed = NULL; |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1704 | lock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1705 | __btrfs_close_devices(cur_devices); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1706 | unlock_chunks(root); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1707 | free_fs_devices(cur_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1708 | } |
| 1709 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 1710 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 1711 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
| 1712 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1713 | /* |
| 1714 | * at this point, the device is zero sized. We want to |
| 1715 | * remove it from the devices list and zero out the old super |
| 1716 | */ |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1717 | if (clear_super && disk_super) { |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1718 | u64 bytenr; |
| 1719 | int i; |
| 1720 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1721 | /* make sure this device isn't detected as part of |
| 1722 | * the FS anymore |
| 1723 | */ |
| 1724 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 1725 | set_buffer_dirty(bh); |
| 1726 | sync_dirty_buffer(bh); |
Anand Jain | 4d90d28 | 2014-04-06 12:59:07 +0800 | [diff] [blame] | 1727 | |
| 1728 | /* clear the mirror copies of super block on the disk |
| 1729 | * being removed, 0th copy is been taken care above and |
| 1730 | * the below would take of the rest |
| 1731 | */ |
| 1732 | for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 1733 | bytenr = btrfs_sb_offset(i); |
| 1734 | if (bytenr + BTRFS_SUPER_INFO_SIZE >= |
| 1735 | i_size_read(bdev->bd_inode)) |
| 1736 | break; |
| 1737 | |
| 1738 | brelse(bh); |
| 1739 | bh = __bread(bdev, bytenr / 4096, |
| 1740 | BTRFS_SUPER_INFO_SIZE); |
| 1741 | if (!bh) |
| 1742 | continue; |
| 1743 | |
| 1744 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1745 | |
| 1746 | if (btrfs_super_bytenr(disk_super) != bytenr || |
| 1747 | btrfs_super_magic(disk_super) != BTRFS_MAGIC) { |
| 1748 | continue; |
| 1749 | } |
| 1750 | memset(&disk_super->magic, 0, |
| 1751 | sizeof(disk_super->magic)); |
| 1752 | set_buffer_dirty(bh); |
| 1753 | sync_dirty_buffer(bh); |
| 1754 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1755 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1756 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1757 | ret = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1758 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1759 | if (bdev) { |
| 1760 | /* Notify udev that device has changed */ |
Eric Sandeen | 3c91160 | 2013-01-31 00:55:02 +0000 | [diff] [blame] | 1761 | btrfs_kobject_uevent(bdev, KOBJ_CHANGE); |
Lukas Czerner | b8b8ff5 | 2012-12-06 19:25:48 +0000 | [diff] [blame] | 1762 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 1763 | /* Update ctime/mtime for device path for libblkid */ |
| 1764 | update_dev_time(device_path); |
| 1765 | } |
| 1766 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1767 | error_brelse: |
| 1768 | brelse(bh); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 1769 | if (bdev) |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 1770 | blkdev_put(bdev, FMODE_READ | FMODE_EXCL); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1771 | out: |
| 1772 | mutex_unlock(&uuid_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1773 | return ret; |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1774 | error_undo: |
| 1775 | if (device->writeable) { |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1776 | lock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1777 | list_add(&device->dev_alloc_list, |
| 1778 | &root->fs_info->fs_devices->alloc_list); |
Xiao Guangrong | 0c1daee | 2011-04-20 10:08:16 +0000 | [diff] [blame] | 1779 | unlock_chunks(root); |
Ilya Dryomov | 9b3517e | 2011-02-15 18:14:25 +0000 | [diff] [blame] | 1780 | root->fs_info->fs_devices->rw_devices++; |
| 1781 | } |
| 1782 | goto error_brelse; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1783 | } |
| 1784 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1785 | void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info, |
| 1786 | struct btrfs_device *srcdev) |
| 1787 | { |
| 1788 | WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex)); |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1789 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1790 | list_del_rcu(&srcdev->dev_list); |
| 1791 | list_del_rcu(&srcdev->dev_alloc_list); |
| 1792 | fs_info->fs_devices->num_devices--; |
| 1793 | if (srcdev->missing) { |
| 1794 | fs_info->fs_devices->missing_devices--; |
| 1795 | fs_info->fs_devices->rw_devices++; |
| 1796 | } |
| 1797 | if (srcdev->can_discard) |
| 1798 | fs_info->fs_devices->num_can_discard--; |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1799 | if (srcdev->bdev) { |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1800 | fs_info->fs_devices->open_devices--; |
| 1801 | |
Ilya Dryomov | 1357272 | 2013-10-02 20:41:01 +0300 | [diff] [blame] | 1802 | /* zero out the old super */ |
| 1803 | btrfs_scratch_superblock(srcdev); |
| 1804 | } |
| 1805 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 1806 | call_rcu(&srcdev->rcu, free_device); |
| 1807 | } |
| 1808 | |
| 1809 | void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info, |
| 1810 | struct btrfs_device *tgtdev) |
| 1811 | { |
| 1812 | struct btrfs_device *next_device; |
| 1813 | |
| 1814 | WARN_ON(!tgtdev); |
| 1815 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
| 1816 | if (tgtdev->bdev) { |
| 1817 | btrfs_scratch_superblock(tgtdev); |
| 1818 | fs_info->fs_devices->open_devices--; |
| 1819 | } |
| 1820 | fs_info->fs_devices->num_devices--; |
| 1821 | if (tgtdev->can_discard) |
| 1822 | fs_info->fs_devices->num_can_discard++; |
| 1823 | |
| 1824 | next_device = list_entry(fs_info->fs_devices->devices.next, |
| 1825 | struct btrfs_device, dev_list); |
| 1826 | if (tgtdev->bdev == fs_info->sb->s_bdev) |
| 1827 | fs_info->sb->s_bdev = next_device->bdev; |
| 1828 | if (tgtdev->bdev == fs_info->fs_devices->latest_bdev) |
| 1829 | fs_info->fs_devices->latest_bdev = next_device->bdev; |
| 1830 | list_del_rcu(&tgtdev->dev_list); |
| 1831 | |
| 1832 | call_rcu(&tgtdev->rcu, free_device); |
| 1833 | |
| 1834 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
| 1835 | } |
| 1836 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 1837 | static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, |
| 1838 | struct btrfs_device **device) |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1839 | { |
| 1840 | int ret = 0; |
| 1841 | struct btrfs_super_block *disk_super; |
| 1842 | u64 devid; |
| 1843 | u8 *dev_uuid; |
| 1844 | struct block_device *bdev; |
| 1845 | struct buffer_head *bh; |
| 1846 | |
| 1847 | *device = NULL; |
| 1848 | ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, |
| 1849 | root->fs_info->bdev_holder, 0, &bdev, &bh); |
| 1850 | if (ret) |
| 1851 | return ret; |
| 1852 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 1853 | devid = btrfs_stack_device_id(&disk_super->dev_item); |
| 1854 | dev_uuid = disk_super->dev_item.uuid; |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 1855 | *device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1856 | disk_super->fsid); |
| 1857 | brelse(bh); |
| 1858 | if (!*device) |
| 1859 | ret = -ENOENT; |
| 1860 | blkdev_put(bdev, FMODE_READ); |
| 1861 | return ret; |
| 1862 | } |
| 1863 | |
| 1864 | int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, |
| 1865 | char *device_path, |
| 1866 | struct btrfs_device **device) |
| 1867 | { |
| 1868 | *device = NULL; |
| 1869 | if (strcmp(device_path, "missing") == 0) { |
| 1870 | struct list_head *devices; |
| 1871 | struct btrfs_device *tmp; |
| 1872 | |
| 1873 | devices = &root->fs_info->fs_devices->devices; |
| 1874 | /* |
| 1875 | * It is safe to read the devices since the volume_mutex |
| 1876 | * is held by the caller. |
| 1877 | */ |
| 1878 | list_for_each_entry(tmp, devices, dev_list) { |
| 1879 | if (tmp->in_fs_metadata && !tmp->bdev) { |
| 1880 | *device = tmp; |
| 1881 | break; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | if (!*device) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1886 | btrfs_err(root->fs_info, "no missing device found"); |
Stefan Behrens | 7ba15b7 | 2012-11-05 14:42:30 +0100 | [diff] [blame] | 1887 | return -ENOENT; |
| 1888 | } |
| 1889 | |
| 1890 | return 0; |
| 1891 | } else { |
| 1892 | return btrfs_find_device_by_path(root, device_path, device); |
| 1893 | } |
| 1894 | } |
| 1895 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1896 | /* |
| 1897 | * does all the dirty work required for changing file system's UUID. |
| 1898 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 1899 | static int btrfs_prepare_sprout(struct btrfs_root *root) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1900 | { |
| 1901 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 1902 | struct btrfs_fs_devices *old_devices; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1903 | struct btrfs_fs_devices *seed_devices; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 1904 | struct btrfs_super_block *disk_super = root->fs_info->super_copy; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1905 | struct btrfs_device *device; |
| 1906 | u64 super_flags; |
| 1907 | |
| 1908 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1909 | if (!fs_devices->seeding) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1910 | return -EINVAL; |
| 1911 | |
Ilya Dryomov | 2208a37 | 2013-08-12 14:33:03 +0300 | [diff] [blame] | 1912 | seed_devices = __alloc_fs_devices(); |
| 1913 | if (IS_ERR(seed_devices)) |
| 1914 | return PTR_ERR(seed_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1915 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1916 | old_devices = clone_fs_devices(fs_devices); |
| 1917 | if (IS_ERR(old_devices)) { |
| 1918 | kfree(seed_devices); |
| 1919 | return PTR_ERR(old_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1920 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1921 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1922 | list_add(&old_devices->list, &fs_uuids); |
| 1923 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1924 | memcpy(seed_devices, fs_devices, sizeof(*seed_devices)); |
| 1925 | seed_devices->opened = 1; |
| 1926 | INIT_LIST_HEAD(&seed_devices->devices); |
| 1927 | INIT_LIST_HEAD(&seed_devices->alloc_list); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 1928 | mutex_init(&seed_devices->device_list_mutex); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1929 | |
| 1930 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 1931 | list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, |
| 1932 | synchronize_rcu); |
Xiao Guangrong | c9513ed | 2011-04-20 10:07:30 +0000 | [diff] [blame] | 1933 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1934 | list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); |
| 1935 | list_for_each_entry(device, &seed_devices->devices, dev_list) { |
| 1936 | device->fs_devices = seed_devices; |
| 1937 | } |
| 1938 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1939 | fs_devices->seeding = 0; |
| 1940 | fs_devices->num_devices = 0; |
| 1941 | fs_devices->open_devices = 0; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 1942 | fs_devices->seed = seed_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1943 | |
| 1944 | generate_random_uuid(fs_devices->fsid); |
| 1945 | memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
| 1946 | memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); |
Filipe David Borba Manana | f717175 | 2013-08-12 20:56:58 +0100 | [diff] [blame] | 1947 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 1948 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1949 | super_flags = btrfs_super_flags(disk_super) & |
| 1950 | ~BTRFS_SUPER_FLAG_SEEDING; |
| 1951 | btrfs_set_super_flags(disk_super, super_flags); |
| 1952 | |
| 1953 | return 0; |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * strore the expected generation for seed devices in device items. |
| 1958 | */ |
| 1959 | static int btrfs_finish_sprout(struct btrfs_trans_handle *trans, |
| 1960 | struct btrfs_root *root) |
| 1961 | { |
| 1962 | struct btrfs_path *path; |
| 1963 | struct extent_buffer *leaf; |
| 1964 | struct btrfs_dev_item *dev_item; |
| 1965 | struct btrfs_device *device; |
| 1966 | struct btrfs_key key; |
| 1967 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
| 1968 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 1969 | u64 devid; |
| 1970 | int ret; |
| 1971 | |
| 1972 | path = btrfs_alloc_path(); |
| 1973 | if (!path) |
| 1974 | return -ENOMEM; |
| 1975 | |
| 1976 | root = root->fs_info->chunk_root; |
| 1977 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 1978 | key.offset = 0; |
| 1979 | key.type = BTRFS_DEV_ITEM_KEY; |
| 1980 | |
| 1981 | while (1) { |
| 1982 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1983 | if (ret < 0) |
| 1984 | goto error; |
| 1985 | |
| 1986 | leaf = path->nodes[0]; |
| 1987 | next_slot: |
| 1988 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1989 | ret = btrfs_next_leaf(root, path); |
| 1990 | if (ret > 0) |
| 1991 | break; |
| 1992 | if (ret < 0) |
| 1993 | goto error; |
| 1994 | leaf = path->nodes[0]; |
| 1995 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1996 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1997 | continue; |
| 1998 | } |
| 1999 | |
| 2000 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2001 | if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID || |
| 2002 | key.type != BTRFS_DEV_ITEM_KEY) |
| 2003 | break; |
| 2004 | |
| 2005 | dev_item = btrfs_item_ptr(leaf, path->slots[0], |
| 2006 | struct btrfs_dev_item); |
| 2007 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 2008 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2009 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 2010 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2011 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 2012 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, |
| 2013 | fs_uuid); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2014 | BUG_ON(!device); /* Logic error */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2015 | |
| 2016 | if (device->fs_devices->seeding) { |
| 2017 | btrfs_set_device_generation(leaf, dev_item, |
| 2018 | device->generation); |
| 2019 | btrfs_mark_buffer_dirty(leaf); |
| 2020 | } |
| 2021 | |
| 2022 | path->slots[0]++; |
| 2023 | goto next_slot; |
| 2024 | } |
| 2025 | ret = 0; |
| 2026 | error: |
| 2027 | btrfs_free_path(path); |
| 2028 | return ret; |
| 2029 | } |
| 2030 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2031 | int btrfs_init_new_device(struct btrfs_root *root, char *device_path) |
| 2032 | { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2033 | struct request_queue *q; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2034 | struct btrfs_trans_handle *trans; |
| 2035 | struct btrfs_device *device; |
| 2036 | struct block_device *bdev; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2037 | struct list_head *devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2038 | struct super_block *sb = root->fs_info->sb; |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2039 | struct rcu_string *name; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2040 | u64 total_bytes; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2041 | int seeding_dev = 0; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2042 | int ret = 0; |
| 2043 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2044 | if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) |
Liu Bo | f8c5d0b | 2012-05-10 18:10:38 +0800 | [diff] [blame] | 2045 | return -EROFS; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2046 | |
Li Zefan | a5d1633 | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 2047 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2048 | root->fs_info->bdev_holder); |
Josef Bacik | 7f59203 | 2010-01-27 02:09:00 +0000 | [diff] [blame] | 2049 | if (IS_ERR(bdev)) |
| 2050 | return PTR_ERR(bdev); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2051 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2052 | if (root->fs_info->fs_devices->seeding) { |
| 2053 | seeding_dev = 1; |
| 2054 | down_write(&sb->s_umount); |
| 2055 | mutex_lock(&uuid_mutex); |
| 2056 | } |
| 2057 | |
Chris Mason | 8c8bee1 | 2008-09-29 11:19:10 -0400 | [diff] [blame] | 2058 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2059 | |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2060 | devices = &root->fs_info->fs_devices->devices; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2061 | |
| 2062 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2063 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2064 | if (device->bdev == bdev) { |
| 2065 | ret = -EEXIST; |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2066 | mutex_unlock( |
| 2067 | &root->fs_info->fs_devices->device_list_mutex); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2068 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2069 | } |
| 2070 | } |
Liu Bo | d25628b | 2012-11-14 14:35:30 +0000 | [diff] [blame] | 2071 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2072 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2073 | device = btrfs_alloc_device(root->fs_info, NULL, NULL); |
| 2074 | if (IS_ERR(device)) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2075 | /* we can safely leave the fs_devices entry around */ |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2076 | ret = PTR_ERR(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2077 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2078 | } |
| 2079 | |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2080 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2081 | if (!name) { |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2082 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2083 | ret = -ENOMEM; |
| 2084 | goto error; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2085 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2086 | rcu_assign_pointer(device->name, name); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2087 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2088 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2089 | if (IS_ERR(trans)) { |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2090 | rcu_string_free(device->name); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 2091 | kfree(device); |
| 2092 | ret = PTR_ERR(trans); |
| 2093 | goto error; |
| 2094 | } |
| 2095 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2096 | lock_chunks(root); |
| 2097 | |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2098 | q = bdev_get_queue(bdev); |
| 2099 | if (blk_queue_discard(q)) |
| 2100 | device->can_discard = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2101 | device->writeable = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2102 | device->generation = trans->transid; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2103 | device->io_width = root->sectorsize; |
| 2104 | device->io_align = root->sectorsize; |
| 2105 | device->sector_size = root->sectorsize; |
| 2106 | device->total_bytes = i_size_read(bdev->bd_inode); |
Yan Zheng | 2cc3c55 | 2009-06-04 09:23:50 -0400 | [diff] [blame] | 2107 | device->disk_total_bytes = device->total_bytes; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2108 | device->dev_root = root->fs_info->dev_root; |
| 2109 | device->bdev = bdev; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2110 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2111 | device->is_tgtdev_for_dev_replace = 0; |
Ilya Dryomov | fb01aa8 | 2011-02-15 18:12:57 +0000 | [diff] [blame] | 2112 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2113 | device->dev_stats_valid = 1; |
Zheng Yan | 325cd4b | 2008-09-05 16:43:54 -0400 | [diff] [blame] | 2114 | set_blocksize(device->bdev, 4096); |
| 2115 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2116 | if (seeding_dev) { |
| 2117 | sb->s_flags &= ~MS_RDONLY; |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 2118 | ret = btrfs_prepare_sprout(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2119 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | device->fs_devices = root->fs_info->fs_devices; |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2123 | |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2124 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
Xiao Guangrong | 1f78160 | 2011-04-20 10:09:16 +0000 | [diff] [blame] | 2125 | list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2126 | list_add(&device->dev_alloc_list, |
| 2127 | &root->fs_info->fs_devices->alloc_list); |
| 2128 | root->fs_info->fs_devices->num_devices++; |
| 2129 | root->fs_info->fs_devices->open_devices++; |
| 2130 | root->fs_info->fs_devices->rw_devices++; |
Josef Bacik | 02db084 | 2012-06-21 16:03:58 -0400 | [diff] [blame] | 2131 | root->fs_info->fs_devices->total_devices++; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 2132 | if (device->can_discard) |
| 2133 | root->fs_info->fs_devices->num_can_discard++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2134 | root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; |
| 2135 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 2136 | spin_lock(&root->fs_info->free_chunk_lock); |
| 2137 | root->fs_info->free_chunk_space += device->total_bytes; |
| 2138 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 2139 | |
Chris Mason | c289811 | 2009-06-10 09:51:32 -0400 | [diff] [blame] | 2140 | if (!blk_queue_nonrot(bdev_get_queue(bdev))) |
| 2141 | root->fs_info->fs_devices->rotating = 1; |
| 2142 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2143 | total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy); |
| 2144 | btrfs_set_super_total_bytes(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2145 | total_bytes + device->total_bytes); |
| 2146 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2147 | total_bytes = btrfs_super_num_devices(root->fs_info->super_copy); |
| 2148 | btrfs_set_super_num_devices(root->fs_info->super_copy, |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2149 | total_bytes + 1); |
Chris Mason | e5e9a52 | 2009-06-10 15:17:02 -0400 | [diff] [blame] | 2150 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2151 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2152 | if (seeding_dev) { |
| 2153 | ret = init_first_rw_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2154 | if (ret) { |
| 2155 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2156 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2157 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2158 | ret = btrfs_finish_sprout(trans, root); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2159 | if (ret) { |
| 2160 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2161 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2162 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2163 | } else { |
| 2164 | ret = btrfs_add_device(trans, root, device); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2165 | if (ret) { |
| 2166 | btrfs_abort_transaction(trans, root, ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2167 | goto error_trans; |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 2168 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2169 | } |
| 2170 | |
Chris Mason | 913d952 | 2009-03-10 13:17:18 -0400 | [diff] [blame] | 2171 | /* |
| 2172 | * we've got more storage, clear any full flags on the space |
| 2173 | * infos |
| 2174 | */ |
| 2175 | btrfs_clear_space_info_full(root->fs_info); |
| 2176 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2177 | unlock_chunks(root); |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 2178 | root->fs_info->num_tolerated_disk_barrier_failures = |
| 2179 | btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2180 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2181 | |
| 2182 | if (seeding_dev) { |
| 2183 | mutex_unlock(&uuid_mutex); |
| 2184 | up_write(&sb->s_umount); |
| 2185 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2186 | if (ret) /* transaction commit */ |
| 2187 | return ret; |
| 2188 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2189 | ret = btrfs_relocate_sys_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2190 | if (ret < 0) |
| 2191 | btrfs_error(root->fs_info, ret, |
| 2192 | "Failed to relocate sys chunks after " |
| 2193 | "device initialization. This can be fixed " |
| 2194 | "using the \"btrfs balance\" command."); |
Miao Xie | 671415b | 2012-10-16 11:26:46 +0000 | [diff] [blame] | 2195 | trans = btrfs_attach_transaction(root); |
| 2196 | if (IS_ERR(trans)) { |
| 2197 | if (PTR_ERR(trans) == -ENOENT) |
| 2198 | return 0; |
| 2199 | return PTR_ERR(trans); |
| 2200 | } |
| 2201 | ret = btrfs_commit_transaction(trans, root); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2202 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2203 | |
Qu Wenruo | 5a1972b | 2014-04-16 17:02:32 +0800 | [diff] [blame] | 2204 | /* Update ctime/mtime for libblkid */ |
| 2205 | update_dev_time(device_path); |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2206 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2207 | |
| 2208 | error_trans: |
| 2209 | unlock_chunks(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2210 | btrfs_end_transaction(trans, root); |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 2211 | rcu_string_free(device->name); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2212 | kfree(device); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2213 | error: |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2214 | blkdev_put(bdev, FMODE_EXCL); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2215 | if (seeding_dev) { |
| 2216 | mutex_unlock(&uuid_mutex); |
| 2217 | up_write(&sb->s_umount); |
| 2218 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2219 | return ret; |
Chris Mason | 788f20e | 2008-04-28 15:29:42 -0400 | [diff] [blame] | 2220 | } |
| 2221 | |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2222 | int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, |
| 2223 | struct btrfs_device **device_out) |
| 2224 | { |
| 2225 | struct request_queue *q; |
| 2226 | struct btrfs_device *device; |
| 2227 | struct block_device *bdev; |
| 2228 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2229 | struct list_head *devices; |
| 2230 | struct rcu_string *name; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2231 | u64 devid = BTRFS_DEV_REPLACE_DEVID; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2232 | int ret = 0; |
| 2233 | |
| 2234 | *device_out = NULL; |
| 2235 | if (fs_info->fs_devices->seeding) |
| 2236 | return -EINVAL; |
| 2237 | |
| 2238 | bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, |
| 2239 | fs_info->bdev_holder); |
| 2240 | if (IS_ERR(bdev)) |
| 2241 | return PTR_ERR(bdev); |
| 2242 | |
| 2243 | filemap_write_and_wait(bdev->bd_inode->i_mapping); |
| 2244 | |
| 2245 | devices = &fs_info->fs_devices->devices; |
| 2246 | list_for_each_entry(device, devices, dev_list) { |
| 2247 | if (device->bdev == bdev) { |
| 2248 | ret = -EEXIST; |
| 2249 | goto error; |
| 2250 | } |
| 2251 | } |
| 2252 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 2253 | device = btrfs_alloc_device(NULL, &devid, NULL); |
| 2254 | if (IS_ERR(device)) { |
| 2255 | ret = PTR_ERR(device); |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2256 | goto error; |
| 2257 | } |
| 2258 | |
| 2259 | name = rcu_string_strdup(device_path, GFP_NOFS); |
| 2260 | if (!name) { |
| 2261 | kfree(device); |
| 2262 | ret = -ENOMEM; |
| 2263 | goto error; |
| 2264 | } |
| 2265 | rcu_assign_pointer(device->name, name); |
| 2266 | |
| 2267 | q = bdev_get_queue(bdev); |
| 2268 | if (blk_queue_discard(q)) |
| 2269 | device->can_discard = 1; |
| 2270 | mutex_lock(&root->fs_info->fs_devices->device_list_mutex); |
| 2271 | device->writeable = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2272 | device->generation = 0; |
| 2273 | device->io_width = root->sectorsize; |
| 2274 | device->io_align = root->sectorsize; |
| 2275 | device->sector_size = root->sectorsize; |
| 2276 | device->total_bytes = i_size_read(bdev->bd_inode); |
| 2277 | device->disk_total_bytes = device->total_bytes; |
| 2278 | device->dev_root = fs_info->dev_root; |
| 2279 | device->bdev = bdev; |
| 2280 | device->in_fs_metadata = 1; |
| 2281 | device->is_tgtdev_for_dev_replace = 1; |
| 2282 | device->mode = FMODE_EXCL; |
Stefan Behrens | 27087f3 | 2013-10-11 15:20:42 +0200 | [diff] [blame] | 2283 | device->dev_stats_valid = 1; |
Stefan Behrens | e93c89c | 2012-11-05 17:33:06 +0100 | [diff] [blame] | 2284 | set_blocksize(device->bdev, 4096); |
| 2285 | device->fs_devices = fs_info->fs_devices; |
| 2286 | list_add(&device->dev_list, &fs_info->fs_devices->devices); |
| 2287 | fs_info->fs_devices->num_devices++; |
| 2288 | fs_info->fs_devices->open_devices++; |
| 2289 | if (device->can_discard) |
| 2290 | fs_info->fs_devices->num_can_discard++; |
| 2291 | mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); |
| 2292 | |
| 2293 | *device_out = device; |
| 2294 | return ret; |
| 2295 | |
| 2296 | error: |
| 2297 | blkdev_put(bdev, FMODE_EXCL); |
| 2298 | return ret; |
| 2299 | } |
| 2300 | |
| 2301 | void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, |
| 2302 | struct btrfs_device *tgtdev) |
| 2303 | { |
| 2304 | WARN_ON(fs_info->fs_devices->rw_devices == 0); |
| 2305 | tgtdev->io_width = fs_info->dev_root->sectorsize; |
| 2306 | tgtdev->io_align = fs_info->dev_root->sectorsize; |
| 2307 | tgtdev->sector_size = fs_info->dev_root->sectorsize; |
| 2308 | tgtdev->dev_root = fs_info->dev_root; |
| 2309 | tgtdev->in_fs_metadata = 1; |
| 2310 | } |
| 2311 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2312 | static noinline int btrfs_update_device(struct btrfs_trans_handle *trans, |
| 2313 | struct btrfs_device *device) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2314 | { |
| 2315 | int ret; |
| 2316 | struct btrfs_path *path; |
| 2317 | struct btrfs_root *root; |
| 2318 | struct btrfs_dev_item *dev_item; |
| 2319 | struct extent_buffer *leaf; |
| 2320 | struct btrfs_key key; |
| 2321 | |
| 2322 | root = device->dev_root->fs_info->chunk_root; |
| 2323 | |
| 2324 | path = btrfs_alloc_path(); |
| 2325 | if (!path) |
| 2326 | return -ENOMEM; |
| 2327 | |
| 2328 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 2329 | key.type = BTRFS_DEV_ITEM_KEY; |
| 2330 | key.offset = device->devid; |
| 2331 | |
| 2332 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 2333 | if (ret < 0) |
| 2334 | goto out; |
| 2335 | |
| 2336 | if (ret > 0) { |
| 2337 | ret = -ENOENT; |
| 2338 | goto out; |
| 2339 | } |
| 2340 | |
| 2341 | leaf = path->nodes[0]; |
| 2342 | dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item); |
| 2343 | |
| 2344 | btrfs_set_device_id(leaf, dev_item, device->devid); |
| 2345 | btrfs_set_device_type(leaf, dev_item, device->type); |
| 2346 | btrfs_set_device_io_align(leaf, dev_item, device->io_align); |
| 2347 | btrfs_set_device_io_width(leaf, dev_item, device->io_width); |
| 2348 | btrfs_set_device_sector_size(leaf, dev_item, device->sector_size); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 2349 | btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2350 | btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used); |
| 2351 | btrfs_mark_buffer_dirty(leaf); |
| 2352 | |
| 2353 | out: |
| 2354 | btrfs_free_path(path); |
| 2355 | return ret; |
| 2356 | } |
| 2357 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2358 | static int __btrfs_grow_device(struct btrfs_trans_handle *trans, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2359 | struct btrfs_device *device, u64 new_size) |
| 2360 | { |
| 2361 | struct btrfs_super_block *super_copy = |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2362 | device->dev_root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2363 | u64 old_total = btrfs_super_total_bytes(super_copy); |
| 2364 | u64 diff = new_size - device->total_bytes; |
| 2365 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2366 | if (!device->writeable) |
| 2367 | return -EACCES; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 2368 | if (new_size <= device->total_bytes || |
| 2369 | device->is_tgtdev_for_dev_replace) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2370 | return -EINVAL; |
| 2371 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2372 | btrfs_set_super_total_bytes(super_copy, old_total + diff); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2373 | device->fs_devices->total_rw_bytes += diff; |
| 2374 | |
| 2375 | device->total_bytes = new_size; |
Chris Mason | 9779b72 | 2009-07-24 16:41:41 -0400 | [diff] [blame] | 2376 | device->disk_total_bytes = new_size; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2377 | btrfs_clear_space_info_full(device->dev_root->fs_info); |
| 2378 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2379 | return btrfs_update_device(trans, device); |
| 2380 | } |
| 2381 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2382 | int btrfs_grow_device(struct btrfs_trans_handle *trans, |
| 2383 | struct btrfs_device *device, u64 new_size) |
| 2384 | { |
| 2385 | int ret; |
| 2386 | lock_chunks(device->dev_root); |
| 2387 | ret = __btrfs_grow_device(trans, device, new_size); |
| 2388 | unlock_chunks(device->dev_root); |
| 2389 | return ret; |
| 2390 | } |
| 2391 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2392 | static int btrfs_free_chunk(struct btrfs_trans_handle *trans, |
| 2393 | struct btrfs_root *root, |
| 2394 | u64 chunk_tree, u64 chunk_objectid, |
| 2395 | u64 chunk_offset) |
| 2396 | { |
| 2397 | int ret; |
| 2398 | struct btrfs_path *path; |
| 2399 | struct btrfs_key key; |
| 2400 | |
| 2401 | root = root->fs_info->chunk_root; |
| 2402 | path = btrfs_alloc_path(); |
| 2403 | if (!path) |
| 2404 | return -ENOMEM; |
| 2405 | |
| 2406 | key.objectid = chunk_objectid; |
| 2407 | key.offset = chunk_offset; |
| 2408 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2409 | |
| 2410 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2411 | if (ret < 0) |
| 2412 | goto out; |
| 2413 | else if (ret > 0) { /* Logic error or corruption */ |
| 2414 | btrfs_error(root->fs_info, -ENOENT, |
| 2415 | "Failed lookup while freeing chunk."); |
| 2416 | ret = -ENOENT; |
| 2417 | goto out; |
| 2418 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2419 | |
| 2420 | ret = btrfs_del_item(trans, root, path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2421 | if (ret < 0) |
| 2422 | btrfs_error(root->fs_info, ret, |
| 2423 | "Failed to delete chunk item."); |
| 2424 | out: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2425 | btrfs_free_path(path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 2426 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2427 | } |
| 2428 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2429 | 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] | 2430 | chunk_offset) |
| 2431 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 2432 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2433 | struct btrfs_disk_key *disk_key; |
| 2434 | struct btrfs_chunk *chunk; |
| 2435 | u8 *ptr; |
| 2436 | int ret = 0; |
| 2437 | u32 num_stripes; |
| 2438 | u32 array_size; |
| 2439 | u32 len = 0; |
| 2440 | u32 cur; |
| 2441 | struct btrfs_key key; |
| 2442 | |
| 2443 | array_size = btrfs_super_sys_array_size(super_copy); |
| 2444 | |
| 2445 | ptr = super_copy->sys_chunk_array; |
| 2446 | cur = 0; |
| 2447 | |
| 2448 | while (cur < array_size) { |
| 2449 | disk_key = (struct btrfs_disk_key *)ptr; |
| 2450 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 2451 | |
| 2452 | len = sizeof(*disk_key); |
| 2453 | |
| 2454 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 2455 | chunk = (struct btrfs_chunk *)(ptr + len); |
| 2456 | num_stripes = btrfs_stack_chunk_num_stripes(chunk); |
| 2457 | len += btrfs_chunk_item_size(num_stripes); |
| 2458 | } else { |
| 2459 | ret = -EIO; |
| 2460 | break; |
| 2461 | } |
| 2462 | if (key.objectid == chunk_objectid && |
| 2463 | key.offset == chunk_offset) { |
| 2464 | memmove(ptr, ptr + len, array_size - (cur + len)); |
| 2465 | array_size -= len; |
| 2466 | btrfs_set_super_sys_array_size(super_copy, array_size); |
| 2467 | } else { |
| 2468 | ptr += len; |
| 2469 | cur += len; |
| 2470 | } |
| 2471 | } |
| 2472 | return ret; |
| 2473 | } |
| 2474 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 2475 | static int btrfs_relocate_chunk(struct btrfs_root *root, |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2476 | u64 chunk_tree, u64 chunk_objectid, |
| 2477 | u64 chunk_offset) |
| 2478 | { |
| 2479 | struct extent_map_tree *em_tree; |
| 2480 | struct btrfs_root *extent_root; |
| 2481 | struct btrfs_trans_handle *trans; |
| 2482 | struct extent_map *em; |
| 2483 | struct map_lookup *map; |
| 2484 | int ret; |
| 2485 | int i; |
| 2486 | |
| 2487 | root = root->fs_info->chunk_root; |
| 2488 | extent_root = root->fs_info->extent_root; |
| 2489 | em_tree = &root->fs_info->mapping_tree.map_tree; |
| 2490 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2491 | ret = btrfs_can_relocate(extent_root, chunk_offset); |
| 2492 | if (ret) |
| 2493 | return -ENOSPC; |
| 2494 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2495 | /* step one, relocate all the extents inside this chunk */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2496 | ret = btrfs_relocate_block_group(extent_root, chunk_offset); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2497 | if (ret) |
| 2498 | return ret; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2499 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2500 | trans = btrfs_start_transaction(root, 0); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 2501 | if (IS_ERR(trans)) { |
| 2502 | ret = PTR_ERR(trans); |
| 2503 | btrfs_std_error(root->fs_info, ret); |
| 2504 | return ret; |
| 2505 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2506 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2507 | lock_chunks(root); |
| 2508 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2509 | /* |
| 2510 | * step two, delete the device extents and the |
| 2511 | * chunk tree entries |
| 2512 | */ |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2513 | read_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2514 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2515 | read_unlock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2516 | |
Tsutomu Itoh | 285190d | 2012-02-16 16:23:58 +0900 | [diff] [blame] | 2517 | BUG_ON(!em || em->start > chunk_offset || |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2518 | em->start + em->len < chunk_offset); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2519 | map = (struct map_lookup *)em->bdev; |
| 2520 | |
| 2521 | for (i = 0; i < map->num_stripes; i++) { |
| 2522 | ret = btrfs_free_dev_extent(trans, map->stripes[i].dev, |
| 2523 | map->stripes[i].physical); |
| 2524 | BUG_ON(ret); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2525 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 2526 | if (map->stripes[i].dev) { |
| 2527 | ret = btrfs_update_device(trans, map->stripes[i].dev); |
| 2528 | BUG_ON(ret); |
| 2529 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2530 | } |
| 2531 | ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid, |
| 2532 | chunk_offset); |
| 2533 | |
| 2534 | BUG_ON(ret); |
| 2535 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 2536 | trace_btrfs_chunk_free(root, map, chunk_offset, em->len); |
| 2537 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2538 | if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2539 | ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset); |
| 2540 | BUG_ON(ret); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2541 | } |
| 2542 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2543 | ret = btrfs_remove_block_group(trans, extent_root, chunk_offset); |
| 2544 | BUG_ON(ret); |
| 2545 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2546 | write_lock(&em_tree->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2547 | remove_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 2548 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2549 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2550 | /* once for the tree */ |
| 2551 | free_extent_map(em); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2552 | /* once for us */ |
| 2553 | free_extent_map(em); |
| 2554 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2555 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2556 | btrfs_end_transaction(trans, root); |
| 2557 | return 0; |
| 2558 | } |
| 2559 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2560 | static int btrfs_relocate_sys_chunks(struct btrfs_root *root) |
| 2561 | { |
| 2562 | struct btrfs_root *chunk_root = root->fs_info->chunk_root; |
| 2563 | struct btrfs_path *path; |
| 2564 | struct extent_buffer *leaf; |
| 2565 | struct btrfs_chunk *chunk; |
| 2566 | struct btrfs_key key; |
| 2567 | struct btrfs_key found_key; |
| 2568 | u64 chunk_tree = chunk_root->root_key.objectid; |
| 2569 | u64 chunk_type; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2570 | bool retried = false; |
| 2571 | int failed = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2572 | int ret; |
| 2573 | |
| 2574 | path = btrfs_alloc_path(); |
| 2575 | if (!path) |
| 2576 | return -ENOMEM; |
| 2577 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2578 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2579 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 2580 | key.offset = (u64)-1; |
| 2581 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 2582 | |
| 2583 | while (1) { |
| 2584 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 2585 | if (ret < 0) |
| 2586 | goto error; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2587 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2588 | |
| 2589 | ret = btrfs_previous_item(chunk_root, path, key.objectid, |
| 2590 | key.type); |
| 2591 | if (ret < 0) |
| 2592 | goto error; |
| 2593 | if (ret > 0) |
| 2594 | break; |
| 2595 | |
| 2596 | leaf = path->nodes[0]; |
| 2597 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2598 | |
| 2599 | chunk = btrfs_item_ptr(leaf, path->slots[0], |
| 2600 | struct btrfs_chunk); |
| 2601 | chunk_type = btrfs_chunk_type(leaf, chunk); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2602 | btrfs_release_path(path); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2603 | |
| 2604 | if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2605 | ret = btrfs_relocate_chunk(chunk_root, chunk_tree, |
| 2606 | found_key.objectid, |
| 2607 | found_key.offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2608 | if (ret == -ENOSPC) |
| 2609 | failed++; |
| 2610 | else if (ret) |
| 2611 | BUG(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | if (found_key.offset == 0) |
| 2615 | break; |
| 2616 | key.offset = found_key.offset - 1; |
| 2617 | } |
| 2618 | ret = 0; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2619 | if (failed && !retried) { |
| 2620 | failed = 0; |
| 2621 | retried = true; |
| 2622 | goto again; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 2623 | } else if (WARN_ON(failed && retried)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 2624 | ret = -ENOSPC; |
| 2625 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2626 | error: |
| 2627 | btrfs_free_path(path); |
| 2628 | return ret; |
| 2629 | } |
| 2630 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2631 | static int insert_balance_item(struct btrfs_root *root, |
| 2632 | struct btrfs_balance_control *bctl) |
| 2633 | { |
| 2634 | struct btrfs_trans_handle *trans; |
| 2635 | struct btrfs_balance_item *item; |
| 2636 | struct btrfs_disk_balance_args disk_bargs; |
| 2637 | struct btrfs_path *path; |
| 2638 | struct extent_buffer *leaf; |
| 2639 | struct btrfs_key key; |
| 2640 | int ret, err; |
| 2641 | |
| 2642 | path = btrfs_alloc_path(); |
| 2643 | if (!path) |
| 2644 | return -ENOMEM; |
| 2645 | |
| 2646 | trans = btrfs_start_transaction(root, 0); |
| 2647 | if (IS_ERR(trans)) { |
| 2648 | btrfs_free_path(path); |
| 2649 | return PTR_ERR(trans); |
| 2650 | } |
| 2651 | |
| 2652 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2653 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2654 | key.offset = 0; |
| 2655 | |
| 2656 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2657 | sizeof(*item)); |
| 2658 | if (ret) |
| 2659 | goto out; |
| 2660 | |
| 2661 | leaf = path->nodes[0]; |
| 2662 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 2663 | |
| 2664 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 2665 | |
| 2666 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data); |
| 2667 | btrfs_set_balance_data(leaf, item, &disk_bargs); |
| 2668 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta); |
| 2669 | btrfs_set_balance_meta(leaf, item, &disk_bargs); |
| 2670 | btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys); |
| 2671 | btrfs_set_balance_sys(leaf, item, &disk_bargs); |
| 2672 | |
| 2673 | btrfs_set_balance_flags(leaf, item, bctl->flags); |
| 2674 | |
| 2675 | btrfs_mark_buffer_dirty(leaf); |
| 2676 | out: |
| 2677 | btrfs_free_path(path); |
| 2678 | err = btrfs_commit_transaction(trans, root); |
| 2679 | if (err && !ret) |
| 2680 | ret = err; |
| 2681 | return ret; |
| 2682 | } |
| 2683 | |
| 2684 | static int del_balance_item(struct btrfs_root *root) |
| 2685 | { |
| 2686 | struct btrfs_trans_handle *trans; |
| 2687 | struct btrfs_path *path; |
| 2688 | struct btrfs_key key; |
| 2689 | int ret, err; |
| 2690 | |
| 2691 | path = btrfs_alloc_path(); |
| 2692 | if (!path) |
| 2693 | return -ENOMEM; |
| 2694 | |
| 2695 | trans = btrfs_start_transaction(root, 0); |
| 2696 | if (IS_ERR(trans)) { |
| 2697 | btrfs_free_path(path); |
| 2698 | return PTR_ERR(trans); |
| 2699 | } |
| 2700 | |
| 2701 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 2702 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 2703 | key.offset = 0; |
| 2704 | |
| 2705 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2706 | if (ret < 0) |
| 2707 | goto out; |
| 2708 | if (ret > 0) { |
| 2709 | ret = -ENOENT; |
| 2710 | goto out; |
| 2711 | } |
| 2712 | |
| 2713 | ret = btrfs_del_item(trans, root, path); |
| 2714 | out: |
| 2715 | btrfs_free_path(path); |
| 2716 | err = btrfs_commit_transaction(trans, root); |
| 2717 | if (err && !ret) |
| 2718 | ret = err; |
| 2719 | return ret; |
| 2720 | } |
| 2721 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2722 | /* |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2723 | * This is a heuristic used to reduce the number of chunks balanced on |
| 2724 | * resume after balance was interrupted. |
| 2725 | */ |
| 2726 | static void update_balance_args(struct btrfs_balance_control *bctl) |
| 2727 | { |
| 2728 | /* |
| 2729 | * Turn on soft mode for chunk types that were being converted. |
| 2730 | */ |
| 2731 | if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2732 | bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2733 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2734 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2735 | if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) |
| 2736 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT; |
| 2737 | |
| 2738 | /* |
| 2739 | * Turn on usage filter if is not already used. The idea is |
| 2740 | * that chunks that we have already balanced should be |
| 2741 | * reasonably full. Don't do it for chunks that are being |
| 2742 | * converted - that will keep us from relocating unconverted |
| 2743 | * (albeit full) chunks. |
| 2744 | */ |
| 2745 | if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2746 | !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2747 | bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2748 | bctl->data.usage = 90; |
| 2749 | } |
| 2750 | if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2751 | !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2752 | bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2753 | bctl->sys.usage = 90; |
| 2754 | } |
| 2755 | if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2756 | !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) { |
| 2757 | bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE; |
| 2758 | bctl->meta.usage = 90; |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | /* |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2763 | * Should be called with both balance and volume mutexes held to |
| 2764 | * serialize other volume operations (add_dev/rm_dev/resize) with |
| 2765 | * restriper. Same goes for unset_balance_control. |
| 2766 | */ |
| 2767 | static void set_balance_control(struct btrfs_balance_control *bctl) |
| 2768 | { |
| 2769 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
| 2770 | |
| 2771 | BUG_ON(fs_info->balance_ctl); |
| 2772 | |
| 2773 | spin_lock(&fs_info->balance_lock); |
| 2774 | fs_info->balance_ctl = bctl; |
| 2775 | spin_unlock(&fs_info->balance_lock); |
| 2776 | } |
| 2777 | |
| 2778 | static void unset_balance_control(struct btrfs_fs_info *fs_info) |
| 2779 | { |
| 2780 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 2781 | |
| 2782 | BUG_ON(!fs_info->balance_ctl); |
| 2783 | |
| 2784 | spin_lock(&fs_info->balance_lock); |
| 2785 | fs_info->balance_ctl = NULL; |
| 2786 | spin_unlock(&fs_info->balance_lock); |
| 2787 | |
| 2788 | kfree(bctl); |
| 2789 | } |
| 2790 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2791 | /* |
| 2792 | * Balance filters. Return 1 if chunk should be filtered out |
| 2793 | * (should not be balanced). |
| 2794 | */ |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2795 | static int chunk_profiles_filter(u64 chunk_type, |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2796 | struct btrfs_balance_args *bargs) |
| 2797 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2798 | chunk_type = chunk_to_extended(chunk_type) & |
| 2799 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2800 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2801 | if (bargs->profiles & chunk_type) |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2802 | return 0; |
| 2803 | |
| 2804 | return 1; |
| 2805 | } |
| 2806 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2807 | static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset, |
| 2808 | struct btrfs_balance_args *bargs) |
| 2809 | { |
| 2810 | struct btrfs_block_group_cache *cache; |
| 2811 | u64 chunk_used, user_thresh; |
| 2812 | int ret = 1; |
| 2813 | |
| 2814 | cache = btrfs_lookup_block_group(fs_info, chunk_offset); |
| 2815 | chunk_used = btrfs_block_group_used(&cache->item); |
| 2816 | |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2817 | if (bargs->usage == 0) |
Ilya Dryomov | 3e39cea | 2013-02-12 16:28:59 +0000 | [diff] [blame] | 2818 | user_thresh = 1; |
Ilya Dryomov | a105bb8 | 2013-01-21 15:15:56 +0200 | [diff] [blame] | 2819 | else if (bargs->usage > 100) |
| 2820 | user_thresh = cache->key.offset; |
| 2821 | else |
| 2822 | user_thresh = div_factor_fine(cache->key.offset, |
| 2823 | bargs->usage); |
| 2824 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2825 | if (chunk_used < user_thresh) |
| 2826 | ret = 0; |
| 2827 | |
| 2828 | btrfs_put_block_group(cache); |
| 2829 | return ret; |
| 2830 | } |
| 2831 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2832 | static int chunk_devid_filter(struct extent_buffer *leaf, |
| 2833 | struct btrfs_chunk *chunk, |
| 2834 | struct btrfs_balance_args *bargs) |
| 2835 | { |
| 2836 | struct btrfs_stripe *stripe; |
| 2837 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2838 | int i; |
| 2839 | |
| 2840 | for (i = 0; i < num_stripes; i++) { |
| 2841 | stripe = btrfs_stripe_nr(chunk, i); |
| 2842 | if (btrfs_stripe_devid(leaf, stripe) == bargs->devid) |
| 2843 | return 0; |
| 2844 | } |
| 2845 | |
| 2846 | return 1; |
| 2847 | } |
| 2848 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2849 | /* [pstart, pend) */ |
| 2850 | static int chunk_drange_filter(struct extent_buffer *leaf, |
| 2851 | struct btrfs_chunk *chunk, |
| 2852 | u64 chunk_offset, |
| 2853 | struct btrfs_balance_args *bargs) |
| 2854 | { |
| 2855 | struct btrfs_stripe *stripe; |
| 2856 | int num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 2857 | u64 stripe_offset; |
| 2858 | u64 stripe_length; |
| 2859 | int factor; |
| 2860 | int i; |
| 2861 | |
| 2862 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID)) |
| 2863 | return 0; |
| 2864 | |
| 2865 | if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 2866 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 2867 | factor = num_stripes / 2; |
| 2868 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) { |
| 2869 | factor = num_stripes - 1; |
| 2870 | } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) { |
| 2871 | factor = num_stripes - 2; |
| 2872 | } else { |
| 2873 | factor = num_stripes; |
| 2874 | } |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2875 | |
| 2876 | for (i = 0; i < num_stripes; i++) { |
| 2877 | stripe = btrfs_stripe_nr(chunk, i); |
| 2878 | if (btrfs_stripe_devid(leaf, stripe) != bargs->devid) |
| 2879 | continue; |
| 2880 | |
| 2881 | stripe_offset = btrfs_stripe_offset(leaf, stripe); |
| 2882 | stripe_length = btrfs_chunk_length(leaf, chunk); |
| 2883 | do_div(stripe_length, factor); |
| 2884 | |
| 2885 | if (stripe_offset < bargs->pend && |
| 2886 | stripe_offset + stripe_length > bargs->pstart) |
| 2887 | return 0; |
| 2888 | } |
| 2889 | |
| 2890 | return 1; |
| 2891 | } |
| 2892 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2893 | /* [vstart, vend) */ |
| 2894 | static int chunk_vrange_filter(struct extent_buffer *leaf, |
| 2895 | struct btrfs_chunk *chunk, |
| 2896 | u64 chunk_offset, |
| 2897 | struct btrfs_balance_args *bargs) |
| 2898 | { |
| 2899 | if (chunk_offset < bargs->vend && |
| 2900 | chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart) |
| 2901 | /* at least part of the chunk is inside this vrange */ |
| 2902 | return 0; |
| 2903 | |
| 2904 | return 1; |
| 2905 | } |
| 2906 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2907 | static int chunk_soft_convert_filter(u64 chunk_type, |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2908 | struct btrfs_balance_args *bargs) |
| 2909 | { |
| 2910 | if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT)) |
| 2911 | return 0; |
| 2912 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2913 | chunk_type = chunk_to_extended(chunk_type) & |
| 2914 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2915 | |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 2916 | if (bargs->target == chunk_type) |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2917 | return 1; |
| 2918 | |
| 2919 | return 0; |
| 2920 | } |
| 2921 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2922 | static int should_balance_chunk(struct btrfs_root *root, |
| 2923 | struct extent_buffer *leaf, |
| 2924 | struct btrfs_chunk *chunk, u64 chunk_offset) |
| 2925 | { |
| 2926 | struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; |
| 2927 | struct btrfs_balance_args *bargs = NULL; |
| 2928 | u64 chunk_type = btrfs_chunk_type(leaf, chunk); |
| 2929 | |
| 2930 | /* type filter */ |
| 2931 | if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) & |
| 2932 | (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) { |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | if (chunk_type & BTRFS_BLOCK_GROUP_DATA) |
| 2937 | bargs = &bctl->data; |
| 2938 | else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2939 | bargs = &bctl->sys; |
| 2940 | else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA) |
| 2941 | bargs = &bctl->meta; |
| 2942 | |
Ilya Dryomov | ed25e9b | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2943 | /* profiles filter */ |
| 2944 | if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) && |
| 2945 | chunk_profiles_filter(chunk_type, bargs)) { |
| 2946 | return 0; |
| 2947 | } |
| 2948 | |
Ilya Dryomov | 5ce5b3c | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2949 | /* usage filter */ |
| 2950 | if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && |
| 2951 | chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
Ilya Dryomov | 409d404 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2955 | /* devid filter */ |
| 2956 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) && |
| 2957 | chunk_devid_filter(leaf, chunk, bargs)) { |
| 2958 | return 0; |
| 2959 | } |
| 2960 | |
Ilya Dryomov | 94e60d5 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2961 | /* drange filter, makes sense only with devid filter */ |
| 2962 | if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) && |
| 2963 | chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2964 | return 0; |
| 2965 | } |
| 2966 | |
Ilya Dryomov | ea67176 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2967 | /* vrange filter */ |
| 2968 | if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) && |
| 2969 | chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) { |
| 2970 | return 0; |
| 2971 | } |
| 2972 | |
Ilya Dryomov | cfa4c96 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 2973 | /* soft profile changing mode */ |
| 2974 | if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) && |
| 2975 | chunk_soft_convert_filter(chunk_type, bargs)) { |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 2979 | /* |
| 2980 | * limited by count, must be the last filter |
| 2981 | */ |
| 2982 | if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) { |
| 2983 | if (bargs->limit == 0) |
| 2984 | return 0; |
| 2985 | else |
| 2986 | bargs->limit--; |
| 2987 | } |
| 2988 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2989 | return 1; |
| 2990 | } |
| 2991 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2992 | static int __btrfs_balance(struct btrfs_fs_info *fs_info) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2993 | { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 2994 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 2995 | struct btrfs_root *chunk_root = fs_info->chunk_root; |
| 2996 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 2997 | struct list_head *devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2998 | struct btrfs_device *device; |
| 2999 | u64 old_size; |
| 3000 | u64 size_to_free; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3001 | struct btrfs_chunk *chunk; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3002 | struct btrfs_path *path; |
| 3003 | struct btrfs_key key; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3004 | struct btrfs_key found_key; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3005 | struct btrfs_trans_handle *trans; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3006 | struct extent_buffer *leaf; |
| 3007 | int slot; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3008 | int ret; |
| 3009 | int enospc_errors = 0; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3010 | bool counting = true; |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3011 | u64 limit_data = bctl->data.limit; |
| 3012 | u64 limit_meta = bctl->meta.limit; |
| 3013 | u64 limit_sys = bctl->sys.limit; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3014 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3015 | /* step one make some room on all the devices */ |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3016 | devices = &fs_info->fs_devices->devices; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3017 | list_for_each_entry(device, devices, dev_list) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3018 | old_size = device->total_bytes; |
| 3019 | size_to_free = div_factor(old_size, 1); |
| 3020 | size_to_free = min(size_to_free, (u64)1 * 1024 * 1024); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3021 | if (!device->writeable || |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3022 | device->total_bytes - device->bytes_used > size_to_free || |
| 3023 | device->is_tgtdev_for_dev_replace) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3024 | continue; |
| 3025 | |
| 3026 | ret = btrfs_shrink_device(device, old_size - size_to_free); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3027 | if (ret == -ENOSPC) |
| 3028 | break; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3029 | BUG_ON(ret); |
| 3030 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3031 | trans = btrfs_start_transaction(dev_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3032 | BUG_ON(IS_ERR(trans)); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3033 | |
| 3034 | ret = btrfs_grow_device(trans, device, old_size); |
| 3035 | BUG_ON(ret); |
| 3036 | |
| 3037 | btrfs_end_transaction(trans, dev_root); |
| 3038 | } |
| 3039 | |
| 3040 | /* step two, relocate all the chunks */ |
| 3041 | path = btrfs_alloc_path(); |
Mark Fasheh | 17e9f79 | 2011-07-12 11:10:23 -0700 | [diff] [blame] | 3042 | if (!path) { |
| 3043 | ret = -ENOMEM; |
| 3044 | goto error; |
| 3045 | } |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3046 | |
| 3047 | /* zero out stat counters */ |
| 3048 | spin_lock(&fs_info->balance_lock); |
| 3049 | memset(&bctl->stat, 0, sizeof(bctl->stat)); |
| 3050 | spin_unlock(&fs_info->balance_lock); |
| 3051 | again: |
David Sterba | 7d824b6 | 2014-05-07 17:37:51 +0200 | [diff] [blame] | 3052 | if (!counting) { |
| 3053 | bctl->data.limit = limit_data; |
| 3054 | bctl->meta.limit = limit_meta; |
| 3055 | bctl->sys.limit = limit_sys; |
| 3056 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3057 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 3058 | key.offset = (u64)-1; |
| 3059 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 3060 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3061 | while (1) { |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3062 | if ((!counting && atomic_read(&fs_info->balance_pause_req)) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3063 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3064 | ret = -ECANCELED; |
| 3065 | goto error; |
| 3066 | } |
| 3067 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3068 | ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); |
| 3069 | if (ret < 0) |
| 3070 | goto error; |
| 3071 | |
| 3072 | /* |
| 3073 | * this shouldn't happen, it means the last relocate |
| 3074 | * failed |
| 3075 | */ |
| 3076 | if (ret == 0) |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3077 | BUG(); /* FIXME break ? */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3078 | |
| 3079 | ret = btrfs_previous_item(chunk_root, path, 0, |
| 3080 | BTRFS_CHUNK_ITEM_KEY); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3081 | if (ret) { |
| 3082 | ret = 0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3083 | break; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3084 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3085 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3086 | leaf = path->nodes[0]; |
| 3087 | slot = path->slots[0]; |
| 3088 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3089 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3090 | if (found_key.objectid != key.objectid) |
| 3091 | break; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3092 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3093 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 3094 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3095 | if (!counting) { |
| 3096 | spin_lock(&fs_info->balance_lock); |
| 3097 | bctl->stat.considered++; |
| 3098 | spin_unlock(&fs_info->balance_lock); |
| 3099 | } |
| 3100 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3101 | ret = should_balance_chunk(chunk_root, leaf, chunk, |
| 3102 | found_key.offset); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3103 | btrfs_release_path(path); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3104 | if (!ret) |
| 3105 | goto loop; |
| 3106 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3107 | if (counting) { |
| 3108 | spin_lock(&fs_info->balance_lock); |
| 3109 | bctl->stat.expected++; |
| 3110 | spin_unlock(&fs_info->balance_lock); |
| 3111 | goto loop; |
| 3112 | } |
| 3113 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3114 | ret = btrfs_relocate_chunk(chunk_root, |
| 3115 | chunk_root->root_key.objectid, |
| 3116 | found_key.objectid, |
| 3117 | found_key.offset); |
Josef Bacik | 508794e | 2011-07-02 21:24:41 +0000 | [diff] [blame] | 3118 | if (ret && ret != -ENOSPC) |
| 3119 | goto error; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3120 | if (ret == -ENOSPC) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3121 | enospc_errors++; |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3122 | } else { |
| 3123 | spin_lock(&fs_info->balance_lock); |
| 3124 | bctl->stat.completed++; |
| 3125 | spin_unlock(&fs_info->balance_lock); |
| 3126 | } |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3127 | loop: |
Ilya Dryomov | 795a332 | 2013-08-27 13:50:44 +0300 | [diff] [blame] | 3128 | if (found_key.offset == 0) |
| 3129 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3130 | key.offset = found_key.offset - 1; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3131 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3132 | |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3133 | if (counting) { |
| 3134 | btrfs_release_path(path); |
| 3135 | counting = false; |
| 3136 | goto again; |
| 3137 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3138 | error: |
| 3139 | btrfs_free_path(path); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3140 | if (enospc_errors) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3141 | btrfs_info(fs_info, "%d enospc errors during balance", |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3142 | enospc_errors); |
| 3143 | if (!ret) |
| 3144 | ret = -ENOSPC; |
| 3145 | } |
| 3146 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3147 | return ret; |
| 3148 | } |
| 3149 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3150 | /** |
| 3151 | * alloc_profile_is_valid - see if a given profile is valid and reduced |
| 3152 | * @flags: profile to validate |
| 3153 | * @extended: if true @flags is treated as an extended profile |
| 3154 | */ |
| 3155 | static int alloc_profile_is_valid(u64 flags, int extended) |
| 3156 | { |
| 3157 | u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK : |
| 3158 | BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 3159 | |
| 3160 | flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 3161 | |
| 3162 | /* 1) check that all other bits are zeroed */ |
| 3163 | if (flags & ~mask) |
| 3164 | return 0; |
| 3165 | |
| 3166 | /* 2) see if profile is reduced */ |
| 3167 | if (flags == 0) |
| 3168 | return !extended; /* "0" is valid for usual profiles */ |
| 3169 | |
| 3170 | /* true if exactly one bit set */ |
| 3171 | return (flags & (flags - 1)) == 0; |
| 3172 | } |
| 3173 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3174 | static inline int balance_need_close(struct btrfs_fs_info *fs_info) |
| 3175 | { |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3176 | /* cancel requested || normal exit path */ |
| 3177 | return atomic_read(&fs_info->balance_cancel_req) || |
| 3178 | (atomic_read(&fs_info->balance_pause_req) == 0 && |
| 3179 | atomic_read(&fs_info->balance_cancel_req) == 0); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3180 | } |
| 3181 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3182 | static void __cancel_balance(struct btrfs_fs_info *fs_info) |
| 3183 | { |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3184 | int ret; |
| 3185 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3186 | unset_balance_control(fs_info); |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3187 | ret = del_balance_item(fs_info->tree_root); |
Liu Bo | 0f788c5 | 2013-03-04 16:25:40 +0000 | [diff] [blame] | 3188 | if (ret) |
| 3189 | btrfs_std_error(fs_info, ret); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3190 | |
| 3191 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3192 | } |
| 3193 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3194 | /* |
| 3195 | * Should be called with both balance and volume mutexes held |
| 3196 | */ |
| 3197 | int btrfs_balance(struct btrfs_balance_control *bctl, |
| 3198 | struct btrfs_ioctl_balance_args *bargs) |
| 3199 | { |
| 3200 | struct btrfs_fs_info *fs_info = bctl->fs_info; |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3201 | u64 allowed; |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3202 | int mixed = 0; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3203 | int ret; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3204 | u64 num_devices; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3205 | unsigned seq; |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3206 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3207 | if (btrfs_fs_closing(fs_info) || |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3208 | atomic_read(&fs_info->balance_pause_req) || |
| 3209 | atomic_read(&fs_info->balance_cancel_req)) { |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3210 | ret = -EINVAL; |
| 3211 | goto out; |
| 3212 | } |
| 3213 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3214 | allowed = btrfs_super_incompat_flags(fs_info->super_copy); |
| 3215 | if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 3216 | mixed = 1; |
| 3217 | |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3218 | /* |
| 3219 | * In case of mixed groups both data and meta should be picked, |
| 3220 | * and identical options should be given for both of them. |
| 3221 | */ |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3222 | allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA; |
| 3223 | if (mixed && (bctl->flags & allowed)) { |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3224 | if (!(bctl->flags & BTRFS_BALANCE_DATA) || |
| 3225 | !(bctl->flags & BTRFS_BALANCE_METADATA) || |
| 3226 | memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3227 | btrfs_err(fs_info, "with mixed groups data and " |
| 3228 | "metadata balance options must be the same"); |
Ilya Dryomov | f43ffb6 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3229 | ret = -EINVAL; |
| 3230 | goto out; |
| 3231 | } |
| 3232 | } |
| 3233 | |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3234 | num_devices = fs_info->fs_devices->num_devices; |
| 3235 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 3236 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) { |
| 3237 | BUG_ON(num_devices < 1); |
| 3238 | num_devices--; |
| 3239 | } |
| 3240 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3241 | allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE; |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 3242 | if (num_devices == 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3243 | allowed |= BTRFS_BLOCK_GROUP_DUP; |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3244 | else if (num_devices > 1) |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3245 | allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1); |
Andreas Philipp | 8250dab | 2013-05-11 11:13:03 +0000 | [diff] [blame] | 3246 | if (num_devices > 2) |
| 3247 | allowed |= BTRFS_BLOCK_GROUP_RAID5; |
| 3248 | if (num_devices > 3) |
| 3249 | allowed |= (BTRFS_BLOCK_GROUP_RAID10 | |
| 3250 | BTRFS_BLOCK_GROUP_RAID6); |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3251 | if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3252 | (!alloc_profile_is_valid(bctl->data.target, 1) || |
| 3253 | (bctl->data.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3254 | btrfs_err(fs_info, "unable to start balance with target " |
| 3255 | "data profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3256 | bctl->data.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3257 | ret = -EINVAL; |
| 3258 | goto out; |
| 3259 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3260 | if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3261 | (!alloc_profile_is_valid(bctl->meta.target, 1) || |
| 3262 | (bctl->meta.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3263 | btrfs_err(fs_info, |
| 3264 | "unable to start balance with target metadata profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3265 | bctl->meta.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3266 | ret = -EINVAL; |
| 3267 | goto out; |
| 3268 | } |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3269 | if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3270 | (!alloc_profile_is_valid(bctl->sys.target, 1) || |
| 3271 | (bctl->sys.target & ~allowed))) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3272 | btrfs_err(fs_info, |
| 3273 | "unable to start balance with target system profile %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 3274 | bctl->sys.target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3275 | ret = -EINVAL; |
| 3276 | goto out; |
| 3277 | } |
| 3278 | |
Ilya Dryomov | e4837f8 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3279 | /* allow dup'ed data chunks only in mixed mode */ |
| 3280 | if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
Ilya Dryomov | 6728b19 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3281 | (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3282 | btrfs_err(fs_info, "dup for data is not allowed"); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3283 | ret = -EINVAL; |
| 3284 | goto out; |
| 3285 | } |
| 3286 | |
| 3287 | /* allow to reduce meta or sys integrity only if force set */ |
| 3288 | allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3289 | BTRFS_BLOCK_GROUP_RAID10 | |
| 3290 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3291 | BTRFS_BLOCK_GROUP_RAID6; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3292 | do { |
| 3293 | seq = read_seqbegin(&fs_info->profiles_lock); |
| 3294 | |
| 3295 | if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3296 | (fs_info->avail_system_alloc_bits & allowed) && |
| 3297 | !(bctl->sys.target & allowed)) || |
| 3298 | ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) && |
| 3299 | (fs_info->avail_metadata_alloc_bits & allowed) && |
| 3300 | !(bctl->meta.target & allowed))) { |
| 3301 | if (bctl->flags & BTRFS_BALANCE_FORCE) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3302 | btrfs_info(fs_info, "force reducing metadata integrity"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3303 | } else { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3304 | btrfs_err(fs_info, "balance will reduce metadata " |
| 3305 | "integrity, use force if you want this"); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3306 | ret = -EINVAL; |
| 3307 | goto out; |
| 3308 | } |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3309 | } |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3310 | } while (read_seqretry(&fs_info->profiles_lock, seq)); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3311 | |
Stefan Behrens | 5af3e8c | 2012-08-01 18:56:49 +0200 | [diff] [blame] | 3312 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3313 | int num_tolerated_disk_barrier_failures; |
| 3314 | u64 target = bctl->sys.target; |
| 3315 | |
| 3316 | num_tolerated_disk_barrier_failures = |
| 3317 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3318 | if (num_tolerated_disk_barrier_failures > 0 && |
| 3319 | (target & |
| 3320 | (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3321 | BTRFS_AVAIL_ALLOC_BIT_SINGLE))) |
| 3322 | num_tolerated_disk_barrier_failures = 0; |
| 3323 | else if (num_tolerated_disk_barrier_failures > 1 && |
| 3324 | (target & |
| 3325 | (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))) |
| 3326 | num_tolerated_disk_barrier_failures = 1; |
| 3327 | |
| 3328 | fs_info->num_tolerated_disk_barrier_failures = |
| 3329 | num_tolerated_disk_barrier_failures; |
| 3330 | } |
| 3331 | |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3332 | ret = insert_balance_item(fs_info->tree_root, bctl); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3333 | if (ret && ret != -EEXIST) |
Ilya Dryomov | 0940ebf | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3334 | goto out; |
| 3335 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3336 | if (!(bctl->flags & BTRFS_BALANCE_RESUME)) { |
| 3337 | BUG_ON(ret == -EEXIST); |
| 3338 | set_balance_control(bctl); |
| 3339 | } else { |
| 3340 | BUG_ON(ret != -EEXIST); |
| 3341 | spin_lock(&fs_info->balance_lock); |
| 3342 | update_balance_args(bctl); |
| 3343 | spin_unlock(&fs_info->balance_lock); |
| 3344 | } |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3345 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3346 | atomic_inc(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3347 | mutex_unlock(&fs_info->balance_mutex); |
| 3348 | |
| 3349 | ret = __btrfs_balance(fs_info); |
| 3350 | |
| 3351 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3352 | atomic_dec(&fs_info->balance_running); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3353 | |
Ilya Dryomov | bf023ec | 2013-02-12 16:27:46 +0000 | [diff] [blame] | 3354 | if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3355 | fs_info->num_tolerated_disk_barrier_failures = |
| 3356 | btrfs_calc_num_tolerated_disk_barrier_failures(fs_info); |
| 3357 | } |
| 3358 | |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3359 | if (bargs) { |
| 3360 | memset(bargs, 0, sizeof(*bargs)); |
Ilya Dryomov | 19a39dc | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3361 | update_ioctl_balance_args(fs_info, 0, bargs); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3362 | } |
| 3363 | |
Ilya Dryomov | 3a01aa7 | 2013-03-06 01:57:55 -0700 | [diff] [blame] | 3364 | if ((ret && ret != -ECANCELED && ret != -ENOSPC) || |
| 3365 | balance_need_close(fs_info)) { |
| 3366 | __cancel_balance(fs_info); |
| 3367 | } |
| 3368 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3369 | wake_up(&fs_info->balance_wait_q); |
Ilya Dryomov | c9e9f97 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3370 | |
| 3371 | return ret; |
| 3372 | out: |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3373 | if (bctl->flags & BTRFS_BALANCE_RESUME) |
| 3374 | __cancel_balance(fs_info); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3375 | else { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3376 | kfree(bctl); |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3377 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); |
| 3378 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3379 | return ret; |
| 3380 | } |
| 3381 | |
| 3382 | static int balance_kthread(void *data) |
| 3383 | { |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3384 | struct btrfs_fs_info *fs_info = data; |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3385 | int ret = 0; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3386 | |
| 3387 | mutex_lock(&fs_info->volume_mutex); |
| 3388 | mutex_lock(&fs_info->balance_mutex); |
| 3389 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3390 | if (fs_info->balance_ctl) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3391 | btrfs_info(fs_info, "continuing balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3392 | ret = btrfs_balance(fs_info->balance_ctl, NULL); |
Ilya Dryomov | 9555c6c | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3393 | } |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3394 | |
| 3395 | mutex_unlock(&fs_info->balance_mutex); |
| 3396 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3397 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3398 | return ret; |
| 3399 | } |
| 3400 | |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3401 | int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info) |
| 3402 | { |
| 3403 | struct task_struct *tsk; |
| 3404 | |
| 3405 | spin_lock(&fs_info->balance_lock); |
| 3406 | if (!fs_info->balance_ctl) { |
| 3407 | spin_unlock(&fs_info->balance_lock); |
| 3408 | return 0; |
| 3409 | } |
| 3410 | spin_unlock(&fs_info->balance_lock); |
| 3411 | |
| 3412 | if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3413 | btrfs_info(fs_info, "force skipping balance"); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3414 | return 0; |
| 3415 | } |
| 3416 | |
| 3417 | tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance"); |
Sachin Kamat | cd63397 | 2013-07-15 16:52:18 +0530 | [diff] [blame] | 3418 | return PTR_ERR_OR_ZERO(tsk); |
Ilya Dryomov | 2b6ba62 | 2012-06-22 12:24:13 -0600 | [diff] [blame] | 3419 | } |
| 3420 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3421 | int btrfs_recover_balance(struct btrfs_fs_info *fs_info) |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3422 | { |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3423 | struct btrfs_balance_control *bctl; |
| 3424 | struct btrfs_balance_item *item; |
| 3425 | struct btrfs_disk_balance_args disk_bargs; |
| 3426 | struct btrfs_path *path; |
| 3427 | struct extent_buffer *leaf; |
| 3428 | struct btrfs_key key; |
| 3429 | int ret; |
| 3430 | |
| 3431 | path = btrfs_alloc_path(); |
| 3432 | if (!path) |
| 3433 | return -ENOMEM; |
| 3434 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3435 | key.objectid = BTRFS_BALANCE_OBJECTID; |
| 3436 | key.type = BTRFS_BALANCE_ITEM_KEY; |
| 3437 | key.offset = 0; |
| 3438 | |
| 3439 | ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0); |
| 3440 | if (ret < 0) |
| 3441 | goto out; |
| 3442 | if (ret > 0) { /* ret = -ENOENT; */ |
| 3443 | ret = 0; |
| 3444 | goto out; |
| 3445 | } |
| 3446 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3447 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
| 3448 | if (!bctl) { |
| 3449 | ret = -ENOMEM; |
| 3450 | goto out; |
| 3451 | } |
| 3452 | |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3453 | leaf = path->nodes[0]; |
| 3454 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item); |
| 3455 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3456 | bctl->fs_info = fs_info; |
| 3457 | bctl->flags = btrfs_balance_flags(leaf, item); |
| 3458 | bctl->flags |= BTRFS_BALANCE_RESUME; |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3459 | |
| 3460 | btrfs_balance_data(leaf, item, &disk_bargs); |
| 3461 | btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs); |
| 3462 | btrfs_balance_meta(leaf, item, &disk_bargs); |
| 3463 | btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs); |
| 3464 | btrfs_balance_sys(leaf, item, &disk_bargs); |
| 3465 | btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs); |
| 3466 | |
Ilya Dryomov | ed0fb78 | 2013-01-20 15:57:57 +0200 | [diff] [blame] | 3467 | WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)); |
| 3468 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3469 | mutex_lock(&fs_info->volume_mutex); |
| 3470 | mutex_lock(&fs_info->balance_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3471 | |
Ilya Dryomov | 68310a5 | 2012-06-22 12:24:12 -0600 | [diff] [blame] | 3472 | set_balance_control(bctl); |
| 3473 | |
| 3474 | mutex_unlock(&fs_info->balance_mutex); |
| 3475 | mutex_unlock(&fs_info->volume_mutex); |
Ilya Dryomov | 5964101 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3476 | out: |
| 3477 | btrfs_free_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3478 | return ret; |
| 3479 | } |
| 3480 | |
Ilya Dryomov | 837d5b6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3481 | int btrfs_pause_balance(struct btrfs_fs_info *fs_info) |
| 3482 | { |
| 3483 | int ret = 0; |
| 3484 | |
| 3485 | mutex_lock(&fs_info->balance_mutex); |
| 3486 | if (!fs_info->balance_ctl) { |
| 3487 | mutex_unlock(&fs_info->balance_mutex); |
| 3488 | return -ENOTCONN; |
| 3489 | } |
| 3490 | |
| 3491 | if (atomic_read(&fs_info->balance_running)) { |
| 3492 | atomic_inc(&fs_info->balance_pause_req); |
| 3493 | mutex_unlock(&fs_info->balance_mutex); |
| 3494 | |
| 3495 | wait_event(fs_info->balance_wait_q, |
| 3496 | atomic_read(&fs_info->balance_running) == 0); |
| 3497 | |
| 3498 | mutex_lock(&fs_info->balance_mutex); |
| 3499 | /* we are good with balance_ctl ripped off from under us */ |
| 3500 | BUG_ON(atomic_read(&fs_info->balance_running)); |
| 3501 | atomic_dec(&fs_info->balance_pause_req); |
| 3502 | } else { |
| 3503 | ret = -ENOTCONN; |
| 3504 | } |
| 3505 | |
| 3506 | mutex_unlock(&fs_info->balance_mutex); |
| 3507 | return ret; |
| 3508 | } |
| 3509 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3510 | int btrfs_cancel_balance(struct btrfs_fs_info *fs_info) |
| 3511 | { |
Ilya Dryomov | e649e58 | 2013-10-10 20:40:21 +0300 | [diff] [blame] | 3512 | if (fs_info->sb->s_flags & MS_RDONLY) |
| 3513 | return -EROFS; |
| 3514 | |
Ilya Dryomov | a7e99c6 | 2012-01-16 22:04:49 +0200 | [diff] [blame] | 3515 | mutex_lock(&fs_info->balance_mutex); |
| 3516 | if (!fs_info->balance_ctl) { |
| 3517 | mutex_unlock(&fs_info->balance_mutex); |
| 3518 | return -ENOTCONN; |
| 3519 | } |
| 3520 | |
| 3521 | atomic_inc(&fs_info->balance_cancel_req); |
| 3522 | /* |
| 3523 | * if we are running just wait and return, balance item is |
| 3524 | * deleted in btrfs_balance in this case |
| 3525 | */ |
| 3526 | if (atomic_read(&fs_info->balance_running)) { |
| 3527 | mutex_unlock(&fs_info->balance_mutex); |
| 3528 | wait_event(fs_info->balance_wait_q, |
| 3529 | atomic_read(&fs_info->balance_running) == 0); |
| 3530 | mutex_lock(&fs_info->balance_mutex); |
| 3531 | } else { |
| 3532 | /* __cancel_balance needs volume_mutex */ |
| 3533 | mutex_unlock(&fs_info->balance_mutex); |
| 3534 | mutex_lock(&fs_info->volume_mutex); |
| 3535 | mutex_lock(&fs_info->balance_mutex); |
| 3536 | |
| 3537 | if (fs_info->balance_ctl) |
| 3538 | __cancel_balance(fs_info); |
| 3539 | |
| 3540 | mutex_unlock(&fs_info->volume_mutex); |
| 3541 | } |
| 3542 | |
| 3543 | BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running)); |
| 3544 | atomic_dec(&fs_info->balance_cancel_req); |
| 3545 | mutex_unlock(&fs_info->balance_mutex); |
| 3546 | return 0; |
| 3547 | } |
| 3548 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3549 | static int btrfs_uuid_scan_kthread(void *data) |
| 3550 | { |
| 3551 | struct btrfs_fs_info *fs_info = data; |
| 3552 | struct btrfs_root *root = fs_info->tree_root; |
| 3553 | struct btrfs_key key; |
| 3554 | struct btrfs_key max_key; |
| 3555 | struct btrfs_path *path = NULL; |
| 3556 | int ret = 0; |
| 3557 | struct extent_buffer *eb; |
| 3558 | int slot; |
| 3559 | struct btrfs_root_item root_item; |
| 3560 | u32 item_size; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3561 | struct btrfs_trans_handle *trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3562 | |
| 3563 | path = btrfs_alloc_path(); |
| 3564 | if (!path) { |
| 3565 | ret = -ENOMEM; |
| 3566 | goto out; |
| 3567 | } |
| 3568 | |
| 3569 | key.objectid = 0; |
| 3570 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3571 | key.offset = 0; |
| 3572 | |
| 3573 | max_key.objectid = (u64)-1; |
| 3574 | max_key.type = BTRFS_ROOT_ITEM_KEY; |
| 3575 | max_key.offset = (u64)-1; |
| 3576 | |
| 3577 | path->keep_locks = 1; |
| 3578 | |
| 3579 | while (1) { |
Filipe David Borba Manana | 6174d3c | 2013-10-01 16:13:42 +0100 | [diff] [blame] | 3580 | ret = btrfs_search_forward(root, &key, path, 0); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3581 | if (ret) { |
| 3582 | if (ret > 0) |
| 3583 | ret = 0; |
| 3584 | break; |
| 3585 | } |
| 3586 | |
| 3587 | if (key.type != BTRFS_ROOT_ITEM_KEY || |
| 3588 | (key.objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 3589 | key.objectid != BTRFS_FS_TREE_OBJECTID) || |
| 3590 | key.objectid > BTRFS_LAST_FREE_OBJECTID) |
| 3591 | goto skip; |
| 3592 | |
| 3593 | eb = path->nodes[0]; |
| 3594 | slot = path->slots[0]; |
| 3595 | item_size = btrfs_item_size_nr(eb, slot); |
| 3596 | if (item_size < sizeof(root_item)) |
| 3597 | goto skip; |
| 3598 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3599 | read_extent_buffer(eb, &root_item, |
| 3600 | btrfs_item_ptr_offset(eb, slot), |
| 3601 | (int)sizeof(root_item)); |
| 3602 | if (btrfs_root_refs(&root_item) == 0) |
| 3603 | goto skip; |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3604 | |
| 3605 | if (!btrfs_is_empty_uuid(root_item.uuid) || |
| 3606 | !btrfs_is_empty_uuid(root_item.received_uuid)) { |
| 3607 | if (trans) |
| 3608 | goto update_tree; |
| 3609 | |
| 3610 | btrfs_release_path(path); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3611 | /* |
| 3612 | * 1 - subvol uuid item |
| 3613 | * 1 - received_subvol uuid item |
| 3614 | */ |
| 3615 | trans = btrfs_start_transaction(fs_info->uuid_root, 2); |
| 3616 | if (IS_ERR(trans)) { |
| 3617 | ret = PTR_ERR(trans); |
| 3618 | break; |
| 3619 | } |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3620 | continue; |
| 3621 | } else { |
| 3622 | goto skip; |
| 3623 | } |
| 3624 | update_tree: |
| 3625 | if (!btrfs_is_empty_uuid(root_item.uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3626 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3627 | root_item.uuid, |
| 3628 | BTRFS_UUID_KEY_SUBVOL, |
| 3629 | key.objectid); |
| 3630 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3631 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3632 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3633 | break; |
| 3634 | } |
| 3635 | } |
| 3636 | |
| 3637 | if (!btrfs_is_empty_uuid(root_item.received_uuid)) { |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3638 | ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root, |
| 3639 | root_item.received_uuid, |
| 3640 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
| 3641 | key.objectid); |
| 3642 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3643 | btrfs_warn(fs_info, "uuid_tree_add failed %d", |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3644 | ret); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3645 | break; |
| 3646 | } |
| 3647 | } |
| 3648 | |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3649 | skip: |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3650 | if (trans) { |
| 3651 | ret = btrfs_end_transaction(trans, fs_info->uuid_root); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3652 | trans = NULL; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3653 | if (ret) |
| 3654 | break; |
| 3655 | } |
| 3656 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3657 | btrfs_release_path(path); |
| 3658 | if (key.offset < (u64)-1) { |
| 3659 | key.offset++; |
| 3660 | } else if (key.type < BTRFS_ROOT_ITEM_KEY) { |
| 3661 | key.offset = 0; |
| 3662 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3663 | } else if (key.objectid < (u64)-1) { |
| 3664 | key.offset = 0; |
| 3665 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3666 | key.objectid++; |
| 3667 | } else { |
| 3668 | break; |
| 3669 | } |
| 3670 | cond_resched(); |
| 3671 | } |
| 3672 | |
| 3673 | out: |
| 3674 | btrfs_free_path(path); |
Filipe David Borba Manana | f45388f | 2013-08-28 10:28:34 +0100 | [diff] [blame] | 3675 | if (trans && !IS_ERR(trans)) |
| 3676 | btrfs_end_transaction(trans, fs_info->uuid_root); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3677 | if (ret) |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3678 | btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3679 | else |
| 3680 | fs_info->update_uuid_tree_gen = 1; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3681 | up(&fs_info->uuid_tree_rescan_sem); |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3685 | /* |
| 3686 | * Callback for btrfs_uuid_tree_iterate(). |
| 3687 | * returns: |
| 3688 | * 0 check succeeded, the entry is not outdated. |
| 3689 | * < 0 if an error occured. |
| 3690 | * > 0 if the check failed, which means the caller shall remove the entry. |
| 3691 | */ |
| 3692 | static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info, |
| 3693 | u8 *uuid, u8 type, u64 subid) |
| 3694 | { |
| 3695 | struct btrfs_key key; |
| 3696 | int ret = 0; |
| 3697 | struct btrfs_root *subvol_root; |
| 3698 | |
| 3699 | if (type != BTRFS_UUID_KEY_SUBVOL && |
| 3700 | type != BTRFS_UUID_KEY_RECEIVED_SUBVOL) |
| 3701 | goto out; |
| 3702 | |
| 3703 | key.objectid = subid; |
| 3704 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 3705 | key.offset = (u64)-1; |
| 3706 | subvol_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 3707 | if (IS_ERR(subvol_root)) { |
| 3708 | ret = PTR_ERR(subvol_root); |
| 3709 | if (ret == -ENOENT) |
| 3710 | ret = 1; |
| 3711 | goto out; |
| 3712 | } |
| 3713 | |
| 3714 | switch (type) { |
| 3715 | case BTRFS_UUID_KEY_SUBVOL: |
| 3716 | if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE)) |
| 3717 | ret = 1; |
| 3718 | break; |
| 3719 | case BTRFS_UUID_KEY_RECEIVED_SUBVOL: |
| 3720 | if (memcmp(uuid, subvol_root->root_item.received_uuid, |
| 3721 | BTRFS_UUID_SIZE)) |
| 3722 | ret = 1; |
| 3723 | break; |
| 3724 | } |
| 3725 | |
| 3726 | out: |
| 3727 | return ret; |
| 3728 | } |
| 3729 | |
| 3730 | static int btrfs_uuid_rescan_kthread(void *data) |
| 3731 | { |
| 3732 | struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data; |
| 3733 | int ret; |
| 3734 | |
| 3735 | /* |
| 3736 | * 1st step is to iterate through the existing UUID tree and |
| 3737 | * to delete all entries that contain outdated data. |
| 3738 | * 2nd step is to add all missing entries to the UUID tree. |
| 3739 | */ |
| 3740 | ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry); |
| 3741 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3742 | btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3743 | up(&fs_info->uuid_tree_rescan_sem); |
| 3744 | return ret; |
| 3745 | } |
| 3746 | return btrfs_uuid_scan_kthread(data); |
| 3747 | } |
| 3748 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3749 | int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3750 | { |
| 3751 | struct btrfs_trans_handle *trans; |
| 3752 | struct btrfs_root *tree_root = fs_info->tree_root; |
| 3753 | struct btrfs_root *uuid_root; |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3754 | struct task_struct *task; |
| 3755 | int ret; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3756 | |
| 3757 | /* |
| 3758 | * 1 - root node |
| 3759 | * 1 - root item |
| 3760 | */ |
| 3761 | trans = btrfs_start_transaction(tree_root, 2); |
| 3762 | if (IS_ERR(trans)) |
| 3763 | return PTR_ERR(trans); |
| 3764 | |
| 3765 | uuid_root = btrfs_create_tree(trans, fs_info, |
| 3766 | BTRFS_UUID_TREE_OBJECTID); |
| 3767 | if (IS_ERR(uuid_root)) { |
| 3768 | btrfs_abort_transaction(trans, tree_root, |
| 3769 | PTR_ERR(uuid_root)); |
| 3770 | return PTR_ERR(uuid_root); |
| 3771 | } |
| 3772 | |
| 3773 | fs_info->uuid_root = uuid_root; |
| 3774 | |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3775 | ret = btrfs_commit_transaction(trans, tree_root); |
| 3776 | if (ret) |
| 3777 | return ret; |
| 3778 | |
| 3779 | down(&fs_info->uuid_tree_rescan_sem); |
| 3780 | task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid"); |
| 3781 | if (IS_ERR(task)) { |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3782 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3783 | btrfs_warn(fs_info, "failed to start uuid_scan task"); |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3784 | up(&fs_info->uuid_tree_rescan_sem); |
| 3785 | return PTR_ERR(task); |
| 3786 | } |
| 3787 | |
| 3788 | return 0; |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 3789 | } |
Stefan Behrens | 803b2f5 | 2013-08-15 17:11:21 +0200 | [diff] [blame] | 3790 | |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3791 | int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info) |
| 3792 | { |
| 3793 | struct task_struct *task; |
| 3794 | |
| 3795 | down(&fs_info->uuid_tree_rescan_sem); |
| 3796 | task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid"); |
| 3797 | if (IS_ERR(task)) { |
| 3798 | /* fs_info->update_uuid_tree_gen remains 0 in all error case */ |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 3799 | btrfs_warn(fs_info, "failed to start uuid_rescan task"); |
Stefan Behrens | 70f8017 | 2013-08-15 17:11:23 +0200 | [diff] [blame] | 3800 | up(&fs_info->uuid_tree_rescan_sem); |
| 3801 | return PTR_ERR(task); |
| 3802 | } |
| 3803 | |
| 3804 | return 0; |
| 3805 | } |
| 3806 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3807 | /* |
| 3808 | * shrinking a device means finding all of the device extents past |
| 3809 | * the new size, and then following the back refs to the chunks. |
| 3810 | * The chunk relocation code actually frees the device extent |
| 3811 | */ |
| 3812 | int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) |
| 3813 | { |
| 3814 | struct btrfs_trans_handle *trans; |
| 3815 | struct btrfs_root *root = device->dev_root; |
| 3816 | struct btrfs_dev_extent *dev_extent = NULL; |
| 3817 | struct btrfs_path *path; |
| 3818 | u64 length; |
| 3819 | u64 chunk_tree; |
| 3820 | u64 chunk_objectid; |
| 3821 | u64 chunk_offset; |
| 3822 | int ret; |
| 3823 | int slot; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3824 | int failed = 0; |
| 3825 | bool retried = false; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3826 | struct extent_buffer *l; |
| 3827 | struct btrfs_key key; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3828 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3829 | u64 old_total = btrfs_super_total_bytes(super_copy); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3830 | u64 old_size = device->total_bytes; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3831 | u64 diff = device->total_bytes - new_size; |
| 3832 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 3833 | if (device->is_tgtdev_for_dev_replace) |
| 3834 | return -EINVAL; |
| 3835 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3836 | path = btrfs_alloc_path(); |
| 3837 | if (!path) |
| 3838 | return -ENOMEM; |
| 3839 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3840 | path->reada = 2; |
| 3841 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3842 | lock_chunks(root); |
| 3843 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3844 | device->total_bytes = new_size; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3845 | if (device->writeable) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3846 | device->fs_devices->total_rw_bytes -= diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3847 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3848 | root->fs_info->free_chunk_space -= diff; |
| 3849 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3850 | } |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3851 | unlock_chunks(root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3852 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3853 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3854 | key.objectid = device->devid; |
| 3855 | key.offset = (u64)-1; |
| 3856 | key.type = BTRFS_DEV_EXTENT_KEY; |
| 3857 | |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3858 | do { |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3859 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3860 | if (ret < 0) |
| 3861 | goto done; |
| 3862 | |
| 3863 | ret = btrfs_previous_item(root, path, 0, key.type); |
| 3864 | if (ret < 0) |
| 3865 | goto done; |
| 3866 | if (ret) { |
| 3867 | ret = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3868 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3869 | break; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | l = path->nodes[0]; |
| 3873 | slot = path->slots[0]; |
| 3874 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 3875 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3876 | if (key.objectid != device->devid) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3877 | btrfs_release_path(path); |
Yan Zheng | bf1fb51 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 3878 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3879 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3880 | |
| 3881 | dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); |
| 3882 | length = btrfs_dev_extent_length(l, dev_extent); |
| 3883 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3884 | if (key.offset + length <= new_size) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3885 | btrfs_release_path(path); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3886 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3887 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3888 | |
| 3889 | chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); |
| 3890 | chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); |
| 3891 | chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3892 | btrfs_release_path(path); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3893 | |
| 3894 | ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid, |
| 3895 | chunk_offset); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3896 | if (ret && ret != -ENOSPC) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3897 | goto done; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3898 | if (ret == -ENOSPC) |
| 3899 | failed++; |
Ilya Dryomov | 213e64d | 2012-03-27 17:09:18 +0300 | [diff] [blame] | 3900 | } while (key.offset-- > 0); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3901 | |
| 3902 | if (failed && !retried) { |
| 3903 | failed = 0; |
| 3904 | retried = true; |
| 3905 | goto again; |
| 3906 | } else if (failed && retried) { |
| 3907 | ret = -ENOSPC; |
| 3908 | lock_chunks(root); |
| 3909 | |
| 3910 | device->total_bytes = old_size; |
| 3911 | if (device->writeable) |
| 3912 | device->fs_devices->total_rw_bytes += diff; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3913 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3914 | root->fs_info->free_chunk_space += diff; |
| 3915 | spin_unlock(&root->fs_info->free_chunk_lock); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3916 | unlock_chunks(root); |
| 3917 | goto done; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3918 | } |
| 3919 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3920 | /* Shrinking succeeded, else we would be at "done". */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3921 | trans = btrfs_start_transaction(root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 3922 | if (IS_ERR(trans)) { |
| 3923 | ret = PTR_ERR(trans); |
| 3924 | goto done; |
| 3925 | } |
| 3926 | |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 3927 | lock_chunks(root); |
| 3928 | |
| 3929 | device->disk_total_bytes = new_size; |
| 3930 | /* Now btrfs_update_device() will change the on-disk size. */ |
| 3931 | ret = btrfs_update_device(trans, device); |
| 3932 | if (ret) { |
| 3933 | unlock_chunks(root); |
| 3934 | btrfs_end_transaction(trans, root); |
| 3935 | goto done; |
| 3936 | } |
| 3937 | WARN_ON(diff > old_total); |
| 3938 | btrfs_set_super_total_bytes(super_copy, old_total - diff); |
| 3939 | unlock_chunks(root); |
| 3940 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3941 | done: |
| 3942 | btrfs_free_path(path); |
| 3943 | return ret; |
| 3944 | } |
| 3945 | |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 3946 | static int btrfs_add_system_chunk(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3947 | struct btrfs_key *key, |
| 3948 | struct btrfs_chunk *chunk, int item_size) |
| 3949 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3950 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3951 | struct btrfs_disk_key disk_key; |
| 3952 | u32 array_size; |
| 3953 | u8 *ptr; |
| 3954 | |
| 3955 | array_size = btrfs_super_sys_array_size(super_copy); |
Gui Hecheng | 5f43f86 | 2014-04-21 20:13:11 +0800 | [diff] [blame] | 3956 | if (array_size + item_size + sizeof(disk_key) |
| 3957 | > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3958 | return -EFBIG; |
| 3959 | |
| 3960 | ptr = super_copy->sys_chunk_array + array_size; |
| 3961 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 3962 | memcpy(ptr, &disk_key, sizeof(disk_key)); |
| 3963 | ptr += sizeof(disk_key); |
| 3964 | memcpy(ptr, chunk, item_size); |
| 3965 | item_size += sizeof(disk_key); |
| 3966 | btrfs_set_super_sys_array_size(super_copy, array_size + item_size); |
| 3967 | return 0; |
| 3968 | } |
| 3969 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3970 | /* |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3971 | * sort the devices in descending order by max_avail, total_avail |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3972 | */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3973 | static int btrfs_cmp_device_info(const void *a, const void *b) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3974 | { |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3975 | const struct btrfs_device_info *di_a = a; |
| 3976 | const struct btrfs_device_info *di_b = b; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3977 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 3978 | if (di_a->max_avail > di_b->max_avail) |
| 3979 | return -1; |
| 3980 | if (di_a->max_avail < di_b->max_avail) |
| 3981 | return 1; |
| 3982 | if (di_a->total_avail > di_b->total_avail) |
| 3983 | return -1; |
| 3984 | if (di_a->total_avail < di_b->total_avail) |
| 3985 | return 1; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 3986 | return 0; |
| 3987 | } |
| 3988 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3989 | static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = { |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 3990 | [BTRFS_RAID_RAID10] = { |
| 3991 | .sub_stripes = 2, |
| 3992 | .dev_stripes = 1, |
| 3993 | .devs_max = 0, /* 0 == as many as possible */ |
| 3994 | .devs_min = 4, |
| 3995 | .devs_increment = 2, |
| 3996 | .ncopies = 2, |
| 3997 | }, |
| 3998 | [BTRFS_RAID_RAID1] = { |
| 3999 | .sub_stripes = 1, |
| 4000 | .dev_stripes = 1, |
| 4001 | .devs_max = 2, |
| 4002 | .devs_min = 2, |
| 4003 | .devs_increment = 2, |
| 4004 | .ncopies = 2, |
| 4005 | }, |
| 4006 | [BTRFS_RAID_DUP] = { |
| 4007 | .sub_stripes = 1, |
| 4008 | .dev_stripes = 2, |
| 4009 | .devs_max = 1, |
| 4010 | .devs_min = 1, |
| 4011 | .devs_increment = 1, |
| 4012 | .ncopies = 2, |
| 4013 | }, |
| 4014 | [BTRFS_RAID_RAID0] = { |
| 4015 | .sub_stripes = 1, |
| 4016 | .dev_stripes = 1, |
| 4017 | .devs_max = 0, |
| 4018 | .devs_min = 2, |
| 4019 | .devs_increment = 1, |
| 4020 | .ncopies = 1, |
| 4021 | }, |
| 4022 | [BTRFS_RAID_SINGLE] = { |
| 4023 | .sub_stripes = 1, |
| 4024 | .dev_stripes = 1, |
| 4025 | .devs_max = 1, |
| 4026 | .devs_min = 1, |
| 4027 | .devs_increment = 1, |
| 4028 | .ncopies = 1, |
| 4029 | }, |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 4030 | [BTRFS_RAID_RAID5] = { |
| 4031 | .sub_stripes = 1, |
| 4032 | .dev_stripes = 1, |
| 4033 | .devs_max = 0, |
| 4034 | .devs_min = 2, |
| 4035 | .devs_increment = 1, |
| 4036 | .ncopies = 2, |
| 4037 | }, |
| 4038 | [BTRFS_RAID_RAID6] = { |
| 4039 | .sub_stripes = 1, |
| 4040 | .dev_stripes = 1, |
| 4041 | .devs_max = 0, |
| 4042 | .devs_min = 3, |
| 4043 | .devs_increment = 1, |
| 4044 | .ncopies = 3, |
| 4045 | }, |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4046 | }; |
| 4047 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4048 | static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target) |
| 4049 | { |
| 4050 | /* TODO allow them to set a preferred stripe size */ |
| 4051 | return 64 * 1024; |
| 4052 | } |
| 4053 | |
| 4054 | static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type) |
| 4055 | { |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4056 | if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))) |
| 4057 | return; |
| 4058 | |
Miao Xie | ceda086 | 2013-04-11 10:30:16 +0000 | [diff] [blame] | 4059 | btrfs_set_fs_incompat(info, RAID56); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4060 | } |
| 4061 | |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4062 | #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \ |
| 4063 | - sizeof(struct btrfs_item) \ |
| 4064 | - sizeof(struct btrfs_chunk)) \ |
| 4065 | / sizeof(struct btrfs_stripe) + 1) |
| 4066 | |
| 4067 | #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \ |
| 4068 | - 2 * sizeof(struct btrfs_disk_key) \ |
| 4069 | - 2 * sizeof(struct btrfs_chunk)) \ |
| 4070 | / sizeof(struct btrfs_stripe) + 1) |
| 4071 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4072 | static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4073 | struct btrfs_root *extent_root, u64 start, |
| 4074 | u64 type) |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4075 | { |
| 4076 | struct btrfs_fs_info *info = extent_root->fs_info; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4077 | struct btrfs_fs_devices *fs_devices = info->fs_devices; |
| 4078 | struct list_head *cur; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4079 | struct map_lookup *map = NULL; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4080 | struct extent_map_tree *em_tree; |
| 4081 | struct extent_map *em; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4082 | struct btrfs_device_info *devices_info = NULL; |
| 4083 | u64 total_avail; |
| 4084 | int num_stripes; /* total number of stripes to allocate */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4085 | int data_stripes; /* number of stripes that count for |
| 4086 | block group size */ |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4087 | int sub_stripes; /* sub_stripes info for map */ |
| 4088 | int dev_stripes; /* stripes per dev */ |
| 4089 | int devs_max; /* max devs to use */ |
| 4090 | int devs_min; /* min devs needed */ |
| 4091 | int devs_increment; /* ndevs has to be a multiple of this */ |
| 4092 | int ncopies; /* how many copies to data has */ |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4093 | int ret; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4094 | u64 max_stripe_size; |
| 4095 | u64 max_chunk_size; |
| 4096 | u64 stripe_size; |
| 4097 | u64 num_bytes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4098 | u64 raid_stripe_len = BTRFS_STRIPE_LEN; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4099 | int ndevs; |
| 4100 | int i; |
| 4101 | int j; |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4102 | int index; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4103 | |
Ilya Dryomov | 0c460c0 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 4104 | BUG_ON(!alloc_profile_is_valid(type, 0)); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4105 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4106 | if (list_empty(&fs_devices->alloc_list)) |
| 4107 | return -ENOSPC; |
| 4108 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4109 | index = __get_raid_index(type); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4110 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 4111 | sub_stripes = btrfs_raid_array[index].sub_stripes; |
| 4112 | dev_stripes = btrfs_raid_array[index].dev_stripes; |
| 4113 | devs_max = btrfs_raid_array[index].devs_max; |
| 4114 | devs_min = btrfs_raid_array[index].devs_min; |
| 4115 | devs_increment = btrfs_raid_array[index].devs_increment; |
| 4116 | ncopies = btrfs_raid_array[index].ncopies; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4117 | |
| 4118 | if (type & BTRFS_BLOCK_GROUP_DATA) { |
| 4119 | max_stripe_size = 1024 * 1024 * 1024; |
| 4120 | max_chunk_size = 10 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4121 | if (!devs_max) |
| 4122 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4123 | } else if (type & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 1100373 | 2012-01-06 15:47:38 -0500 | [diff] [blame] | 4124 | /* for larger filesystems, use larger metadata chunks */ |
| 4125 | if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024) |
| 4126 | max_stripe_size = 1024 * 1024 * 1024; |
| 4127 | else |
| 4128 | max_stripe_size = 256 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4129 | max_chunk_size = max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4130 | if (!devs_max) |
| 4131 | devs_max = BTRFS_MAX_DEVS(info->chunk_root); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4132 | } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) { |
Chris Mason | 96bdc7d | 2012-01-16 08:13:11 -0500 | [diff] [blame] | 4133 | max_stripe_size = 32 * 1024 * 1024; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4134 | max_chunk_size = 2 * max_stripe_size; |
Gui Hecheng | 23f8f9b | 2014-04-21 20:13:12 +0800 | [diff] [blame] | 4135 | if (!devs_max) |
| 4136 | devs_max = BTRFS_MAX_DEVS_SYS_CHUNK; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4137 | } else { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4138 | btrfs_err(info, "invalid chunk type 0x%llx requested", |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4139 | type); |
| 4140 | BUG_ON(1); |
| 4141 | } |
| 4142 | |
| 4143 | /* we don't want a chunk larger than 10% of writeable space */ |
| 4144 | max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1), |
| 4145 | max_chunk_size); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4146 | |
| 4147 | devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices, |
| 4148 | GFP_NOFS); |
| 4149 | if (!devices_info) |
| 4150 | return -ENOMEM; |
| 4151 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4152 | cur = fs_devices->alloc_list.next; |
| 4153 | |
| 4154 | /* |
| 4155 | * in the first pass through the devices list, we gather information |
| 4156 | * about the available holes on each device. |
| 4157 | */ |
| 4158 | ndevs = 0; |
| 4159 | while (cur != &fs_devices->alloc_list) { |
| 4160 | struct btrfs_device *device; |
| 4161 | u64 max_avail; |
| 4162 | u64 dev_offset; |
| 4163 | |
| 4164 | device = list_entry(cur, struct btrfs_device, dev_alloc_list); |
| 4165 | |
| 4166 | cur = cur->next; |
| 4167 | |
| 4168 | if (!device->writeable) { |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 4169 | WARN(1, KERN_ERR |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 4170 | "BTRFS: read-only device in alloc_list\n"); |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4171 | continue; |
| 4172 | } |
| 4173 | |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 4174 | if (!device->in_fs_metadata || |
| 4175 | device->is_tgtdev_for_dev_replace) |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4176 | continue; |
| 4177 | |
| 4178 | if (device->total_bytes > device->bytes_used) |
| 4179 | total_avail = device->total_bytes - device->bytes_used; |
| 4180 | else |
| 4181 | total_avail = 0; |
liubo | 38c01b9 | 2011-08-02 02:39:03 +0000 | [diff] [blame] | 4182 | |
| 4183 | /* If there is no space on this device, skip it. */ |
| 4184 | if (total_avail == 0) |
| 4185 | continue; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4186 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4187 | ret = find_free_dev_extent(trans, device, |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4188 | max_stripe_size * dev_stripes, |
| 4189 | &dev_offset, &max_avail); |
| 4190 | if (ret && ret != -ENOSPC) |
| 4191 | goto error; |
| 4192 | |
| 4193 | if (ret == 0) |
| 4194 | max_avail = max_stripe_size * dev_stripes; |
| 4195 | |
| 4196 | if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) |
| 4197 | continue; |
| 4198 | |
Eric Sandeen | 063d006 | 2013-01-31 00:55:01 +0000 | [diff] [blame] | 4199 | if (ndevs == fs_devices->rw_devices) { |
| 4200 | WARN(1, "%s: found more than %llu devices\n", |
| 4201 | __func__, fs_devices->rw_devices); |
| 4202 | break; |
| 4203 | } |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4204 | devices_info[ndevs].dev_offset = dev_offset; |
| 4205 | devices_info[ndevs].max_avail = max_avail; |
| 4206 | devices_info[ndevs].total_avail = total_avail; |
| 4207 | devices_info[ndevs].dev = device; |
| 4208 | ++ndevs; |
| 4209 | } |
| 4210 | |
| 4211 | /* |
| 4212 | * now sort the devices by hole size / available space |
| 4213 | */ |
| 4214 | sort(devices_info, ndevs, sizeof(struct btrfs_device_info), |
| 4215 | btrfs_cmp_device_info, NULL); |
| 4216 | |
| 4217 | /* round down to number of usable stripes */ |
| 4218 | ndevs -= ndevs % devs_increment; |
| 4219 | |
| 4220 | if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) { |
| 4221 | ret = -ENOSPC; |
| 4222 | goto error; |
| 4223 | } |
| 4224 | |
| 4225 | if (devs_max && ndevs > devs_max) |
| 4226 | ndevs = devs_max; |
| 4227 | /* |
| 4228 | * the primary goal is to maximize the number of stripes, so use as many |
| 4229 | * devices as possible, even if the stripes are not maximum sized. |
| 4230 | */ |
| 4231 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4232 | num_stripes = ndevs * dev_stripes; |
| 4233 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4234 | /* |
| 4235 | * this will have to be fixed for RAID1 and RAID10 over |
| 4236 | * more drives |
| 4237 | */ |
| 4238 | data_stripes = num_stripes / ncopies; |
| 4239 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4240 | if (type & BTRFS_BLOCK_GROUP_RAID5) { |
| 4241 | raid_stripe_len = find_raid56_stripe_len(ndevs - 1, |
| 4242 | btrfs_super_stripesize(info->super_copy)); |
| 4243 | data_stripes = num_stripes - 1; |
| 4244 | } |
| 4245 | if (type & BTRFS_BLOCK_GROUP_RAID6) { |
| 4246 | raid_stripe_len = find_raid56_stripe_len(ndevs - 2, |
| 4247 | btrfs_super_stripesize(info->super_copy)); |
| 4248 | data_stripes = num_stripes - 2; |
| 4249 | } |
Chris Mason | 86db257 | 2013-02-20 16:23:40 -0500 | [diff] [blame] | 4250 | |
| 4251 | /* |
| 4252 | * Use the number of data stripes to figure out how big this chunk |
| 4253 | * is really going to be in terms of logical address space, |
| 4254 | * and compare that answer with the max chunk size |
| 4255 | */ |
| 4256 | if (stripe_size * data_stripes > max_chunk_size) { |
| 4257 | u64 mask = (1ULL << 24) - 1; |
| 4258 | stripe_size = max_chunk_size; |
| 4259 | do_div(stripe_size, data_stripes); |
| 4260 | |
| 4261 | /* bump the answer up to a 16MB boundary */ |
| 4262 | stripe_size = (stripe_size + mask) & ~mask; |
| 4263 | |
| 4264 | /* but don't go higher than the limits we found |
| 4265 | * while searching for free extents |
| 4266 | */ |
| 4267 | if (stripe_size > devices_info[ndevs-1].max_avail) |
| 4268 | stripe_size = devices_info[ndevs-1].max_avail; |
| 4269 | } |
| 4270 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4271 | do_div(stripe_size, dev_stripes); |
Ilya Dryomov | 37db63a | 2012-04-13 17:05:08 +0300 | [diff] [blame] | 4272 | |
| 4273 | /* align to BTRFS_STRIPE_LEN */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4274 | do_div(stripe_size, raid_stripe_len); |
| 4275 | stripe_size *= raid_stripe_len; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4276 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4277 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
| 4278 | if (!map) { |
| 4279 | ret = -ENOMEM; |
| 4280 | goto error; |
| 4281 | } |
| 4282 | map->num_stripes = num_stripes; |
Chris Mason | 9b3f68b | 2008-04-18 10:29:51 -0400 | [diff] [blame] | 4283 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4284 | for (i = 0; i < ndevs; ++i) { |
| 4285 | for (j = 0; j < dev_stripes; ++j) { |
| 4286 | int s = i * dev_stripes + j; |
| 4287 | map->stripes[s].dev = devices_info[i].dev; |
| 4288 | map->stripes[s].physical = devices_info[i].dev_offset + |
| 4289 | j * stripe_size; |
Chris Mason | a40a90a | 2008-04-18 11:55:51 -0400 | [diff] [blame] | 4290 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4291 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4292 | map->sector_size = extent_root->sectorsize; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4293 | map->stripe_len = raid_stripe_len; |
| 4294 | map->io_align = raid_stripe_len; |
| 4295 | map->io_width = raid_stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4296 | map->type = type; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4297 | map->sub_stripes = sub_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4298 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4299 | num_bytes = stripe_size * data_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4300 | |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4301 | trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4302 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 4303 | em = alloc_extent_map(); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4304 | if (!em) { |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4305 | kfree(map); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4306 | ret = -ENOMEM; |
| 4307 | goto error; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4308 | } |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 4309 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4310 | em->bdev = (struct block_device *)map; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4311 | em->start = start; |
Arne Jansen | 73c5de0 | 2011-04-12 12:07:57 +0200 | [diff] [blame] | 4312 | em->len = num_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4313 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4314 | em->block_len = em->len; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4315 | em->orig_block_len = stripe_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4316 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4317 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4318 | write_lock(&em_tree->lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 4319 | ret = add_extent_mapping(em_tree, em, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4320 | if (!ret) { |
| 4321 | list_add_tail(&em->list, &trans->transaction->pending_chunks); |
| 4322 | atomic_inc(&em->refs); |
| 4323 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4324 | write_unlock(&em_tree->lock); |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4325 | if (ret) { |
| 4326 | free_extent_map(em); |
Mark Fasheh | 1dd4602 | 2011-09-08 17:29:00 -0700 | [diff] [blame] | 4327 | goto error; |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4328 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4329 | |
Josef Bacik | 0448748 | 2013-01-29 15:03:37 -0500 | [diff] [blame] | 4330 | ret = btrfs_make_block_group(trans, extent_root, 0, type, |
| 4331 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4332 | start, num_bytes); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4333 | if (ret) |
| 4334 | goto error_del_extent; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4335 | |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4336 | free_extent_map(em); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4337 | check_raid56_incompat_flag(extent_root->fs_info, type); |
| 4338 | |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4339 | kfree(devices_info); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4340 | return 0; |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4341 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4342 | error_del_extent: |
Josef Bacik | 0f5d42b | 2013-01-31 10:23:04 -0500 | [diff] [blame] | 4343 | write_lock(&em_tree->lock); |
| 4344 | remove_extent_mapping(em_tree, em); |
| 4345 | write_unlock(&em_tree->lock); |
| 4346 | |
| 4347 | /* One for our allocation */ |
| 4348 | free_extent_map(em); |
| 4349 | /* One for the tree reference */ |
| 4350 | free_extent_map(em); |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4351 | error: |
Miao Xie | b2117a3 | 2011-01-05 10:07:28 +0000 | [diff] [blame] | 4352 | kfree(devices_info); |
| 4353 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4354 | } |
| 4355 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4356 | int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4357 | struct btrfs_root *extent_root, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4358 | u64 chunk_offset, u64 chunk_size) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4359 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4360 | struct btrfs_key key; |
| 4361 | struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root; |
| 4362 | struct btrfs_device *device; |
| 4363 | struct btrfs_chunk *chunk; |
| 4364 | struct btrfs_stripe *stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4365 | struct extent_map_tree *em_tree; |
| 4366 | struct extent_map *em; |
| 4367 | struct map_lookup *map; |
| 4368 | size_t item_size; |
| 4369 | u64 dev_offset; |
| 4370 | u64 stripe_size; |
| 4371 | int i = 0; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4372 | int ret; |
| 4373 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4374 | em_tree = &extent_root->fs_info->mapping_tree.map_tree; |
| 4375 | read_lock(&em_tree->lock); |
| 4376 | em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); |
| 4377 | read_unlock(&em_tree->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4378 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4379 | if (!em) { |
| 4380 | btrfs_crit(extent_root->fs_info, "unable to find logical " |
| 4381 | "%Lu len %Lu", chunk_offset, chunk_size); |
| 4382 | return -EINVAL; |
| 4383 | } |
| 4384 | |
| 4385 | if (em->start != chunk_offset || em->len != chunk_size) { |
| 4386 | btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted" |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4387 | " %Lu-%Lu, found %Lu-%Lu", chunk_offset, |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4388 | chunk_size, em->start, em->len); |
| 4389 | free_extent_map(em); |
| 4390 | return -EINVAL; |
| 4391 | } |
| 4392 | |
| 4393 | map = (struct map_lookup *)em->bdev; |
| 4394 | item_size = btrfs_chunk_item_size(map->num_stripes); |
| 4395 | stripe_size = em->orig_block_len; |
| 4396 | |
| 4397 | chunk = kzalloc(item_size, GFP_NOFS); |
| 4398 | if (!chunk) { |
| 4399 | ret = -ENOMEM; |
| 4400 | goto out; |
| 4401 | } |
| 4402 | |
| 4403 | for (i = 0; i < map->num_stripes; i++) { |
| 4404 | device = map->stripes[i].dev; |
| 4405 | dev_offset = map->stripes[i].physical; |
| 4406 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4407 | device->bytes_used += stripe_size; |
| 4408 | ret = btrfs_update_device(trans, device); |
Mark Fasheh | 3acd395 | 2011-09-08 17:40:01 -0700 | [diff] [blame] | 4409 | if (ret) |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4410 | goto out; |
| 4411 | ret = btrfs_alloc_dev_extent(trans, device, |
| 4412 | chunk_root->root_key.objectid, |
| 4413 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, |
| 4414 | chunk_offset, dev_offset, |
| 4415 | stripe_size); |
| 4416 | if (ret) |
| 4417 | goto out; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4418 | } |
| 4419 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4420 | spin_lock(&extent_root->fs_info->free_chunk_lock); |
| 4421 | extent_root->fs_info->free_chunk_space -= (stripe_size * |
| 4422 | map->num_stripes); |
| 4423 | spin_unlock(&extent_root->fs_info->free_chunk_lock); |
| 4424 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4425 | stripe = &chunk->stripe; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4426 | for (i = 0; i < map->num_stripes; i++) { |
| 4427 | device = map->stripes[i].dev; |
| 4428 | dev_offset = map->stripes[i].physical; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4429 | |
| 4430 | btrfs_set_stack_stripe_devid(stripe, device->devid); |
| 4431 | btrfs_set_stack_stripe_offset(stripe, dev_offset); |
| 4432 | memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); |
| 4433 | stripe++; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | btrfs_set_stack_chunk_length(chunk, chunk_size); |
| 4437 | btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); |
| 4438 | btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len); |
| 4439 | btrfs_set_stack_chunk_type(chunk, map->type); |
| 4440 | btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); |
| 4441 | btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); |
| 4442 | btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); |
| 4443 | btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize); |
| 4444 | btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); |
| 4445 | |
| 4446 | key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; |
| 4447 | key.type = BTRFS_CHUNK_ITEM_KEY; |
| 4448 | key.offset = chunk_offset; |
| 4449 | |
| 4450 | ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4451 | if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 4452 | /* |
| 4453 | * TODO: Cleanup of inserted chunk root in case of |
| 4454 | * failure. |
| 4455 | */ |
Li Zefan | 125ccb0 | 2011-12-08 15:07:24 +0800 | [diff] [blame] | 4456 | ret = btrfs_add_system_chunk(chunk_root, &key, chunk, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4457 | item_size); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4458 | } |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 4459 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4460 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4461 | kfree(chunk); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4462 | free_extent_map(em); |
Mark Fasheh | 4ed1d16 | 2011-08-10 12:32:10 -0700 | [diff] [blame] | 4463 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | /* |
| 4467 | * Chunk allocation falls into two parts. The first part does works |
| 4468 | * that make the new allocated chunk useable, but not do any operation |
| 4469 | * that modifies the chunk tree. The second part does the works that |
| 4470 | * require modifying the chunk tree. This division is important for the |
| 4471 | * bootstrap process of adding storage to a seed btrfs. |
| 4472 | */ |
| 4473 | int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, |
| 4474 | struct btrfs_root *extent_root, u64 type) |
| 4475 | { |
| 4476 | u64 chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4477 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4478 | chunk_offset = find_next_chunk(extent_root->fs_info); |
| 4479 | return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4480 | } |
| 4481 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4482 | static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4483 | struct btrfs_root *root, |
| 4484 | struct btrfs_device *device) |
| 4485 | { |
| 4486 | u64 chunk_offset; |
| 4487 | u64 sys_chunk_offset; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4488 | u64 alloc_profile; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4489 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4490 | struct btrfs_root *extent_root = fs_info->extent_root; |
| 4491 | int ret; |
| 4492 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4493 | chunk_offset = find_next_chunk(fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4494 | alloc_profile = btrfs_get_alloc_profile(extent_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4495 | ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, |
| 4496 | alloc_profile); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4497 | if (ret) |
| 4498 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4499 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4500 | sys_chunk_offset = find_next_chunk(root->fs_info); |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 4501 | alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 4502 | ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, |
| 4503 | alloc_profile); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4504 | if (ret) { |
| 4505 | btrfs_abort_transaction(trans, root, ret); |
| 4506 | goto out; |
| 4507 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4508 | |
| 4509 | ret = btrfs_add_device(trans, fs_info->chunk_root, device); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4510 | if (ret) |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4511 | btrfs_abort_transaction(trans, root, ret); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 4512 | out: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4513 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) |
| 4517 | { |
| 4518 | struct extent_map *em; |
| 4519 | struct map_lookup *map; |
| 4520 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 4521 | int readonly = 0; |
| 4522 | int i; |
| 4523 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4524 | read_lock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4525 | em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4526 | read_unlock(&map_tree->map_tree.lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4527 | if (!em) |
| 4528 | return 1; |
| 4529 | |
Josef Bacik | f48b907 | 2010-01-27 02:07:59 +0000 | [diff] [blame] | 4530 | if (btrfs_test_opt(root, DEGRADED)) { |
| 4531 | free_extent_map(em); |
| 4532 | return 0; |
| 4533 | } |
| 4534 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 4535 | map = (struct map_lookup *)em->bdev; |
| 4536 | for (i = 0; i < map->num_stripes; i++) { |
| 4537 | if (!map->stripes[i].dev->writeable) { |
| 4538 | readonly = 1; |
| 4539 | break; |
| 4540 | } |
| 4541 | } |
| 4542 | free_extent_map(em); |
| 4543 | return readonly; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | void btrfs_mapping_init(struct btrfs_mapping_tree *tree) |
| 4547 | { |
David Sterba | a8067e0 | 2011-04-21 00:34:43 +0200 | [diff] [blame] | 4548 | extent_map_tree_init(&tree->map_tree); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree) |
| 4552 | { |
| 4553 | struct extent_map *em; |
| 4554 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4555 | while (1) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4556 | write_lock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4557 | em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1); |
| 4558 | if (em) |
| 4559 | remove_extent_mapping(&tree->map_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4560 | write_unlock(&tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4561 | if (!em) |
| 4562 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4563 | /* once for us */ |
| 4564 | free_extent_map(em); |
| 4565 | /* once for the tree */ |
| 4566 | free_extent_map(em); |
| 4567 | } |
| 4568 | } |
| 4569 | |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4570 | 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] | 4571 | { |
Stefan Behrens | 5d96405 | 2012-11-05 14:59:07 +0100 | [diff] [blame] | 4572 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4573 | struct extent_map *em; |
| 4574 | struct map_lookup *map; |
| 4575 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4576 | int ret; |
| 4577 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4578 | read_lock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4579 | em = lookup_extent_mapping(em_tree, logical, len); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4580 | read_unlock(&em_tree->lock); |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4581 | |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4582 | /* |
| 4583 | * We could return errors for these cases, but that could get ugly and |
| 4584 | * we'd probably do the same thing which is just not do anything else |
| 4585 | * and exit, so return 1 so the callers don't try to use other copies. |
| 4586 | */ |
| 4587 | if (!em) { |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4588 | btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4589 | logical+len); |
| 4590 | return 1; |
| 4591 | } |
| 4592 | |
| 4593 | if (em->start > logical || em->start + em->len < logical) { |
David Sterba | ccf39f9 | 2013-07-11 15:41:11 +0200 | [diff] [blame] | 4594 | btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4595 | "%Lu-%Lu", logical, logical+len, em->start, |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4596 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4597 | free_extent_map(em); |
Josef Bacik | fb7669b | 2013-04-23 10:53:18 -0400 | [diff] [blame] | 4598 | return 1; |
| 4599 | } |
| 4600 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4601 | map = (struct map_lookup *)em->bdev; |
| 4602 | if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) |
| 4603 | ret = map->num_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4604 | else if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 4605 | ret = map->sub_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4606 | else if (map->type & BTRFS_BLOCK_GROUP_RAID5) |
| 4607 | ret = 2; |
| 4608 | else if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 4609 | ret = 3; |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4610 | else |
| 4611 | ret = 1; |
| 4612 | free_extent_map(em); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4613 | |
| 4614 | btrfs_dev_replace_lock(&fs_info->dev_replace); |
| 4615 | if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) |
| 4616 | ret++; |
| 4617 | btrfs_dev_replace_unlock(&fs_info->dev_replace); |
| 4618 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4619 | return ret; |
| 4620 | } |
| 4621 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4622 | unsigned long btrfs_full_stripe_len(struct btrfs_root *root, |
| 4623 | struct btrfs_mapping_tree *map_tree, |
| 4624 | u64 logical) |
| 4625 | { |
| 4626 | struct extent_map *em; |
| 4627 | struct map_lookup *map; |
| 4628 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4629 | unsigned long len = root->sectorsize; |
| 4630 | |
| 4631 | read_lock(&em_tree->lock); |
| 4632 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4633 | read_unlock(&em_tree->lock); |
| 4634 | BUG_ON(!em); |
| 4635 | |
| 4636 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4637 | map = (struct map_lookup *)em->bdev; |
| 4638 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4639 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4640 | len = map->stripe_len * nr_data_stripes(map); |
| 4641 | } |
| 4642 | free_extent_map(em); |
| 4643 | return len; |
| 4644 | } |
| 4645 | |
| 4646 | int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, |
| 4647 | u64 logical, u64 len, int mirror_num) |
| 4648 | { |
| 4649 | struct extent_map *em; |
| 4650 | struct map_lookup *map; |
| 4651 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4652 | int ret = 0; |
| 4653 | |
| 4654 | read_lock(&em_tree->lock); |
| 4655 | em = lookup_extent_mapping(em_tree, logical, len); |
| 4656 | read_unlock(&em_tree->lock); |
| 4657 | BUG_ON(!em); |
| 4658 | |
| 4659 | BUG_ON(em->start > logical || em->start + em->len < logical); |
| 4660 | map = (struct map_lookup *)em->bdev; |
| 4661 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4662 | BTRFS_BLOCK_GROUP_RAID6)) |
| 4663 | ret = 1; |
| 4664 | free_extent_map(em); |
| 4665 | return ret; |
| 4666 | } |
| 4667 | |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4668 | static int find_live_mirror(struct btrfs_fs_info *fs_info, |
| 4669 | struct map_lookup *map, int first, int num, |
| 4670 | int optimal, int dev_replace_is_ongoing) |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4671 | { |
| 4672 | int i; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4673 | int tolerance; |
| 4674 | struct btrfs_device *srcdev; |
| 4675 | |
| 4676 | if (dev_replace_is_ongoing && |
| 4677 | fs_info->dev_replace.cont_reading_from_srcdev_mode == |
| 4678 | BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID) |
| 4679 | srcdev = fs_info->dev_replace.srcdev; |
| 4680 | else |
| 4681 | srcdev = NULL; |
| 4682 | |
| 4683 | /* |
| 4684 | * try to avoid the drive that is the source drive for a |
| 4685 | * dev-replace procedure, only choose it if no other non-missing |
| 4686 | * mirror is available |
| 4687 | */ |
| 4688 | for (tolerance = 0; tolerance < 2; tolerance++) { |
| 4689 | if (map->stripes[optimal].dev->bdev && |
| 4690 | (tolerance || map->stripes[optimal].dev != srcdev)) |
| 4691 | return optimal; |
| 4692 | for (i = first; i < first + num; i++) { |
| 4693 | if (map->stripes[i].dev->bdev && |
| 4694 | (tolerance || map->stripes[i].dev != srcdev)) |
| 4695 | return i; |
| 4696 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4697 | } |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4698 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4699 | /* we couldn't find one that doesn't fail. Just return something |
| 4700 | * and the io error handling code will clean up eventually |
| 4701 | */ |
| 4702 | return optimal; |
| 4703 | } |
| 4704 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4705 | static inline int parity_smaller(u64 a, u64 b) |
| 4706 | { |
| 4707 | return a > b; |
| 4708 | } |
| 4709 | |
| 4710 | /* Bubble-sort the stripe set to put the parity/syndrome stripes last */ |
| 4711 | static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map) |
| 4712 | { |
| 4713 | struct btrfs_bio_stripe s; |
| 4714 | int i; |
| 4715 | u64 l; |
| 4716 | int again = 1; |
| 4717 | |
| 4718 | while (again) { |
| 4719 | again = 0; |
| 4720 | for (i = 0; i < bbio->num_stripes - 1; i++) { |
| 4721 | if (parity_smaller(raid_map[i], raid_map[i+1])) { |
| 4722 | s = bbio->stripes[i]; |
| 4723 | l = raid_map[i]; |
| 4724 | bbio->stripes[i] = bbio->stripes[i+1]; |
| 4725 | raid_map[i] = raid_map[i+1]; |
| 4726 | bbio->stripes[i+1] = s; |
| 4727 | raid_map[i+1] = l; |
| 4728 | again = 1; |
| 4729 | } |
| 4730 | } |
| 4731 | } |
| 4732 | } |
| 4733 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4734 | 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] | 4735 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4736 | struct btrfs_bio **bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4737 | int mirror_num, u64 **raid_map_ret) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4738 | { |
| 4739 | struct extent_map *em; |
| 4740 | struct map_lookup *map; |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 4741 | struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4742 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 4743 | u64 offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4744 | u64 stripe_offset; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4745 | u64 stripe_end_offset; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4746 | u64 stripe_nr; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4747 | u64 stripe_nr_orig; |
| 4748 | u64 stripe_nr_end; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4749 | u64 stripe_len; |
| 4750 | u64 *raid_map = NULL; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4751 | int stripe_index; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4752 | int i; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 4753 | int ret = 0; |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4754 | int num_stripes; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 4755 | int max_errors = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4756 | struct btrfs_bio *bbio = NULL; |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4757 | struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; |
| 4758 | int dev_replace_is_ongoing = 0; |
| 4759 | int num_alloc_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4760 | int patch_the_first_stripe_for_dev_replace = 0; |
| 4761 | u64 physical_to_patch_in_first_stripe = 0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4762 | u64 raid56_full_stripe_start = (u64)-1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4763 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4764 | read_lock(&em_tree->lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4765 | em = lookup_extent_mapping(em_tree, logical, *length); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 4766 | read_unlock(&em_tree->lock); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4767 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4768 | if (!em) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 4769 | btrfs_crit(fs_info, "unable to find logical %llu len %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 4770 | logical, *length); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4771 | return -EINVAL; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 4772 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4773 | |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4774 | if (em->start > logical || em->start + em->len < logical) { |
| 4775 | btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 4776 | "found %Lu-%Lu", logical, em->start, |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4777 | em->start + em->len); |
Liu Bo | 7d3d174 | 2013-09-29 10:33:16 +0800 | [diff] [blame] | 4778 | free_extent_map(em); |
Josef Bacik | 9bb9187 | 2013-04-19 23:45:33 -0400 | [diff] [blame] | 4779 | return -EINVAL; |
| 4780 | } |
| 4781 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4782 | map = (struct map_lookup *)em->bdev; |
| 4783 | offset = logical - em->start; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4784 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4785 | stripe_len = map->stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4786 | stripe_nr = offset; |
| 4787 | /* |
| 4788 | * stripe_nr counts the total number of stripes we have to stride |
| 4789 | * to get to this block |
| 4790 | */ |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4791 | do_div(stripe_nr, stripe_len); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4792 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4793 | stripe_offset = stripe_nr * stripe_len; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 4794 | BUG_ON(offset < stripe_offset); |
| 4795 | |
| 4796 | /* stripe_offset is the offset of this block in its stripe*/ |
| 4797 | stripe_offset = offset - stripe_offset; |
| 4798 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4799 | /* if we're here for raid56, we need to know the stripe aligned start */ |
| 4800 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4801 | unsigned long full_stripe_len = stripe_len * nr_data_stripes(map); |
| 4802 | raid56_full_stripe_start = offset; |
| 4803 | |
| 4804 | /* allow a write of a full stripe, but make sure we don't |
| 4805 | * allow straddling of stripes |
| 4806 | */ |
| 4807 | do_div(raid56_full_stripe_start, full_stripe_len); |
| 4808 | raid56_full_stripe_start *= full_stripe_len; |
| 4809 | } |
| 4810 | |
| 4811 | if (rw & REQ_DISCARD) { |
| 4812 | /* we don't discard raid56 yet */ |
| 4813 | if (map->type & |
| 4814 | (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4815 | ret = -EOPNOTSUPP; |
| 4816 | goto out; |
| 4817 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4818 | *length = min_t(u64, em->len - offset, *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4819 | } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) { |
| 4820 | u64 max_len; |
| 4821 | /* For writes to RAID[56], allow a full stripeset across all disks. |
| 4822 | For other RAID types and for RAID[56] reads, just allow a single |
| 4823 | stripe (on a single disk). */ |
| 4824 | if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) && |
| 4825 | (rw & REQ_WRITE)) { |
| 4826 | max_len = stripe_len * nr_data_stripes(map) - |
| 4827 | (offset - raid56_full_stripe_start); |
| 4828 | } else { |
| 4829 | /* we limit the length of each bio to what fits in a stripe */ |
| 4830 | max_len = stripe_len - stripe_offset; |
| 4831 | } |
| 4832 | *length = min_t(u64, em->len - offset, max_len); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4833 | } else { |
| 4834 | *length = em->len - offset; |
| 4835 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4836 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4837 | /* This is for when we're called from btrfs_merge_bio_hook() and all |
| 4838 | it cares about is the length */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4839 | if (!bbio_ret) |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4840 | goto out; |
| 4841 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 4842 | btrfs_dev_replace_lock(dev_replace); |
| 4843 | dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace); |
| 4844 | if (!dev_replace_is_ongoing) |
| 4845 | btrfs_dev_replace_unlock(dev_replace); |
| 4846 | |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4847 | if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 && |
| 4848 | !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) && |
| 4849 | dev_replace->tgtdev != NULL) { |
| 4850 | /* |
| 4851 | * in dev-replace case, for repair case (that's the only |
| 4852 | * case where the mirror is selected explicitly when |
| 4853 | * calling btrfs_map_block), blocks left of the left cursor |
| 4854 | * can also be read from the target drive. |
| 4855 | * For REQ_GET_READ_MIRRORS, the target drive is added as |
| 4856 | * the last one to the array of stripes. For READ, it also |
| 4857 | * needs to be supported using the same mirror number. |
| 4858 | * If the requested block is not left of the left cursor, |
| 4859 | * EIO is returned. This can happen because btrfs_num_copies() |
| 4860 | * returns one more in the dev-replace case. |
| 4861 | */ |
| 4862 | u64 tmp_length = *length; |
| 4863 | struct btrfs_bio *tmp_bbio = NULL; |
| 4864 | int tmp_num_stripes; |
| 4865 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 4866 | int index_srcdev = 0; |
| 4867 | int found = 0; |
| 4868 | u64 physical_of_found = 0; |
| 4869 | |
| 4870 | ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4871 | logical, &tmp_length, &tmp_bbio, 0, NULL); |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 4872 | if (ret) { |
| 4873 | WARN_ON(tmp_bbio != NULL); |
| 4874 | goto out; |
| 4875 | } |
| 4876 | |
| 4877 | tmp_num_stripes = tmp_bbio->num_stripes; |
| 4878 | if (mirror_num > tmp_num_stripes) { |
| 4879 | /* |
| 4880 | * REQ_GET_READ_MIRRORS does not contain this |
| 4881 | * mirror, that means that the requested area |
| 4882 | * is not left of the left cursor |
| 4883 | */ |
| 4884 | ret = -EIO; |
| 4885 | kfree(tmp_bbio); |
| 4886 | goto out; |
| 4887 | } |
| 4888 | |
| 4889 | /* |
| 4890 | * process the rest of the function using the mirror_num |
| 4891 | * of the source drive. Therefore look it up first. |
| 4892 | * At the end, patch the device pointer to the one of the |
| 4893 | * target drive. |
| 4894 | */ |
| 4895 | for (i = 0; i < tmp_num_stripes; i++) { |
| 4896 | if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { |
| 4897 | /* |
| 4898 | * In case of DUP, in order to keep it |
| 4899 | * simple, only add the mirror with the |
| 4900 | * lowest physical address |
| 4901 | */ |
| 4902 | if (found && |
| 4903 | physical_of_found <= |
| 4904 | tmp_bbio->stripes[i].physical) |
| 4905 | continue; |
| 4906 | index_srcdev = i; |
| 4907 | found = 1; |
| 4908 | physical_of_found = |
| 4909 | tmp_bbio->stripes[i].physical; |
| 4910 | } |
| 4911 | } |
| 4912 | |
| 4913 | if (found) { |
| 4914 | mirror_num = index_srcdev + 1; |
| 4915 | patch_the_first_stripe_for_dev_replace = 1; |
| 4916 | physical_to_patch_in_first_stripe = physical_of_found; |
| 4917 | } else { |
| 4918 | WARN_ON(1); |
| 4919 | ret = -EIO; |
| 4920 | kfree(tmp_bbio); |
| 4921 | goto out; |
| 4922 | } |
| 4923 | |
| 4924 | kfree(tmp_bbio); |
| 4925 | } else if (mirror_num > map->num_stripes) { |
| 4926 | mirror_num = 0; |
| 4927 | } |
| 4928 | |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4929 | num_stripes = 1; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4930 | stripe_index = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4931 | stripe_nr_orig = stripe_nr; |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 4932 | stripe_nr_end = ALIGN(offset + *length, map->stripe_len); |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4933 | do_div(stripe_nr_end, map->stripe_len); |
| 4934 | stripe_end_offset = stripe_nr_end * map->stripe_len - |
| 4935 | (offset + *length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4936 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4937 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 4938 | if (rw & REQ_DISCARD) |
| 4939 | num_stripes = min_t(u64, map->num_stripes, |
| 4940 | stripe_nr_end - stripe_nr_orig); |
| 4941 | stripe_index = do_div(stripe_nr, map->num_stripes); |
| 4942 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4943 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4944 | num_stripes = map->num_stripes; |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4945 | else if (mirror_num) |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4946 | stripe_index = mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4947 | else { |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4948 | stripe_index = find_live_mirror(fs_info, map, 0, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4949 | map->num_stripes, |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4950 | current->pid % map->num_stripes, |
| 4951 | dev_replace_is_ongoing); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4952 | mirror_num = stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4953 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4954 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 4955 | } else if (map->type & BTRFS_BLOCK_GROUP_DUP) { |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4956 | if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) { |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4957 | num_stripes = map->num_stripes; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4958 | } else if (mirror_num) { |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 4959 | stripe_index = mirror_num - 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 4960 | } else { |
| 4961 | mirror_num = 1; |
| 4962 | } |
Chris Mason | 2fff734 | 2008-04-29 14:12:09 -0400 | [diff] [blame] | 4963 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4964 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 4965 | int factor = map->num_stripes / map->sub_stripes; |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4966 | |
| 4967 | stripe_index = do_div(stripe_nr, factor); |
| 4968 | stripe_index *= map->sub_stripes; |
| 4969 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 4970 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 4971 | num_stripes = map->sub_stripes; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 4972 | else if (rw & REQ_DISCARD) |
| 4973 | num_stripes = min_t(u64, map->sub_stripes * |
| 4974 | (stripe_nr_end - stripe_nr_orig), |
| 4975 | map->num_stripes); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 4976 | else if (mirror_num) |
| 4977 | stripe_index += mirror_num - 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4978 | else { |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 4979 | int old_stripe_index = stripe_index; |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4980 | stripe_index = find_live_mirror(fs_info, map, |
| 4981 | stripe_index, |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4982 | map->sub_stripes, stripe_index + |
Stefan Behrens | 30d9861 | 2012-11-06 14:52:18 +0100 | [diff] [blame] | 4983 | current->pid % map->sub_stripes, |
| 4984 | dev_replace_is_ongoing); |
Jan Schmidt | 3e74317 | 2012-04-27 12:41:45 -0400 | [diff] [blame] | 4985 | mirror_num = stripe_index - old_stripe_index + 1; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 4986 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4987 | |
| 4988 | } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 4989 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 4990 | u64 tmp; |
| 4991 | |
| 4992 | if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1) |
| 4993 | && raid_map_ret) { |
| 4994 | int i, rot; |
| 4995 | |
| 4996 | /* push stripe_nr back to the start of the full stripe */ |
| 4997 | stripe_nr = raid56_full_stripe_start; |
| 4998 | do_div(stripe_nr, stripe_len); |
| 4999 | |
| 5000 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5001 | |
| 5002 | /* RAID[56] write or recovery. Return all stripes */ |
| 5003 | num_stripes = map->num_stripes; |
| 5004 | max_errors = nr_parity_stripes(map); |
| 5005 | |
Dulshani Gunawardhana | d9b0d9b | 2013-10-31 10:32:18 +0530 | [diff] [blame] | 5006 | raid_map = kmalloc_array(num_stripes, sizeof(u64), |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5007 | GFP_NOFS); |
| 5008 | if (!raid_map) { |
| 5009 | ret = -ENOMEM; |
| 5010 | goto out; |
| 5011 | } |
| 5012 | |
| 5013 | /* Work out the disk rotation on this stripe-set */ |
| 5014 | tmp = stripe_nr; |
| 5015 | rot = do_div(tmp, num_stripes); |
| 5016 | |
| 5017 | /* Fill in the logical address of each stripe */ |
| 5018 | tmp = stripe_nr * nr_data_stripes(map); |
| 5019 | for (i = 0; i < nr_data_stripes(map); i++) |
| 5020 | raid_map[(i+rot) % num_stripes] = |
| 5021 | em->start + (tmp + i) * map->stripe_len; |
| 5022 | |
| 5023 | raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE; |
| 5024 | if (map->type & BTRFS_BLOCK_GROUP_RAID6) |
| 5025 | raid_map[(i+rot+1) % num_stripes] = |
| 5026 | RAID6_Q_STRIPE; |
| 5027 | |
| 5028 | *length = map->stripe_len; |
| 5029 | stripe_index = 0; |
| 5030 | stripe_offset = 0; |
| 5031 | } else { |
| 5032 | /* |
| 5033 | * Mirror #0 or #1 means the original data block. |
| 5034 | * Mirror #2 is RAID5 parity block. |
| 5035 | * Mirror #3 is RAID6 Q block. |
| 5036 | */ |
| 5037 | stripe_index = do_div(stripe_nr, nr_data_stripes(map)); |
| 5038 | if (mirror_num > 1) |
| 5039 | stripe_index = nr_data_stripes(map) + |
| 5040 | mirror_num - 2; |
| 5041 | |
| 5042 | /* We distribute the parity blocks across stripes */ |
| 5043 | tmp = stripe_nr + stripe_index; |
| 5044 | stripe_index = do_div(tmp, map->num_stripes); |
| 5045 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5046 | } else { |
| 5047 | /* |
| 5048 | * after this do_div call, stripe_nr is the number of stripes |
| 5049 | * on this device we have to walk to find the data, and |
| 5050 | * stripe_index is the number of our device in the stripe array |
| 5051 | */ |
| 5052 | stripe_index = do_div(stripe_nr, map->num_stripes); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5053 | mirror_num = stripe_index + 1; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5054 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5055 | BUG_ON(stripe_index >= map->num_stripes); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5056 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5057 | num_alloc_stripes = num_stripes; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5058 | if (dev_replace_is_ongoing) { |
| 5059 | if (rw & (REQ_WRITE | REQ_DISCARD)) |
| 5060 | num_alloc_stripes <<= 1; |
| 5061 | if (rw & REQ_GET_READ_MIRRORS) |
| 5062 | num_alloc_stripes++; |
| 5063 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5064 | bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5065 | if (!bbio) { |
Dave Jones | eb2067f | 2013-07-30 13:42:17 -0400 | [diff] [blame] | 5066 | kfree(raid_map); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5067 | ret = -ENOMEM; |
| 5068 | goto out; |
| 5069 | } |
| 5070 | atomic_set(&bbio->error, 0); |
| 5071 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5072 | if (rw & REQ_DISCARD) { |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5073 | int factor = 0; |
| 5074 | int sub_stripes = 0; |
| 5075 | u64 stripes_per_dev = 0; |
| 5076 | u32 remaining_stripes = 0; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5077 | u32 last_stripe = 0; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5078 | |
| 5079 | if (map->type & |
| 5080 | (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5081 | if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5082 | sub_stripes = 1; |
| 5083 | else |
| 5084 | sub_stripes = map->sub_stripes; |
| 5085 | |
| 5086 | factor = map->num_stripes / sub_stripes; |
| 5087 | stripes_per_dev = div_u64_rem(stripe_nr_end - |
| 5088 | stripe_nr_orig, |
| 5089 | factor, |
| 5090 | &remaining_stripes); |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5091 | div_u64_rem(stripe_nr_end - 1, factor, &last_stripe); |
| 5092 | last_stripe *= sub_stripes; |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5093 | } |
| 5094 | |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5095 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5096 | bbio->stripes[i].physical = |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5097 | map->stripes[stripe_index].physical + |
| 5098 | stripe_offset + stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5099 | bbio->stripes[i].dev = map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5100 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5101 | if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | |
| 5102 | BTRFS_BLOCK_GROUP_RAID10)) { |
| 5103 | bbio->stripes[i].length = stripes_per_dev * |
| 5104 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5105 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5106 | if (i / sub_stripes < remaining_stripes) |
| 5107 | bbio->stripes[i].length += |
| 5108 | map->stripe_len; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5109 | |
| 5110 | /* |
| 5111 | * Special for the first stripe and |
| 5112 | * the last stripe: |
| 5113 | * |
| 5114 | * |-------|...|-------| |
| 5115 | * |----------| |
| 5116 | * off end_off |
| 5117 | */ |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5118 | if (i < sub_stripes) |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5119 | bbio->stripes[i].length -= |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5120 | stripe_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5121 | |
| 5122 | if (stripe_index >= last_stripe && |
| 5123 | stripe_index <= (last_stripe + |
| 5124 | sub_stripes - 1)) |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5125 | bbio->stripes[i].length -= |
| 5126 | stripe_end_offset; |
Liu Bo | b89203f | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 5127 | |
Li Zefan | ec9ef7a | 2011-12-01 14:06:42 +0800 | [diff] [blame] | 5128 | if (i == sub_stripes - 1) |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5129 | stripe_offset = 0; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5130 | } else |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5131 | bbio->stripes[i].length = *length; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5132 | |
| 5133 | stripe_index++; |
| 5134 | if (stripe_index == map->num_stripes) { |
| 5135 | /* This could only happen for RAID0/10 */ |
| 5136 | stripe_index = 0; |
| 5137 | stripe_nr++; |
| 5138 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5139 | } |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5140 | } else { |
| 5141 | for (i = 0; i < num_stripes; i++) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5142 | bbio->stripes[i].physical = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5143 | map->stripes[stripe_index].physical + |
| 5144 | stripe_offset + |
| 5145 | stripe_nr * map->stripe_len; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5146 | bbio->stripes[i].dev = |
Linus Torvalds | 212a17a | 2011-03-28 15:31:05 -0700 | [diff] [blame] | 5147 | map->stripes[stripe_index].dev; |
Li Dongyang | fce3bb9 | 2011-03-24 10:24:26 +0000 | [diff] [blame] | 5148 | stripe_index++; |
| 5149 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5150 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5151 | |
Stefan Behrens | 29a8d9a | 2012-11-06 14:16:24 +0100 | [diff] [blame] | 5152 | if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) { |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5153 | if (map->type & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5154 | BTRFS_BLOCK_GROUP_RAID10 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5155 | BTRFS_BLOCK_GROUP_RAID5 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5156 | BTRFS_BLOCK_GROUP_DUP)) { |
| 5157 | max_errors = 1; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5158 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) { |
| 5159 | max_errors = 2; |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5160 | } |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5161 | } |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5162 | |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5163 | if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) && |
| 5164 | dev_replace->tgtdev != NULL) { |
| 5165 | int index_where_to_add; |
| 5166 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5167 | |
| 5168 | /* |
| 5169 | * duplicate the write operations while the dev replace |
| 5170 | * procedure is running. Since the copying of the old disk |
| 5171 | * to the new disk takes place at run time while the |
| 5172 | * filesystem is mounted writable, the regular write |
| 5173 | * operations to the old disk have to be duplicated to go |
| 5174 | * to the new disk as well. |
| 5175 | * Note that device->missing is handled by the caller, and |
| 5176 | * that the write to the old disk is already set up in the |
| 5177 | * stripes array. |
| 5178 | */ |
| 5179 | index_where_to_add = num_stripes; |
| 5180 | for (i = 0; i < num_stripes; i++) { |
| 5181 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5182 | /* write to new disk, too */ |
| 5183 | struct btrfs_bio_stripe *new = |
| 5184 | bbio->stripes + index_where_to_add; |
| 5185 | struct btrfs_bio_stripe *old = |
| 5186 | bbio->stripes + i; |
| 5187 | |
| 5188 | new->physical = old->physical; |
| 5189 | new->length = old->length; |
| 5190 | new->dev = dev_replace->tgtdev; |
| 5191 | index_where_to_add++; |
| 5192 | max_errors++; |
| 5193 | } |
| 5194 | } |
| 5195 | num_stripes = index_where_to_add; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5196 | } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) && |
| 5197 | dev_replace->tgtdev != NULL) { |
| 5198 | u64 srcdev_devid = dev_replace->srcdev->devid; |
| 5199 | int index_srcdev = 0; |
| 5200 | int found = 0; |
| 5201 | u64 physical_of_found = 0; |
| 5202 | |
| 5203 | /* |
| 5204 | * During the dev-replace procedure, the target drive can |
| 5205 | * also be used to read data in case it is needed to repair |
| 5206 | * a corrupt block elsewhere. This is possible if the |
| 5207 | * requested area is left of the left cursor. In this area, |
| 5208 | * the target drive is a full copy of the source drive. |
| 5209 | */ |
| 5210 | for (i = 0; i < num_stripes; i++) { |
| 5211 | if (bbio->stripes[i].dev->devid == srcdev_devid) { |
| 5212 | /* |
| 5213 | * In case of DUP, in order to keep it |
| 5214 | * simple, only add the mirror with the |
| 5215 | * lowest physical address |
| 5216 | */ |
| 5217 | if (found && |
| 5218 | physical_of_found <= |
| 5219 | bbio->stripes[i].physical) |
| 5220 | continue; |
| 5221 | index_srcdev = i; |
| 5222 | found = 1; |
| 5223 | physical_of_found = bbio->stripes[i].physical; |
| 5224 | } |
| 5225 | } |
| 5226 | if (found) { |
| 5227 | u64 length = map->stripe_len; |
| 5228 | |
| 5229 | if (physical_of_found + length <= |
| 5230 | dev_replace->cursor_left) { |
| 5231 | struct btrfs_bio_stripe *tgtdev_stripe = |
| 5232 | bbio->stripes + num_stripes; |
| 5233 | |
| 5234 | tgtdev_stripe->physical = physical_of_found; |
| 5235 | tgtdev_stripe->length = |
| 5236 | bbio->stripes[index_srcdev].length; |
| 5237 | tgtdev_stripe->dev = dev_replace->tgtdev; |
| 5238 | |
| 5239 | num_stripes++; |
| 5240 | } |
| 5241 | } |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5242 | } |
| 5243 | |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5244 | *bbio_ret = bbio; |
| 5245 | bbio->num_stripes = num_stripes; |
| 5246 | bbio->max_errors = max_errors; |
| 5247 | bbio->mirror_num = mirror_num; |
Stefan Behrens | ad6d620 | 2012-11-06 15:06:47 +0100 | [diff] [blame] | 5248 | |
| 5249 | /* |
| 5250 | * this is the case that REQ_READ && dev_replace_is_ongoing && |
| 5251 | * mirror_num == num_stripes + 1 && dev_replace target drive is |
| 5252 | * available as a mirror |
| 5253 | */ |
| 5254 | if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) { |
| 5255 | WARN_ON(num_stripes > 1); |
| 5256 | bbio->stripes[0].dev = dev_replace->tgtdev; |
| 5257 | bbio->stripes[0].physical = physical_to_patch_in_first_stripe; |
| 5258 | bbio->mirror_num = map->num_stripes + 1; |
| 5259 | } |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5260 | if (raid_map) { |
| 5261 | sort_parity_stripes(bbio, raid_map); |
| 5262 | *raid_map_ret = raid_map; |
| 5263 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5264 | out: |
Stefan Behrens | 472262f | 2012-11-06 14:43:46 +0100 | [diff] [blame] | 5265 | if (dev_replace_is_ongoing) |
| 5266 | btrfs_dev_replace_unlock(dev_replace); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5267 | free_extent_map(em); |
Li Zefan | de11cc1 | 2011-12-01 12:55:47 +0800 | [diff] [blame] | 5268 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5269 | } |
| 5270 | |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5271 | int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw, |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5272 | u64 logical, u64 *length, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5273 | struct btrfs_bio **bbio_ret, int mirror_num) |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5274 | { |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 5275 | return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5276 | mirror_num, NULL); |
Chris Mason | f2d8d74 | 2008-04-21 10:03:05 -0400 | [diff] [blame] | 5277 | } |
| 5278 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5279 | int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree, |
| 5280 | u64 chunk_start, u64 physical, u64 devid, |
| 5281 | u64 **logical, int *naddrs, int *stripe_len) |
| 5282 | { |
| 5283 | struct extent_map_tree *em_tree = &map_tree->map_tree; |
| 5284 | struct extent_map *em; |
| 5285 | struct map_lookup *map; |
| 5286 | u64 *buf; |
| 5287 | u64 bytenr; |
| 5288 | u64 length; |
| 5289 | u64 stripe_nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5290 | u64 rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5291 | int i, j, nr = 0; |
| 5292 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5293 | read_lock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5294 | em = lookup_extent_mapping(em_tree, chunk_start, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5295 | read_unlock(&em_tree->lock); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5296 | |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5297 | if (!em) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5298 | printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5299 | chunk_start); |
| 5300 | return -EIO; |
| 5301 | } |
| 5302 | |
| 5303 | if (em->start != chunk_start) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5304 | printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n", |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 5305 | em->start, chunk_start); |
| 5306 | free_extent_map(em); |
| 5307 | return -EIO; |
| 5308 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5309 | map = (struct map_lookup *)em->bdev; |
| 5310 | |
| 5311 | length = em->len; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5312 | rmap_len = map->stripe_len; |
| 5313 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5314 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) |
| 5315 | do_div(length, map->num_stripes / map->sub_stripes); |
| 5316 | else if (map->type & BTRFS_BLOCK_GROUP_RAID0) |
| 5317 | do_div(length, map->num_stripes); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5318 | else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | |
| 5319 | BTRFS_BLOCK_GROUP_RAID6)) { |
| 5320 | do_div(length, nr_data_stripes(map)); |
| 5321 | rmap_len = map->stripe_len * nr_data_stripes(map); |
| 5322 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5323 | |
| 5324 | buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5325 | BUG_ON(!buf); /* -ENOMEM */ |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5326 | |
| 5327 | for (i = 0; i < map->num_stripes; i++) { |
| 5328 | if (devid && map->stripes[i].dev->devid != devid) |
| 5329 | continue; |
| 5330 | if (map->stripes[i].physical > physical || |
| 5331 | map->stripes[i].physical + length <= physical) |
| 5332 | continue; |
| 5333 | |
| 5334 | stripe_nr = physical - map->stripes[i].physical; |
| 5335 | do_div(stripe_nr, map->stripe_len); |
| 5336 | |
| 5337 | if (map->type & BTRFS_BLOCK_GROUP_RAID10) { |
| 5338 | stripe_nr = stripe_nr * map->num_stripes + i; |
| 5339 | do_div(stripe_nr, map->sub_stripes); |
| 5340 | } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) { |
| 5341 | stripe_nr = stripe_nr * map->num_stripes + i; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5342 | } /* else if RAID[56], multiply by nr_data_stripes(). |
| 5343 | * Alternatively, just use rmap_len below instead of |
| 5344 | * map->stripe_len */ |
| 5345 | |
| 5346 | bytenr = chunk_start + stripe_nr * rmap_len; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5347 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5348 | for (j = 0; j < nr; j++) { |
| 5349 | if (buf[j] == bytenr) |
| 5350 | break; |
| 5351 | } |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5352 | if (j == nr) { |
| 5353 | WARN_ON(nr >= map->num_stripes); |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5354 | buf[nr++] = bytenr; |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5355 | } |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5356 | } |
| 5357 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5358 | *logical = buf; |
| 5359 | *naddrs = nr; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5360 | *stripe_len = rmap_len; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 5361 | |
| 5362 | free_extent_map(em); |
| 5363 | return 0; |
| 5364 | } |
| 5365 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5366 | static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err) |
| 5367 | { |
| 5368 | if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED)) |
| 5369 | bio_endio_nodec(bio, err); |
| 5370 | else |
| 5371 | bio_endio(bio, err); |
| 5372 | kfree(bbio); |
| 5373 | } |
| 5374 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5375 | static void btrfs_end_bio(struct bio *bio, int err) |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5376 | { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5377 | struct btrfs_bio *bbio = bio->bi_private; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5378 | struct btrfs_device *dev = bbio->stripes[0].dev; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5379 | int is_orig_bio = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5380 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5381 | if (err) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5382 | atomic_inc(&bbio->error); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5383 | if (err == -EIO || err == -EREMOTEIO) { |
| 5384 | unsigned int stripe_index = |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5385 | btrfs_io_bio(bio)->stripe_index; |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5386 | |
| 5387 | BUG_ON(stripe_index >= bbio->num_stripes); |
| 5388 | dev = bbio->stripes[stripe_index].dev; |
Stefan Behrens | 597a60f | 2012-06-14 16:42:31 +0200 | [diff] [blame] | 5389 | if (dev->bdev) { |
| 5390 | if (bio->bi_rw & WRITE) |
| 5391 | btrfs_dev_stat_inc(dev, |
| 5392 | BTRFS_DEV_STAT_WRITE_ERRS); |
| 5393 | else |
| 5394 | btrfs_dev_stat_inc(dev, |
| 5395 | BTRFS_DEV_STAT_READ_ERRS); |
| 5396 | if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH) |
| 5397 | btrfs_dev_stat_inc(dev, |
| 5398 | BTRFS_DEV_STAT_FLUSH_ERRS); |
| 5399 | btrfs_dev_stat_print_on_error(dev); |
| 5400 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 5401 | } |
| 5402 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5403 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5404 | if (bio == bbio->orig_bio) |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5405 | is_orig_bio = 1; |
| 5406 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5407 | btrfs_bio_counter_dec(bbio->fs_info); |
| 5408 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5409 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5410 | if (!is_orig_bio) { |
| 5411 | bio_put(bio); |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5412 | bio = bbio->orig_bio; |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5413 | } |
Muthu Kumar | c7b22bb | 2014-01-08 14:19:52 -0700 | [diff] [blame] | 5414 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5415 | bio->bi_private = bbio->private; |
| 5416 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5417 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5418 | /* only send an error to the higher layers if it is |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5419 | * beyond the tolerance of the btrfs bio |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5420 | */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5421 | if (atomic_read(&bbio->error) > bbio->max_errors) { |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5422 | err = -EIO; |
Chris Mason | 5dbc8fc | 2011-12-09 11:07:37 -0500 | [diff] [blame] | 5423 | } else { |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5424 | /* |
| 5425 | * this bio is actually up to date, we didn't |
| 5426 | * go over the max number of errors |
| 5427 | */ |
| 5428 | set_bit(BIO_UPTODATE, &bio->bi_flags); |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 5429 | err = 0; |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 5430 | } |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5431 | |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5432 | btrfs_end_bbio(bbio, bio, err); |
Chris Mason | 7d2b4da | 2008-08-05 10:13:57 -0400 | [diff] [blame] | 5433 | } else if (!is_orig_bio) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5434 | bio_put(bio); |
| 5435 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5436 | } |
| 5437 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5438 | /* |
| 5439 | * see run_scheduled_bios for a description of why bios are collected for |
| 5440 | * async submit. |
| 5441 | * |
| 5442 | * This will add one bio to the pending list for a device and make sure |
| 5443 | * the work struct is scheduled. |
| 5444 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 5445 | static noinline void btrfs_schedule_bio(struct btrfs_root *root, |
| 5446 | struct btrfs_device *device, |
| 5447 | int rw, struct bio *bio) |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5448 | { |
| 5449 | int should_queue = 1; |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5450 | struct btrfs_pending_bios *pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5451 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5452 | if (device->missing || !device->bdev) { |
| 5453 | bio_endio(bio, -EIO); |
| 5454 | return; |
| 5455 | } |
| 5456 | |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5457 | /* don't bother with additional async steps for reads, right now */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5458 | if (!(rw & REQ_WRITE)) { |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5459 | bio_get(bio); |
Stefan Behrens | 21adbd5 | 2011-11-09 13:44:05 +0100 | [diff] [blame] | 5460 | btrfsic_submit_bio(rw, bio); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5461 | bio_put(bio); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5462 | return; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | /* |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5466 | * nr_async_bios allows us to reliably return congestion to the |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5467 | * higher layers. Otherwise, the async bio makes it appear we have |
| 5468 | * made progress against dirty pages when we've really just put it |
| 5469 | * on a queue for later |
| 5470 | */ |
Chris Mason | 0986fe9e | 2008-08-15 15:34:15 -0400 | [diff] [blame] | 5471 | atomic_inc(&root->fs_info->nr_async_bios); |
Chris Mason | 492bb6de | 2008-07-31 16:29:02 -0400 | [diff] [blame] | 5472 | WARN_ON(bio->bi_next); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5473 | bio->bi_next = NULL; |
| 5474 | bio->bi_rw |= rw; |
| 5475 | |
| 5476 | spin_lock(&device->io_lock); |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 5477 | if (bio->bi_rw & REQ_SYNC) |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5478 | pending_bios = &device->pending_sync_bios; |
| 5479 | else |
| 5480 | pending_bios = &device->pending_bios; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5481 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5482 | if (pending_bios->tail) |
| 5483 | pending_bios->tail->bi_next = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5484 | |
Chris Mason | ffbd517 | 2009-04-20 15:50:09 -0400 | [diff] [blame] | 5485 | pending_bios->tail = bio; |
| 5486 | if (!pending_bios->head) |
| 5487 | pending_bios->head = bio; |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5488 | if (device->running_pending) |
| 5489 | should_queue = 0; |
| 5490 | |
| 5491 | spin_unlock(&device->io_lock); |
| 5492 | |
| 5493 | if (should_queue) |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5494 | btrfs_queue_work(root->fs_info->submit_workers, |
| 5495 | &device->work); |
Chris Mason | 8b71284 | 2008-06-11 16:50:36 -0400 | [diff] [blame] | 5496 | } |
| 5497 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5498 | static int bio_size_ok(struct block_device *bdev, struct bio *bio, |
| 5499 | sector_t sector) |
| 5500 | { |
| 5501 | struct bio_vec *prev; |
| 5502 | struct request_queue *q = bdev_get_queue(bdev); |
Akinobu Mita | 475bf36 | 2013-11-18 22:13:18 +0900 | [diff] [blame] | 5503 | unsigned int max_sectors = queue_max_sectors(q); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5504 | struct bvec_merge_data bvm = { |
| 5505 | .bi_bdev = bdev, |
| 5506 | .bi_sector = sector, |
| 5507 | .bi_rw = bio->bi_rw, |
| 5508 | }; |
| 5509 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5510 | if (WARN_ON(bio->bi_vcnt == 0)) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5511 | return 1; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5512 | |
| 5513 | prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5514 | if (bio_sectors(bio) > max_sectors) |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5515 | return 0; |
| 5516 | |
| 5517 | if (!q->merge_bvec_fn) |
| 5518 | return 1; |
| 5519 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5520 | bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5521 | if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) |
| 5522 | return 0; |
| 5523 | return 1; |
| 5524 | } |
| 5525 | |
| 5526 | static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5527 | struct bio *bio, u64 physical, int dev_nr, |
| 5528 | int rw, int async) |
| 5529 | { |
| 5530 | struct btrfs_device *dev = bbio->stripes[dev_nr].dev; |
| 5531 | |
| 5532 | bio->bi_private = bbio; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5533 | btrfs_io_bio(bio)->stripe_index = dev_nr; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5534 | bio->bi_end_io = btrfs_end_bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5535 | bio->bi_iter.bi_sector = physical >> 9; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5536 | #ifdef DEBUG |
| 5537 | { |
| 5538 | struct rcu_string *name; |
| 5539 | |
| 5540 | rcu_read_lock(); |
| 5541 | name = rcu_dereference(dev->name); |
Masanari Iida | d142324 | 2012-10-31 15:16:32 +0000 | [diff] [blame] | 5542 | pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu " |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5543 | "(%s id %llu), size=%u\n", rw, |
| 5544 | (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev, |
| 5545 | name->str, dev->devid, bio->bi_size); |
| 5546 | rcu_read_unlock(); |
| 5547 | } |
| 5548 | #endif |
| 5549 | bio->bi_bdev = dev->bdev; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5550 | |
| 5551 | btrfs_bio_counter_inc_noblocked(root->fs_info); |
| 5552 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5553 | if (async) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5554 | btrfs_schedule_bio(root, dev, rw, bio); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5555 | else |
| 5556 | btrfsic_submit_bio(rw, bio); |
| 5557 | } |
| 5558 | |
| 5559 | static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, |
| 5560 | struct bio *first_bio, struct btrfs_device *dev, |
| 5561 | int dev_nr, int rw, int async) |
| 5562 | { |
| 5563 | struct bio_vec *bvec = first_bio->bi_io_vec; |
| 5564 | struct bio *bio; |
| 5565 | int nr_vecs = bio_get_nr_vecs(dev->bdev); |
| 5566 | u64 physical = bbio->stripes[dev_nr].physical; |
| 5567 | |
| 5568 | again: |
| 5569 | bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS); |
| 5570 | if (!bio) |
| 5571 | return -ENOMEM; |
| 5572 | |
| 5573 | while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) { |
| 5574 | if (bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
| 5575 | bvec->bv_offset) < bvec->bv_len) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5576 | u64 len = bio->bi_iter.bi_size; |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5577 | |
| 5578 | atomic_inc(&bbio->stripes_pending); |
| 5579 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, |
| 5580 | rw, async); |
| 5581 | physical += len; |
| 5582 | goto again; |
| 5583 | } |
| 5584 | bvec++; |
| 5585 | } |
| 5586 | |
| 5587 | submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async); |
| 5588 | return 0; |
| 5589 | } |
| 5590 | |
| 5591 | static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical) |
| 5592 | { |
| 5593 | atomic_inc(&bbio->error); |
| 5594 | if (atomic_dec_and_test(&bbio->stripes_pending)) { |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5595 | /* Shoud be the original bio. */ |
| 5596 | WARN_ON(bio != bbio->orig_bio); |
| 5597 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5598 | bio->bi_private = bbio->private; |
| 5599 | bio->bi_end_io = bbio->end_io; |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5600 | btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5601 | bio->bi_iter.bi_sector = logical >> 9; |
Miao Xie | 8408c71 | 2014-06-19 10:42:55 +0800 | [diff] [blame] | 5602 | |
| 5603 | btrfs_end_bbio(bbio, bio, -EIO); |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | |
Chris Mason | f188591 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5607 | 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] | 5608 | int mirror_num, int async_submit) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5609 | { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5610 | struct btrfs_device *dev; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5611 | struct bio *first_bio = bio; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5612 | u64 logical = (u64)bio->bi_iter.bi_sector << 9; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5613 | u64 length = 0; |
| 5614 | u64 map_length; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5615 | u64 *raid_map = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5616 | int ret; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5617 | int dev_nr = 0; |
| 5618 | int total_devs = 1; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5619 | struct btrfs_bio *bbio = NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5620 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5621 | length = bio->bi_iter.bi_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5622 | map_length = length; |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5623 | |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5624 | btrfs_bio_counter_inc_blocked(root->fs_info); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5625 | ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio, |
| 5626 | mirror_num, &raid_map); |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5627 | if (ret) { |
| 5628 | btrfs_bio_counter_dec(root->fs_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5629 | return ret; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5630 | } |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5631 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5632 | total_devs = bbio->num_stripes; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5633 | bbio->orig_bio = first_bio; |
| 5634 | bbio->private = first_bio->bi_private; |
| 5635 | bbio->end_io = first_bio->bi_end_io; |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5636 | bbio->fs_info = root->fs_info; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5637 | atomic_set(&bbio->stripes_pending, bbio->num_stripes); |
| 5638 | |
| 5639 | if (raid_map) { |
| 5640 | /* In this case, map_length has been set to the length of |
| 5641 | a single stripe; not the whole write */ |
| 5642 | if (rw & WRITE) { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5643 | ret = raid56_parity_write(root, bio, bbio, |
| 5644 | raid_map, map_length); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5645 | } else { |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5646 | ret = raid56_parity_recover(root, bio, bbio, |
| 5647 | raid_map, map_length, |
| 5648 | mirror_num); |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5649 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5650 | /* |
| 5651 | * FIXME, replace dosen't support raid56 yet, please fix |
| 5652 | * it in the future. |
| 5653 | */ |
| 5654 | btrfs_bio_counter_dec(root->fs_info); |
| 5655 | return ret; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 5656 | } |
| 5657 | |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5658 | if (map_length < length) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5659 | 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] | 5660 | logical, length, map_length); |
Chris Mason | cea9e44 | 2008-04-09 16:28:12 -0400 | [diff] [blame] | 5661 | BUG(); |
| 5662 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5663 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5664 | while (dev_nr < total_devs) { |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5665 | dev = bbio->stripes[dev_nr].dev; |
| 5666 | if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) { |
| 5667 | bbio_error(bbio, first_bio, logical); |
| 5668 | dev_nr++; |
| 5669 | continue; |
| 5670 | } |
| 5671 | |
| 5672 | /* |
| 5673 | * Check and see if we're ok with this bio based on it's size |
| 5674 | * and offset with the given device. |
| 5675 | */ |
| 5676 | if (!bio_size_ok(dev->bdev, first_bio, |
| 5677 | bbio->stripes[dev_nr].physical >> 9)) { |
| 5678 | ret = breakup_stripe_bio(root, bbio, first_bio, dev, |
| 5679 | dev_nr, rw, async_submit); |
| 5680 | BUG_ON(ret); |
| 5681 | dev_nr++; |
| 5682 | continue; |
| 5683 | } |
| 5684 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5685 | if (dev_nr < total_devs - 1) { |
Chris Mason | 9be3395 | 2013-05-17 18:30:14 -0400 | [diff] [blame] | 5686 | bio = btrfs_bio_clone(first_bio, GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5687 | BUG_ON(!bio); /* -ENOMEM */ |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 5688 | } else { |
| 5689 | bio = first_bio; |
Miao Xie | c55f139 | 2014-06-19 10:42:54 +0800 | [diff] [blame] | 5690 | bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5691 | } |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 5692 | |
Josef Bacik | de1ee92 | 2012-10-19 16:50:56 -0400 | [diff] [blame] | 5693 | submit_stripe_bio(root, bbio, bio, |
| 5694 | bbio->stripes[dev_nr].physical, dev_nr, rw, |
| 5695 | async_submit); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 5696 | dev_nr++; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5697 | } |
Miao Xie | c404e0d | 2014-01-30 16:46:55 +0800 | [diff] [blame] | 5698 | btrfs_bio_counter_dec(root->fs_info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5699 | return 0; |
| 5700 | } |
| 5701 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5702 | 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] | 5703 | u8 *uuid, u8 *fsid) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5704 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5705 | struct btrfs_device *device; |
| 5706 | struct btrfs_fs_devices *cur_devices; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5707 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5708 | cur_devices = fs_info->fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5709 | while (cur_devices) { |
| 5710 | if (!fsid || |
| 5711 | !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5712 | device = __find_device(&cur_devices->devices, |
| 5713 | devid, uuid); |
| 5714 | if (device) |
| 5715 | return device; |
| 5716 | } |
| 5717 | cur_devices = cur_devices->seed; |
| 5718 | } |
| 5719 | return NULL; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5720 | } |
| 5721 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5722 | static struct btrfs_device *add_missing_dev(struct btrfs_root *root, |
| 5723 | u64 devid, u8 *dev_uuid) |
| 5724 | { |
| 5725 | struct btrfs_device *device; |
| 5726 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 5727 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5728 | device = btrfs_alloc_device(NULL, &devid, dev_uuid); |
| 5729 | if (IS_ERR(device)) |
yanhai zhu | 7cbd8a8 | 2008-11-12 14:38:54 -0500 | [diff] [blame] | 5730 | return NULL; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5731 | |
| 5732 | list_add(&device->dev_list, &fs_devices->devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5733 | device->fs_devices = fs_devices; |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5734 | fs_devices->num_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5735 | |
| 5736 | device->missing = 1; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5737 | fs_devices->missing_devices++; |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5738 | |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5739 | return device; |
| 5740 | } |
| 5741 | |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5742 | /** |
| 5743 | * btrfs_alloc_device - allocate struct btrfs_device |
| 5744 | * @fs_info: used only for generating a new devid, can be NULL if |
| 5745 | * devid is provided (i.e. @devid != NULL). |
| 5746 | * @devid: a pointer to devid for this device. If NULL a new devid |
| 5747 | * is generated. |
| 5748 | * @uuid: a pointer to UUID for this device. If NULL a new UUID |
| 5749 | * is generated. |
| 5750 | * |
| 5751 | * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR() |
| 5752 | * on error. Returned struct is not linked onto any lists and can be |
| 5753 | * destroyed with kfree() right away. |
| 5754 | */ |
| 5755 | struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, |
| 5756 | const u64 *devid, |
| 5757 | const u8 *uuid) |
| 5758 | { |
| 5759 | struct btrfs_device *dev; |
| 5760 | u64 tmp; |
| 5761 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5762 | if (WARN_ON(!devid && !fs_info)) |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5763 | return ERR_PTR(-EINVAL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5764 | |
| 5765 | dev = __alloc_device(); |
| 5766 | if (IS_ERR(dev)) |
| 5767 | return dev; |
| 5768 | |
| 5769 | if (devid) |
| 5770 | tmp = *devid; |
| 5771 | else { |
| 5772 | int ret; |
| 5773 | |
| 5774 | ret = find_next_devid(fs_info, &tmp); |
| 5775 | if (ret) { |
| 5776 | kfree(dev); |
| 5777 | return ERR_PTR(ret); |
| 5778 | } |
| 5779 | } |
| 5780 | dev->devid = tmp; |
| 5781 | |
| 5782 | if (uuid) |
| 5783 | memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE); |
| 5784 | else |
| 5785 | generate_random_uuid(dev->uuid); |
| 5786 | |
Qu Wenruo | a8c93d4 | 2014-02-28 10:46:08 +0800 | [diff] [blame] | 5787 | btrfs_init_work(&dev->work, pending_bios_fn, NULL, NULL); |
Ilya Dryomov | 12bd2fc | 2013-08-23 13:20:17 +0300 | [diff] [blame] | 5788 | |
| 5789 | return dev; |
| 5790 | } |
| 5791 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5792 | static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, |
| 5793 | struct extent_buffer *leaf, |
| 5794 | struct btrfs_chunk *chunk) |
| 5795 | { |
| 5796 | struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; |
| 5797 | struct map_lookup *map; |
| 5798 | struct extent_map *em; |
| 5799 | u64 logical; |
| 5800 | u64 length; |
| 5801 | u64 devid; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5802 | u8 uuid[BTRFS_UUID_SIZE]; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5803 | int num_stripes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5804 | int ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5805 | int i; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5806 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5807 | logical = key->offset; |
| 5808 | length = btrfs_chunk_length(leaf, chunk); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5809 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5810 | read_lock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5811 | em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5812 | read_unlock(&map_tree->map_tree.lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5813 | |
| 5814 | /* already mapped? */ |
| 5815 | if (em && em->start <= logical && em->start + em->len > logical) { |
| 5816 | free_extent_map(em); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5817 | return 0; |
| 5818 | } else if (em) { |
| 5819 | free_extent_map(em); |
| 5820 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5821 | |
David Sterba | 172ddd6 | 2011-04-21 00:48:27 +0200 | [diff] [blame] | 5822 | em = alloc_extent_map(); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5823 | if (!em) |
| 5824 | return -ENOMEM; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5825 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 5826 | map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5827 | if (!map) { |
| 5828 | free_extent_map(em); |
| 5829 | return -ENOMEM; |
| 5830 | } |
| 5831 | |
Wang Shilong | 298a8f9 | 2014-06-19 10:42:52 +0800 | [diff] [blame] | 5832 | set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5833 | em->bdev = (struct block_device *)map; |
| 5834 | em->start = logical; |
| 5835 | em->len = length; |
Josef Bacik | 70c8a91 | 2012-10-11 16:54:30 -0400 | [diff] [blame] | 5836 | em->orig_start = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5837 | em->block_start = 0; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5838 | em->block_len = em->len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5839 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5840 | map->num_stripes = num_stripes; |
| 5841 | map->io_width = btrfs_chunk_io_width(leaf, chunk); |
| 5842 | map->io_align = btrfs_chunk_io_align(leaf, chunk); |
| 5843 | map->sector_size = btrfs_chunk_sector_size(leaf, chunk); |
| 5844 | map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 5845 | map->type = btrfs_chunk_type(leaf, chunk); |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 5846 | map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5847 | for (i = 0; i < num_stripes; i++) { |
| 5848 | map->stripes[i].physical = |
| 5849 | btrfs_stripe_offset_nr(leaf, chunk, i); |
| 5850 | devid = btrfs_stripe_devid_nr(leaf, chunk, i); |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5851 | read_extent_buffer(leaf, uuid, (unsigned long) |
| 5852 | btrfs_stripe_dev_uuid_nr(chunk, i), |
| 5853 | BTRFS_UUID_SIZE); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5854 | map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, |
| 5855 | uuid, NULL); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5856 | if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 5857 | free_extent_map(em); |
| 5858 | return -EIO; |
| 5859 | } |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5860 | if (!map->stripes[i].dev) { |
| 5861 | map->stripes[i].dev = |
| 5862 | add_missing_dev(root, devid, uuid); |
| 5863 | if (!map->stripes[i].dev) { |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 5864 | free_extent_map(em); |
| 5865 | return -EIO; |
| 5866 | } |
| 5867 | } |
| 5868 | map->stripes[i].dev->in_fs_metadata = 1; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5869 | } |
| 5870 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5871 | write_lock(&map_tree->map_tree.lock); |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 5872 | ret = add_extent_mapping(&map_tree->map_tree, em, 0); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 5873 | write_unlock(&map_tree->map_tree.lock); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5874 | BUG_ON(ret); /* Tree corruption */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5875 | free_extent_map(em); |
| 5876 | |
| 5877 | return 0; |
| 5878 | } |
| 5879 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5880 | static void fill_device_from_item(struct extent_buffer *leaf, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5881 | struct btrfs_dev_item *dev_item, |
| 5882 | struct btrfs_device *device) |
| 5883 | { |
| 5884 | unsigned long ptr; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5885 | |
| 5886 | device->devid = btrfs_device_id(leaf, dev_item); |
Chris Ball | d6397ba | 2009-04-27 07:29:03 -0400 | [diff] [blame] | 5887 | device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item); |
| 5888 | device->total_bytes = device->disk_total_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5889 | device->bytes_used = btrfs_device_bytes_used(leaf, dev_item); |
| 5890 | device->type = btrfs_device_type(leaf, dev_item); |
| 5891 | device->io_align = btrfs_device_io_align(leaf, dev_item); |
| 5892 | device->io_width = btrfs_device_io_width(leaf, dev_item); |
| 5893 | device->sector_size = btrfs_device_sector_size(leaf, dev_item); |
Stefan Behrens | 8dabb74 | 2012-11-06 13:15:27 +0100 | [diff] [blame] | 5894 | WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID); |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 5895 | device->is_tgtdev_for_dev_replace = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5896 | |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 5897 | ptr = btrfs_device_uuid(dev_item); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5898 | read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5899 | } |
| 5900 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5901 | static int open_seed_devices(struct btrfs_root *root, u8 *fsid) |
| 5902 | { |
| 5903 | struct btrfs_fs_devices *fs_devices; |
| 5904 | int ret; |
| 5905 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 5906 | BUG_ON(!mutex_is_locked(&uuid_mutex)); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5907 | |
| 5908 | fs_devices = root->fs_info->fs_devices->seed; |
| 5909 | while (fs_devices) { |
| 5910 | if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) { |
| 5911 | ret = 0; |
| 5912 | goto out; |
| 5913 | } |
| 5914 | fs_devices = fs_devices->seed; |
| 5915 | } |
| 5916 | |
| 5917 | fs_devices = find_fsid(fsid); |
| 5918 | if (!fs_devices) { |
| 5919 | ret = -ENOENT; |
| 5920 | goto out; |
| 5921 | } |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5922 | |
| 5923 | fs_devices = clone_fs_devices(fs_devices); |
| 5924 | if (IS_ERR(fs_devices)) { |
| 5925 | ret = PTR_ERR(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5926 | goto out; |
| 5927 | } |
| 5928 | |
Christoph Hellwig | 97288f2 | 2008-12-02 06:36:09 -0500 | [diff] [blame] | 5929 | ret = __btrfs_open_devices(fs_devices, FMODE_READ, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 5930 | root->fs_info->bdev_holder); |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5931 | if (ret) { |
| 5932 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5933 | goto out; |
Julia Lawall | 48d2823 | 2012-04-14 11:24:33 +0200 | [diff] [blame] | 5934 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5935 | |
| 5936 | if (!fs_devices->seeding) { |
| 5937 | __btrfs_close_devices(fs_devices); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5938 | free_fs_devices(fs_devices); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5939 | ret = -EINVAL; |
| 5940 | goto out; |
| 5941 | } |
| 5942 | |
| 5943 | fs_devices->seed = root->fs_info->fs_devices->seed; |
| 5944 | root->fs_info->fs_devices->seed = fs_devices; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5945 | out: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5946 | return ret; |
| 5947 | } |
| 5948 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5949 | static int read_one_dev(struct btrfs_root *root, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5950 | struct extent_buffer *leaf, |
| 5951 | struct btrfs_dev_item *dev_item) |
| 5952 | { |
| 5953 | struct btrfs_device *device; |
| 5954 | u64 devid; |
| 5955 | int ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5956 | u8 fs_uuid[BTRFS_UUID_SIZE]; |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5957 | u8 dev_uuid[BTRFS_UUID_SIZE]; |
| 5958 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5959 | devid = btrfs_device_id(leaf, dev_item); |
Geert Uytterhoeven | 410ba3a | 2013-08-20 13:20:11 +0200 | [diff] [blame] | 5960 | read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item), |
Chris Mason | a443755 | 2008-04-18 10:29:38 -0400 | [diff] [blame] | 5961 | BTRFS_UUID_SIZE); |
Geert Uytterhoeven | 1473b24 | 2013-08-20 13:20:12 +0200 | [diff] [blame] | 5962 | read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5963 | BTRFS_UUID_SIZE); |
| 5964 | |
| 5965 | if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { |
| 5966 | ret = open_seed_devices(root, fs_uuid); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5967 | if (ret && !btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5968 | return ret; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5969 | } |
| 5970 | |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 5971 | device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5972 | if (!device || !device->bdev) { |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5973 | if (!btrfs_test_opt(root, DEGRADED)) |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5974 | return -EIO; |
| 5975 | |
| 5976 | if (!device) { |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5977 | btrfs_warn(root->fs_info, "devid %llu missing", devid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5978 | device = add_missing_dev(root, devid, dev_uuid); |
| 5979 | if (!device) |
| 5980 | return -ENOMEM; |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 5981 | } else if (!device->missing) { |
| 5982 | /* |
| 5983 | * this happens when a device that was properly setup |
| 5984 | * in the device info lists suddenly goes bad. |
| 5985 | * device->bdev is NULL, and so we have to set |
| 5986 | * device->missing to one here |
| 5987 | */ |
| 5988 | root->fs_info->fs_devices->missing_devices++; |
| 5989 | device->missing = 1; |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | if (device->fs_devices != root->fs_info->fs_devices) { |
| 5994 | BUG_ON(device->writeable); |
| 5995 | if (device->generation != |
| 5996 | btrfs_device_generation(leaf, dev_item)) |
| 5997 | return -EINVAL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5998 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5999 | |
| 6000 | fill_device_from_item(leaf, dev_item, device); |
Chris Mason | dfe2502 | 2008-05-13 13:46:40 -0400 | [diff] [blame] | 6001 | device->in_fs_metadata = 1; |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 6002 | if (device->writeable && !device->is_tgtdev_for_dev_replace) { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6003 | device->fs_devices->total_rw_bytes += device->total_bytes; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 6004 | spin_lock(&root->fs_info->free_chunk_lock); |
| 6005 | root->fs_info->free_chunk_space += device->total_bytes - |
| 6006 | device->bytes_used; |
| 6007 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 6008 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6009 | ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6010 | return ret; |
| 6011 | } |
| 6012 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6013 | int btrfs_read_sys_array(struct btrfs_root *root) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6014 | { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 6015 | struct btrfs_super_block *super_copy = root->fs_info->super_copy; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6016 | struct extent_buffer *sb; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6017 | struct btrfs_disk_key *disk_key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6018 | struct btrfs_chunk *chunk; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6019 | u8 *ptr; |
| 6020 | unsigned long sb_ptr; |
| 6021 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6022 | u32 num_stripes; |
| 6023 | u32 array_size; |
| 6024 | u32 len = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6025 | u32 cur; |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6026 | struct btrfs_key key; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6027 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6028 | sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET, |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6029 | BTRFS_SUPER_INFO_SIZE); |
| 6030 | if (!sb) |
| 6031 | return -ENOMEM; |
| 6032 | btrfs_set_buffer_uptodate(sb); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 6033 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0); |
David Sterba | 8a33442 | 2011-10-07 18:06:13 +0200 | [diff] [blame] | 6034 | /* |
| 6035 | * The sb extent buffer is artifical and just used to read the system array. |
| 6036 | * btrfs_set_buffer_uptodate() call does not properly mark all it's |
| 6037 | * pages up-to-date when the page is larger: extent does not cover the |
| 6038 | * whole page and consequently check_page_uptodate does not find all |
| 6039 | * the page's extents up-to-date (the hole beyond sb), |
| 6040 | * write_extent_buffer then triggers a WARN_ON. |
| 6041 | * |
| 6042 | * Regular short extents go through mark_extent_buffer_dirty/writeback cycle, |
| 6043 | * but sb spans only this function. Add an explicit SetPageUptodate call |
| 6044 | * to silence the warning eg. on PowerPC 64. |
| 6045 | */ |
| 6046 | if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE) |
Chris Mason | 727011e | 2010-08-06 13:21:20 -0400 | [diff] [blame] | 6047 | SetPageUptodate(sb->pages[0]); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 6048 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6049 | write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6050 | array_size = btrfs_super_sys_array_size(super_copy); |
| 6051 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6052 | ptr = super_copy->sys_chunk_array; |
| 6053 | sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array); |
| 6054 | cur = 0; |
| 6055 | |
| 6056 | while (cur < array_size) { |
| 6057 | disk_key = (struct btrfs_disk_key *)ptr; |
| 6058 | btrfs_disk_key_to_cpu(&key, disk_key); |
| 6059 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6060 | len = sizeof(*disk_key); ptr += len; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6061 | sb_ptr += len; |
| 6062 | cur += len; |
| 6063 | |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6064 | if (key.type == BTRFS_CHUNK_ITEM_KEY) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6065 | chunk = (struct btrfs_chunk *)sb_ptr; |
Chris Mason | 0d81ba5 | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6066 | ret = read_one_chunk(root, &key, sb, chunk); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6067 | if (ret) |
| 6068 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6069 | num_stripes = btrfs_chunk_num_stripes(sb, chunk); |
| 6070 | len = btrfs_chunk_item_size(num_stripes); |
| 6071 | } else { |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6072 | ret = -EIO; |
| 6073 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6074 | } |
| 6075 | ptr += len; |
| 6076 | sb_ptr += len; |
| 6077 | cur += len; |
| 6078 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6079 | free_extent_buffer(sb); |
Chris Mason | 84eed90 | 2008-04-25 09:04:37 -0400 | [diff] [blame] | 6080 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6081 | } |
| 6082 | |
| 6083 | int btrfs_read_chunk_tree(struct btrfs_root *root) |
| 6084 | { |
| 6085 | struct btrfs_path *path; |
| 6086 | struct extent_buffer *leaf; |
| 6087 | struct btrfs_key key; |
| 6088 | struct btrfs_key found_key; |
| 6089 | int ret; |
| 6090 | int slot; |
| 6091 | |
| 6092 | root = root->fs_info->chunk_root; |
| 6093 | |
| 6094 | path = btrfs_alloc_path(); |
| 6095 | if (!path) |
| 6096 | return -ENOMEM; |
| 6097 | |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6098 | mutex_lock(&uuid_mutex); |
| 6099 | lock_chunks(root); |
| 6100 | |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6101 | /* |
| 6102 | * Read all device items, and then all the chunk items. All |
| 6103 | * device items are found before any chunk item (their object id |
| 6104 | * is smaller than the lowest possible object id for a chunk |
| 6105 | * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID). |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6106 | */ |
| 6107 | key.objectid = BTRFS_DEV_ITEMS_OBJECTID; |
| 6108 | key.offset = 0; |
| 6109 | key.type = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6110 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Zhao Lei | ab59381 | 2010-03-25 12:34:49 +0000 | [diff] [blame] | 6111 | if (ret < 0) |
| 6112 | goto error; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6113 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6114 | leaf = path->nodes[0]; |
| 6115 | slot = path->slots[0]; |
| 6116 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6117 | ret = btrfs_next_leaf(root, path); |
| 6118 | if (ret == 0) |
| 6119 | continue; |
| 6120 | if (ret < 0) |
| 6121 | goto error; |
| 6122 | break; |
| 6123 | } |
| 6124 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6125 | if (found_key.type == BTRFS_DEV_ITEM_KEY) { |
| 6126 | struct btrfs_dev_item *dev_item; |
| 6127 | dev_item = btrfs_item_ptr(leaf, slot, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6128 | struct btrfs_dev_item); |
Filipe David Borba Manana | 395927a | 2013-07-30 12:03:04 +0100 | [diff] [blame] | 6129 | ret = read_one_dev(root, leaf, dev_item); |
| 6130 | if (ret) |
| 6131 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6132 | } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { |
| 6133 | struct btrfs_chunk *chunk; |
| 6134 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 6135 | ret = read_one_chunk(root, &found_key, leaf, chunk); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6136 | if (ret) |
| 6137 | goto error; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6138 | } |
| 6139 | path->slots[0]++; |
| 6140 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6141 | ret = 0; |
| 6142 | error: |
Li Zefan | b367e47 | 2011-12-07 11:38:24 +0800 | [diff] [blame] | 6143 | unlock_chunks(root); |
| 6144 | mutex_unlock(&uuid_mutex); |
| 6145 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6146 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6147 | return ret; |
| 6148 | } |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6149 | |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6150 | void btrfs_init_devices_late(struct btrfs_fs_info *fs_info) |
| 6151 | { |
| 6152 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6153 | struct btrfs_device *device; |
| 6154 | |
Liu Bo | 29cc83f | 2014-05-11 23:14:59 +0800 | [diff] [blame] | 6155 | while (fs_devices) { |
| 6156 | mutex_lock(&fs_devices->device_list_mutex); |
| 6157 | list_for_each_entry(device, &fs_devices->devices, dev_list) |
| 6158 | device->dev_root = fs_info->dev_root; |
| 6159 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6160 | |
| 6161 | fs_devices = fs_devices->seed; |
| 6162 | } |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6165 | static void __btrfs_reset_dev_stats(struct btrfs_device *dev) |
| 6166 | { |
| 6167 | int i; |
| 6168 | |
| 6169 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6170 | btrfs_dev_stat_reset(dev, i); |
| 6171 | } |
| 6172 | |
| 6173 | int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info) |
| 6174 | { |
| 6175 | struct btrfs_key key; |
| 6176 | struct btrfs_key found_key; |
| 6177 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6178 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6179 | struct extent_buffer *eb; |
| 6180 | int slot; |
| 6181 | int ret = 0; |
| 6182 | struct btrfs_device *device; |
| 6183 | struct btrfs_path *path = NULL; |
| 6184 | int i; |
| 6185 | |
| 6186 | path = btrfs_alloc_path(); |
| 6187 | if (!path) { |
| 6188 | ret = -ENOMEM; |
| 6189 | goto out; |
| 6190 | } |
| 6191 | |
| 6192 | mutex_lock(&fs_devices->device_list_mutex); |
| 6193 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6194 | int item_size; |
| 6195 | struct btrfs_dev_stats_item *ptr; |
| 6196 | |
| 6197 | key.objectid = 0; |
| 6198 | key.type = BTRFS_DEV_STATS_KEY; |
| 6199 | key.offset = device->devid; |
| 6200 | ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0); |
| 6201 | if (ret) { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6202 | __btrfs_reset_dev_stats(device); |
| 6203 | device->dev_stats_valid = 1; |
| 6204 | btrfs_release_path(path); |
| 6205 | continue; |
| 6206 | } |
| 6207 | slot = path->slots[0]; |
| 6208 | eb = path->nodes[0]; |
| 6209 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 6210 | item_size = btrfs_item_size_nr(eb, slot); |
| 6211 | |
| 6212 | ptr = btrfs_item_ptr(eb, slot, |
| 6213 | struct btrfs_dev_stats_item); |
| 6214 | |
| 6215 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6216 | if (item_size >= (1 + i) * sizeof(__le64)) |
| 6217 | btrfs_dev_stat_set(device, i, |
| 6218 | btrfs_dev_stats_value(eb, ptr, i)); |
| 6219 | else |
| 6220 | btrfs_dev_stat_reset(device, i); |
| 6221 | } |
| 6222 | |
| 6223 | device->dev_stats_valid = 1; |
| 6224 | btrfs_dev_stat_print_on_load(device); |
| 6225 | btrfs_release_path(path); |
| 6226 | } |
| 6227 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6228 | |
| 6229 | out: |
| 6230 | btrfs_free_path(path); |
| 6231 | return ret < 0 ? ret : 0; |
| 6232 | } |
| 6233 | |
| 6234 | static int update_dev_stat_item(struct btrfs_trans_handle *trans, |
| 6235 | struct btrfs_root *dev_root, |
| 6236 | struct btrfs_device *device) |
| 6237 | { |
| 6238 | struct btrfs_path *path; |
| 6239 | struct btrfs_key key; |
| 6240 | struct extent_buffer *eb; |
| 6241 | struct btrfs_dev_stats_item *ptr; |
| 6242 | int ret; |
| 6243 | int i; |
| 6244 | |
| 6245 | key.objectid = 0; |
| 6246 | key.type = BTRFS_DEV_STATS_KEY; |
| 6247 | key.offset = device->devid; |
| 6248 | |
| 6249 | path = btrfs_alloc_path(); |
| 6250 | BUG_ON(!path); |
| 6251 | ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); |
| 6252 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6253 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6254 | "error %d while searching for dev_stats item for device %s!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6255 | ret, rcu_str_deref(device->name)); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6256 | goto out; |
| 6257 | } |
| 6258 | |
| 6259 | if (ret == 0 && |
| 6260 | btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) { |
| 6261 | /* need to delete old one and insert a new one */ |
| 6262 | ret = btrfs_del_item(trans, dev_root, path); |
| 6263 | if (ret != 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6264 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6265 | "delete too small dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6266 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6267 | goto out; |
| 6268 | } |
| 6269 | ret = 1; |
| 6270 | } |
| 6271 | |
| 6272 | if (ret == 1) { |
| 6273 | /* need to insert a new item */ |
| 6274 | btrfs_release_path(path); |
| 6275 | ret = btrfs_insert_empty_item(trans, dev_root, path, |
| 6276 | &key, sizeof(*ptr)); |
| 6277 | if (ret < 0) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6278 | printk_in_rcu(KERN_WARNING "BTRFS: " |
| 6279 | "insert dev_stats item for device %s failed %d!\n", |
Josef Bacik | 606686e | 2012-06-04 14:03:51 -0400 | [diff] [blame] | 6280 | rcu_str_deref(device->name), ret); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6281 | goto out; |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | eb = path->nodes[0]; |
| 6286 | ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item); |
| 6287 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6288 | btrfs_set_dev_stats_value(eb, ptr, i, |
| 6289 | btrfs_dev_stat_read(device, i)); |
| 6290 | btrfs_mark_buffer_dirty(eb); |
| 6291 | |
| 6292 | out: |
| 6293 | btrfs_free_path(path); |
| 6294 | return ret; |
| 6295 | } |
| 6296 | |
| 6297 | /* |
| 6298 | * called from commit_transaction. Writes all changed device stats to disk. |
| 6299 | */ |
| 6300 | int btrfs_run_dev_stats(struct btrfs_trans_handle *trans, |
| 6301 | struct btrfs_fs_info *fs_info) |
| 6302 | { |
| 6303 | struct btrfs_root *dev_root = fs_info->dev_root; |
| 6304 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
| 6305 | struct btrfs_device *device; |
| 6306 | int ret = 0; |
| 6307 | |
| 6308 | mutex_lock(&fs_devices->device_list_mutex); |
| 6309 | list_for_each_entry(device, &fs_devices->devices, dev_list) { |
| 6310 | if (!device->dev_stats_valid || !device->dev_stats_dirty) |
| 6311 | continue; |
| 6312 | |
| 6313 | ret = update_dev_stat_item(trans, dev_root, device); |
| 6314 | if (!ret) |
| 6315 | device->dev_stats_dirty = 0; |
| 6316 | } |
| 6317 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6318 | |
| 6319 | return ret; |
| 6320 | } |
| 6321 | |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6322 | void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index) |
| 6323 | { |
| 6324 | btrfs_dev_stat_inc(dev, index); |
| 6325 | btrfs_dev_stat_print_on_error(dev); |
| 6326 | } |
| 6327 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 6328 | static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev) |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6329 | { |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6330 | if (!dev->dev_stats_valid) |
| 6331 | return; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6332 | printk_ratelimited_in_rcu(KERN_ERR "BTRFS: " |
| 6333 | "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] | 6334 | rcu_str_deref(dev->name), |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6335 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6336 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6337 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6338 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6339 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
Stefan Behrens | 442a4f6 | 2012-05-25 16:06:08 +0200 | [diff] [blame] | 6340 | } |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6341 | |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6342 | static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev) |
| 6343 | { |
Stefan Behrens | a98cdb8 | 2012-07-17 09:02:11 -0600 | [diff] [blame] | 6344 | int i; |
| 6345 | |
| 6346 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6347 | if (btrfs_dev_stat_read(dev, i) != 0) |
| 6348 | break; |
| 6349 | if (i == BTRFS_DEV_STAT_VALUES_MAX) |
| 6350 | return; /* all values == 0, suppress message */ |
| 6351 | |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6352 | printk_in_rcu(KERN_INFO "BTRFS: " |
| 6353 | "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] | 6354 | rcu_str_deref(dev->name), |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6355 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS), |
| 6356 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS), |
| 6357 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS), |
| 6358 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS), |
| 6359 | btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); |
| 6360 | } |
| 6361 | |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6362 | int btrfs_get_dev_stats(struct btrfs_root *root, |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6363 | struct btrfs_ioctl_get_dev_stats *stats) |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6364 | { |
| 6365 | struct btrfs_device *dev; |
| 6366 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6367 | int i; |
| 6368 | |
| 6369 | mutex_lock(&fs_devices->device_list_mutex); |
Stefan Behrens | aa1b8cd | 2012-11-05 17:03:39 +0100 | [diff] [blame] | 6370 | dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6371 | mutex_unlock(&fs_devices->device_list_mutex); |
| 6372 | |
| 6373 | if (!dev) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6374 | btrfs_warn(root->fs_info, "get dev_stats failed, device not found"); |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6375 | return -ENODEV; |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6376 | } else if (!dev->dev_stats_valid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6377 | btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid"); |
Stefan Behrens | 733f4fb | 2012-05-25 16:06:10 +0200 | [diff] [blame] | 6378 | return -ENODEV; |
David Sterba | b27f7c0 | 2012-06-22 06:30:39 -0600 | [diff] [blame] | 6379 | } else if (stats->flags & BTRFS_DEV_STATS_RESET) { |
Stefan Behrens | c11d2c2 | 2012-05-25 16:06:09 +0200 | [diff] [blame] | 6380 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { |
| 6381 | if (stats->nr_items > i) |
| 6382 | stats->values[i] = |
| 6383 | btrfs_dev_stat_read_and_reset(dev, i); |
| 6384 | else |
| 6385 | btrfs_dev_stat_reset(dev, i); |
| 6386 | } |
| 6387 | } else { |
| 6388 | for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) |
| 6389 | if (stats->nr_items > i) |
| 6390 | stats->values[i] = btrfs_dev_stat_read(dev, i); |
| 6391 | } |
| 6392 | if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX) |
| 6393 | stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX; |
| 6394 | return 0; |
| 6395 | } |
Stefan Behrens | a8a6dab | 2012-11-05 15:50:14 +0100 | [diff] [blame] | 6396 | |
| 6397 | int btrfs_scratch_superblock(struct btrfs_device *device) |
| 6398 | { |
| 6399 | struct buffer_head *bh; |
| 6400 | struct btrfs_super_block *disk_super; |
| 6401 | |
| 6402 | bh = btrfs_read_dev_super(device->bdev); |
| 6403 | if (!bh) |
| 6404 | return -EINVAL; |
| 6405 | disk_super = (struct btrfs_super_block *)bh->b_data; |
| 6406 | |
| 6407 | memset(&disk_super->magic, 0, sizeof(disk_super->magic)); |
| 6408 | set_buffer_dirty(bh); |
| 6409 | sync_dirty_buffer(bh); |
| 6410 | brelse(bh); |
| 6411 | |
| 6412 | return 0; |
| 6413 | } |